1 /* Scalar evolution detector.
2 Copyright (C) 2003-2017 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"
264 #include "gimple-pretty-print.h"
265 #include "fold-const.h"
266 #include "gimplify.h"
267 #include "gimple-iterator.h"
268 #include "gimplify-me.h"
269 #include "tree-cfg.h"
270 #include "tree-ssa-loop-ivopts.h"
271 #include "tree-ssa-loop-manip.h"
272 #include "tree-ssa-loop-niter.h"
273 #include "tree-ssa-loop.h"
274 #include "tree-ssa.h"
276 #include "tree-chrec.h"
277 #include "tree-affine.h"
278 #include "tree-scalar-evolution.h"
279 #include "dumpfile.h"
281 #include "tree-ssa-propagate.h"
282 #include "gimple-fold.h"
284 static tree
analyze_scalar_evolution_1 (struct loop
*, tree
);
285 static tree
analyze_scalar_evolution_for_address_of (struct loop
*loop
,
288 /* The cached information about an SSA name with version NAME_VERSION,
289 claiming that below basic block with index INSTANTIATED_BELOW, the
290 value of the SSA name can be expressed as CHREC. */
292 struct GTY((for_user
)) scev_info_str
{
293 unsigned int name_version
;
294 int instantiated_below
;
298 /* Counters for the scev database. */
299 static unsigned nb_set_scev
= 0;
300 static unsigned nb_get_scev
= 0;
302 /* The following trees are unique elements. Thus the comparison of
303 another element to these elements should be done on the pointer to
304 these trees, and not on their value. */
306 /* The SSA_NAMEs that are not yet analyzed are qualified with NULL_TREE. */
307 tree chrec_not_analyzed_yet
;
309 /* Reserved to the cases where the analyzer has detected an
310 undecidable property at compile time. */
311 tree chrec_dont_know
;
313 /* When the analyzer has detected that a property will never
314 happen, then it qualifies it with chrec_known. */
317 struct scev_info_hasher
: ggc_ptr_hash
<scev_info_str
>
319 static hashval_t
hash (scev_info_str
*i
);
320 static bool equal (const scev_info_str
*a
, const scev_info_str
*b
);
323 static GTY (()) hash_table
<scev_info_hasher
> *scalar_evolution_info
;
326 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */
328 static inline struct scev_info_str
*
329 new_scev_info_str (basic_block instantiated_below
, tree var
)
331 struct scev_info_str
*res
;
333 res
= ggc_alloc
<scev_info_str
> ();
334 res
->name_version
= SSA_NAME_VERSION (var
);
335 res
->chrec
= chrec_not_analyzed_yet
;
336 res
->instantiated_below
= instantiated_below
->index
;
341 /* Computes a hash function for database element ELT. */
344 scev_info_hasher::hash (scev_info_str
*elt
)
346 return elt
->name_version
^ elt
->instantiated_below
;
349 /* Compares database elements E1 and E2. */
352 scev_info_hasher::equal (const scev_info_str
*elt1
, const scev_info_str
*elt2
)
354 return (elt1
->name_version
== elt2
->name_version
355 && elt1
->instantiated_below
== elt2
->instantiated_below
);
358 /* Get the scalar evolution of VAR for INSTANTIATED_BELOW basic block.
359 A first query on VAR returns chrec_not_analyzed_yet. */
362 find_var_scev_info (basic_block instantiated_below
, tree var
)
364 struct scev_info_str
*res
;
365 struct scev_info_str tmp
;
367 tmp
.name_version
= SSA_NAME_VERSION (var
);
368 tmp
.instantiated_below
= instantiated_below
->index
;
369 scev_info_str
**slot
= scalar_evolution_info
->find_slot (&tmp
, INSERT
);
372 *slot
= new_scev_info_str (instantiated_below
, var
);
378 /* Return true when CHREC contains symbolic names defined in
382 chrec_contains_symbols_defined_in_loop (const_tree chrec
, unsigned loop_nb
)
386 if (chrec
== NULL_TREE
)
389 if (is_gimple_min_invariant (chrec
))
392 if (TREE_CODE (chrec
) == SSA_NAME
)
395 loop_p def_loop
, loop
;
397 if (SSA_NAME_IS_DEFAULT_DEF (chrec
))
400 def
= SSA_NAME_DEF_STMT (chrec
);
401 def_loop
= loop_containing_stmt (def
);
402 loop
= get_loop (cfun
, loop_nb
);
404 if (def_loop
== NULL
)
407 if (loop
== def_loop
|| flow_loop_nested_p (loop
, def_loop
))
413 n
= TREE_OPERAND_LENGTH (chrec
);
414 for (i
= 0; i
< n
; i
++)
415 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec
, i
),
421 /* Return true when PHI is a loop-phi-node. */
424 loop_phi_node_p (gimple
*phi
)
426 /* The implementation of this function is based on the following
427 property: "all the loop-phi-nodes of a loop are contained in the
428 loop's header basic block". */
430 return loop_containing_stmt (phi
)->header
== gimple_bb (phi
);
433 /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
434 In general, in the case of multivariate evolutions we want to get
435 the evolution in different loops. LOOP specifies the level for
436 which to get the evolution.
440 | for (j = 0; j < 100; j++)
442 | for (k = 0; k < 100; k++)
444 | i = k + j; - Here the value of i is a function of j, k.
446 | ... = i - Here the value of i is a function of j.
448 | ... = i - Here the value of i is a scalar.
454 | i_1 = phi (i_0, i_2)
458 This loop has the same effect as:
459 LOOP_1 has the same effect as:
463 The overall effect of the loop, "i_0 + 20" in the previous example,
464 is obtained by passing in the parameters: LOOP = 1,
465 EVOLUTION_FN = {i_0, +, 2}_1.
469 compute_overall_effect_of_inner_loop (struct loop
*loop
, tree evolution_fn
)
473 if (evolution_fn
== chrec_dont_know
)
474 return chrec_dont_know
;
476 else if (TREE_CODE (evolution_fn
) == POLYNOMIAL_CHREC
)
478 struct loop
*inner_loop
= get_chrec_loop (evolution_fn
);
480 if (inner_loop
== loop
481 || flow_loop_nested_p (loop
, inner_loop
))
483 tree nb_iter
= number_of_latch_executions (inner_loop
);
485 if (nb_iter
== chrec_dont_know
)
486 return chrec_dont_know
;
491 /* evolution_fn is the evolution function in LOOP. Get
492 its value in the nb_iter-th iteration. */
493 res
= chrec_apply (inner_loop
->num
, evolution_fn
, nb_iter
);
495 if (chrec_contains_symbols_defined_in_loop (res
, loop
->num
))
496 res
= instantiate_parameters (loop
, res
);
498 /* Continue the computation until ending on a parent of LOOP. */
499 return compute_overall_effect_of_inner_loop (loop
, res
);
506 /* If the evolution function is an invariant, there is nothing to do. */
507 else if (no_evolution_in_loop_p (evolution_fn
, loop
->num
, &val
) && val
)
511 return chrec_dont_know
;
514 /* Associate CHREC to SCALAR. */
517 set_scalar_evolution (basic_block instantiated_below
, tree scalar
, tree chrec
)
521 if (TREE_CODE (scalar
) != SSA_NAME
)
524 scalar_info
= find_var_scev_info (instantiated_below
, scalar
);
528 if (dump_flags
& TDF_SCEV
)
530 fprintf (dump_file
, "(set_scalar_evolution \n");
531 fprintf (dump_file
, " instantiated_below = %d \n",
532 instantiated_below
->index
);
533 fprintf (dump_file
, " (scalar = ");
534 print_generic_expr (dump_file
, scalar
);
535 fprintf (dump_file
, ")\n (scalar_evolution = ");
536 print_generic_expr (dump_file
, chrec
);
537 fprintf (dump_file
, "))\n");
539 if (dump_flags
& TDF_STATS
)
543 *scalar_info
= chrec
;
546 /* Retrieve the chrec associated to SCALAR instantiated below
547 INSTANTIATED_BELOW block. */
550 get_scalar_evolution (basic_block instantiated_below
, tree scalar
)
556 if (dump_flags
& TDF_SCEV
)
558 fprintf (dump_file
, "(get_scalar_evolution \n");
559 fprintf (dump_file
, " (scalar = ");
560 print_generic_expr (dump_file
, scalar
);
561 fprintf (dump_file
, ")\n");
563 if (dump_flags
& TDF_STATS
)
567 if (VECTOR_TYPE_P (TREE_TYPE (scalar
))
568 || TREE_CODE (TREE_TYPE (scalar
)) == COMPLEX_TYPE
)
569 /* For chrec_dont_know we keep the symbolic form. */
572 switch (TREE_CODE (scalar
))
575 if (SSA_NAME_IS_DEFAULT_DEF (scalar
))
578 res
= *find_var_scev_info (instantiated_below
, scalar
);
588 res
= chrec_not_analyzed_yet
;
592 if (dump_file
&& (dump_flags
& TDF_SCEV
))
594 fprintf (dump_file
, " (scalar_evolution = ");
595 print_generic_expr (dump_file
, res
);
596 fprintf (dump_file
, "))\n");
602 /* Helper function for add_to_evolution. Returns the evolution
603 function for an assignment of the form "a = b + c", where "a" and
604 "b" are on the strongly connected component. CHREC_BEFORE is the
605 information that we already have collected up to this point.
606 TO_ADD is the evolution of "c".
608 When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
609 evolution the expression TO_ADD, otherwise construct an evolution
610 part for this loop. */
613 add_to_evolution_1 (unsigned loop_nb
, tree chrec_before
, tree to_add
,
616 tree type
, left
, right
;
617 struct loop
*loop
= get_loop (cfun
, loop_nb
), *chloop
;
619 switch (TREE_CODE (chrec_before
))
621 case POLYNOMIAL_CHREC
:
622 chloop
= get_chrec_loop (chrec_before
);
624 || flow_loop_nested_p (chloop
, loop
))
628 type
= chrec_type (chrec_before
);
630 /* When there is no evolution part in this loop, build it. */
635 right
= SCALAR_FLOAT_TYPE_P (type
)
636 ? build_real (type
, dconst0
)
637 : build_int_cst (type
, 0);
641 var
= CHREC_VARIABLE (chrec_before
);
642 left
= CHREC_LEFT (chrec_before
);
643 right
= CHREC_RIGHT (chrec_before
);
646 to_add
= chrec_convert (type
, to_add
, at_stmt
);
647 right
= chrec_convert_rhs (type
, right
, at_stmt
);
648 right
= chrec_fold_plus (chrec_type (right
), right
, to_add
);
649 return build_polynomial_chrec (var
, left
, right
);
653 gcc_assert (flow_loop_nested_p (loop
, chloop
));
655 /* Search the evolution in LOOP_NB. */
656 left
= add_to_evolution_1 (loop_nb
, CHREC_LEFT (chrec_before
),
658 right
= CHREC_RIGHT (chrec_before
);
659 right
= chrec_convert_rhs (chrec_type (left
), right
, at_stmt
);
660 return build_polynomial_chrec (CHREC_VARIABLE (chrec_before
),
665 /* These nodes do not depend on a loop. */
666 if (chrec_before
== chrec_dont_know
)
667 return chrec_dont_know
;
670 right
= chrec_convert_rhs (chrec_type (left
), to_add
, at_stmt
);
671 return build_polynomial_chrec (loop_nb
, left
, right
);
675 /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
678 Description (provided for completeness, for those who read code in
679 a plane, and for my poor 62 bytes brain that would have forgotten
680 all this in the next two or three months):
682 The algorithm of translation of programs from the SSA representation
683 into the chrecs syntax is based on a pattern matching. After having
684 reconstructed the overall tree expression for a loop, there are only
685 two cases that can arise:
687 1. a = loop-phi (init, a + expr)
688 2. a = loop-phi (init, expr)
690 where EXPR is either a scalar constant with respect to the analyzed
691 loop (this is a degree 0 polynomial), or an expression containing
692 other loop-phi definitions (these are higher degree polynomials).
699 | a = phi (init, a + 5)
706 | a = phi (inita, 2 * b + 3)
707 | b = phi (initb, b + 1)
710 For the first case, the semantics of the SSA representation is:
712 | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
714 that is, there is a loop index "x" that determines the scalar value
715 of the variable during the loop execution. During the first
716 iteration, the value is that of the initial condition INIT, while
717 during the subsequent iterations, it is the sum of the initial
718 condition with the sum of all the values of EXPR from the initial
719 iteration to the before last considered iteration.
721 For the second case, the semantics of the SSA program is:
723 | a (x) = init, if x = 0;
724 | expr (x - 1), otherwise.
726 The second case corresponds to the PEELED_CHREC, whose syntax is
727 close to the syntax of a loop-phi-node:
729 | phi (init, expr) vs. (init, expr)_x
731 The proof of the translation algorithm for the first case is a
732 proof by structural induction based on the degree of EXPR.
735 When EXPR is a constant with respect to the analyzed loop, or in
736 other words when EXPR is a polynomial of degree 0, the evolution of
737 the variable A in the loop is an affine function with an initial
738 condition INIT, and a step EXPR. In order to show this, we start
739 from the semantics of the SSA representation:
741 f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
743 and since "expr (j)" is a constant with respect to "j",
745 f (x) = init + x * expr
747 Finally, based on the semantics of the pure sum chrecs, by
748 identification we get the corresponding chrecs syntax:
750 f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
751 f (x) -> {init, +, expr}_x
754 Suppose that EXPR is a polynomial of degree N with respect to the
755 analyzed loop_x for which we have already determined that it is
756 written under the chrecs syntax:
758 | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
760 We start from the semantics of the SSA program:
762 | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
764 | f (x) = init + \sum_{j = 0}^{x - 1}
765 | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
767 | f (x) = init + \sum_{j = 0}^{x - 1}
768 | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
770 | f (x) = init + \sum_{k = 0}^{n - 1}
771 | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
773 | f (x) = init + \sum_{k = 0}^{n - 1}
774 | (b_k * \binom{x}{k + 1})
776 | f (x) = init + b_0 * \binom{x}{1} + ...
777 | + b_{n-1} * \binom{x}{n}
779 | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
780 | + b_{n-1} * \binom{x}{n}
783 And finally from the definition of the chrecs syntax, we identify:
784 | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x
786 This shows the mechanism that stands behind the add_to_evolution
787 function. An important point is that the use of symbolic
788 parameters avoids the need of an analysis schedule.
795 | a = phi (inita, a + 2 + b)
796 | b = phi (initb, b + 1)
799 When analyzing "a", the algorithm keeps "b" symbolically:
801 | a -> {inita, +, 2 + b}_1
803 Then, after instantiation, the analyzer ends on the evolution:
805 | a -> {inita, +, 2 + initb, +, 1}_1
810 add_to_evolution (unsigned loop_nb
, tree chrec_before
, enum tree_code code
,
811 tree to_add
, gimple
*at_stmt
)
813 tree type
= chrec_type (to_add
);
814 tree res
= NULL_TREE
;
816 if (to_add
== NULL_TREE
)
819 /* TO_ADD is either a scalar, or a parameter. TO_ADD is not
820 instantiated at this point. */
821 if (TREE_CODE (to_add
) == POLYNOMIAL_CHREC
)
822 /* This should not happen. */
823 return chrec_dont_know
;
825 if (dump_file
&& (dump_flags
& TDF_SCEV
))
827 fprintf (dump_file
, "(add_to_evolution \n");
828 fprintf (dump_file
, " (loop_nb = %d)\n", loop_nb
);
829 fprintf (dump_file
, " (chrec_before = ");
830 print_generic_expr (dump_file
, chrec_before
);
831 fprintf (dump_file
, ")\n (to_add = ");
832 print_generic_expr (dump_file
, to_add
);
833 fprintf (dump_file
, ")\n");
836 if (code
== MINUS_EXPR
)
837 to_add
= chrec_fold_multiply (type
, to_add
, SCALAR_FLOAT_TYPE_P (type
)
838 ? build_real (type
, dconstm1
)
839 : build_int_cst_type (type
, -1));
841 res
= add_to_evolution_1 (loop_nb
, chrec_before
, to_add
, at_stmt
);
843 if (dump_file
&& (dump_flags
& TDF_SCEV
))
845 fprintf (dump_file
, " (res = ");
846 print_generic_expr (dump_file
, res
);
847 fprintf (dump_file
, "))\n");
855 /* This section selects the loops that will be good candidates for the
856 scalar evolution analysis. For the moment, greedily select all the
857 loop nests we could analyze. */
859 /* For a loop with a single exit edge, return the COND_EXPR that
860 guards the exit edge. If the expression is too difficult to
861 analyze, then give up. */
864 get_loop_exit_condition (const struct loop
*loop
)
867 edge exit_edge
= single_exit (loop
);
869 if (dump_file
&& (dump_flags
& TDF_SCEV
))
870 fprintf (dump_file
, "(get_loop_exit_condition \n ");
876 stmt
= last_stmt (exit_edge
->src
);
877 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
881 if (dump_file
&& (dump_flags
& TDF_SCEV
))
883 print_gimple_stmt (dump_file
, res
, 0);
884 fprintf (dump_file
, ")\n");
891 /* Depth first search algorithm. */
900 static t_bool
follow_ssa_edge (struct loop
*loop
, gimple
*, gphi
*,
903 /* Follow the ssa edge into the binary expression RHS0 CODE RHS1.
904 Return true if the strongly connected component has been found. */
907 follow_ssa_edge_binary (struct loop
*loop
, gimple
*at_stmt
,
908 tree type
, tree rhs0
, enum tree_code code
, tree rhs1
,
909 gphi
*halting_phi
, tree
*evolution_of_loop
,
912 t_bool res
= t_false
;
917 case POINTER_PLUS_EXPR
:
919 if (TREE_CODE (rhs0
) == SSA_NAME
)
921 if (TREE_CODE (rhs1
) == SSA_NAME
)
923 /* Match an assignment under the form:
926 /* We want only assignments of form "name + name" contribute to
927 LIMIT, as the other cases do not necessarily contribute to
928 the complexity of the expression. */
931 evol
= *evolution_of_loop
;
932 evol
= add_to_evolution
934 chrec_convert (type
, evol
, at_stmt
),
935 code
, rhs1
, at_stmt
);
936 res
= follow_ssa_edge
937 (loop
, SSA_NAME_DEF_STMT (rhs0
), halting_phi
, &evol
, limit
);
939 *evolution_of_loop
= evol
;
940 else if (res
== t_false
)
942 *evolution_of_loop
= add_to_evolution
944 chrec_convert (type
, *evolution_of_loop
, at_stmt
),
945 code
, rhs0
, at_stmt
);
946 res
= follow_ssa_edge
947 (loop
, SSA_NAME_DEF_STMT (rhs1
), halting_phi
,
948 evolution_of_loop
, limit
);
951 else if (res
== t_dont_know
)
952 *evolution_of_loop
= chrec_dont_know
;
955 else if (res
== t_dont_know
)
956 *evolution_of_loop
= chrec_dont_know
;
961 /* Match an assignment under the form:
963 *evolution_of_loop
= add_to_evolution
964 (loop
->num
, chrec_convert (type
, *evolution_of_loop
,
966 code
, rhs1
, at_stmt
);
967 res
= follow_ssa_edge
968 (loop
, SSA_NAME_DEF_STMT (rhs0
), halting_phi
,
969 evolution_of_loop
, limit
);
972 else if (res
== t_dont_know
)
973 *evolution_of_loop
= chrec_dont_know
;
977 else if (TREE_CODE (rhs1
) == SSA_NAME
)
979 /* Match an assignment under the form:
981 *evolution_of_loop
= add_to_evolution
982 (loop
->num
, chrec_convert (type
, *evolution_of_loop
,
984 code
, rhs0
, at_stmt
);
985 res
= follow_ssa_edge
986 (loop
, SSA_NAME_DEF_STMT (rhs1
), halting_phi
,
987 evolution_of_loop
, limit
);
990 else if (res
== t_dont_know
)
991 *evolution_of_loop
= chrec_dont_know
;
995 /* Otherwise, match an assignment under the form:
997 /* And there is nothing to do. */
1002 /* This case is under the form "opnd0 = rhs0 - rhs1". */
1003 if (TREE_CODE (rhs0
) == SSA_NAME
)
1005 /* Match an assignment under the form:
1008 /* We want only assignments of form "name - name" contribute to
1009 LIMIT, as the other cases do not necessarily contribute to
1010 the complexity of the expression. */
1011 if (TREE_CODE (rhs1
) == SSA_NAME
)
1014 *evolution_of_loop
= add_to_evolution
1015 (loop
->num
, chrec_convert (type
, *evolution_of_loop
, at_stmt
),
1016 MINUS_EXPR
, rhs1
, at_stmt
);
1017 res
= follow_ssa_edge (loop
, SSA_NAME_DEF_STMT (rhs0
), halting_phi
,
1018 evolution_of_loop
, limit
);
1021 else if (res
== t_dont_know
)
1022 *evolution_of_loop
= chrec_dont_know
;
1025 /* Otherwise, match an assignment under the form:
1027 /* And there is nothing to do. */
1038 /* Follow the ssa edge into the expression EXPR.
1039 Return true if the strongly connected component has been found. */
1042 follow_ssa_edge_expr (struct loop
*loop
, gimple
*at_stmt
, tree expr
,
1043 gphi
*halting_phi
, tree
*evolution_of_loop
,
1046 enum tree_code code
= TREE_CODE (expr
);
1047 tree type
= TREE_TYPE (expr
), rhs0
, rhs1
;
1050 /* The EXPR is one of the following cases:
1054 - a POINTER_PLUS_EXPR,
1057 - other cases are not yet handled. */
1062 /* This assignment is under the form "a_1 = (cast) rhs. */
1063 res
= follow_ssa_edge_expr (loop
, at_stmt
, TREE_OPERAND (expr
, 0),
1064 halting_phi
, evolution_of_loop
, limit
);
1065 *evolution_of_loop
= chrec_convert (type
, *evolution_of_loop
, at_stmt
);
1069 /* This assignment is under the form "a_1 = 7". */
1074 /* This assignment is under the form: "a_1 = b_2". */
1075 res
= follow_ssa_edge
1076 (loop
, SSA_NAME_DEF_STMT (expr
), halting_phi
, evolution_of_loop
, limit
);
1079 case POINTER_PLUS_EXPR
:
1082 /* This case is under the form "rhs0 +- rhs1". */
1083 rhs0
= TREE_OPERAND (expr
, 0);
1084 rhs1
= TREE_OPERAND (expr
, 1);
1085 type
= TREE_TYPE (rhs0
);
1086 STRIP_USELESS_TYPE_CONVERSION (rhs0
);
1087 STRIP_USELESS_TYPE_CONVERSION (rhs1
);
1088 res
= follow_ssa_edge_binary (loop
, at_stmt
, type
, rhs0
, code
, rhs1
,
1089 halting_phi
, evolution_of_loop
, limit
);
1093 /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR. */
1094 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == MEM_REF
)
1096 expr
= TREE_OPERAND (expr
, 0);
1097 rhs0
= TREE_OPERAND (expr
, 0);
1098 rhs1
= TREE_OPERAND (expr
, 1);
1099 type
= TREE_TYPE (rhs0
);
1100 STRIP_USELESS_TYPE_CONVERSION (rhs0
);
1101 STRIP_USELESS_TYPE_CONVERSION (rhs1
);
1102 res
= follow_ssa_edge_binary (loop
, at_stmt
, type
,
1103 rhs0
, POINTER_PLUS_EXPR
, rhs1
,
1104 halting_phi
, evolution_of_loop
, limit
);
1111 /* This assignment is of the form: "a_1 = ASSERT_EXPR <a_2, ...>"
1112 It must be handled as a copy assignment of the form a_1 = a_2. */
1113 rhs0
= ASSERT_EXPR_VAR (expr
);
1114 if (TREE_CODE (rhs0
) == SSA_NAME
)
1115 res
= follow_ssa_edge (loop
, SSA_NAME_DEF_STMT (rhs0
),
1116 halting_phi
, evolution_of_loop
, limit
);
1129 /* Follow the ssa edge into the right hand side of an assignment STMT.
1130 Return true if the strongly connected component has been found. */
1133 follow_ssa_edge_in_rhs (struct loop
*loop
, gimple
*stmt
,
1134 gphi
*halting_phi
, tree
*evolution_of_loop
,
1137 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1138 tree type
= gimple_expr_type (stmt
), rhs1
, rhs2
;
1144 /* This assignment is under the form "a_1 = (cast) rhs. */
1145 res
= follow_ssa_edge_expr (loop
, stmt
, gimple_assign_rhs1 (stmt
),
1146 halting_phi
, evolution_of_loop
, limit
);
1147 *evolution_of_loop
= chrec_convert (type
, *evolution_of_loop
, stmt
);
1150 case POINTER_PLUS_EXPR
:
1153 rhs1
= gimple_assign_rhs1 (stmt
);
1154 rhs2
= gimple_assign_rhs2 (stmt
);
1155 type
= TREE_TYPE (rhs1
);
1156 res
= follow_ssa_edge_binary (loop
, stmt
, type
, rhs1
, code
, rhs2
,
1157 halting_phi
, evolution_of_loop
, limit
);
1161 if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
1162 res
= follow_ssa_edge_expr (loop
, stmt
, gimple_assign_rhs1 (stmt
),
1163 halting_phi
, evolution_of_loop
, limit
);
1172 /* Checks whether the I-th argument of a PHI comes from a backedge. */
1175 backedge_phi_arg_p (gphi
*phi
, int i
)
1177 const_edge e
= gimple_phi_arg_edge (phi
, i
);
1179 /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
1180 about updating it anywhere, and this should work as well most of the
1182 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1188 /* Helper function for one branch of the condition-phi-node. Return
1189 true if the strongly connected component has been found following
1192 static inline t_bool
1193 follow_ssa_edge_in_condition_phi_branch (int i
,
1195 gphi
*condition_phi
,
1197 tree
*evolution_of_branch
,
1198 tree init_cond
, int limit
)
1200 tree branch
= PHI_ARG_DEF (condition_phi
, i
);
1201 *evolution_of_branch
= chrec_dont_know
;
1203 /* Do not follow back edges (they must belong to an irreducible loop, which
1204 we really do not want to worry about). */
1205 if (backedge_phi_arg_p (condition_phi
, i
))
1208 if (TREE_CODE (branch
) == SSA_NAME
)
1210 *evolution_of_branch
= init_cond
;
1211 return follow_ssa_edge (loop
, SSA_NAME_DEF_STMT (branch
), halting_phi
,
1212 evolution_of_branch
, limit
);
1215 /* This case occurs when one of the condition branches sets
1216 the variable to a constant: i.e. a phi-node like
1217 "a_2 = PHI <a_7(5), 2(6)>;".
1219 FIXME: This case have to be refined correctly:
1220 in some cases it is possible to say something better than
1221 chrec_dont_know, for example using a wrap-around notation. */
1225 /* This function merges the branches of a condition-phi-node in a
1229 follow_ssa_edge_in_condition_phi (struct loop
*loop
,
1230 gphi
*condition_phi
,
1232 tree
*evolution_of_loop
, int limit
)
1235 tree init
= *evolution_of_loop
;
1236 tree evolution_of_branch
;
1237 t_bool res
= follow_ssa_edge_in_condition_phi_branch (0, loop
, condition_phi
,
1239 &evolution_of_branch
,
1241 if (res
== t_false
|| res
== t_dont_know
)
1244 *evolution_of_loop
= evolution_of_branch
;
1246 n
= gimple_phi_num_args (condition_phi
);
1247 for (i
= 1; i
< n
; i
++)
1249 /* Quickly give up when the evolution of one of the branches is
1251 if (*evolution_of_loop
== chrec_dont_know
)
1254 /* Increase the limit by the PHI argument number to avoid exponential
1255 time and memory complexity. */
1256 res
= follow_ssa_edge_in_condition_phi_branch (i
, loop
, condition_phi
,
1258 &evolution_of_branch
,
1260 if (res
== t_false
|| res
== t_dont_know
)
1263 *evolution_of_loop
= chrec_merge (*evolution_of_loop
,
1264 evolution_of_branch
);
1270 /* Follow an SSA edge in an inner loop. It computes the overall
1271 effect of the loop, and following the symbolic initial conditions,
1272 it follows the edges in the parent loop. The inner loop is
1273 considered as a single statement. */
1276 follow_ssa_edge_inner_loop_phi (struct loop
*outer_loop
,
1277 gphi
*loop_phi_node
,
1279 tree
*evolution_of_loop
, int limit
)
1281 struct loop
*loop
= loop_containing_stmt (loop_phi_node
);
1282 tree ev
= analyze_scalar_evolution (loop
, PHI_RESULT (loop_phi_node
));
1284 /* Sometimes, the inner loop is too difficult to analyze, and the
1285 result of the analysis is a symbolic parameter. */
1286 if (ev
== PHI_RESULT (loop_phi_node
))
1288 t_bool res
= t_false
;
1289 int i
, n
= gimple_phi_num_args (loop_phi_node
);
1291 for (i
= 0; i
< n
; i
++)
1293 tree arg
= PHI_ARG_DEF (loop_phi_node
, i
);
1296 /* Follow the edges that exit the inner loop. */
1297 bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1298 if (!flow_bb_inside_loop_p (loop
, bb
))
1299 res
= follow_ssa_edge_expr (outer_loop
, loop_phi_node
,
1301 evolution_of_loop
, limit
);
1306 /* If the path crosses this loop-phi, give up. */
1308 *evolution_of_loop
= chrec_dont_know
;
1313 /* Otherwise, compute the overall effect of the inner loop. */
1314 ev
= compute_overall_effect_of_inner_loop (loop
, ev
);
1315 return follow_ssa_edge_expr (outer_loop
, loop_phi_node
, ev
, halting_phi
,
1316 evolution_of_loop
, limit
);
1319 /* Follow an SSA edge from a loop-phi-node to itself, constructing a
1320 path that is analyzed on the return walk. */
1323 follow_ssa_edge (struct loop
*loop
, gimple
*def
, gphi
*halting_phi
,
1324 tree
*evolution_of_loop
, int limit
)
1326 struct loop
*def_loop
;
1328 if (gimple_nop_p (def
))
1331 /* Give up if the path is longer than the MAX that we allow. */
1332 if (limit
> PARAM_VALUE (PARAM_SCEV_MAX_EXPR_COMPLEXITY
))
1335 def_loop
= loop_containing_stmt (def
);
1337 switch (gimple_code (def
))
1340 if (!loop_phi_node_p (def
))
1341 /* DEF is a condition-phi-node. Follow the branches, and
1342 record their evolutions. Finally, merge the collected
1343 information and set the approximation to the main
1345 return follow_ssa_edge_in_condition_phi
1346 (loop
, as_a
<gphi
*> (def
), halting_phi
, evolution_of_loop
,
1349 /* When the analyzed phi is the halting_phi, the
1350 depth-first search is over: we have found a path from
1351 the halting_phi to itself in the loop. */
1352 if (def
== halting_phi
)
1355 /* Otherwise, the evolution of the HALTING_PHI depends
1356 on the evolution of another loop-phi-node, i.e. the
1357 evolution function is a higher degree polynomial. */
1358 if (def_loop
== loop
)
1362 if (flow_loop_nested_p (loop
, def_loop
))
1363 return follow_ssa_edge_inner_loop_phi
1364 (loop
, as_a
<gphi
*> (def
), halting_phi
, evolution_of_loop
,
1371 return follow_ssa_edge_in_rhs (loop
, def
, halting_phi
,
1372 evolution_of_loop
, limit
);
1375 /* At this level of abstraction, the program is just a set
1376 of GIMPLE_ASSIGNs and PHI_NODEs. In principle there is no
1377 other node to be handled. */
1383 /* Simplify PEELED_CHREC represented by (init_cond, arg) in LOOP.
1384 Handle below case and return the corresponding POLYNOMIAL_CHREC:
1386 # i_17 = PHI <i_13(5), 0(3)>
1387 # _20 = PHI <_5(5), start_4(D)(3)>
1390 _5 = start_4(D) + i_13;
1392 Though variable _20 appears as a PEELED_CHREC in the form of
1393 (start_4, _5)_LOOP, it's a POLYNOMIAL_CHREC like {start_4, 1}_LOOP.
1398 simplify_peeled_chrec (struct loop
*loop
, tree arg
, tree init_cond
)
1400 aff_tree aff1
, aff2
;
1401 tree ev
, left
, right
, type
, step_val
;
1402 hash_map
<tree
, name_expansion
*> *peeled_chrec_map
= NULL
;
1404 ev
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, arg
));
1405 if (ev
== NULL_TREE
|| TREE_CODE (ev
) != POLYNOMIAL_CHREC
)
1406 return chrec_dont_know
;
1408 left
= CHREC_LEFT (ev
);
1409 right
= CHREC_RIGHT (ev
);
1410 type
= TREE_TYPE (left
);
1411 step_val
= chrec_fold_plus (type
, init_cond
, right
);
1413 /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1414 if "left" equals to "init + right". */
1415 if (operand_equal_p (left
, step_val
, 0))
1417 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1418 fprintf (dump_file
, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1420 return build_polynomial_chrec (loop
->num
, init_cond
, right
);
1423 /* Try harder to check if they are equal. */
1424 tree_to_aff_combination_expand (left
, type
, &aff1
, &peeled_chrec_map
);
1425 tree_to_aff_combination_expand (step_val
, type
, &aff2
, &peeled_chrec_map
);
1426 free_affine_expand_cache (&peeled_chrec_map
);
1427 aff_combination_scale (&aff2
, -1);
1428 aff_combination_add (&aff1
, &aff2
);
1430 /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1431 if "left" equals to "init + right". */
1432 if (aff_combination_zero_p (&aff1
))
1434 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1435 fprintf (dump_file
, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1437 return build_polynomial_chrec (loop
->num
, init_cond
, right
);
1439 return chrec_dont_know
;
1442 /* Given a LOOP_PHI_NODE, this function determines the evolution
1443 function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */
1446 analyze_evolution_in_loop (gphi
*loop_phi_node
,
1449 int i
, n
= gimple_phi_num_args (loop_phi_node
);
1450 tree evolution_function
= chrec_not_analyzed_yet
;
1451 struct loop
*loop
= loop_containing_stmt (loop_phi_node
);
1453 static bool simplify_peeled_chrec_p
= true;
1455 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1457 fprintf (dump_file
, "(analyze_evolution_in_loop \n");
1458 fprintf (dump_file
, " (loop_phi_node = ");
1459 print_gimple_stmt (dump_file
, loop_phi_node
, 0);
1460 fprintf (dump_file
, ")\n");
1463 for (i
= 0; i
< n
; i
++)
1465 tree arg
= PHI_ARG_DEF (loop_phi_node
, i
);
1470 /* Select the edges that enter the loop body. */
1471 bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1472 if (!flow_bb_inside_loop_p (loop
, bb
))
1475 if (TREE_CODE (arg
) == SSA_NAME
)
1479 ssa_chain
= SSA_NAME_DEF_STMT (arg
);
1481 /* Pass in the initial condition to the follow edge function. */
1483 res
= follow_ssa_edge (loop
, ssa_chain
, loop_phi_node
, &ev_fn
, 0);
1485 /* If ev_fn has no evolution in the inner loop, and the
1486 init_cond is not equal to ev_fn, then we have an
1487 ambiguity between two possible values, as we cannot know
1488 the number of iterations at this point. */
1489 if (TREE_CODE (ev_fn
) != POLYNOMIAL_CHREC
1490 && no_evolution_in_loop_p (ev_fn
, loop
->num
, &val
) && val
1491 && !operand_equal_p (init_cond
, ev_fn
, 0))
1492 ev_fn
= chrec_dont_know
;
1497 /* When it is impossible to go back on the same
1498 loop_phi_node by following the ssa edges, the
1499 evolution is represented by a peeled chrec, i.e. the
1500 first iteration, EV_FN has the value INIT_COND, then
1501 all the other iterations it has the value of ARG.
1502 For the moment, PEELED_CHREC nodes are not built. */
1505 ev_fn
= chrec_dont_know
;
1506 /* Try to recognize POLYNOMIAL_CHREC which appears in
1507 the form of PEELED_CHREC, but guard the process with
1508 a bool variable to keep the analyzer from infinite
1509 recurrence for real PEELED_RECs. */
1510 if (simplify_peeled_chrec_p
&& TREE_CODE (arg
) == SSA_NAME
)
1512 simplify_peeled_chrec_p
= false;
1513 ev_fn
= simplify_peeled_chrec (loop
, arg
, init_cond
);
1514 simplify_peeled_chrec_p
= true;
1518 /* When there are multiple back edges of the loop (which in fact never
1519 happens currently, but nevertheless), merge their evolutions. */
1520 evolution_function
= chrec_merge (evolution_function
, ev_fn
);
1522 if (evolution_function
== chrec_dont_know
)
1526 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1528 fprintf (dump_file
, " (evolution_function = ");
1529 print_generic_expr (dump_file
, evolution_function
);
1530 fprintf (dump_file
, "))\n");
1533 return evolution_function
;
1536 /* Looks to see if VAR is a copy of a constant (via straightforward assignments
1537 or degenerate phi's). If so, returns the constant; else, returns VAR. */
1540 follow_copies_to_constant (tree var
)
1543 while (TREE_CODE (res
) == SSA_NAME
)
1545 gimple
*def
= SSA_NAME_DEF_STMT (res
);
1546 if (gphi
*phi
= dyn_cast
<gphi
*> (def
))
1548 if (tree rhs
= degenerate_phi_result (phi
))
1553 else if (gimple_assign_single_p (def
))
1554 /* Will exit loop if not an SSA_NAME. */
1555 res
= gimple_assign_rhs1 (def
);
1559 if (CONSTANT_CLASS_P (res
))
1564 /* Given a loop-phi-node, return the initial conditions of the
1565 variable on entry of the loop. When the CCP has propagated
1566 constants into the loop-phi-node, the initial condition is
1567 instantiated, otherwise the initial condition is kept symbolic.
1568 This analyzer does not analyze the evolution outside the current
1569 loop, and leaves this task to the on-demand tree reconstructor. */
1572 analyze_initial_condition (gphi
*loop_phi_node
)
1575 tree init_cond
= chrec_not_analyzed_yet
;
1576 struct loop
*loop
= loop_containing_stmt (loop_phi_node
);
1578 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1580 fprintf (dump_file
, "(analyze_initial_condition \n");
1581 fprintf (dump_file
, " (loop_phi_node = \n");
1582 print_gimple_stmt (dump_file
, loop_phi_node
, 0);
1583 fprintf (dump_file
, ")\n");
1586 n
= gimple_phi_num_args (loop_phi_node
);
1587 for (i
= 0; i
< n
; i
++)
1589 tree branch
= PHI_ARG_DEF (loop_phi_node
, i
);
1590 basic_block bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1592 /* When the branch is oriented to the loop's body, it does
1593 not contribute to the initial condition. */
1594 if (flow_bb_inside_loop_p (loop
, bb
))
1597 if (init_cond
== chrec_not_analyzed_yet
)
1603 if (TREE_CODE (branch
) == SSA_NAME
)
1605 init_cond
= chrec_dont_know
;
1609 init_cond
= chrec_merge (init_cond
, branch
);
1612 /* Ooops -- a loop without an entry??? */
1613 if (init_cond
== chrec_not_analyzed_yet
)
1614 init_cond
= chrec_dont_know
;
1616 /* We may not have fully constant propagated IL. Handle degenerate PHIs here
1617 to not miss important early loop unrollings. */
1618 init_cond
= follow_copies_to_constant (init_cond
);
1620 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1622 fprintf (dump_file
, " (init_cond = ");
1623 print_generic_expr (dump_file
, init_cond
);
1624 fprintf (dump_file
, "))\n");
1630 /* Analyze the scalar evolution for LOOP_PHI_NODE. */
1633 interpret_loop_phi (struct loop
*loop
, gphi
*loop_phi_node
)
1636 struct loop
*phi_loop
= loop_containing_stmt (loop_phi_node
);
1639 gcc_assert (phi_loop
== loop
);
1641 /* Otherwise really interpret the loop phi. */
1642 init_cond
= analyze_initial_condition (loop_phi_node
);
1643 res
= analyze_evolution_in_loop (loop_phi_node
, init_cond
);
1645 /* Verify we maintained the correct initial condition throughout
1646 possible conversions in the SSA chain. */
1647 if (res
!= chrec_dont_know
)
1649 tree new_init
= res
;
1650 if (CONVERT_EXPR_P (res
)
1651 && TREE_CODE (TREE_OPERAND (res
, 0)) == POLYNOMIAL_CHREC
)
1652 new_init
= fold_convert (TREE_TYPE (res
),
1653 CHREC_LEFT (TREE_OPERAND (res
, 0)));
1654 else if (TREE_CODE (res
) == POLYNOMIAL_CHREC
)
1655 new_init
= CHREC_LEFT (res
);
1656 STRIP_USELESS_TYPE_CONVERSION (new_init
);
1657 if (TREE_CODE (new_init
) == POLYNOMIAL_CHREC
1658 || !operand_equal_p (init_cond
, new_init
, 0))
1659 return chrec_dont_know
;
1665 /* This function merges the branches of a condition-phi-node,
1666 contained in the outermost loop, and whose arguments are already
1670 interpret_condition_phi (struct loop
*loop
, gphi
*condition_phi
)
1672 int i
, n
= gimple_phi_num_args (condition_phi
);
1673 tree res
= chrec_not_analyzed_yet
;
1675 for (i
= 0; i
< n
; i
++)
1679 if (backedge_phi_arg_p (condition_phi
, i
))
1681 res
= chrec_dont_know
;
1685 branch_chrec
= analyze_scalar_evolution
1686 (loop
, PHI_ARG_DEF (condition_phi
, i
));
1688 res
= chrec_merge (res
, branch_chrec
);
1689 if (res
== chrec_dont_know
)
1696 /* Interpret the operation RHS1 OP RHS2. If we didn't
1697 analyze this node before, follow the definitions until ending
1698 either on an analyzed GIMPLE_ASSIGN, or on a loop-phi-node. On the
1699 return path, this function propagates evolutions (ala constant copy
1700 propagation). OPND1 is not a GIMPLE expression because we could
1701 analyze the effect of an inner loop: see interpret_loop_phi. */
1704 interpret_rhs_expr (struct loop
*loop
, gimple
*at_stmt
,
1705 tree type
, tree rhs1
, enum tree_code code
, tree rhs2
)
1707 tree res
, chrec1
, chrec2
, ctype
;
1710 if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
1712 if (is_gimple_min_invariant (rhs1
))
1713 return chrec_convert (type
, rhs1
, at_stmt
);
1715 if (code
== SSA_NAME
)
1716 return chrec_convert (type
, analyze_scalar_evolution (loop
, rhs1
),
1719 if (code
== ASSERT_EXPR
)
1721 rhs1
= ASSERT_EXPR_VAR (rhs1
);
1722 return chrec_convert (type
, analyze_scalar_evolution (loop
, rhs1
),
1730 if (TREE_CODE (TREE_OPERAND (rhs1
, 0)) == MEM_REF
1731 || handled_component_p (TREE_OPERAND (rhs1
, 0)))
1734 HOST_WIDE_INT bitsize
, bitpos
;
1735 int unsignedp
, reversep
;
1741 base
= get_inner_reference (TREE_OPERAND (rhs1
, 0),
1742 &bitsize
, &bitpos
, &offset
, &mode
,
1743 &unsignedp
, &reversep
, &volatilep
);
1745 if (TREE_CODE (base
) == MEM_REF
)
1747 rhs2
= TREE_OPERAND (base
, 1);
1748 rhs1
= TREE_OPERAND (base
, 0);
1750 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1751 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1752 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1753 chrec2
= chrec_convert (TREE_TYPE (rhs2
), chrec2
, at_stmt
);
1754 chrec1
= instantiate_parameters (loop
, chrec1
);
1755 chrec2
= instantiate_parameters (loop
, chrec2
);
1756 res
= chrec_fold_plus (type
, chrec1
, chrec2
);
1760 chrec1
= analyze_scalar_evolution_for_address_of (loop
, base
);
1761 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1765 if (offset
!= NULL_TREE
)
1767 chrec2
= analyze_scalar_evolution (loop
, offset
);
1768 chrec2
= chrec_convert (TREE_TYPE (offset
), chrec2
, at_stmt
);
1769 chrec2
= instantiate_parameters (loop
, chrec2
);
1770 res
= chrec_fold_plus (type
, res
, chrec2
);
1775 gcc_assert ((bitpos
% BITS_PER_UNIT
) == 0);
1777 unitpos
= size_int (bitpos
/ BITS_PER_UNIT
);
1778 chrec3
= analyze_scalar_evolution (loop
, unitpos
);
1779 chrec3
= chrec_convert (TREE_TYPE (unitpos
), chrec3
, at_stmt
);
1780 chrec3
= instantiate_parameters (loop
, chrec3
);
1781 res
= chrec_fold_plus (type
, res
, chrec3
);
1785 res
= chrec_dont_know
;
1788 case POINTER_PLUS_EXPR
:
1789 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1790 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1791 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1792 chrec2
= chrec_convert (TREE_TYPE (rhs2
), chrec2
, at_stmt
);
1793 chrec1
= instantiate_parameters (loop
, chrec1
);
1794 chrec2
= instantiate_parameters (loop
, chrec2
);
1795 res
= chrec_fold_plus (type
, chrec1
, chrec2
);
1799 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1800 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1802 /* When the stmt is conditionally executed re-write the CHREC
1803 into a form that has well-defined behavior on overflow. */
1805 && INTEGRAL_TYPE_P (type
)
1806 && ! TYPE_OVERFLOW_WRAPS (type
)
1807 && ! dominated_by_p (CDI_DOMINATORS
, loop
->latch
,
1808 gimple_bb (at_stmt
)))
1809 ctype
= unsigned_type_for (type
);
1810 chrec1
= chrec_convert (ctype
, chrec1
, at_stmt
);
1811 chrec2
= chrec_convert (ctype
, chrec2
, at_stmt
);
1812 chrec1
= instantiate_parameters (loop
, chrec1
);
1813 chrec2
= instantiate_parameters (loop
, chrec2
);
1814 res
= chrec_fold_plus (ctype
, chrec1
, chrec2
);
1816 res
= chrec_convert (type
, res
, at_stmt
);
1820 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1821 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1823 /* When the stmt is conditionally executed re-write the CHREC
1824 into a form that has well-defined behavior on overflow. */
1826 && INTEGRAL_TYPE_P (type
)
1827 && ! TYPE_OVERFLOW_WRAPS (type
)
1828 && ! dominated_by_p (CDI_DOMINATORS
,
1829 loop
->latch
, gimple_bb (at_stmt
)))
1830 ctype
= unsigned_type_for (type
);
1831 chrec1
= chrec_convert (ctype
, chrec1
, at_stmt
);
1832 chrec2
= chrec_convert (ctype
, chrec2
, at_stmt
);
1833 chrec1
= instantiate_parameters (loop
, chrec1
);
1834 chrec2
= instantiate_parameters (loop
, chrec2
);
1835 res
= chrec_fold_minus (ctype
, chrec1
, chrec2
);
1837 res
= chrec_convert (type
, res
, at_stmt
);
1841 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1843 /* When the stmt is conditionally executed re-write the CHREC
1844 into a form that has well-defined behavior on overflow. */
1846 && INTEGRAL_TYPE_P (type
)
1847 && ! TYPE_OVERFLOW_WRAPS (type
)
1848 && ! dominated_by_p (CDI_DOMINATORS
,
1849 loop
->latch
, gimple_bb (at_stmt
)))
1850 ctype
= unsigned_type_for (type
);
1851 chrec1
= chrec_convert (ctype
, chrec1
, at_stmt
);
1852 /* TYPE may be integer, real or complex, so use fold_convert. */
1853 chrec1
= instantiate_parameters (loop
, chrec1
);
1854 res
= chrec_fold_multiply (ctype
, chrec1
,
1855 fold_convert (ctype
, integer_minus_one_node
));
1857 res
= chrec_convert (type
, res
, at_stmt
);
1861 /* Handle ~X as -1 - X. */
1862 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1863 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1864 chrec1
= instantiate_parameters (loop
, chrec1
);
1865 res
= chrec_fold_minus (type
,
1866 fold_convert (type
, integer_minus_one_node
),
1871 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1872 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1874 /* When the stmt is conditionally executed re-write the CHREC
1875 into a form that has well-defined behavior on overflow. */
1877 && INTEGRAL_TYPE_P (type
)
1878 && ! TYPE_OVERFLOW_WRAPS (type
)
1879 && ! dominated_by_p (CDI_DOMINATORS
,
1880 loop
->latch
, gimple_bb (at_stmt
)))
1881 ctype
= unsigned_type_for (type
);
1882 chrec1
= chrec_convert (ctype
, chrec1
, at_stmt
);
1883 chrec2
= chrec_convert (ctype
, chrec2
, at_stmt
);
1884 chrec1
= instantiate_parameters (loop
, chrec1
);
1885 chrec2
= instantiate_parameters (loop
, chrec2
);
1886 res
= chrec_fold_multiply (ctype
, chrec1
, chrec2
);
1888 res
= chrec_convert (type
, res
, at_stmt
);
1893 /* Handle A<<B as A * (1<<B). */
1894 tree uns
= unsigned_type_for (type
);
1895 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1896 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1897 chrec1
= chrec_convert (uns
, chrec1
, at_stmt
);
1898 chrec1
= instantiate_parameters (loop
, chrec1
);
1899 chrec2
= instantiate_parameters (loop
, chrec2
);
1901 tree one
= build_int_cst (uns
, 1);
1902 chrec2
= fold_build2 (LSHIFT_EXPR
, uns
, one
, chrec2
);
1903 res
= chrec_fold_multiply (uns
, chrec1
, chrec2
);
1904 res
= chrec_convert (type
, res
, at_stmt
);
1909 /* In case we have a truncation of a widened operation that in
1910 the truncated type has undefined overflow behavior analyze
1911 the operation done in an unsigned type of the same precision
1912 as the final truncation. We cannot derive a scalar evolution
1913 for the widened operation but for the truncated result. */
1914 if (TREE_CODE (type
) == INTEGER_TYPE
1915 && TREE_CODE (TREE_TYPE (rhs1
)) == INTEGER_TYPE
1916 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (rhs1
))
1917 && TYPE_OVERFLOW_UNDEFINED (type
)
1918 && TREE_CODE (rhs1
) == SSA_NAME
1919 && (def
= SSA_NAME_DEF_STMT (rhs1
))
1920 && is_gimple_assign (def
)
1921 && TREE_CODE_CLASS (gimple_assign_rhs_code (def
)) == tcc_binary
1922 && TREE_CODE (gimple_assign_rhs2 (def
)) == INTEGER_CST
)
1924 tree utype
= unsigned_type_for (type
);
1925 chrec1
= interpret_rhs_expr (loop
, at_stmt
, utype
,
1926 gimple_assign_rhs1 (def
),
1927 gimple_assign_rhs_code (def
),
1928 gimple_assign_rhs2 (def
));
1931 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1932 res
= chrec_convert (type
, chrec1
, at_stmt
, true, rhs1
);
1936 /* Given int variable A, handle A&0xffff as (int)(unsigned short)A.
1937 If A is SCEV and its value is in the range of representable set
1938 of type unsigned short, the result expression is a (no-overflow)
1940 res
= chrec_dont_know
;
1941 if (tree_fits_uhwi_p (rhs2
))
1944 unsigned HOST_WIDE_INT val
= tree_to_uhwi (rhs2
);
1947 /* Skip if value of rhs2 wraps in unsigned HOST_WIDE_INT or
1948 it's not the maximum value of a smaller type than rhs1. */
1950 && (precision
= exact_log2 (val
)) > 0
1951 && (unsigned) precision
< TYPE_PRECISION (TREE_TYPE (rhs1
)))
1953 tree utype
= build_nonstandard_integer_type (precision
, 1);
1955 if (TYPE_PRECISION (utype
) < TYPE_PRECISION (TREE_TYPE (rhs1
)))
1957 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1958 chrec1
= chrec_convert (utype
, chrec1
, at_stmt
);
1959 res
= chrec_convert (TREE_TYPE (rhs1
), chrec1
, at_stmt
);
1966 res
= chrec_dont_know
;
1973 /* Interpret the expression EXPR. */
1976 interpret_expr (struct loop
*loop
, gimple
*at_stmt
, tree expr
)
1978 enum tree_code code
;
1979 tree type
= TREE_TYPE (expr
), op0
, op1
;
1981 if (automatically_generated_chrec_p (expr
))
1984 if (TREE_CODE (expr
) == POLYNOMIAL_CHREC
1985 || get_gimple_rhs_class (TREE_CODE (expr
)) == GIMPLE_TERNARY_RHS
)
1986 return chrec_dont_know
;
1988 extract_ops_from_tree (expr
, &code
, &op0
, &op1
);
1990 return interpret_rhs_expr (loop
, at_stmt
, type
,
1994 /* Interpret the rhs of the assignment STMT. */
1997 interpret_gimple_assign (struct loop
*loop
, gimple
*stmt
)
1999 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
2000 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2002 return interpret_rhs_expr (loop
, stmt
, type
,
2003 gimple_assign_rhs1 (stmt
), code
,
2004 gimple_assign_rhs2 (stmt
));
2009 /* This section contains all the entry points:
2010 - number_of_iterations_in_loop,
2011 - analyze_scalar_evolution,
2012 - instantiate_parameters.
2015 /* Helper recursive function. */
2018 analyze_scalar_evolution_1 (struct loop
*loop
, tree var
)
2022 struct loop
*def_loop
;
2025 if (TREE_CODE (var
) != SSA_NAME
)
2026 return interpret_expr (loop
, NULL
, var
);
2028 def
= SSA_NAME_DEF_STMT (var
);
2029 bb
= gimple_bb (def
);
2030 def_loop
= bb
->loop_father
;
2032 if (!flow_bb_inside_loop_p (loop
, bb
))
2034 /* Keep symbolic form, but look through obvious copies for constants. */
2035 res
= follow_copies_to_constant (var
);
2039 if (loop
!= def_loop
)
2041 res
= analyze_scalar_evolution_1 (def_loop
, var
);
2042 struct loop
*loop_to_skip
= superloop_at_depth (def_loop
,
2043 loop_depth (loop
) + 1);
2044 res
= compute_overall_effect_of_inner_loop (loop_to_skip
, res
);
2045 if (chrec_contains_symbols_defined_in_loop (res
, loop
->num
))
2046 res
= analyze_scalar_evolution_1 (loop
, res
);
2050 switch (gimple_code (def
))
2053 res
= interpret_gimple_assign (loop
, def
);
2057 if (loop_phi_node_p (def
))
2058 res
= interpret_loop_phi (loop
, as_a
<gphi
*> (def
));
2060 res
= interpret_condition_phi (loop
, as_a
<gphi
*> (def
));
2064 res
= chrec_dont_know
;
2070 /* Keep the symbolic form. */
2071 if (res
== chrec_dont_know
)
2074 if (loop
== def_loop
)
2075 set_scalar_evolution (block_before_loop (loop
), var
, res
);
2080 /* Analyzes and returns the scalar evolution of the ssa_name VAR in
2081 LOOP. LOOP is the loop in which the variable is used.
2083 Example of use: having a pointer VAR to a SSA_NAME node, STMT a
2084 pointer to the statement that uses this variable, in order to
2085 determine the evolution function of the variable, use the following
2088 loop_p loop = loop_containing_stmt (stmt);
2089 tree chrec_with_symbols = analyze_scalar_evolution (loop, var);
2090 tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols);
2094 analyze_scalar_evolution (struct loop
*loop
, tree var
)
2098 /* ??? Fix callers. */
2102 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2104 fprintf (dump_file
, "(analyze_scalar_evolution \n");
2105 fprintf (dump_file
, " (loop_nb = %d)\n", loop
->num
);
2106 fprintf (dump_file
, " (scalar = ");
2107 print_generic_expr (dump_file
, var
);
2108 fprintf (dump_file
, ")\n");
2111 res
= get_scalar_evolution (block_before_loop (loop
), var
);
2112 if (res
== chrec_not_analyzed_yet
)
2113 res
= analyze_scalar_evolution_1 (loop
, var
);
2115 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2116 fprintf (dump_file
, ")\n");
2121 /* Analyzes and returns the scalar evolution of VAR address in LOOP. */
2124 analyze_scalar_evolution_for_address_of (struct loop
*loop
, tree var
)
2126 return analyze_scalar_evolution (loop
, build_fold_addr_expr (var
));
2129 /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
2130 WRTO_LOOP (which should be a superloop of USE_LOOP)
2132 FOLDED_CASTS is set to true if resolve_mixers used
2133 chrec_convert_aggressive (TODO -- not really, we are way too conservative
2134 at the moment in order to keep things simple).
2136 To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following
2139 for (i = 0; i < 100; i++) -- loop 1
2141 for (j = 0; j < 100; j++) -- loop 2
2148 for (t = 0; t < 100; t++) -- loop 3
2155 Both k1 and k2 are invariants in loop3, thus
2156 analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1
2157 analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2
2159 As they are invariant, it does not matter whether we consider their
2160 usage in loop 3 or loop 2, hence
2161 analyze_scalar_evolution_in_loop (loop2, loop3, k1) =
2162 analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i
2163 analyze_scalar_evolution_in_loop (loop2, loop3, k2) =
2164 analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2
2166 Similarly for their evolutions with respect to loop 1. The values of K2
2167 in the use in loop 2 vary independently on loop 1, thus we cannot express
2168 the evolution with respect to loop 1:
2169 analyze_scalar_evolution_in_loop (loop1, loop3, k1) =
2170 analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1
2171 analyze_scalar_evolution_in_loop (loop1, loop3, k2) =
2172 analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know
2174 The value of k2 in the use in loop 1 is known, though:
2175 analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1
2176 analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100
2180 analyze_scalar_evolution_in_loop (struct loop
*wrto_loop
, struct loop
*use_loop
,
2181 tree version
, bool *folded_casts
)
2184 tree ev
= version
, tmp
;
2186 /* We cannot just do
2188 tmp = analyze_scalar_evolution (use_loop, version);
2189 ev = resolve_mixers (wrto_loop, tmp, folded_casts);
2191 as resolve_mixers would query the scalar evolution with respect to
2192 wrto_loop. For example, in the situation described in the function
2193 comment, suppose that wrto_loop = loop1, use_loop = loop3 and
2196 analyze_scalar_evolution (use_loop, version) = k2
2198 and resolve_mixers (loop1, k2, folded_casts) finds that the value of
2199 k2 in loop 1 is 100, which is a wrong result, since we are interested
2200 in the value in loop 3.
2202 Instead, we need to proceed from use_loop to wrto_loop loop by loop,
2203 each time checking that there is no evolution in the inner loop. */
2206 *folded_casts
= false;
2209 tmp
= analyze_scalar_evolution (use_loop
, ev
);
2210 ev
= resolve_mixers (use_loop
, tmp
, folded_casts
);
2212 if (use_loop
== wrto_loop
)
2215 /* If the value of the use changes in the inner loop, we cannot express
2216 its value in the outer loop (we might try to return interval chrec,
2217 but we do not have a user for it anyway) */
2218 if (!no_evolution_in_loop_p (ev
, use_loop
->num
, &val
)
2220 return chrec_dont_know
;
2222 use_loop
= loop_outer (use_loop
);
2227 /* Hashtable helpers for a temporary hash-table used when
2228 instantiating a CHREC or resolving mixers. For this use
2229 instantiated_below is always the same. */
2231 struct instantiate_cache_type
2234 vec
<scev_info_str
> entries
;
2236 instantiate_cache_type () : map (NULL
), entries (vNULL
) {}
2237 ~instantiate_cache_type ();
2238 tree
get (unsigned slot
) { return entries
[slot
].chrec
; }
2239 void set (unsigned slot
, tree chrec
) { entries
[slot
].chrec
= chrec
; }
2242 instantiate_cache_type::~instantiate_cache_type ()
2251 /* Cache to avoid infinite recursion when instantiating an SSA name.
2252 Live during the outermost instantiate_scev or resolve_mixers call. */
2253 static instantiate_cache_type
*global_cache
;
2255 /* Computes a hash function for database element ELT. */
2257 static inline hashval_t
2258 hash_idx_scev_info (const void *elt_
)
2260 unsigned idx
= ((size_t) elt_
) - 2;
2261 return scev_info_hasher::hash (&global_cache
->entries
[idx
]);
2264 /* Compares database elements E1 and E2. */
2267 eq_idx_scev_info (const void *e1
, const void *e2
)
2269 unsigned idx1
= ((size_t) e1
) - 2;
2270 return scev_info_hasher::equal (&global_cache
->entries
[idx1
],
2271 (const scev_info_str
*) e2
);
2274 /* Returns from CACHE the slot number of the cached chrec for NAME. */
2277 get_instantiated_value_entry (instantiate_cache_type
&cache
,
2278 tree name
, edge instantiate_below
)
2282 cache
.map
= htab_create (10, hash_idx_scev_info
, eq_idx_scev_info
, NULL
);
2283 cache
.entries
.create (10);
2287 e
.name_version
= SSA_NAME_VERSION (name
);
2288 e
.instantiated_below
= instantiate_below
->dest
->index
;
2289 void **slot
= htab_find_slot_with_hash (cache
.map
, &e
,
2290 scev_info_hasher::hash (&e
), INSERT
);
2293 e
.chrec
= chrec_not_analyzed_yet
;
2294 *slot
= (void *)(size_t)(cache
.entries
.length () + 2);
2295 cache
.entries
.safe_push (e
);
2298 return ((size_t)*slot
) - 2;
2302 /* Return the closed_loop_phi node for VAR. If there is none, return
2306 loop_closed_phi_def (tree var
)
2313 if (var
== NULL_TREE
2314 || TREE_CODE (var
) != SSA_NAME
)
2317 loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (var
));
2318 exit
= single_exit (loop
);
2322 for (psi
= gsi_start_phis (exit
->dest
); !gsi_end_p (psi
); gsi_next (&psi
))
2325 if (PHI_ARG_DEF_FROM_EDGE (phi
, exit
) == var
)
2326 return PHI_RESULT (phi
);
2332 static tree
instantiate_scev_r (edge
, struct loop
*, struct loop
*,
2335 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2336 and EVOLUTION_LOOP, that were left under a symbolic form.
2338 CHREC is an SSA_NAME to be instantiated.
2340 CACHE is the cache of already instantiated values.
2342 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2343 conversions that may wrap in signed/pointer type are folded, as long
2344 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2345 then we don't do such fold.
2347 SIZE_EXPR is used for computing the size of the expression to be
2348 instantiated, and to stop if it exceeds some limit. */
2351 instantiate_scev_name (edge instantiate_below
,
2352 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2354 bool *fold_conversions
,
2358 struct loop
*def_loop
;
2359 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (chrec
));
2361 /* A parameter, nothing to do. */
2363 || !dominated_by_p (CDI_DOMINATORS
, def_bb
, instantiate_below
->dest
))
2366 /* We cache the value of instantiated variable to avoid exponential
2367 time complexity due to reevaluations. We also store the convenient
2368 value in the cache in order to prevent infinite recursion -- we do
2369 not want to instantiate the SSA_NAME if it is in a mixer
2370 structure. This is used for avoiding the instantiation of
2371 recursively defined functions, such as:
2373 | a_2 -> {0, +, 1, +, a_2}_1 */
2375 unsigned si
= get_instantiated_value_entry (*global_cache
,
2376 chrec
, instantiate_below
);
2377 if (global_cache
->get (si
) != chrec_not_analyzed_yet
)
2378 return global_cache
->get (si
);
2380 /* On recursion return chrec_dont_know. */
2381 global_cache
->set (si
, chrec_dont_know
);
2383 def_loop
= find_common_loop (evolution_loop
, def_bb
->loop_father
);
2385 if (! dominated_by_p (CDI_DOMINATORS
,
2386 def_loop
->header
, instantiate_below
->dest
))
2388 gimple
*def
= SSA_NAME_DEF_STMT (chrec
);
2389 if (gassign
*ass
= dyn_cast
<gassign
*> (def
))
2391 switch (gimple_assign_rhs_class (ass
))
2393 case GIMPLE_UNARY_RHS
:
2395 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2396 inner_loop
, gimple_assign_rhs1 (ass
),
2397 fold_conversions
, size_expr
);
2398 if (op0
== chrec_dont_know
)
2399 return chrec_dont_know
;
2400 res
= fold_build1 (gimple_assign_rhs_code (ass
),
2401 TREE_TYPE (chrec
), op0
);
2404 case GIMPLE_BINARY_RHS
:
2406 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2407 inner_loop
, gimple_assign_rhs1 (ass
),
2408 fold_conversions
, size_expr
);
2409 if (op0
== chrec_dont_know
)
2410 return chrec_dont_know
;
2411 tree op1
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2412 inner_loop
, gimple_assign_rhs2 (ass
),
2413 fold_conversions
, size_expr
);
2414 if (op1
== chrec_dont_know
)
2415 return chrec_dont_know
;
2416 res
= fold_build2 (gimple_assign_rhs_code (ass
),
2417 TREE_TYPE (chrec
), op0
, op1
);
2421 res
= chrec_dont_know
;
2425 res
= chrec_dont_know
;
2426 global_cache
->set (si
, res
);
2430 /* If the analysis yields a parametric chrec, instantiate the
2432 res
= analyze_scalar_evolution (def_loop
, chrec
);
2434 /* Don't instantiate default definitions. */
2435 if (TREE_CODE (res
) == SSA_NAME
2436 && SSA_NAME_IS_DEFAULT_DEF (res
))
2439 /* Don't instantiate loop-closed-ssa phi nodes. */
2440 else if (TREE_CODE (res
) == SSA_NAME
2441 && loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res
)))
2442 > loop_depth (def_loop
))
2445 res
= loop_closed_phi_def (chrec
);
2449 /* When there is no loop_closed_phi_def, it means that the
2450 variable is not used after the loop: try to still compute the
2451 value of the variable when exiting the loop. */
2452 if (res
== NULL_TREE
)
2454 loop_p loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (chrec
));
2455 res
= analyze_scalar_evolution (loop
, chrec
);
2456 res
= compute_overall_effect_of_inner_loop (loop
, res
);
2457 res
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2459 fold_conversions
, size_expr
);
2461 else if (dominated_by_p (CDI_DOMINATORS
,
2462 gimple_bb (SSA_NAME_DEF_STMT (res
)),
2463 instantiate_below
->dest
))
2464 res
= chrec_dont_know
;
2467 else if (res
!= chrec_dont_know
)
2470 && def_bb
->loop_father
!= inner_loop
2471 && !flow_loop_nested_p (def_bb
->loop_father
, inner_loop
))
2472 /* ??? We could try to compute the overall effect of the loop here. */
2473 res
= chrec_dont_know
;
2475 res
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2477 fold_conversions
, size_expr
);
2480 /* Store the correct value to the cache. */
2481 global_cache
->set (si
, res
);
2485 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2486 and EVOLUTION_LOOP, that were left under a symbolic form.
2488 CHREC is a polynomial chain of recurrence to be instantiated.
2490 CACHE is the cache of already instantiated values.
2492 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2493 conversions that may wrap in signed/pointer type are folded, as long
2494 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2495 then we don't do such fold.
2497 SIZE_EXPR is used for computing the size of the expression to be
2498 instantiated, and to stop if it exceeds some limit. */
2501 instantiate_scev_poly (edge instantiate_below
,
2502 struct loop
*evolution_loop
, struct loop
*,
2503 tree chrec
, bool *fold_conversions
, int size_expr
)
2506 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2507 get_chrec_loop (chrec
),
2508 CHREC_LEFT (chrec
), fold_conversions
,
2510 if (op0
== chrec_dont_know
)
2511 return chrec_dont_know
;
2513 op1
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2514 get_chrec_loop (chrec
),
2515 CHREC_RIGHT (chrec
), fold_conversions
,
2517 if (op1
== chrec_dont_know
)
2518 return chrec_dont_know
;
2520 if (CHREC_LEFT (chrec
) != op0
2521 || CHREC_RIGHT (chrec
) != op1
)
2523 op1
= chrec_convert_rhs (chrec_type (op0
), op1
, NULL
);
2524 chrec
= build_polynomial_chrec (CHREC_VARIABLE (chrec
), op0
, op1
);
2530 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2531 and EVOLUTION_LOOP, that were left under a symbolic form.
2533 "C0 CODE C1" is a binary expression of type TYPE to be instantiated.
2535 CACHE is the cache of already instantiated values.
2537 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2538 conversions that may wrap in signed/pointer type are folded, as long
2539 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2540 then we don't do such fold.
2542 SIZE_EXPR is used for computing the size of the expression to be
2543 instantiated, and to stop if it exceeds some limit. */
2546 instantiate_scev_binary (edge instantiate_below
,
2547 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2548 tree chrec
, enum tree_code code
,
2549 tree type
, tree c0
, tree c1
,
2550 bool *fold_conversions
, int size_expr
)
2553 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
, inner_loop
,
2554 c0
, fold_conversions
, size_expr
);
2555 if (op0
== chrec_dont_know
)
2556 return chrec_dont_know
;
2558 op1
= instantiate_scev_r (instantiate_below
, evolution_loop
, inner_loop
,
2559 c1
, fold_conversions
, size_expr
);
2560 if (op1
== chrec_dont_know
)
2561 return chrec_dont_know
;
2566 op0
= chrec_convert (type
, op0
, NULL
);
2567 op1
= chrec_convert_rhs (type
, op1
, NULL
);
2571 case POINTER_PLUS_EXPR
:
2573 return chrec_fold_plus (type
, op0
, op1
);
2576 return chrec_fold_minus (type
, op0
, op1
);
2579 return chrec_fold_multiply (type
, op0
, op1
);
2586 return chrec
? chrec
: fold_build2 (code
, type
, c0
, c1
);
2589 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2590 and EVOLUTION_LOOP, that were left under a symbolic form.
2592 "CHREC" that stands for a convert expression "(TYPE) OP" is to be
2595 CACHE is the cache of already instantiated values.
2597 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2598 conversions that may wrap in signed/pointer type are folded, as long
2599 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2600 then we don't do such fold.
2602 SIZE_EXPR is used for computing the size of the expression to be
2603 instantiated, and to stop if it exceeds some limit. */
2606 instantiate_scev_convert (edge instantiate_below
,
2607 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2608 tree chrec
, tree type
, tree op
,
2609 bool *fold_conversions
, int size_expr
)
2611 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2613 fold_conversions
, size_expr
);
2615 if (op0
== chrec_dont_know
)
2616 return chrec_dont_know
;
2618 if (fold_conversions
)
2620 tree tmp
= chrec_convert_aggressive (type
, op0
, fold_conversions
);
2624 /* If we used chrec_convert_aggressive, we can no longer assume that
2625 signed chrecs do not overflow, as chrec_convert does, so avoid
2626 calling it in that case. */
2627 if (*fold_conversions
)
2629 if (chrec
&& op0
== op
)
2632 return fold_convert (type
, op0
);
2636 return chrec_convert (type
, op0
, NULL
);
2639 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2640 and EVOLUTION_LOOP, that were left under a symbolic form.
2642 CHREC is a BIT_NOT_EXPR or a NEGATE_EXPR expression to be instantiated.
2643 Handle ~X as -1 - X.
2644 Handle -X as -1 * X.
2646 CACHE is the cache of already instantiated values.
2648 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2649 conversions that may wrap in signed/pointer type are folded, as long
2650 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2651 then we don't do such fold.
2653 SIZE_EXPR is used for computing the size of the expression to be
2654 instantiated, and to stop if it exceeds some limit. */
2657 instantiate_scev_not (edge instantiate_below
,
2658 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2660 enum tree_code code
, tree type
, tree op
,
2661 bool *fold_conversions
, int size_expr
)
2663 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2665 fold_conversions
, size_expr
);
2667 if (op0
== chrec_dont_know
)
2668 return chrec_dont_know
;
2672 op0
= chrec_convert (type
, op0
, NULL
);
2677 return chrec_fold_minus
2678 (type
, fold_convert (type
, integer_minus_one_node
), op0
);
2681 return chrec_fold_multiply
2682 (type
, fold_convert (type
, integer_minus_one_node
), op0
);
2689 return chrec
? chrec
: fold_build1 (code
, type
, op0
);
2692 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2693 and EVOLUTION_LOOP, that were left under a symbolic form.
2695 CHREC is the scalar evolution to instantiate.
2697 CACHE is the cache of already instantiated values.
2699 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2700 conversions that may wrap in signed/pointer type are folded, as long
2701 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2702 then we don't do such fold.
2704 SIZE_EXPR is used for computing the size of the expression to be
2705 instantiated, and to stop if it exceeds some limit. */
2708 instantiate_scev_r (edge instantiate_below
,
2709 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2711 bool *fold_conversions
, int size_expr
)
2713 /* Give up if the expression is larger than the MAX that we allow. */
2714 if (size_expr
++ > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE
))
2715 return chrec_dont_know
;
2717 if (chrec
== NULL_TREE
2718 || automatically_generated_chrec_p (chrec
)
2719 || is_gimple_min_invariant (chrec
))
2722 switch (TREE_CODE (chrec
))
2725 return instantiate_scev_name (instantiate_below
, evolution_loop
,
2727 fold_conversions
, size_expr
);
2729 case POLYNOMIAL_CHREC
:
2730 return instantiate_scev_poly (instantiate_below
, evolution_loop
,
2732 fold_conversions
, size_expr
);
2734 case POINTER_PLUS_EXPR
:
2738 return instantiate_scev_binary (instantiate_below
, evolution_loop
,
2740 TREE_CODE (chrec
), chrec_type (chrec
),
2741 TREE_OPERAND (chrec
, 0),
2742 TREE_OPERAND (chrec
, 1),
2743 fold_conversions
, size_expr
);
2746 return instantiate_scev_convert (instantiate_below
, evolution_loop
,
2748 TREE_TYPE (chrec
), TREE_OPERAND (chrec
, 0),
2749 fold_conversions
, size_expr
);
2753 return instantiate_scev_not (instantiate_below
, evolution_loop
,
2755 TREE_CODE (chrec
), TREE_TYPE (chrec
),
2756 TREE_OPERAND (chrec
, 0),
2757 fold_conversions
, size_expr
);
2760 if (is_gimple_min_invariant (chrec
))
2763 case SCEV_NOT_KNOWN
:
2764 return chrec_dont_know
;
2770 if (CONSTANT_CLASS_P (chrec
))
2772 return chrec_dont_know
;
2776 /* Analyze all the parameters of the chrec that were left under a
2777 symbolic form. INSTANTIATE_BELOW is the basic block that stops the
2778 recursive instantiation of parameters: a parameter is a variable
2779 that is defined in a basic block that dominates INSTANTIATE_BELOW or
2780 a function parameter. */
2783 instantiate_scev (edge instantiate_below
, struct loop
*evolution_loop
,
2788 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2790 fprintf (dump_file
, "(instantiate_scev \n");
2791 fprintf (dump_file
, " (instantiate_below = %d -> %d)\n",
2792 instantiate_below
->src
->index
, instantiate_below
->dest
->index
);
2794 fprintf (dump_file
, " (evolution_loop = %d)\n", evolution_loop
->num
);
2795 fprintf (dump_file
, " (chrec = ");
2796 print_generic_expr (dump_file
, chrec
);
2797 fprintf (dump_file
, ")\n");
2803 global_cache
= new instantiate_cache_type
;
2807 res
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2808 NULL
, chrec
, NULL
, 0);
2812 delete global_cache
;
2813 global_cache
= NULL
;
2816 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2818 fprintf (dump_file
, " (res = ");
2819 print_generic_expr (dump_file
, res
);
2820 fprintf (dump_file
, "))\n");
2826 /* Similar to instantiate_parameters, but does not introduce the
2827 evolutions in outer loops for LOOP invariants in CHREC, and does not
2828 care about causing overflows, as long as they do not affect value
2829 of an expression. */
2832 resolve_mixers (struct loop
*loop
, tree chrec
, bool *folded_casts
)
2835 bool fold_conversions
= false;
2838 global_cache
= new instantiate_cache_type
;
2842 tree ret
= instantiate_scev_r (loop_preheader_edge (loop
), loop
, NULL
,
2843 chrec
, &fold_conversions
, 0);
2845 if (folded_casts
&& !*folded_casts
)
2846 *folded_casts
= fold_conversions
;
2850 delete global_cache
;
2851 global_cache
= NULL
;
2857 /* Entry point for the analysis of the number of iterations pass.
2858 This function tries to safely approximate the number of iterations
2859 the loop will run. When this property is not decidable at compile
2860 time, the result is chrec_dont_know. Otherwise the result is a
2861 scalar or a symbolic parameter. When the number of iterations may
2862 be equal to zero and the property cannot be determined at compile
2863 time, the result is a COND_EXPR that represents in a symbolic form
2864 the conditions under which the number of iterations is not zero.
2866 Example of analysis: suppose that the loop has an exit condition:
2868 "if (b > 49) goto end_loop;"
2870 and that in a previous analysis we have determined that the
2871 variable 'b' has an evolution function:
2873 "EF = {23, +, 5}_2".
2875 When we evaluate the function at the point 5, i.e. the value of the
2876 variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
2877 and EF (6) = 53. In this case the value of 'b' on exit is '53' and
2878 the loop body has been executed 6 times. */
2881 number_of_latch_executions (struct loop
*loop
)
2884 struct tree_niter_desc niter_desc
;
2888 /* Determine whether the number of iterations in loop has already
2890 res
= loop
->nb_iterations
;
2894 may_be_zero
= NULL_TREE
;
2896 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2897 fprintf (dump_file
, "(number_of_iterations_in_loop = \n");
2899 res
= chrec_dont_know
;
2900 exit
= single_exit (loop
);
2902 if (exit
&& number_of_iterations_exit (loop
, exit
, &niter_desc
, false))
2904 may_be_zero
= niter_desc
.may_be_zero
;
2905 res
= niter_desc
.niter
;
2908 if (res
== chrec_dont_know
2910 || integer_zerop (may_be_zero
))
2912 else if (integer_nonzerop (may_be_zero
))
2913 res
= build_int_cst (TREE_TYPE (res
), 0);
2915 else if (COMPARISON_CLASS_P (may_be_zero
))
2916 res
= fold_build3 (COND_EXPR
, TREE_TYPE (res
), may_be_zero
,
2917 build_int_cst (TREE_TYPE (res
), 0), res
);
2919 res
= chrec_dont_know
;
2921 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2923 fprintf (dump_file
, " (set_nb_iterations_in_loop = ");
2924 print_generic_expr (dump_file
, res
);
2925 fprintf (dump_file
, "))\n");
2928 loop
->nb_iterations
= res
;
2933 /* Counters for the stats. */
2939 unsigned nb_affine_multivar
;
2940 unsigned nb_higher_poly
;
2941 unsigned nb_chrec_dont_know
;
2942 unsigned nb_undetermined
;
2945 /* Reset the counters. */
2948 reset_chrecs_counters (struct chrec_stats
*stats
)
2950 stats
->nb_chrecs
= 0;
2951 stats
->nb_affine
= 0;
2952 stats
->nb_affine_multivar
= 0;
2953 stats
->nb_higher_poly
= 0;
2954 stats
->nb_chrec_dont_know
= 0;
2955 stats
->nb_undetermined
= 0;
2958 /* Dump the contents of a CHREC_STATS structure. */
2961 dump_chrecs_stats (FILE *file
, struct chrec_stats
*stats
)
2963 fprintf (file
, "\n(\n");
2964 fprintf (file
, "-----------------------------------------\n");
2965 fprintf (file
, "%d\taffine univariate chrecs\n", stats
->nb_affine
);
2966 fprintf (file
, "%d\taffine multivariate chrecs\n", stats
->nb_affine_multivar
);
2967 fprintf (file
, "%d\tdegree greater than 2 polynomials\n",
2968 stats
->nb_higher_poly
);
2969 fprintf (file
, "%d\tchrec_dont_know chrecs\n", stats
->nb_chrec_dont_know
);
2970 fprintf (file
, "-----------------------------------------\n");
2971 fprintf (file
, "%d\ttotal chrecs\n", stats
->nb_chrecs
);
2972 fprintf (file
, "%d\twith undetermined coefficients\n",
2973 stats
->nb_undetermined
);
2974 fprintf (file
, "-----------------------------------------\n");
2975 fprintf (file
, "%d\tchrecs in the scev database\n",
2976 (int) scalar_evolution_info
->elements ());
2977 fprintf (file
, "%d\tsets in the scev database\n", nb_set_scev
);
2978 fprintf (file
, "%d\tgets in the scev database\n", nb_get_scev
);
2979 fprintf (file
, "-----------------------------------------\n");
2980 fprintf (file
, ")\n\n");
2983 /* Gather statistics about CHREC. */
2986 gather_chrec_stats (tree chrec
, struct chrec_stats
*stats
)
2988 if (dump_file
&& (dump_flags
& TDF_STATS
))
2990 fprintf (dump_file
, "(classify_chrec ");
2991 print_generic_expr (dump_file
, chrec
);
2992 fprintf (dump_file
, "\n");
2997 if (chrec
== NULL_TREE
)
2999 stats
->nb_undetermined
++;
3003 switch (TREE_CODE (chrec
))
3005 case POLYNOMIAL_CHREC
:
3006 if (evolution_function_is_affine_p (chrec
))
3008 if (dump_file
&& (dump_flags
& TDF_STATS
))
3009 fprintf (dump_file
, " affine_univariate\n");
3012 else if (evolution_function_is_affine_multivariate_p (chrec
, 0))
3014 if (dump_file
&& (dump_flags
& TDF_STATS
))
3015 fprintf (dump_file
, " affine_multivariate\n");
3016 stats
->nb_affine_multivar
++;
3020 if (dump_file
&& (dump_flags
& TDF_STATS
))
3021 fprintf (dump_file
, " higher_degree_polynomial\n");
3022 stats
->nb_higher_poly
++;
3031 if (chrec_contains_undetermined (chrec
))
3033 if (dump_file
&& (dump_flags
& TDF_STATS
))
3034 fprintf (dump_file
, " undetermined\n");
3035 stats
->nb_undetermined
++;
3038 if (dump_file
&& (dump_flags
& TDF_STATS
))
3039 fprintf (dump_file
, ")\n");
3042 /* Classify the chrecs of the whole database. */
3045 gather_stats_on_scev_database (void)
3047 struct chrec_stats stats
;
3052 reset_chrecs_counters (&stats
);
3054 hash_table
<scev_info_hasher
>::iterator iter
;
3056 FOR_EACH_HASH_TABLE_ELEMENT (*scalar_evolution_info
, elt
, scev_info_str
*,
3058 gather_chrec_stats (elt
->chrec
, &stats
);
3060 dump_chrecs_stats (dump_file
, &stats
);
3068 initialize_scalar_evolutions_analyzer (void)
3070 /* The elements below are unique. */
3071 if (chrec_dont_know
== NULL_TREE
)
3073 chrec_not_analyzed_yet
= NULL_TREE
;
3074 chrec_dont_know
= make_node (SCEV_NOT_KNOWN
);
3075 chrec_known
= make_node (SCEV_KNOWN
);
3076 TREE_TYPE (chrec_dont_know
) = void_type_node
;
3077 TREE_TYPE (chrec_known
) = void_type_node
;
3081 /* Initialize the analysis of scalar evolutions for LOOPS. */
3084 scev_initialize (void)
3088 gcc_assert (! scev_initialized_p ());
3090 scalar_evolution_info
= hash_table
<scev_info_hasher
>::create_ggc (100);
3092 initialize_scalar_evolutions_analyzer ();
3094 FOR_EACH_LOOP (loop
, 0)
3096 loop
->nb_iterations
= NULL_TREE
;
3100 /* Return true if SCEV is initialized. */
3103 scev_initialized_p (void)
3105 return scalar_evolution_info
!= NULL
;
3108 /* Cleans up the information cached by the scalar evolutions analysis
3109 in the hash table. */
3112 scev_reset_htab (void)
3114 if (!scalar_evolution_info
)
3117 scalar_evolution_info
->empty ();
3120 /* Cleans up the information cached by the scalar evolutions analysis
3121 in the hash table and in the loop->nb_iterations. */
3130 FOR_EACH_LOOP (loop
, 0)
3132 loop
->nb_iterations
= NULL_TREE
;
3136 /* Return true if the IV calculation in TYPE can overflow based on the knowledge
3137 of the upper bound on the number of iterations of LOOP, the BASE and STEP
3140 We do not use information whether TYPE can overflow so it is safe to
3141 use this test even for derived IVs not computed every iteration or
3142 hypotetical IVs to be inserted into code. */
3145 iv_can_overflow_p (struct loop
*loop
, tree type
, tree base
, tree step
)
3148 wide_int base_min
, base_max
, step_min
, step_max
, type_min
, type_max
;
3149 signop sgn
= TYPE_SIGN (type
);
3151 if (integer_zerop (step
))
3154 if (TREE_CODE (base
) == INTEGER_CST
)
3155 base_min
= base_max
= wi::to_wide (base
);
3156 else if (TREE_CODE (base
) == SSA_NAME
3157 && INTEGRAL_TYPE_P (TREE_TYPE (base
))
3158 && get_range_info (base
, &base_min
, &base_max
) == VR_RANGE
)
3163 if (TREE_CODE (step
) == INTEGER_CST
)
3164 step_min
= step_max
= wi::to_wide (step
);
3165 else if (TREE_CODE (step
) == SSA_NAME
3166 && INTEGRAL_TYPE_P (TREE_TYPE (step
))
3167 && get_range_info (step
, &step_min
, &step_max
) == VR_RANGE
)
3172 if (!get_max_loop_iterations (loop
, &nit
))
3175 type_min
= wi::min_value (type
);
3176 type_max
= wi::max_value (type
);
3178 /* Just sanity check that we don't see values out of the range of the type.
3179 In this case the arithmetics bellow would overflow. */
3180 gcc_checking_assert (wi::ge_p (base_min
, type_min
, sgn
)
3181 && wi::le_p (base_max
, type_max
, sgn
));
3183 /* Account the possible increment in the last ieration. */
3184 bool overflow
= false;
3185 nit
= wi::add (nit
, 1, SIGNED
, &overflow
);
3189 /* NIT is typeless and can exceed the precision of the type. In this case
3190 overflow is always possible, because we know STEP is non-zero. */
3191 if (wi::min_precision (nit
, UNSIGNED
) > TYPE_PRECISION (type
))
3193 wide_int nit2
= wide_int::from (nit
, TYPE_PRECISION (type
), UNSIGNED
);
3195 /* If step can be positive, check that nit*step <= type_max-base.
3196 This can be done by unsigned arithmetic and we only need to watch overflow
3197 in the multiplication. The right hand side can always be represented in
3199 if (sgn
== UNSIGNED
|| !wi::neg_p (step_max
))
3201 bool overflow
= false;
3202 if (wi::gtu_p (wi::mul (step_max
, nit2
, UNSIGNED
, &overflow
),
3203 type_max
- base_max
)
3207 /* If step can be negative, check that nit*(-step) <= base_min-type_min. */
3208 if (sgn
== SIGNED
&& wi::neg_p (step_min
))
3210 bool overflow
= false, overflow2
= false;
3211 if (wi::gtu_p (wi::mul (wi::neg (step_min
, &overflow2
),
3212 nit2
, UNSIGNED
, &overflow
),
3213 base_min
- type_min
)
3214 || overflow
|| overflow2
)
3221 /* Given EV with form of "(type) {inner_base, inner_step}_loop", this
3222 function tries to derive condition under which it can be simplified
3223 into "{(type)inner_base, (type)inner_step}_loop". The condition is
3224 the maximum number that inner iv can iterate. */
3227 derive_simple_iv_with_niters (tree ev
, tree
*niters
)
3229 if (!CONVERT_EXPR_P (ev
))
3232 tree inner_ev
= TREE_OPERAND (ev
, 0);
3233 if (TREE_CODE (inner_ev
) != POLYNOMIAL_CHREC
)
3236 tree init
= CHREC_LEFT (inner_ev
);
3237 tree step
= CHREC_RIGHT (inner_ev
);
3238 if (TREE_CODE (init
) != INTEGER_CST
3239 || TREE_CODE (step
) != INTEGER_CST
|| integer_zerop (step
))
3242 tree type
= TREE_TYPE (ev
);
3243 tree inner_type
= TREE_TYPE (inner_ev
);
3244 if (TYPE_PRECISION (inner_type
) >= TYPE_PRECISION (type
))
3247 /* Type conversion in "(type) {inner_base, inner_step}_loop" can be
3248 folded only if inner iv won't overflow. We compute the maximum
3249 number the inner iv can iterate before overflowing and return the
3250 simplified affine iv. */
3252 init
= fold_convert (type
, init
);
3253 step
= fold_convert (type
, step
);
3254 ev
= build_polynomial_chrec (CHREC_VARIABLE (inner_ev
), init
, step
);
3255 if (tree_int_cst_sign_bit (step
))
3257 tree bound
= lower_bound_in_type (inner_type
, inner_type
);
3258 delta
= fold_build2 (MINUS_EXPR
, type
, init
, fold_convert (type
, bound
));
3259 step
= fold_build1 (NEGATE_EXPR
, type
, step
);
3263 tree bound
= upper_bound_in_type (inner_type
, inner_type
);
3264 delta
= fold_build2 (MINUS_EXPR
, type
, fold_convert (type
, bound
), init
);
3266 *niters
= fold_build2 (FLOOR_DIV_EXPR
, type
, delta
, step
);
3270 /* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with
3271 respect to WRTO_LOOP and returns its base and step in IV if possible
3272 (see analyze_scalar_evolution_in_loop for more details on USE_LOOP
3273 and WRTO_LOOP). If ALLOW_NONCONSTANT_STEP is true, we want step to be
3274 invariant in LOOP. Otherwise we require it to be an integer constant.
3276 IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g.
3277 because it is computed in signed arithmetics). Consequently, adding an
3280 for (i = IV->base; ; i += IV->step)
3282 is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is
3283 false for the type of the induction variable, or you can prove that i does
3284 not wrap by some other argument. Otherwise, this might introduce undefined
3288 for (; ; i = (type) ((unsigned type) i + (unsigned type) iv->step))
3290 must be used instead.
3292 When IV_NITERS is not NULL, this function also checks case in which OP
3293 is a conversion of an inner simple iv of below form:
3295 (outer_type){inner_base, inner_step}_loop.
3297 If type of inner iv has smaller precision than outer_type, it can't be
3298 folded into {(outer_type)inner_base, (outer_type)inner_step}_loop because
3299 the inner iv could overflow/wrap. In this case, we derive a condition
3300 under which the inner iv won't overflow/wrap and do the simplification.
3301 The derived condition normally is the maximum number the inner iv can
3302 iterate, and will be stored in IV_NITERS. This is useful in loop niter
3303 analysis, to derive break conditions when a loop must terminate, when is
3307 simple_iv_with_niters (struct loop
*wrto_loop
, struct loop
*use_loop
,
3308 tree op
, affine_iv
*iv
, tree
*iv_niters
,
3309 bool allow_nonconstant_step
)
3311 enum tree_code code
;
3312 tree type
, ev
, base
, e
;
3314 bool folded_casts
, overflow
;
3316 iv
->base
= NULL_TREE
;
3317 iv
->step
= NULL_TREE
;
3318 iv
->no_overflow
= false;
3320 type
= TREE_TYPE (op
);
3321 if (!POINTER_TYPE_P (type
)
3322 && !INTEGRAL_TYPE_P (type
))
3325 ev
= analyze_scalar_evolution_in_loop (wrto_loop
, use_loop
, op
,
3327 if (chrec_contains_undetermined (ev
)
3328 || chrec_contains_symbols_defined_in_loop (ev
, wrto_loop
->num
))
3331 if (tree_does_not_contain_chrecs (ev
))
3334 iv
->step
= build_int_cst (TREE_TYPE (ev
), 0);
3335 iv
->no_overflow
= true;
3339 /* If we can derive valid scalar evolution with assumptions. */
3340 if (iv_niters
&& TREE_CODE (ev
) != POLYNOMIAL_CHREC
)
3341 ev
= derive_simple_iv_with_niters (ev
, iv_niters
);
3343 if (TREE_CODE (ev
) != POLYNOMIAL_CHREC
)
3346 if (CHREC_VARIABLE (ev
) != (unsigned) wrto_loop
->num
)
3349 iv
->step
= CHREC_RIGHT (ev
);
3350 if ((!allow_nonconstant_step
&& TREE_CODE (iv
->step
) != INTEGER_CST
)
3351 || tree_contains_chrecs (iv
->step
, NULL
))
3354 iv
->base
= CHREC_LEFT (ev
);
3355 if (tree_contains_chrecs (iv
->base
, NULL
))
3358 iv
->no_overflow
= !folded_casts
&& nowrap_type_p (type
);
3360 if (!iv
->no_overflow
3361 && !iv_can_overflow_p (wrto_loop
, type
, iv
->base
, iv
->step
))
3362 iv
->no_overflow
= true;
3364 /* Try to simplify iv base:
3366 (signed T) ((unsigned T)base + step) ;; TREE_TYPE (base) == signed T
3367 == (signed T)(unsigned T)base + step
3370 If we can prove operation (base + step) doesn't overflow or underflow.
3371 Specifically, we try to prove below conditions are satisfied:
3373 base <= UPPER_BOUND (type) - step ;;step > 0
3374 base >= LOWER_BOUND (type) - step ;;step < 0
3376 This is done by proving the reverse conditions are false using loop's
3379 The is necessary to make loop niter, or iv overflow analysis easier
3382 int foo (int *a, signed char s, signed char l)
3385 for (i = s; i < l; i++)
3390 Note variable I is firstly converted to type unsigned char, incremented,
3391 then converted back to type signed char. */
3393 if (wrto_loop
->num
!= use_loop
->num
)
3396 if (!CONVERT_EXPR_P (iv
->base
) || TREE_CODE (iv
->step
) != INTEGER_CST
)
3399 type
= TREE_TYPE (iv
->base
);
3400 e
= TREE_OPERAND (iv
->base
, 0);
3401 if (TREE_CODE (e
) != PLUS_EXPR
3402 || TREE_CODE (TREE_OPERAND (e
, 1)) != INTEGER_CST
3403 || !tree_int_cst_equal (iv
->step
,
3404 fold_convert (type
, TREE_OPERAND (e
, 1))))
3406 e
= TREE_OPERAND (e
, 0);
3407 if (!CONVERT_EXPR_P (e
))
3409 base
= TREE_OPERAND (e
, 0);
3410 if (!useless_type_conversion_p (type
, TREE_TYPE (base
)))
3413 if (tree_int_cst_sign_bit (iv
->step
))
3416 extreme
= wi::min_value (type
);
3421 extreme
= wi::max_value (type
);
3424 extreme
= wi::sub (extreme
, wi::to_wide (iv
->step
),
3425 TYPE_SIGN (type
), &overflow
);
3428 e
= fold_build2 (code
, boolean_type_node
, base
,
3429 wide_int_to_tree (type
, extreme
));
3430 e
= simplify_using_initial_conditions (use_loop
, e
);
3431 if (!integer_zerop (e
))
3434 if (POINTER_TYPE_P (TREE_TYPE (base
)))
3435 code
= POINTER_PLUS_EXPR
;
3439 iv
->base
= fold_build2 (code
, TREE_TYPE (base
), base
, iv
->step
);
3443 /* Like simple_iv_with_niters, but return TRUE when OP behaves as a simple
3444 affine iv unconditionally. */
3447 simple_iv (struct loop
*wrto_loop
, struct loop
*use_loop
, tree op
,
3448 affine_iv
*iv
, bool allow_nonconstant_step
)
3450 return simple_iv_with_niters (wrto_loop
, use_loop
, op
, iv
,
3451 NULL
, allow_nonconstant_step
);
3454 /* Finalize the scalar evolution analysis. */
3457 scev_finalize (void)
3459 if (!scalar_evolution_info
)
3461 scalar_evolution_info
->empty ();
3462 scalar_evolution_info
= NULL
;
3463 free_numbers_of_iterations_estimates (cfun
);
3466 /* Returns true if the expression EXPR is considered to be too expensive
3467 for scev_const_prop. */
3470 expression_expensive_p (tree expr
)
3472 enum tree_code code
;
3474 if (is_gimple_val (expr
))
3477 code
= TREE_CODE (expr
);
3478 if (code
== TRUNC_DIV_EXPR
3479 || code
== CEIL_DIV_EXPR
3480 || code
== FLOOR_DIV_EXPR
3481 || code
== ROUND_DIV_EXPR
3482 || code
== TRUNC_MOD_EXPR
3483 || code
== CEIL_MOD_EXPR
3484 || code
== FLOOR_MOD_EXPR
3485 || code
== ROUND_MOD_EXPR
3486 || code
== EXACT_DIV_EXPR
)
3488 /* Division by power of two is usually cheap, so we allow it.
3489 Forbid anything else. */
3490 if (!integer_pow2p (TREE_OPERAND (expr
, 1)))
3494 switch (TREE_CODE_CLASS (code
))
3497 case tcc_comparison
:
3498 if (expression_expensive_p (TREE_OPERAND (expr
, 1)))
3503 return expression_expensive_p (TREE_OPERAND (expr
, 0));
3510 /* Do final value replacement for LOOP. */
3513 final_value_replacement_loop (struct loop
*loop
)
3515 /* If we do not know exact number of iterations of the loop, we cannot
3516 replace the final value. */
3517 edge exit
= single_exit (loop
);
3521 tree niter
= number_of_latch_executions (loop
);
3522 if (niter
== chrec_dont_know
)
3525 /* Ensure that it is possible to insert new statements somewhere. */
3526 if (!single_pred_p (exit
->dest
))
3527 split_loop_exit_edge (exit
);
3529 /* Set stmt insertion pointer. All stmts are inserted before this point. */
3530 gimple_stmt_iterator gsi
= gsi_after_labels (exit
->dest
);
3532 struct loop
*ex_loop
3533 = superloop_at_depth (loop
,
3534 loop_depth (exit
->dest
->loop_father
) + 1);
3537 for (psi
= gsi_start_phis (exit
->dest
); !gsi_end_p (psi
); )
3539 gphi
*phi
= psi
.phi ();
3540 tree rslt
= PHI_RESULT (phi
);
3541 tree def
= PHI_ARG_DEF_FROM_EDGE (phi
, exit
);
3542 if (virtual_operand_p (def
))
3548 if (!POINTER_TYPE_P (TREE_TYPE (def
))
3549 && !INTEGRAL_TYPE_P (TREE_TYPE (def
)))
3556 def
= analyze_scalar_evolution_in_loop (ex_loop
, loop
, def
,
3558 def
= compute_overall_effect_of_inner_loop (ex_loop
, def
);
3559 if (!tree_does_not_contain_chrecs (def
)
3560 || chrec_contains_symbols_defined_in_loop (def
, ex_loop
->num
)
3561 /* Moving the computation from the loop may prolong life range
3562 of some ssa names, which may cause problems if they appear
3563 on abnormal edges. */
3564 || contains_abnormal_ssa_name_p (def
)
3565 /* Do not emit expensive expressions. The rationale is that
3566 when someone writes a code like
3568 while (n > 45) n -= 45;
3570 he probably knows that n is not large, and does not want it
3571 to be turned into n %= 45. */
3572 || expression_expensive_p (def
))
3574 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3576 fprintf (dump_file
, "not replacing:\n ");
3577 print_gimple_stmt (dump_file
, phi
, 0);
3578 fprintf (dump_file
, "\n");
3584 /* Eliminate the PHI node and replace it by a computation outside
3588 fprintf (dump_file
, "\nfinal value replacement:\n ");
3589 print_gimple_stmt (dump_file
, phi
, 0);
3590 fprintf (dump_file
, " with\n ");
3592 def
= unshare_expr (def
);
3593 remove_phi_node (&psi
, false);
3595 /* If def's type has undefined overflow and there were folded
3596 casts, rewrite all stmts added for def into arithmetics
3597 with defined overflow behavior. */
3598 if (folded_casts
&& ANY_INTEGRAL_TYPE_P (TREE_TYPE (def
))
3599 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (def
)))
3602 gimple_stmt_iterator gsi2
;
3603 def
= force_gimple_operand (def
, &stmts
, true, NULL_TREE
);
3604 gsi2
= gsi_start (stmts
);
3605 while (!gsi_end_p (gsi2
))
3607 gimple
*stmt
= gsi_stmt (gsi2
);
3608 gimple_stmt_iterator gsi3
= gsi2
;
3610 gsi_remove (&gsi3
, false);
3611 if (is_gimple_assign (stmt
)
3612 && arith_code_with_undefined_signed_overflow
3613 (gimple_assign_rhs_code (stmt
)))
3614 gsi_insert_seq_before (&gsi
,
3615 rewrite_to_defined_overflow (stmt
),
3618 gsi_insert_before (&gsi
, stmt
, GSI_SAME_STMT
);
3622 def
= force_gimple_operand_gsi (&gsi
, def
, false, NULL_TREE
,
3623 true, GSI_SAME_STMT
);
3625 gassign
*ass
= gimple_build_assign (rslt
, def
);
3626 gsi_insert_before (&gsi
, ass
, GSI_SAME_STMT
);
3629 print_gimple_stmt (dump_file
, ass
, 0);
3630 fprintf (dump_file
, "\n");
3635 /* Replace ssa names for that scev can prove they are constant by the
3636 appropriate constants. Also perform final value replacement in loops,
3637 in case the replacement expressions are cheap.
3639 We only consider SSA names defined by phi nodes; rest is left to the
3640 ordinary constant propagation pass. */
3643 scev_const_prop (void)
3646 tree name
, type
, ev
;
3649 bitmap ssa_names_to_remove
= NULL
;
3653 if (number_of_loops (cfun
) <= 1)
3656 FOR_EACH_BB_FN (bb
, cfun
)
3658 loop
= bb
->loop_father
;
3660 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
3663 name
= PHI_RESULT (phi
);
3665 if (virtual_operand_p (name
))
3668 type
= TREE_TYPE (name
);
3670 if (!POINTER_TYPE_P (type
)
3671 && !INTEGRAL_TYPE_P (type
))
3674 ev
= resolve_mixers (loop
, analyze_scalar_evolution (loop
, name
),
3676 if (!is_gimple_min_invariant (ev
)
3677 || !may_propagate_copy (name
, ev
))
3680 /* Replace the uses of the name. */
3683 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3685 fprintf (dump_file
, "Replacing uses of: ");
3686 print_generic_expr (dump_file
, name
);
3687 fprintf (dump_file
, " with: ");
3688 print_generic_expr (dump_file
, ev
);
3689 fprintf (dump_file
, "\n");
3691 replace_uses_by (name
, ev
);
3694 if (!ssa_names_to_remove
)
3695 ssa_names_to_remove
= BITMAP_ALLOC (NULL
);
3696 bitmap_set_bit (ssa_names_to_remove
, SSA_NAME_VERSION (name
));
3700 /* Remove the ssa names that were replaced by constants. We do not
3701 remove them directly in the previous cycle, since this
3702 invalidates scev cache. */
3703 if (ssa_names_to_remove
)
3707 EXECUTE_IF_SET_IN_BITMAP (ssa_names_to_remove
, 0, i
, bi
)
3709 gimple_stmt_iterator psi
;
3710 name
= ssa_name (i
);
3711 phi
= as_a
<gphi
*> (SSA_NAME_DEF_STMT (name
));
3713 gcc_assert (gimple_code (phi
) == GIMPLE_PHI
);
3714 psi
= gsi_for_stmt (phi
);
3715 remove_phi_node (&psi
, true);
3718 BITMAP_FREE (ssa_names_to_remove
);
3722 /* Now the regular final value replacement. */
3723 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
3724 final_value_replacement_loop (loop
);
3729 #include "gt-tree-scalar-evolution.h"