1 /* Scalar evolution detector.
2 Copyright (C) 2003-2014 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"
261 #include "gimple-pretty-print.h"
265 #include "hash-set.h"
266 #include "machmode.h"
268 #include "hard-reg-set.h"
270 #include "function.h"
271 #include "dominance.h"
273 #include "basic-block.h"
274 #include "tree-ssa-alias.h"
275 #include "internal-fn.h"
276 #include "gimple-expr.h"
279 #include "gimplify.h"
280 #include "gimple-iterator.h"
281 #include "gimplify-me.h"
282 #include "gimple-ssa.h"
283 #include "tree-cfg.h"
284 #include "tree-phinodes.h"
285 #include "stringpool.h"
286 #include "tree-ssanames.h"
287 #include "tree-ssa-loop-ivopts.h"
288 #include "tree-ssa-loop-manip.h"
289 #include "tree-ssa-loop-niter.h"
290 #include "tree-ssa-loop.h"
291 #include "tree-ssa.h"
293 #include "tree-chrec.h"
294 #include "tree-affine.h"
295 #include "tree-scalar-evolution.h"
296 #include "dumpfile.h"
298 #include "tree-ssa-propagate.h"
299 #include "gimple-fold.h"
300 #include "gimplify-me.h"
302 static tree
analyze_scalar_evolution_1 (struct loop
*, tree
, tree
);
303 static tree
analyze_scalar_evolution_for_address_of (struct loop
*loop
,
306 /* The cached information about an SSA name with version NAME_VERSION,
307 claiming that below basic block with index INSTANTIATED_BELOW, the
308 value of the SSA name can be expressed as CHREC. */
310 struct GTY((for_user
)) scev_info_str
{
311 unsigned int name_version
;
312 int instantiated_below
;
316 /* Counters for the scev database. */
317 static unsigned nb_set_scev
= 0;
318 static unsigned nb_get_scev
= 0;
320 /* The following trees are unique elements. Thus the comparison of
321 another element to these elements should be done on the pointer to
322 these trees, and not on their value. */
324 /* The SSA_NAMEs that are not yet analyzed are qualified with NULL_TREE. */
325 tree chrec_not_analyzed_yet
;
327 /* Reserved to the cases where the analyzer has detected an
328 undecidable property at compile time. */
329 tree chrec_dont_know
;
331 /* When the analyzer has detected that a property will never
332 happen, then it qualifies it with chrec_known. */
335 struct scev_info_hasher
: ggc_hasher
<scev_info_str
*>
337 static hashval_t
hash (scev_info_str
*i
);
338 static bool equal (const scev_info_str
*a
, const scev_info_str
*b
);
341 static GTY (()) hash_table
<scev_info_hasher
> *scalar_evolution_info
;
344 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */
346 static inline struct scev_info_str
*
347 new_scev_info_str (basic_block instantiated_below
, tree var
)
349 struct scev_info_str
*res
;
351 res
= ggc_alloc
<scev_info_str
> ();
352 res
->name_version
= SSA_NAME_VERSION (var
);
353 res
->chrec
= chrec_not_analyzed_yet
;
354 res
->instantiated_below
= instantiated_below
->index
;
359 /* Computes a hash function for database element ELT. */
362 scev_info_hasher::hash (scev_info_str
*elt
)
364 return elt
->name_version
^ elt
->instantiated_below
;
367 /* Compares database elements E1 and E2. */
370 scev_info_hasher::equal (const scev_info_str
*elt1
, const scev_info_str
*elt2
)
372 return (elt1
->name_version
== elt2
->name_version
373 && elt1
->instantiated_below
== elt2
->instantiated_below
);
376 /* Get the scalar evolution of VAR for INSTANTIATED_BELOW basic block.
377 A first query on VAR returns chrec_not_analyzed_yet. */
380 find_var_scev_info (basic_block instantiated_below
, tree var
)
382 struct scev_info_str
*res
;
383 struct scev_info_str tmp
;
385 tmp
.name_version
= SSA_NAME_VERSION (var
);
386 tmp
.instantiated_below
= instantiated_below
->index
;
387 scev_info_str
**slot
= scalar_evolution_info
->find_slot (&tmp
, INSERT
);
390 *slot
= new_scev_info_str (instantiated_below
, var
);
396 /* Return true when CHREC contains symbolic names defined in
400 chrec_contains_symbols_defined_in_loop (const_tree chrec
, unsigned loop_nb
)
404 if (chrec
== NULL_TREE
)
407 if (is_gimple_min_invariant (chrec
))
410 if (TREE_CODE (chrec
) == SSA_NAME
)
413 loop_p def_loop
, loop
;
415 if (SSA_NAME_IS_DEFAULT_DEF (chrec
))
418 def
= SSA_NAME_DEF_STMT (chrec
);
419 def_loop
= loop_containing_stmt (def
);
420 loop
= get_loop (cfun
, loop_nb
);
422 if (def_loop
== NULL
)
425 if (loop
== def_loop
|| flow_loop_nested_p (loop
, def_loop
))
431 n
= TREE_OPERAND_LENGTH (chrec
);
432 for (i
= 0; i
< n
; i
++)
433 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec
, i
),
439 /* Return true when PHI is a loop-phi-node. */
442 loop_phi_node_p (gimple phi
)
444 /* The implementation of this function is based on the following
445 property: "all the loop-phi-nodes of a loop are contained in the
446 loop's header basic block". */
448 return loop_containing_stmt (phi
)->header
== gimple_bb (phi
);
451 /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
452 In general, in the case of multivariate evolutions we want to get
453 the evolution in different loops. LOOP specifies the level for
454 which to get the evolution.
458 | for (j = 0; j < 100; j++)
460 | for (k = 0; k < 100; k++)
462 | i = k + j; - Here the value of i is a function of j, k.
464 | ... = i - Here the value of i is a function of j.
466 | ... = i - Here the value of i is a scalar.
472 | i_1 = phi (i_0, i_2)
476 This loop has the same effect as:
477 LOOP_1 has the same effect as:
481 The overall effect of the loop, "i_0 + 20" in the previous example,
482 is obtained by passing in the parameters: LOOP = 1,
483 EVOLUTION_FN = {i_0, +, 2}_1.
487 compute_overall_effect_of_inner_loop (struct loop
*loop
, tree evolution_fn
)
491 if (evolution_fn
== chrec_dont_know
)
492 return chrec_dont_know
;
494 else if (TREE_CODE (evolution_fn
) == POLYNOMIAL_CHREC
)
496 struct loop
*inner_loop
= get_chrec_loop (evolution_fn
);
498 if (inner_loop
== loop
499 || flow_loop_nested_p (loop
, inner_loop
))
501 tree nb_iter
= number_of_latch_executions (inner_loop
);
503 if (nb_iter
== chrec_dont_know
)
504 return chrec_dont_know
;
509 /* evolution_fn is the evolution function in LOOP. Get
510 its value in the nb_iter-th iteration. */
511 res
= chrec_apply (inner_loop
->num
, evolution_fn
, nb_iter
);
513 if (chrec_contains_symbols_defined_in_loop (res
, loop
->num
))
514 res
= instantiate_parameters (loop
, res
);
516 /* Continue the computation until ending on a parent of LOOP. */
517 return compute_overall_effect_of_inner_loop (loop
, res
);
524 /* If the evolution function is an invariant, there is nothing to do. */
525 else if (no_evolution_in_loop_p (evolution_fn
, loop
->num
, &val
) && val
)
529 return chrec_dont_know
;
532 /* Associate CHREC to SCALAR. */
535 set_scalar_evolution (basic_block instantiated_below
, tree scalar
, tree chrec
)
539 if (TREE_CODE (scalar
) != SSA_NAME
)
542 scalar_info
= find_var_scev_info (instantiated_below
, scalar
);
546 if (dump_flags
& TDF_SCEV
)
548 fprintf (dump_file
, "(set_scalar_evolution \n");
549 fprintf (dump_file
, " instantiated_below = %d \n",
550 instantiated_below
->index
);
551 fprintf (dump_file
, " (scalar = ");
552 print_generic_expr (dump_file
, scalar
, 0);
553 fprintf (dump_file
, ")\n (scalar_evolution = ");
554 print_generic_expr (dump_file
, chrec
, 0);
555 fprintf (dump_file
, "))\n");
557 if (dump_flags
& TDF_STATS
)
561 *scalar_info
= chrec
;
564 /* Retrieve the chrec associated to SCALAR instantiated below
565 INSTANTIATED_BELOW block. */
568 get_scalar_evolution (basic_block instantiated_below
, tree scalar
)
574 if (dump_flags
& TDF_SCEV
)
576 fprintf (dump_file
, "(get_scalar_evolution \n");
577 fprintf (dump_file
, " (scalar = ");
578 print_generic_expr (dump_file
, scalar
, 0);
579 fprintf (dump_file
, ")\n");
581 if (dump_flags
& TDF_STATS
)
585 switch (TREE_CODE (scalar
))
588 res
= *find_var_scev_info (instantiated_below
, scalar
);
598 res
= chrec_not_analyzed_yet
;
602 if (dump_file
&& (dump_flags
& TDF_SCEV
))
604 fprintf (dump_file
, " (scalar_evolution = ");
605 print_generic_expr (dump_file
, res
, 0);
606 fprintf (dump_file
, "))\n");
612 /* Helper function for add_to_evolution. Returns the evolution
613 function for an assignment of the form "a = b + c", where "a" and
614 "b" are on the strongly connected component. CHREC_BEFORE is the
615 information that we already have collected up to this point.
616 TO_ADD is the evolution of "c".
618 When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
619 evolution the expression TO_ADD, otherwise construct an evolution
620 part for this loop. */
623 add_to_evolution_1 (unsigned loop_nb
, tree chrec_before
, tree to_add
,
626 tree type
, left
, right
;
627 struct loop
*loop
= get_loop (cfun
, loop_nb
), *chloop
;
629 switch (TREE_CODE (chrec_before
))
631 case POLYNOMIAL_CHREC
:
632 chloop
= get_chrec_loop (chrec_before
);
634 || flow_loop_nested_p (chloop
, loop
))
638 type
= chrec_type (chrec_before
);
640 /* When there is no evolution part in this loop, build it. */
645 right
= SCALAR_FLOAT_TYPE_P (type
)
646 ? build_real (type
, dconst0
)
647 : build_int_cst (type
, 0);
651 var
= CHREC_VARIABLE (chrec_before
);
652 left
= CHREC_LEFT (chrec_before
);
653 right
= CHREC_RIGHT (chrec_before
);
656 to_add
= chrec_convert (type
, to_add
, at_stmt
);
657 right
= chrec_convert_rhs (type
, right
, at_stmt
);
658 right
= chrec_fold_plus (chrec_type (right
), right
, to_add
);
659 return build_polynomial_chrec (var
, left
, right
);
663 gcc_assert (flow_loop_nested_p (loop
, chloop
));
665 /* Search the evolution in LOOP_NB. */
666 left
= add_to_evolution_1 (loop_nb
, CHREC_LEFT (chrec_before
),
668 right
= CHREC_RIGHT (chrec_before
);
669 right
= chrec_convert_rhs (chrec_type (left
), right
, at_stmt
);
670 return build_polynomial_chrec (CHREC_VARIABLE (chrec_before
),
675 /* These nodes do not depend on a loop. */
676 if (chrec_before
== chrec_dont_know
)
677 return chrec_dont_know
;
680 right
= chrec_convert_rhs (chrec_type (left
), to_add
, at_stmt
);
681 return build_polynomial_chrec (loop_nb
, left
, right
);
685 /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
688 Description (provided for completeness, for those who read code in
689 a plane, and for my poor 62 bytes brain that would have forgotten
690 all this in the next two or three months):
692 The algorithm of translation of programs from the SSA representation
693 into the chrecs syntax is based on a pattern matching. After having
694 reconstructed the overall tree expression for a loop, there are only
695 two cases that can arise:
697 1. a = loop-phi (init, a + expr)
698 2. a = loop-phi (init, expr)
700 where EXPR is either a scalar constant with respect to the analyzed
701 loop (this is a degree 0 polynomial), or an expression containing
702 other loop-phi definitions (these are higher degree polynomials).
709 | a = phi (init, a + 5)
716 | a = phi (inita, 2 * b + 3)
717 | b = phi (initb, b + 1)
720 For the first case, the semantics of the SSA representation is:
722 | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
724 that is, there is a loop index "x" that determines the scalar value
725 of the variable during the loop execution. During the first
726 iteration, the value is that of the initial condition INIT, while
727 during the subsequent iterations, it is the sum of the initial
728 condition with the sum of all the values of EXPR from the initial
729 iteration to the before last considered iteration.
731 For the second case, the semantics of the SSA program is:
733 | a (x) = init, if x = 0;
734 | expr (x - 1), otherwise.
736 The second case corresponds to the PEELED_CHREC, whose syntax is
737 close to the syntax of a loop-phi-node:
739 | phi (init, expr) vs. (init, expr)_x
741 The proof of the translation algorithm for the first case is a
742 proof by structural induction based on the degree of EXPR.
745 When EXPR is a constant with respect to the analyzed loop, or in
746 other words when EXPR is a polynomial of degree 0, the evolution of
747 the variable A in the loop is an affine function with an initial
748 condition INIT, and a step EXPR. In order to show this, we start
749 from the semantics of the SSA representation:
751 f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
753 and since "expr (j)" is a constant with respect to "j",
755 f (x) = init + x * expr
757 Finally, based on the semantics of the pure sum chrecs, by
758 identification we get the corresponding chrecs syntax:
760 f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
761 f (x) -> {init, +, expr}_x
764 Suppose that EXPR is a polynomial of degree N with respect to the
765 analyzed loop_x for which we have already determined that it is
766 written under the chrecs syntax:
768 | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
770 We start from the semantics of the SSA program:
772 | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
774 | f (x) = init + \sum_{j = 0}^{x - 1}
775 | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
777 | f (x) = init + \sum_{j = 0}^{x - 1}
778 | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
780 | f (x) = init + \sum_{k = 0}^{n - 1}
781 | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
783 | f (x) = init + \sum_{k = 0}^{n - 1}
784 | (b_k * \binom{x}{k + 1})
786 | f (x) = init + b_0 * \binom{x}{1} + ...
787 | + b_{n-1} * \binom{x}{n}
789 | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
790 | + b_{n-1} * \binom{x}{n}
793 And finally from the definition of the chrecs syntax, we identify:
794 | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x
796 This shows the mechanism that stands behind the add_to_evolution
797 function. An important point is that the use of symbolic
798 parameters avoids the need of an analysis schedule.
805 | a = phi (inita, a + 2 + b)
806 | b = phi (initb, b + 1)
809 When analyzing "a", the algorithm keeps "b" symbolically:
811 | a -> {inita, +, 2 + b}_1
813 Then, after instantiation, the analyzer ends on the evolution:
815 | a -> {inita, +, 2 + initb, +, 1}_1
820 add_to_evolution (unsigned loop_nb
, tree chrec_before
, enum tree_code code
,
821 tree to_add
, gimple at_stmt
)
823 tree type
= chrec_type (to_add
);
824 tree res
= NULL_TREE
;
826 if (to_add
== NULL_TREE
)
829 /* TO_ADD is either a scalar, or a parameter. TO_ADD is not
830 instantiated at this point. */
831 if (TREE_CODE (to_add
) == POLYNOMIAL_CHREC
)
832 /* This should not happen. */
833 return chrec_dont_know
;
835 if (dump_file
&& (dump_flags
& TDF_SCEV
))
837 fprintf (dump_file
, "(add_to_evolution \n");
838 fprintf (dump_file
, " (loop_nb = %d)\n", loop_nb
);
839 fprintf (dump_file
, " (chrec_before = ");
840 print_generic_expr (dump_file
, chrec_before
, 0);
841 fprintf (dump_file
, ")\n (to_add = ");
842 print_generic_expr (dump_file
, to_add
, 0);
843 fprintf (dump_file
, ")\n");
846 if (code
== MINUS_EXPR
)
847 to_add
= chrec_fold_multiply (type
, to_add
, SCALAR_FLOAT_TYPE_P (type
)
848 ? build_real (type
, dconstm1
)
849 : build_int_cst_type (type
, -1));
851 res
= add_to_evolution_1 (loop_nb
, chrec_before
, to_add
, at_stmt
);
853 if (dump_file
&& (dump_flags
& TDF_SCEV
))
855 fprintf (dump_file
, " (res = ");
856 print_generic_expr (dump_file
, res
, 0);
857 fprintf (dump_file
, "))\n");
865 /* This section selects the loops that will be good candidates for the
866 scalar evolution analysis. For the moment, greedily select all the
867 loop nests we could analyze. */
869 /* For a loop with a single exit edge, return the COND_EXPR that
870 guards the exit edge. If the expression is too difficult to
871 analyze, then give up. */
874 get_loop_exit_condition (const struct loop
*loop
)
877 edge exit_edge
= single_exit (loop
);
879 if (dump_file
&& (dump_flags
& TDF_SCEV
))
880 fprintf (dump_file
, "(get_loop_exit_condition \n ");
886 stmt
= last_stmt (exit_edge
->src
);
887 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
891 if (dump_file
&& (dump_flags
& TDF_SCEV
))
893 print_gimple_stmt (dump_file
, res
, 0, 0);
894 fprintf (dump_file
, ")\n");
901 /* Depth first search algorithm. */
903 typedef enum t_bool
{
910 static t_bool
follow_ssa_edge (struct loop
*loop
, gimple
, gphi
*,
913 /* Follow the ssa edge into the binary expression RHS0 CODE RHS1.
914 Return true if the strongly connected component has been found. */
917 follow_ssa_edge_binary (struct loop
*loop
, gimple at_stmt
,
918 tree type
, tree rhs0
, enum tree_code code
, tree rhs1
,
919 gphi
*halting_phi
, tree
*evolution_of_loop
,
922 t_bool res
= t_false
;
927 case POINTER_PLUS_EXPR
:
929 if (TREE_CODE (rhs0
) == SSA_NAME
)
931 if (TREE_CODE (rhs1
) == SSA_NAME
)
933 /* Match an assignment under the form:
936 /* We want only assignments of form "name + name" contribute to
937 LIMIT, as the other cases do not necessarily contribute to
938 the complexity of the expression. */
941 evol
= *evolution_of_loop
;
942 res
= follow_ssa_edge
943 (loop
, SSA_NAME_DEF_STMT (rhs0
), halting_phi
, &evol
, limit
);
946 *evolution_of_loop
= add_to_evolution
948 chrec_convert (type
, evol
, at_stmt
),
949 code
, rhs1
, at_stmt
);
951 else if (res
== t_false
)
953 res
= follow_ssa_edge
954 (loop
, SSA_NAME_DEF_STMT (rhs1
), halting_phi
,
955 evolution_of_loop
, limit
);
958 *evolution_of_loop
= add_to_evolution
960 chrec_convert (type
, *evolution_of_loop
, at_stmt
),
961 code
, rhs0
, at_stmt
);
963 else if (res
== t_dont_know
)
964 *evolution_of_loop
= chrec_dont_know
;
967 else if (res
== t_dont_know
)
968 *evolution_of_loop
= chrec_dont_know
;
973 /* Match an assignment under the form:
975 res
= follow_ssa_edge
976 (loop
, SSA_NAME_DEF_STMT (rhs0
), halting_phi
,
977 evolution_of_loop
, limit
);
979 *evolution_of_loop
= add_to_evolution
980 (loop
->num
, chrec_convert (type
, *evolution_of_loop
,
982 code
, rhs1
, at_stmt
);
984 else if (res
== t_dont_know
)
985 *evolution_of_loop
= chrec_dont_know
;
989 else if (TREE_CODE (rhs1
) == SSA_NAME
)
991 /* Match an assignment under the form:
993 res
= follow_ssa_edge
994 (loop
, SSA_NAME_DEF_STMT (rhs1
), halting_phi
,
995 evolution_of_loop
, limit
);
997 *evolution_of_loop
= add_to_evolution
998 (loop
->num
, chrec_convert (type
, *evolution_of_loop
,
1000 code
, rhs0
, at_stmt
);
1002 else if (res
== t_dont_know
)
1003 *evolution_of_loop
= chrec_dont_know
;
1007 /* Otherwise, match an assignment under the form:
1009 /* And there is nothing to do. */
1014 /* This case is under the form "opnd0 = rhs0 - rhs1". */
1015 if (TREE_CODE (rhs0
) == SSA_NAME
)
1017 /* Match an assignment under the form:
1020 /* We want only assignments of form "name - name" contribute to
1021 LIMIT, as the other cases do not necessarily contribute to
1022 the complexity of the expression. */
1023 if (TREE_CODE (rhs1
) == SSA_NAME
)
1026 res
= follow_ssa_edge (loop
, SSA_NAME_DEF_STMT (rhs0
), halting_phi
,
1027 evolution_of_loop
, limit
);
1029 *evolution_of_loop
= add_to_evolution
1030 (loop
->num
, chrec_convert (type
, *evolution_of_loop
, at_stmt
),
1031 MINUS_EXPR
, rhs1
, at_stmt
);
1033 else if (res
== t_dont_know
)
1034 *evolution_of_loop
= chrec_dont_know
;
1037 /* Otherwise, match an assignment under the form:
1039 /* And there is nothing to do. */
1050 /* Follow the ssa edge into the expression EXPR.
1051 Return true if the strongly connected component has been found. */
1054 follow_ssa_edge_expr (struct loop
*loop
, gimple at_stmt
, tree expr
,
1055 gphi
*halting_phi
, tree
*evolution_of_loop
,
1058 enum tree_code code
= TREE_CODE (expr
);
1059 tree type
= TREE_TYPE (expr
), rhs0
, rhs1
;
1062 /* The EXPR is one of the following cases:
1066 - a POINTER_PLUS_EXPR,
1069 - other cases are not yet handled. */
1074 /* This assignment is under the form "a_1 = (cast) rhs. */
1075 res
= follow_ssa_edge_expr (loop
, at_stmt
, TREE_OPERAND (expr
, 0),
1076 halting_phi
, evolution_of_loop
, limit
);
1077 *evolution_of_loop
= chrec_convert (type
, *evolution_of_loop
, at_stmt
);
1081 /* This assignment is under the form "a_1 = 7". */
1086 /* This assignment is under the form: "a_1 = b_2". */
1087 res
= follow_ssa_edge
1088 (loop
, SSA_NAME_DEF_STMT (expr
), halting_phi
, evolution_of_loop
, limit
);
1091 case POINTER_PLUS_EXPR
:
1094 /* This case is under the form "rhs0 +- rhs1". */
1095 rhs0
= TREE_OPERAND (expr
, 0);
1096 rhs1
= TREE_OPERAND (expr
, 1);
1097 type
= TREE_TYPE (rhs0
);
1098 STRIP_USELESS_TYPE_CONVERSION (rhs0
);
1099 STRIP_USELESS_TYPE_CONVERSION (rhs1
);
1100 res
= follow_ssa_edge_binary (loop
, at_stmt
, type
, rhs0
, code
, rhs1
,
1101 halting_phi
, evolution_of_loop
, limit
);
1105 /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR. */
1106 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == MEM_REF
)
1108 expr
= TREE_OPERAND (expr
, 0);
1109 rhs0
= TREE_OPERAND (expr
, 0);
1110 rhs1
= TREE_OPERAND (expr
, 1);
1111 type
= TREE_TYPE (rhs0
);
1112 STRIP_USELESS_TYPE_CONVERSION (rhs0
);
1113 STRIP_USELESS_TYPE_CONVERSION (rhs1
);
1114 res
= follow_ssa_edge_binary (loop
, at_stmt
, type
,
1115 rhs0
, POINTER_PLUS_EXPR
, rhs1
,
1116 halting_phi
, evolution_of_loop
, limit
);
1123 /* This assignment is of the form: "a_1 = ASSERT_EXPR <a_2, ...>"
1124 It must be handled as a copy assignment of the form a_1 = a_2. */
1125 rhs0
= ASSERT_EXPR_VAR (expr
);
1126 if (TREE_CODE (rhs0
) == SSA_NAME
)
1127 res
= follow_ssa_edge (loop
, SSA_NAME_DEF_STMT (rhs0
),
1128 halting_phi
, evolution_of_loop
, limit
);
1141 /* Follow the ssa edge into the right hand side of an assignment STMT.
1142 Return true if the strongly connected component has been found. */
1145 follow_ssa_edge_in_rhs (struct loop
*loop
, gimple stmt
,
1146 gphi
*halting_phi
, tree
*evolution_of_loop
,
1149 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1150 tree type
= gimple_expr_type (stmt
), rhs1
, rhs2
;
1156 /* This assignment is under the form "a_1 = (cast) rhs. */
1157 res
= follow_ssa_edge_expr (loop
, stmt
, gimple_assign_rhs1 (stmt
),
1158 halting_phi
, evolution_of_loop
, limit
);
1159 *evolution_of_loop
= chrec_convert (type
, *evolution_of_loop
, stmt
);
1162 case POINTER_PLUS_EXPR
:
1165 rhs1
= gimple_assign_rhs1 (stmt
);
1166 rhs2
= gimple_assign_rhs2 (stmt
);
1167 type
= TREE_TYPE (rhs1
);
1168 res
= follow_ssa_edge_binary (loop
, stmt
, type
, rhs1
, code
, rhs2
,
1169 halting_phi
, evolution_of_loop
, limit
);
1173 if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
1174 res
= follow_ssa_edge_expr (loop
, stmt
, gimple_assign_rhs1 (stmt
),
1175 halting_phi
, evolution_of_loop
, limit
);
1184 /* Checks whether the I-th argument of a PHI comes from a backedge. */
1187 backedge_phi_arg_p (gphi
*phi
, int i
)
1189 const_edge e
= gimple_phi_arg_edge (phi
, i
);
1191 /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
1192 about updating it anywhere, and this should work as well most of the
1194 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1200 /* Helper function for one branch of the condition-phi-node. Return
1201 true if the strongly connected component has been found following
1204 static inline t_bool
1205 follow_ssa_edge_in_condition_phi_branch (int i
,
1207 gphi
*condition_phi
,
1209 tree
*evolution_of_branch
,
1210 tree init_cond
, int limit
)
1212 tree branch
= PHI_ARG_DEF (condition_phi
, i
);
1213 *evolution_of_branch
= chrec_dont_know
;
1215 /* Do not follow back edges (they must belong to an irreducible loop, which
1216 we really do not want to worry about). */
1217 if (backedge_phi_arg_p (condition_phi
, i
))
1220 if (TREE_CODE (branch
) == SSA_NAME
)
1222 *evolution_of_branch
= init_cond
;
1223 return follow_ssa_edge (loop
, SSA_NAME_DEF_STMT (branch
), halting_phi
,
1224 evolution_of_branch
, limit
);
1227 /* This case occurs when one of the condition branches sets
1228 the variable to a constant: i.e. a phi-node like
1229 "a_2 = PHI <a_7(5), 2(6)>;".
1231 FIXME: This case have to be refined correctly:
1232 in some cases it is possible to say something better than
1233 chrec_dont_know, for example using a wrap-around notation. */
1237 /* This function merges the branches of a condition-phi-node in a
1241 follow_ssa_edge_in_condition_phi (struct loop
*loop
,
1242 gphi
*condition_phi
,
1244 tree
*evolution_of_loop
, int limit
)
1247 tree init
= *evolution_of_loop
;
1248 tree evolution_of_branch
;
1249 t_bool res
= follow_ssa_edge_in_condition_phi_branch (0, loop
, condition_phi
,
1251 &evolution_of_branch
,
1253 if (res
== t_false
|| res
== t_dont_know
)
1256 *evolution_of_loop
= evolution_of_branch
;
1258 n
= gimple_phi_num_args (condition_phi
);
1259 for (i
= 1; i
< n
; i
++)
1261 /* Quickly give up when the evolution of one of the branches is
1263 if (*evolution_of_loop
== chrec_dont_know
)
1266 /* Increase the limit by the PHI argument number to avoid exponential
1267 time and memory complexity. */
1268 res
= follow_ssa_edge_in_condition_phi_branch (i
, loop
, condition_phi
,
1270 &evolution_of_branch
,
1272 if (res
== t_false
|| res
== t_dont_know
)
1275 *evolution_of_loop
= chrec_merge (*evolution_of_loop
,
1276 evolution_of_branch
);
1282 /* Follow an SSA edge in an inner loop. It computes the overall
1283 effect of the loop, and following the symbolic initial conditions,
1284 it follows the edges in the parent loop. The inner loop is
1285 considered as a single statement. */
1288 follow_ssa_edge_inner_loop_phi (struct loop
*outer_loop
,
1289 gphi
*loop_phi_node
,
1291 tree
*evolution_of_loop
, int limit
)
1293 struct loop
*loop
= loop_containing_stmt (loop_phi_node
);
1294 tree ev
= analyze_scalar_evolution (loop
, PHI_RESULT (loop_phi_node
));
1296 /* Sometimes, the inner loop is too difficult to analyze, and the
1297 result of the analysis is a symbolic parameter. */
1298 if (ev
== PHI_RESULT (loop_phi_node
))
1300 t_bool res
= t_false
;
1301 int i
, n
= gimple_phi_num_args (loop_phi_node
);
1303 for (i
= 0; i
< n
; i
++)
1305 tree arg
= PHI_ARG_DEF (loop_phi_node
, i
);
1308 /* Follow the edges that exit the inner loop. */
1309 bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1310 if (!flow_bb_inside_loop_p (loop
, bb
))
1311 res
= follow_ssa_edge_expr (outer_loop
, loop_phi_node
,
1313 evolution_of_loop
, limit
);
1318 /* If the path crosses this loop-phi, give up. */
1320 *evolution_of_loop
= chrec_dont_know
;
1325 /* Otherwise, compute the overall effect of the inner loop. */
1326 ev
= compute_overall_effect_of_inner_loop (loop
, ev
);
1327 return follow_ssa_edge_expr (outer_loop
, loop_phi_node
, ev
, halting_phi
,
1328 evolution_of_loop
, limit
);
1331 /* Follow an SSA edge from a loop-phi-node to itself, constructing a
1332 path that is analyzed on the return walk. */
1335 follow_ssa_edge (struct loop
*loop
, gimple def
, gphi
*halting_phi
,
1336 tree
*evolution_of_loop
, int limit
)
1338 struct loop
*def_loop
;
1340 if (gimple_nop_p (def
))
1343 /* Give up if the path is longer than the MAX that we allow. */
1344 if (limit
> PARAM_VALUE (PARAM_SCEV_MAX_EXPR_COMPLEXITY
))
1347 def_loop
= loop_containing_stmt (def
);
1349 switch (gimple_code (def
))
1352 if (!loop_phi_node_p (def
))
1353 /* DEF is a condition-phi-node. Follow the branches, and
1354 record their evolutions. Finally, merge the collected
1355 information and set the approximation to the main
1357 return follow_ssa_edge_in_condition_phi
1358 (loop
, as_a
<gphi
*> (def
), halting_phi
, evolution_of_loop
,
1361 /* When the analyzed phi is the halting_phi, the
1362 depth-first search is over: we have found a path from
1363 the halting_phi to itself in the loop. */
1364 if (def
== halting_phi
)
1367 /* Otherwise, the evolution of the HALTING_PHI depends
1368 on the evolution of another loop-phi-node, i.e. the
1369 evolution function is a higher degree polynomial. */
1370 if (def_loop
== loop
)
1374 if (flow_loop_nested_p (loop
, def_loop
))
1375 return follow_ssa_edge_inner_loop_phi
1376 (loop
, as_a
<gphi
*> (def
), halting_phi
, evolution_of_loop
,
1383 return follow_ssa_edge_in_rhs (loop
, def
, halting_phi
,
1384 evolution_of_loop
, limit
);
1387 /* At this level of abstraction, the program is just a set
1388 of GIMPLE_ASSIGNs and PHI_NODEs. In principle there is no
1389 other node to be handled. */
1395 /* Simplify PEELED_CHREC represented by (init_cond, arg) in LOOP.
1396 Handle below case and return the corresponding POLYNOMIAL_CHREC:
1398 # i_17 = PHI <i_13(5), 0(3)>
1399 # _20 = PHI <_5(5), start_4(D)(3)>
1402 _5 = start_4(D) + i_13;
1404 Though variable _20 appears as a PEELED_CHREC in the form of
1405 (start_4, _5)_LOOP, it's a POLYNOMIAL_CHREC like {start_4, 1}_LOOP.
1410 simplify_peeled_chrec (struct loop
*loop
, tree arg
, tree init_cond
)
1412 aff_tree aff1
, aff2
;
1413 tree ev
, left
, right
, type
, step_val
;
1414 hash_map
<tree
, name_expansion
*> *peeled_chrec_map
= NULL
;
1416 ev
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, arg
));
1417 if (ev
== NULL_TREE
|| TREE_CODE (ev
) != POLYNOMIAL_CHREC
)
1418 return chrec_dont_know
;
1420 left
= CHREC_LEFT (ev
);
1421 right
= CHREC_RIGHT (ev
);
1422 type
= TREE_TYPE (left
);
1423 step_val
= chrec_fold_plus (type
, init_cond
, right
);
1425 /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1426 if "left" equals to "init + right". */
1427 if (operand_equal_p (left
, step_val
, 0))
1429 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1430 fprintf (dump_file
, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1432 return build_polynomial_chrec (loop
->num
, init_cond
, right
);
1435 /* Try harder to check if they are equal. */
1436 tree_to_aff_combination_expand (left
, type
, &aff1
, &peeled_chrec_map
);
1437 tree_to_aff_combination_expand (step_val
, type
, &aff2
, &peeled_chrec_map
);
1438 free_affine_expand_cache (&peeled_chrec_map
);
1439 aff_combination_scale (&aff2
, -1);
1440 aff_combination_add (&aff1
, &aff2
);
1442 /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1443 if "left" equals to "init + right". */
1444 if (aff_combination_zero_p (&aff1
))
1446 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1447 fprintf (dump_file
, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1449 return build_polynomial_chrec (loop
->num
, init_cond
, right
);
1451 return chrec_dont_know
;
1454 /* Given a LOOP_PHI_NODE, this function determines the evolution
1455 function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */
1458 analyze_evolution_in_loop (gphi
*loop_phi_node
,
1461 int i
, n
= gimple_phi_num_args (loop_phi_node
);
1462 tree evolution_function
= chrec_not_analyzed_yet
;
1463 struct loop
*loop
= loop_containing_stmt (loop_phi_node
);
1465 static bool simplify_peeled_chrec_p
= true;
1467 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1469 fprintf (dump_file
, "(analyze_evolution_in_loop \n");
1470 fprintf (dump_file
, " (loop_phi_node = ");
1471 print_gimple_stmt (dump_file
, loop_phi_node
, 0, 0);
1472 fprintf (dump_file
, ")\n");
1475 for (i
= 0; i
< n
; i
++)
1477 tree arg
= PHI_ARG_DEF (loop_phi_node
, i
);
1482 /* Select the edges that enter the loop body. */
1483 bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1484 if (!flow_bb_inside_loop_p (loop
, bb
))
1487 if (TREE_CODE (arg
) == SSA_NAME
)
1491 ssa_chain
= SSA_NAME_DEF_STMT (arg
);
1493 /* Pass in the initial condition to the follow edge function. */
1495 res
= follow_ssa_edge (loop
, ssa_chain
, loop_phi_node
, &ev_fn
, 0);
1497 /* If ev_fn has no evolution in the inner loop, and the
1498 init_cond is not equal to ev_fn, then we have an
1499 ambiguity between two possible values, as we cannot know
1500 the number of iterations at this point. */
1501 if (TREE_CODE (ev_fn
) != POLYNOMIAL_CHREC
1502 && no_evolution_in_loop_p (ev_fn
, loop
->num
, &val
) && val
1503 && !operand_equal_p (init_cond
, ev_fn
, 0))
1504 ev_fn
= chrec_dont_know
;
1509 /* When it is impossible to go back on the same
1510 loop_phi_node by following the ssa edges, the
1511 evolution is represented by a peeled chrec, i.e. the
1512 first iteration, EV_FN has the value INIT_COND, then
1513 all the other iterations it has the value of ARG.
1514 For the moment, PEELED_CHREC nodes are not built. */
1517 ev_fn
= chrec_dont_know
;
1518 /* Try to recognize POLYNOMIAL_CHREC which appears in
1519 the form of PEELED_CHREC, but guard the process with
1520 a bool variable to keep the analyzer from infinite
1521 recurrence for real PEELED_RECs. */
1522 if (simplify_peeled_chrec_p
&& TREE_CODE (arg
) == SSA_NAME
)
1524 simplify_peeled_chrec_p
= false;
1525 ev_fn
= simplify_peeled_chrec (loop
, arg
, init_cond
);
1526 simplify_peeled_chrec_p
= true;
1530 /* When there are multiple back edges of the loop (which in fact never
1531 happens currently, but nevertheless), merge their evolutions. */
1532 evolution_function
= chrec_merge (evolution_function
, ev_fn
);
1535 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1537 fprintf (dump_file
, " (evolution_function = ");
1538 print_generic_expr (dump_file
, evolution_function
, 0);
1539 fprintf (dump_file
, "))\n");
1542 return evolution_function
;
1545 /* Given a loop-phi-node, return the initial conditions of the
1546 variable on entry of the loop. When the CCP has propagated
1547 constants into the loop-phi-node, the initial condition is
1548 instantiated, otherwise the initial condition is kept symbolic.
1549 This analyzer does not analyze the evolution outside the current
1550 loop, and leaves this task to the on-demand tree reconstructor. */
1553 analyze_initial_condition (gphi
*loop_phi_node
)
1556 tree init_cond
= chrec_not_analyzed_yet
;
1557 struct loop
*loop
= loop_containing_stmt (loop_phi_node
);
1559 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1561 fprintf (dump_file
, "(analyze_initial_condition \n");
1562 fprintf (dump_file
, " (loop_phi_node = \n");
1563 print_gimple_stmt (dump_file
, loop_phi_node
, 0, 0);
1564 fprintf (dump_file
, ")\n");
1567 n
= gimple_phi_num_args (loop_phi_node
);
1568 for (i
= 0; i
< n
; i
++)
1570 tree branch
= PHI_ARG_DEF (loop_phi_node
, i
);
1571 basic_block bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1573 /* When the branch is oriented to the loop's body, it does
1574 not contribute to the initial condition. */
1575 if (flow_bb_inside_loop_p (loop
, bb
))
1578 if (init_cond
== chrec_not_analyzed_yet
)
1584 if (TREE_CODE (branch
) == SSA_NAME
)
1586 init_cond
= chrec_dont_know
;
1590 init_cond
= chrec_merge (init_cond
, branch
);
1593 /* Ooops -- a loop without an entry??? */
1594 if (init_cond
== chrec_not_analyzed_yet
)
1595 init_cond
= chrec_dont_know
;
1597 /* During early loop unrolling we do not have fully constant propagated IL.
1598 Handle degenerate PHIs here to not miss important unrollings. */
1599 if (TREE_CODE (init_cond
) == SSA_NAME
)
1601 gimple def
= SSA_NAME_DEF_STMT (init_cond
);
1602 if (gphi
*phi
= dyn_cast
<gphi
*> (def
))
1604 tree res
= degenerate_phi_result (phi
);
1605 if (res
!= NULL_TREE
1606 /* Only allow invariants here, otherwise we may break
1607 loop-closed SSA form. */
1608 && is_gimple_min_invariant (res
))
1613 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1615 fprintf (dump_file
, " (init_cond = ");
1616 print_generic_expr (dump_file
, init_cond
, 0);
1617 fprintf (dump_file
, "))\n");
1623 /* Analyze the scalar evolution for LOOP_PHI_NODE. */
1626 interpret_loop_phi (struct loop
*loop
, gphi
*loop_phi_node
)
1629 struct loop
*phi_loop
= loop_containing_stmt (loop_phi_node
);
1632 if (phi_loop
!= loop
)
1634 struct loop
*subloop
;
1635 tree evolution_fn
= analyze_scalar_evolution
1636 (phi_loop
, PHI_RESULT (loop_phi_node
));
1638 /* Dive one level deeper. */
1639 subloop
= superloop_at_depth (phi_loop
, loop_depth (loop
) + 1);
1641 /* Interpret the subloop. */
1642 res
= compute_overall_effect_of_inner_loop (subloop
, evolution_fn
);
1646 /* Otherwise really interpret the loop phi. */
1647 init_cond
= analyze_initial_condition (loop_phi_node
);
1648 res
= analyze_evolution_in_loop (loop_phi_node
, init_cond
);
1650 /* Verify we maintained the correct initial condition throughout
1651 possible conversions in the SSA chain. */
1652 if (res
!= chrec_dont_know
)
1654 tree new_init
= res
;
1655 if (CONVERT_EXPR_P (res
)
1656 && TREE_CODE (TREE_OPERAND (res
, 0)) == POLYNOMIAL_CHREC
)
1657 new_init
= fold_convert (TREE_TYPE (res
),
1658 CHREC_LEFT (TREE_OPERAND (res
, 0)));
1659 else if (TREE_CODE (res
) == POLYNOMIAL_CHREC
)
1660 new_init
= CHREC_LEFT (res
);
1661 STRIP_USELESS_TYPE_CONVERSION (new_init
);
1662 if (TREE_CODE (new_init
) == POLYNOMIAL_CHREC
1663 || !operand_equal_p (init_cond
, new_init
, 0))
1664 return chrec_dont_know
;
1670 /* This function merges the branches of a condition-phi-node,
1671 contained in the outermost loop, and whose arguments are already
1675 interpret_condition_phi (struct loop
*loop
, gphi
*condition_phi
)
1677 int i
, n
= gimple_phi_num_args (condition_phi
);
1678 tree res
= chrec_not_analyzed_yet
;
1680 for (i
= 0; i
< n
; i
++)
1684 if (backedge_phi_arg_p (condition_phi
, i
))
1686 res
= chrec_dont_know
;
1690 branch_chrec
= analyze_scalar_evolution
1691 (loop
, PHI_ARG_DEF (condition_phi
, i
));
1693 res
= chrec_merge (res
, branch_chrec
);
1699 /* Interpret the operation RHS1 OP RHS2. If we didn't
1700 analyze this node before, follow the definitions until ending
1701 either on an analyzed GIMPLE_ASSIGN, or on a loop-phi-node. On the
1702 return path, this function propagates evolutions (ala constant copy
1703 propagation). OPND1 is not a GIMPLE expression because we could
1704 analyze the effect of an inner loop: see interpret_loop_phi. */
1707 interpret_rhs_expr (struct loop
*loop
, gimple at_stmt
,
1708 tree type
, tree rhs1
, enum tree_code code
, tree rhs2
)
1710 tree res
, chrec1
, chrec2
;
1713 if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
1715 if (is_gimple_min_invariant (rhs1
))
1716 return chrec_convert (type
, rhs1
, at_stmt
);
1718 if (code
== SSA_NAME
)
1719 return chrec_convert (type
, analyze_scalar_evolution (loop
, rhs1
),
1722 if (code
== ASSERT_EXPR
)
1724 rhs1
= ASSERT_EXPR_VAR (rhs1
);
1725 return chrec_convert (type
, analyze_scalar_evolution (loop
, rhs1
),
1733 if (TREE_CODE (TREE_OPERAND (rhs1
, 0)) == MEM_REF
1734 || handled_component_p (TREE_OPERAND (rhs1
, 0)))
1737 HOST_WIDE_INT bitsize
, bitpos
;
1744 base
= get_inner_reference (TREE_OPERAND (rhs1
, 0),
1745 &bitsize
, &bitpos
, &offset
,
1746 &mode
, &unsignedp
, &volatilep
, false);
1748 if (TREE_CODE (base
) == MEM_REF
)
1750 rhs2
= TREE_OPERAND (base
, 1);
1751 rhs1
= TREE_OPERAND (base
, 0);
1753 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1754 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1755 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1756 chrec2
= chrec_convert (TREE_TYPE (rhs2
), chrec2
, at_stmt
);
1757 chrec1
= instantiate_parameters (loop
, chrec1
);
1758 chrec2
= instantiate_parameters (loop
, chrec2
);
1759 res
= chrec_fold_plus (type
, chrec1
, chrec2
);
1763 chrec1
= analyze_scalar_evolution_for_address_of (loop
, base
);
1764 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1768 if (offset
!= NULL_TREE
)
1770 chrec2
= analyze_scalar_evolution (loop
, offset
);
1771 chrec2
= chrec_convert (TREE_TYPE (offset
), chrec2
, at_stmt
);
1772 chrec2
= instantiate_parameters (loop
, chrec2
);
1773 res
= chrec_fold_plus (type
, res
, chrec2
);
1778 gcc_assert ((bitpos
% BITS_PER_UNIT
) == 0);
1780 unitpos
= size_int (bitpos
/ BITS_PER_UNIT
);
1781 chrec3
= analyze_scalar_evolution (loop
, unitpos
);
1782 chrec3
= chrec_convert (TREE_TYPE (unitpos
), chrec3
, at_stmt
);
1783 chrec3
= instantiate_parameters (loop
, chrec3
);
1784 res
= chrec_fold_plus (type
, res
, chrec3
);
1788 res
= chrec_dont_know
;
1791 case POINTER_PLUS_EXPR
:
1792 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1793 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1794 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1795 chrec2
= chrec_convert (TREE_TYPE (rhs2
), chrec2
, at_stmt
);
1796 chrec1
= instantiate_parameters (loop
, chrec1
);
1797 chrec2
= instantiate_parameters (loop
, chrec2
);
1798 res
= chrec_fold_plus (type
, chrec1
, chrec2
);
1802 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1803 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1804 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1805 chrec2
= chrec_convert (type
, chrec2
, at_stmt
);
1806 chrec1
= instantiate_parameters (loop
, chrec1
);
1807 chrec2
= instantiate_parameters (loop
, chrec2
);
1808 res
= chrec_fold_plus (type
, chrec1
, chrec2
);
1812 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1813 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1814 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1815 chrec2
= chrec_convert (type
, chrec2
, at_stmt
);
1816 chrec1
= instantiate_parameters (loop
, chrec1
);
1817 chrec2
= instantiate_parameters (loop
, chrec2
);
1818 res
= chrec_fold_minus (type
, chrec1
, chrec2
);
1822 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1823 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1824 /* TYPE may be integer, real or complex, so use fold_convert. */
1825 chrec1
= instantiate_parameters (loop
, chrec1
);
1826 res
= chrec_fold_multiply (type
, chrec1
,
1827 fold_convert (type
, integer_minus_one_node
));
1831 /* Handle ~X as -1 - X. */
1832 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1833 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1834 chrec1
= instantiate_parameters (loop
, chrec1
);
1835 res
= chrec_fold_minus (type
,
1836 fold_convert (type
, integer_minus_one_node
),
1841 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1842 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1843 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1844 chrec2
= chrec_convert (type
, chrec2
, at_stmt
);
1845 chrec1
= instantiate_parameters (loop
, chrec1
);
1846 chrec2
= instantiate_parameters (loop
, chrec2
);
1847 res
= chrec_fold_multiply (type
, chrec1
, chrec2
);
1851 /* In case we have a truncation of a widened operation that in
1852 the truncated type has undefined overflow behavior analyze
1853 the operation done in an unsigned type of the same precision
1854 as the final truncation. We cannot derive a scalar evolution
1855 for the widened operation but for the truncated result. */
1856 if (TREE_CODE (type
) == INTEGER_TYPE
1857 && TREE_CODE (TREE_TYPE (rhs1
)) == INTEGER_TYPE
1858 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (rhs1
))
1859 && TYPE_OVERFLOW_UNDEFINED (type
)
1860 && TREE_CODE (rhs1
) == SSA_NAME
1861 && (def
= SSA_NAME_DEF_STMT (rhs1
))
1862 && is_gimple_assign (def
)
1863 && TREE_CODE_CLASS (gimple_assign_rhs_code (def
)) == tcc_binary
1864 && TREE_CODE (gimple_assign_rhs2 (def
)) == INTEGER_CST
)
1866 tree utype
= unsigned_type_for (type
);
1867 chrec1
= interpret_rhs_expr (loop
, at_stmt
, utype
,
1868 gimple_assign_rhs1 (def
),
1869 gimple_assign_rhs_code (def
),
1870 gimple_assign_rhs2 (def
));
1873 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1874 res
= chrec_convert (type
, chrec1
, at_stmt
);
1878 res
= chrec_dont_know
;
1885 /* Interpret the expression EXPR. */
1888 interpret_expr (struct loop
*loop
, gimple at_stmt
, tree expr
)
1890 enum tree_code code
;
1891 tree type
= TREE_TYPE (expr
), op0
, op1
;
1893 if (automatically_generated_chrec_p (expr
))
1896 if (TREE_CODE (expr
) == POLYNOMIAL_CHREC
1897 || get_gimple_rhs_class (TREE_CODE (expr
)) == GIMPLE_TERNARY_RHS
)
1898 return chrec_dont_know
;
1900 extract_ops_from_tree (expr
, &code
, &op0
, &op1
);
1902 return interpret_rhs_expr (loop
, at_stmt
, type
,
1906 /* Interpret the rhs of the assignment STMT. */
1909 interpret_gimple_assign (struct loop
*loop
, gimple stmt
)
1911 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1912 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1914 return interpret_rhs_expr (loop
, stmt
, type
,
1915 gimple_assign_rhs1 (stmt
), code
,
1916 gimple_assign_rhs2 (stmt
));
1921 /* This section contains all the entry points:
1922 - number_of_iterations_in_loop,
1923 - analyze_scalar_evolution,
1924 - instantiate_parameters.
1927 /* Compute and return the evolution function in WRTO_LOOP, the nearest
1928 common ancestor of DEF_LOOP and USE_LOOP. */
1931 compute_scalar_evolution_in_loop (struct loop
*wrto_loop
,
1932 struct loop
*def_loop
,
1938 if (def_loop
== wrto_loop
)
1941 def_loop
= superloop_at_depth (def_loop
, loop_depth (wrto_loop
) + 1);
1942 res
= compute_overall_effect_of_inner_loop (def_loop
, ev
);
1944 if (no_evolution_in_loop_p (res
, wrto_loop
->num
, &val
) && val
)
1947 return analyze_scalar_evolution_1 (wrto_loop
, res
, chrec_not_analyzed_yet
);
1950 /* Helper recursive function. */
1953 analyze_scalar_evolution_1 (struct loop
*loop
, tree var
, tree res
)
1955 tree type
= TREE_TYPE (var
);
1958 struct loop
*def_loop
;
1960 if (loop
== NULL
|| TREE_CODE (type
) == VECTOR_TYPE
)
1961 return chrec_dont_know
;
1963 if (TREE_CODE (var
) != SSA_NAME
)
1964 return interpret_expr (loop
, NULL
, var
);
1966 def
= SSA_NAME_DEF_STMT (var
);
1967 bb
= gimple_bb (def
);
1968 def_loop
= bb
? bb
->loop_father
: NULL
;
1971 || !flow_bb_inside_loop_p (loop
, bb
))
1973 /* Keep the symbolic form. */
1978 if (res
!= chrec_not_analyzed_yet
)
1980 if (loop
!= bb
->loop_father
)
1981 res
= compute_scalar_evolution_in_loop
1982 (find_common_loop (loop
, bb
->loop_father
), bb
->loop_father
, res
);
1987 if (loop
!= def_loop
)
1989 res
= analyze_scalar_evolution_1 (def_loop
, var
, chrec_not_analyzed_yet
);
1990 res
= compute_scalar_evolution_in_loop (loop
, def_loop
, res
);
1995 switch (gimple_code (def
))
1998 res
= interpret_gimple_assign (loop
, def
);
2002 if (loop_phi_node_p (def
))
2003 res
= interpret_loop_phi (loop
, as_a
<gphi
*> (def
));
2005 res
= interpret_condition_phi (loop
, as_a
<gphi
*> (def
));
2009 res
= chrec_dont_know
;
2015 /* Keep the symbolic form. */
2016 if (res
== chrec_dont_know
)
2019 if (loop
== def_loop
)
2020 set_scalar_evolution (block_before_loop (loop
), var
, res
);
2025 /* Analyzes and returns the scalar evolution of the ssa_name VAR in
2026 LOOP. LOOP is the loop in which the variable is used.
2028 Example of use: having a pointer VAR to a SSA_NAME node, STMT a
2029 pointer to the statement that uses this variable, in order to
2030 determine the evolution function of the variable, use the following
2033 loop_p loop = loop_containing_stmt (stmt);
2034 tree chrec_with_symbols = analyze_scalar_evolution (loop, var);
2035 tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols);
2039 analyze_scalar_evolution (struct loop
*loop
, tree var
)
2043 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2045 fprintf (dump_file
, "(analyze_scalar_evolution \n");
2046 fprintf (dump_file
, " (loop_nb = %d)\n", loop
->num
);
2047 fprintf (dump_file
, " (scalar = ");
2048 print_generic_expr (dump_file
, var
, 0);
2049 fprintf (dump_file
, ")\n");
2052 res
= get_scalar_evolution (block_before_loop (loop
), var
);
2053 res
= analyze_scalar_evolution_1 (loop
, var
, res
);
2055 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2056 fprintf (dump_file
, ")\n");
2061 /* Analyzes and returns the scalar evolution of VAR address in LOOP. */
2064 analyze_scalar_evolution_for_address_of (struct loop
*loop
, tree var
)
2066 return analyze_scalar_evolution (loop
, build_fold_addr_expr (var
));
2069 /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
2070 WRTO_LOOP (which should be a superloop of USE_LOOP)
2072 FOLDED_CASTS is set to true if resolve_mixers used
2073 chrec_convert_aggressive (TODO -- not really, we are way too conservative
2074 at the moment in order to keep things simple).
2076 To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following
2079 for (i = 0; i < 100; i++) -- loop 1
2081 for (j = 0; j < 100; j++) -- loop 2
2088 for (t = 0; t < 100; t++) -- loop 3
2095 Both k1 and k2 are invariants in loop3, thus
2096 analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1
2097 analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2
2099 As they are invariant, it does not matter whether we consider their
2100 usage in loop 3 or loop 2, hence
2101 analyze_scalar_evolution_in_loop (loop2, loop3, k1) =
2102 analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i
2103 analyze_scalar_evolution_in_loop (loop2, loop3, k2) =
2104 analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2
2106 Similarly for their evolutions with respect to loop 1. The values of K2
2107 in the use in loop 2 vary independently on loop 1, thus we cannot express
2108 the evolution with respect to loop 1:
2109 analyze_scalar_evolution_in_loop (loop1, loop3, k1) =
2110 analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1
2111 analyze_scalar_evolution_in_loop (loop1, loop3, k2) =
2112 analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know
2114 The value of k2 in the use in loop 1 is known, though:
2115 analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1
2116 analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100
2120 analyze_scalar_evolution_in_loop (struct loop
*wrto_loop
, struct loop
*use_loop
,
2121 tree version
, bool *folded_casts
)
2124 tree ev
= version
, tmp
;
2126 /* We cannot just do
2128 tmp = analyze_scalar_evolution (use_loop, version);
2129 ev = resolve_mixers (wrto_loop, tmp);
2131 as resolve_mixers would query the scalar evolution with respect to
2132 wrto_loop. For example, in the situation described in the function
2133 comment, suppose that wrto_loop = loop1, use_loop = loop3 and
2136 analyze_scalar_evolution (use_loop, version) = k2
2138 and resolve_mixers (loop1, k2) finds that the value of k2 in loop 1
2139 is 100, which is a wrong result, since we are interested in the
2142 Instead, we need to proceed from use_loop to wrto_loop loop by loop,
2143 each time checking that there is no evolution in the inner loop. */
2146 *folded_casts
= false;
2149 tmp
= analyze_scalar_evolution (use_loop
, ev
);
2150 ev
= resolve_mixers (use_loop
, tmp
);
2152 if (folded_casts
&& tmp
!= ev
)
2153 *folded_casts
= true;
2155 if (use_loop
== wrto_loop
)
2158 /* If the value of the use changes in the inner loop, we cannot express
2159 its value in the outer loop (we might try to return interval chrec,
2160 but we do not have a user for it anyway) */
2161 if (!no_evolution_in_loop_p (ev
, use_loop
->num
, &val
)
2163 return chrec_dont_know
;
2165 use_loop
= loop_outer (use_loop
);
2170 /* Hashtable helpers for a temporary hash-table used when
2171 instantiating a CHREC or resolving mixers. For this use
2172 instantiated_below is always the same. */
2174 struct instantiate_cache_type
2177 vec
<scev_info_str
> entries
;
2179 instantiate_cache_type () : map (NULL
), entries (vNULL
) {}
2180 ~instantiate_cache_type ();
2181 tree
get (unsigned slot
) { return entries
[slot
].chrec
; }
2182 void set (unsigned slot
, tree chrec
) { entries
[slot
].chrec
= chrec
; }
2185 instantiate_cache_type::~instantiate_cache_type ()
2194 /* Cache to avoid infinite recursion when instantiating an SSA name.
2195 Live during the outermost instantiate_scev or resolve_mixers call. */
2196 static instantiate_cache_type
*global_cache
;
2198 /* Computes a hash function for database element ELT. */
2200 static inline hashval_t
2201 hash_idx_scev_info (const void *elt_
)
2203 unsigned idx
= ((size_t) elt_
) - 2;
2204 return scev_info_hasher::hash (&global_cache
->entries
[idx
]);
2207 /* Compares database elements E1 and E2. */
2210 eq_idx_scev_info (const void *e1
, const void *e2
)
2212 unsigned idx1
= ((size_t) e1
) - 2;
2213 return scev_info_hasher::equal (&global_cache
->entries
[idx1
],
2214 (const scev_info_str
*) e2
);
2217 /* Returns from CACHE the slot number of the cached chrec for NAME. */
2220 get_instantiated_value_entry (instantiate_cache_type
&cache
,
2221 tree name
, basic_block instantiate_below
)
2225 cache
.map
= htab_create (10, hash_idx_scev_info
, eq_idx_scev_info
, NULL
);
2226 cache
.entries
.create (10);
2230 e
.name_version
= SSA_NAME_VERSION (name
);
2231 e
.instantiated_below
= instantiate_below
->index
;
2232 void **slot
= htab_find_slot_with_hash (cache
.map
, &e
,
2233 scev_info_hasher::hash (&e
), INSERT
);
2236 e
.chrec
= chrec_not_analyzed_yet
;
2237 *slot
= (void *)(size_t)(cache
.entries
.length () + 2);
2238 cache
.entries
.safe_push (e
);
2241 return ((size_t)*slot
) - 2;
2245 /* Return the closed_loop_phi node for VAR. If there is none, return
2249 loop_closed_phi_def (tree var
)
2256 if (var
== NULL_TREE
2257 || TREE_CODE (var
) != SSA_NAME
)
2260 loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (var
));
2261 exit
= single_exit (loop
);
2265 for (psi
= gsi_start_phis (exit
->dest
); !gsi_end_p (psi
); gsi_next (&psi
))
2268 if (PHI_ARG_DEF_FROM_EDGE (phi
, exit
) == var
)
2269 return PHI_RESULT (phi
);
2275 static tree
instantiate_scev_r (basic_block
, struct loop
*, struct loop
*,
2278 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2279 and EVOLUTION_LOOP, that were left under a symbolic form.
2281 CHREC is an SSA_NAME to be instantiated.
2283 CACHE is the cache of already instantiated values.
2285 FOLD_CONVERSIONS should be set to true when the conversions that
2286 may wrap in signed/pointer type are folded, as long as the value of
2287 the chrec is preserved.
2289 SIZE_EXPR is used for computing the size of the expression to be
2290 instantiated, and to stop if it exceeds some limit. */
2293 instantiate_scev_name (basic_block instantiate_below
,
2294 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2296 bool fold_conversions
,
2300 struct loop
*def_loop
;
2301 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (chrec
));
2303 /* A parameter (or loop invariant and we do not want to include
2304 evolutions in outer loops), nothing to do. */
2306 || loop_depth (def_bb
->loop_father
) == 0
2307 || dominated_by_p (CDI_DOMINATORS
, instantiate_below
, def_bb
))
2310 /* We cache the value of instantiated variable to avoid exponential
2311 time complexity due to reevaluations. We also store the convenient
2312 value in the cache in order to prevent infinite recursion -- we do
2313 not want to instantiate the SSA_NAME if it is in a mixer
2314 structure. This is used for avoiding the instantiation of
2315 recursively defined functions, such as:
2317 | a_2 -> {0, +, 1, +, a_2}_1 */
2319 unsigned si
= get_instantiated_value_entry (*global_cache
,
2320 chrec
, instantiate_below
);
2321 if (global_cache
->get (si
) != chrec_not_analyzed_yet
)
2322 return global_cache
->get (si
);
2324 /* On recursion return chrec_dont_know. */
2325 global_cache
->set (si
, chrec_dont_know
);
2327 def_loop
= find_common_loop (evolution_loop
, def_bb
->loop_father
);
2329 /* If the analysis yields a parametric chrec, instantiate the
2331 res
= analyze_scalar_evolution (def_loop
, chrec
);
2333 /* Don't instantiate default definitions. */
2334 if (TREE_CODE (res
) == SSA_NAME
2335 && SSA_NAME_IS_DEFAULT_DEF (res
))
2338 /* Don't instantiate loop-closed-ssa phi nodes. */
2339 else if (TREE_CODE (res
) == SSA_NAME
2340 && loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res
)))
2341 > loop_depth (def_loop
))
2344 res
= loop_closed_phi_def (chrec
);
2348 /* When there is no loop_closed_phi_def, it means that the
2349 variable is not used after the loop: try to still compute the
2350 value of the variable when exiting the loop. */
2351 if (res
== NULL_TREE
)
2353 loop_p loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (chrec
));
2354 res
= analyze_scalar_evolution (loop
, chrec
);
2355 res
= compute_overall_effect_of_inner_loop (loop
, res
);
2356 res
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2358 fold_conversions
, size_expr
);
2360 else if (!dominated_by_p (CDI_DOMINATORS
, instantiate_below
,
2361 gimple_bb (SSA_NAME_DEF_STMT (res
))))
2362 res
= chrec_dont_know
;
2365 else if (res
!= chrec_dont_know
)
2368 && def_bb
->loop_father
!= inner_loop
2369 && !flow_loop_nested_p (def_bb
->loop_father
, inner_loop
))
2370 /* ??? We could try to compute the overall effect of the loop here. */
2371 res
= chrec_dont_know
;
2373 res
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2375 fold_conversions
, size_expr
);
2378 /* Store the correct value to the cache. */
2379 global_cache
->set (si
, res
);
2383 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2384 and EVOLUTION_LOOP, that were left under a symbolic form.
2386 CHREC is a polynomial chain of recurrence to be instantiated.
2388 CACHE is the cache of already instantiated values.
2390 FOLD_CONVERSIONS should be set to true when the conversions that
2391 may wrap in signed/pointer type are folded, as long as the value of
2392 the chrec is preserved.
2394 SIZE_EXPR is used for computing the size of the expression to be
2395 instantiated, and to stop if it exceeds some limit. */
2398 instantiate_scev_poly (basic_block instantiate_below
,
2399 struct loop
*evolution_loop
, struct loop
*,
2400 tree chrec
, bool fold_conversions
, int size_expr
)
2403 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2404 get_chrec_loop (chrec
),
2405 CHREC_LEFT (chrec
), fold_conversions
,
2407 if (op0
== chrec_dont_know
)
2408 return chrec_dont_know
;
2410 op1
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2411 get_chrec_loop (chrec
),
2412 CHREC_RIGHT (chrec
), fold_conversions
,
2414 if (op1
== chrec_dont_know
)
2415 return chrec_dont_know
;
2417 if (CHREC_LEFT (chrec
) != op0
2418 || CHREC_RIGHT (chrec
) != op1
)
2420 op1
= chrec_convert_rhs (chrec_type (op0
), op1
, NULL
);
2421 chrec
= build_polynomial_chrec (CHREC_VARIABLE (chrec
), op0
, op1
);
2427 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2428 and EVOLUTION_LOOP, that were left under a symbolic form.
2430 "C0 CODE C1" is a binary expression of type TYPE to be instantiated.
2432 CACHE is the cache of already instantiated values.
2434 FOLD_CONVERSIONS should be set to true when the conversions that
2435 may wrap in signed/pointer type are folded, as long as the value of
2436 the chrec is preserved.
2438 SIZE_EXPR is used for computing the size of the expression to be
2439 instantiated, and to stop if it exceeds some limit. */
2442 instantiate_scev_binary (basic_block instantiate_below
,
2443 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2444 tree chrec
, enum tree_code code
,
2445 tree type
, tree c0
, tree c1
,
2446 bool fold_conversions
, int size_expr
)
2449 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
, inner_loop
,
2450 c0
, fold_conversions
, size_expr
);
2451 if (op0
== chrec_dont_know
)
2452 return chrec_dont_know
;
2454 op1
= instantiate_scev_r (instantiate_below
, evolution_loop
, inner_loop
,
2455 c1
, fold_conversions
, size_expr
);
2456 if (op1
== chrec_dont_know
)
2457 return chrec_dont_know
;
2462 op0
= chrec_convert (type
, op0
, NULL
);
2463 op1
= chrec_convert_rhs (type
, op1
, NULL
);
2467 case POINTER_PLUS_EXPR
:
2469 return chrec_fold_plus (type
, op0
, op1
);
2472 return chrec_fold_minus (type
, op0
, op1
);
2475 return chrec_fold_multiply (type
, op0
, op1
);
2482 return chrec
? chrec
: fold_build2 (code
, type
, c0
, c1
);
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 an array reference to be instantiated.
2490 CACHE is the cache of already instantiated values.
2492 FOLD_CONVERSIONS should be set to true when the conversions that
2493 may wrap in signed/pointer type are folded, as long as the value of
2494 the chrec is preserved.
2496 SIZE_EXPR is used for computing the size of the expression to be
2497 instantiated, and to stop if it exceeds some limit. */
2500 instantiate_array_ref (basic_block instantiate_below
,
2501 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2502 tree chrec
, bool fold_conversions
, int size_expr
)
2505 tree index
= TREE_OPERAND (chrec
, 1);
2506 tree op1
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2508 fold_conversions
, size_expr
);
2510 if (op1
== chrec_dont_know
)
2511 return chrec_dont_know
;
2513 if (chrec
&& op1
== index
)
2516 res
= unshare_expr (chrec
);
2517 TREE_OPERAND (res
, 1) = op1
;
2521 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2522 and EVOLUTION_LOOP, that were left under a symbolic form.
2524 "CHREC" that stands for a convert expression "(TYPE) OP" is to be
2527 CACHE is the cache of already instantiated values.
2529 FOLD_CONVERSIONS should be set to true when the conversions that
2530 may wrap in signed/pointer type are folded, as long as the value of
2531 the chrec is preserved.
2533 SIZE_EXPR is used for computing the size of the expression to be
2534 instantiated, and to stop if it exceeds some limit. */
2537 instantiate_scev_convert (basic_block instantiate_below
,
2538 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2539 tree chrec
, tree type
, tree op
,
2540 bool fold_conversions
, int size_expr
)
2542 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2544 fold_conversions
, size_expr
);
2546 if (op0
== chrec_dont_know
)
2547 return chrec_dont_know
;
2549 if (fold_conversions
)
2551 tree tmp
= chrec_convert_aggressive (type
, op0
);
2556 if (chrec
&& op0
== op
)
2559 /* If we used chrec_convert_aggressive, we can no longer assume that
2560 signed chrecs do not overflow, as chrec_convert does, so avoid
2561 calling it in that case. */
2562 if (fold_conversions
)
2563 return fold_convert (type
, op0
);
2565 return chrec_convert (type
, op0
, NULL
);
2568 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2569 and EVOLUTION_LOOP, that were left under a symbolic form.
2571 CHREC is a BIT_NOT_EXPR or a NEGATE_EXPR expression to be instantiated.
2572 Handle ~X as -1 - X.
2573 Handle -X as -1 * X.
2575 CACHE is the cache of already instantiated values.
2577 FOLD_CONVERSIONS should be set to true when the conversions that
2578 may wrap in signed/pointer type are folded, as long as the value of
2579 the chrec is preserved.
2581 SIZE_EXPR is used for computing the size of the expression to be
2582 instantiated, and to stop if it exceeds some limit. */
2585 instantiate_scev_not (basic_block instantiate_below
,
2586 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2588 enum tree_code code
, tree type
, tree op
,
2589 bool fold_conversions
, int size_expr
)
2591 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2593 fold_conversions
, size_expr
);
2595 if (op0
== chrec_dont_know
)
2596 return chrec_dont_know
;
2600 op0
= chrec_convert (type
, op0
, NULL
);
2605 return chrec_fold_minus
2606 (type
, fold_convert (type
, integer_minus_one_node
), op0
);
2609 return chrec_fold_multiply
2610 (type
, fold_convert (type
, integer_minus_one_node
), op0
);
2617 return chrec
? chrec
: fold_build1 (code
, type
, op0
);
2620 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2621 and EVOLUTION_LOOP, that were left under a symbolic form.
2623 CHREC is an expression with 3 operands to be instantiated.
2625 CACHE is the cache of already instantiated values.
2627 FOLD_CONVERSIONS should be set to true when the conversions that
2628 may wrap in signed/pointer type are folded, as long as the value of
2629 the chrec is preserved.
2631 SIZE_EXPR is used for computing the size of the expression to be
2632 instantiated, and to stop if it exceeds some limit. */
2635 instantiate_scev_3 (basic_block instantiate_below
,
2636 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2638 bool fold_conversions
, int size_expr
)
2641 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2642 inner_loop
, TREE_OPERAND (chrec
, 0),
2643 fold_conversions
, size_expr
);
2644 if (op0
== chrec_dont_know
)
2645 return chrec_dont_know
;
2647 op1
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2648 inner_loop
, TREE_OPERAND (chrec
, 1),
2649 fold_conversions
, size_expr
);
2650 if (op1
== chrec_dont_know
)
2651 return chrec_dont_know
;
2653 op2
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2654 inner_loop
, TREE_OPERAND (chrec
, 2),
2655 fold_conversions
, size_expr
);
2656 if (op2
== chrec_dont_know
)
2657 return chrec_dont_know
;
2659 if (op0
== TREE_OPERAND (chrec
, 0)
2660 && op1
== TREE_OPERAND (chrec
, 1)
2661 && op2
== TREE_OPERAND (chrec
, 2))
2664 return fold_build3 (TREE_CODE (chrec
),
2665 TREE_TYPE (chrec
), op0
, op1
, op2
);
2668 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2669 and EVOLUTION_LOOP, that were left under a symbolic form.
2671 CHREC is an expression with 2 operands to be instantiated.
2673 CACHE is the cache of already instantiated values.
2675 FOLD_CONVERSIONS should be set to true when the conversions that
2676 may wrap in signed/pointer type are folded, as long as the value of
2677 the chrec is preserved.
2679 SIZE_EXPR is used for computing the size of the expression to be
2680 instantiated, and to stop if it exceeds some limit. */
2683 instantiate_scev_2 (basic_block instantiate_below
,
2684 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2686 bool fold_conversions
, int size_expr
)
2689 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2690 inner_loop
, TREE_OPERAND (chrec
, 0),
2691 fold_conversions
, size_expr
);
2692 if (op0
== chrec_dont_know
)
2693 return chrec_dont_know
;
2695 op1
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2696 inner_loop
, TREE_OPERAND (chrec
, 1),
2697 fold_conversions
, size_expr
);
2698 if (op1
== chrec_dont_know
)
2699 return chrec_dont_know
;
2701 if (op0
== TREE_OPERAND (chrec
, 0)
2702 && op1
== TREE_OPERAND (chrec
, 1))
2705 return fold_build2 (TREE_CODE (chrec
), TREE_TYPE (chrec
), op0
, op1
);
2708 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2709 and EVOLUTION_LOOP, that were left under a symbolic form.
2711 CHREC is an expression with 2 operands to be instantiated.
2713 CACHE is the cache of already instantiated values.
2715 FOLD_CONVERSIONS should be set to true when the conversions that
2716 may wrap in signed/pointer type are folded, as long as the value of
2717 the chrec is preserved.
2719 SIZE_EXPR is used for computing the size of the expression to be
2720 instantiated, and to stop if it exceeds some limit. */
2723 instantiate_scev_1 (basic_block instantiate_below
,
2724 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2726 bool fold_conversions
, int size_expr
)
2728 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2729 inner_loop
, TREE_OPERAND (chrec
, 0),
2730 fold_conversions
, size_expr
);
2732 if (op0
== chrec_dont_know
)
2733 return chrec_dont_know
;
2735 if (op0
== TREE_OPERAND (chrec
, 0))
2738 return fold_build1 (TREE_CODE (chrec
), TREE_TYPE (chrec
), op0
);
2741 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2742 and EVOLUTION_LOOP, that were left under a symbolic form.
2744 CHREC is the scalar evolution to instantiate.
2746 CACHE is the cache of already instantiated values.
2748 FOLD_CONVERSIONS should be set to true when the conversions that
2749 may wrap in signed/pointer type are folded, as long as the value of
2750 the chrec is preserved.
2752 SIZE_EXPR is used for computing the size of the expression to be
2753 instantiated, and to stop if it exceeds some limit. */
2756 instantiate_scev_r (basic_block instantiate_below
,
2757 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2759 bool fold_conversions
, int size_expr
)
2761 /* Give up if the expression is larger than the MAX that we allow. */
2762 if (size_expr
++ > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE
))
2763 return chrec_dont_know
;
2765 if (chrec
== NULL_TREE
2766 || automatically_generated_chrec_p (chrec
)
2767 || is_gimple_min_invariant (chrec
))
2770 switch (TREE_CODE (chrec
))
2773 return instantiate_scev_name (instantiate_below
, evolution_loop
,
2775 fold_conversions
, size_expr
);
2777 case POLYNOMIAL_CHREC
:
2778 return instantiate_scev_poly (instantiate_below
, evolution_loop
,
2780 fold_conversions
, size_expr
);
2782 case POINTER_PLUS_EXPR
:
2786 return instantiate_scev_binary (instantiate_below
, evolution_loop
,
2788 TREE_CODE (chrec
), chrec_type (chrec
),
2789 TREE_OPERAND (chrec
, 0),
2790 TREE_OPERAND (chrec
, 1),
2791 fold_conversions
, size_expr
);
2794 return instantiate_scev_convert (instantiate_below
, evolution_loop
,
2796 TREE_TYPE (chrec
), TREE_OPERAND (chrec
, 0),
2797 fold_conversions
, size_expr
);
2801 return instantiate_scev_not (instantiate_below
, evolution_loop
,
2803 TREE_CODE (chrec
), TREE_TYPE (chrec
),
2804 TREE_OPERAND (chrec
, 0),
2805 fold_conversions
, size_expr
);
2808 case SCEV_NOT_KNOWN
:
2809 return chrec_dont_know
;
2815 return instantiate_array_ref (instantiate_below
, evolution_loop
,
2817 fold_conversions
, size_expr
);
2823 if (VL_EXP_CLASS_P (chrec
))
2824 return chrec_dont_know
;
2826 switch (TREE_CODE_LENGTH (TREE_CODE (chrec
)))
2829 return instantiate_scev_3 (instantiate_below
, evolution_loop
,
2831 fold_conversions
, size_expr
);
2834 return instantiate_scev_2 (instantiate_below
, evolution_loop
,
2836 fold_conversions
, size_expr
);
2839 return instantiate_scev_1 (instantiate_below
, evolution_loop
,
2841 fold_conversions
, size_expr
);
2850 /* Too complicated to handle. */
2851 return chrec_dont_know
;
2854 /* Analyze all the parameters of the chrec that were left under a
2855 symbolic form. INSTANTIATE_BELOW is the basic block that stops the
2856 recursive instantiation of parameters: a parameter is a variable
2857 that is defined in a basic block that dominates INSTANTIATE_BELOW or
2858 a function parameter. */
2861 instantiate_scev (basic_block instantiate_below
, struct loop
*evolution_loop
,
2866 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2868 fprintf (dump_file
, "(instantiate_scev \n");
2869 fprintf (dump_file
, " (instantiate_below = %d)\n", instantiate_below
->index
);
2870 fprintf (dump_file
, " (evolution_loop = %d)\n", evolution_loop
->num
);
2871 fprintf (dump_file
, " (chrec = ");
2872 print_generic_expr (dump_file
, chrec
, 0);
2873 fprintf (dump_file
, ")\n");
2879 global_cache
= new instantiate_cache_type
;
2883 res
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2884 NULL
, chrec
, false, 0);
2888 delete global_cache
;
2889 global_cache
= NULL
;
2892 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2894 fprintf (dump_file
, " (res = ");
2895 print_generic_expr (dump_file
, res
, 0);
2896 fprintf (dump_file
, "))\n");
2902 /* Similar to instantiate_parameters, but does not introduce the
2903 evolutions in outer loops for LOOP invariants in CHREC, and does not
2904 care about causing overflows, as long as they do not affect value
2905 of an expression. */
2908 resolve_mixers (struct loop
*loop
, tree chrec
)
2913 global_cache
= new instantiate_cache_type
;
2917 tree ret
= instantiate_scev_r (block_before_loop (loop
), loop
, NULL
,
2922 delete global_cache
;
2923 global_cache
= NULL
;
2929 /* Entry point for the analysis of the number of iterations pass.
2930 This function tries to safely approximate the number of iterations
2931 the loop will run. When this property is not decidable at compile
2932 time, the result is chrec_dont_know. Otherwise the result is a
2933 scalar or a symbolic parameter. When the number of iterations may
2934 be equal to zero and the property cannot be determined at compile
2935 time, the result is a COND_EXPR that represents in a symbolic form
2936 the conditions under which the number of iterations is not zero.
2938 Example of analysis: suppose that the loop has an exit condition:
2940 "if (b > 49) goto end_loop;"
2942 and that in a previous analysis we have determined that the
2943 variable 'b' has an evolution function:
2945 "EF = {23, +, 5}_2".
2947 When we evaluate the function at the point 5, i.e. the value of the
2948 variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
2949 and EF (6) = 53. In this case the value of 'b' on exit is '53' and
2950 the loop body has been executed 6 times. */
2953 number_of_latch_executions (struct loop
*loop
)
2956 struct tree_niter_desc niter_desc
;
2960 /* Determine whether the number of iterations in loop has already
2962 res
= loop
->nb_iterations
;
2966 may_be_zero
= NULL_TREE
;
2968 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2969 fprintf (dump_file
, "(number_of_iterations_in_loop = \n");
2971 res
= chrec_dont_know
;
2972 exit
= single_exit (loop
);
2974 if (exit
&& number_of_iterations_exit (loop
, exit
, &niter_desc
, false))
2976 may_be_zero
= niter_desc
.may_be_zero
;
2977 res
= niter_desc
.niter
;
2980 if (res
== chrec_dont_know
2982 || integer_zerop (may_be_zero
))
2984 else if (integer_nonzerop (may_be_zero
))
2985 res
= build_int_cst (TREE_TYPE (res
), 0);
2987 else if (COMPARISON_CLASS_P (may_be_zero
))
2988 res
= fold_build3 (COND_EXPR
, TREE_TYPE (res
), may_be_zero
,
2989 build_int_cst (TREE_TYPE (res
), 0), res
);
2991 res
= chrec_dont_know
;
2993 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2995 fprintf (dump_file
, " (set_nb_iterations_in_loop = ");
2996 print_generic_expr (dump_file
, res
, 0);
2997 fprintf (dump_file
, "))\n");
3000 loop
->nb_iterations
= res
;
3005 /* Counters for the stats. */
3011 unsigned nb_affine_multivar
;
3012 unsigned nb_higher_poly
;
3013 unsigned nb_chrec_dont_know
;
3014 unsigned nb_undetermined
;
3017 /* Reset the counters. */
3020 reset_chrecs_counters (struct chrec_stats
*stats
)
3022 stats
->nb_chrecs
= 0;
3023 stats
->nb_affine
= 0;
3024 stats
->nb_affine_multivar
= 0;
3025 stats
->nb_higher_poly
= 0;
3026 stats
->nb_chrec_dont_know
= 0;
3027 stats
->nb_undetermined
= 0;
3030 /* Dump the contents of a CHREC_STATS structure. */
3033 dump_chrecs_stats (FILE *file
, struct chrec_stats
*stats
)
3035 fprintf (file
, "\n(\n");
3036 fprintf (file
, "-----------------------------------------\n");
3037 fprintf (file
, "%d\taffine univariate chrecs\n", stats
->nb_affine
);
3038 fprintf (file
, "%d\taffine multivariate chrecs\n", stats
->nb_affine_multivar
);
3039 fprintf (file
, "%d\tdegree greater than 2 polynomials\n",
3040 stats
->nb_higher_poly
);
3041 fprintf (file
, "%d\tchrec_dont_know chrecs\n", stats
->nb_chrec_dont_know
);
3042 fprintf (file
, "-----------------------------------------\n");
3043 fprintf (file
, "%d\ttotal chrecs\n", stats
->nb_chrecs
);
3044 fprintf (file
, "%d\twith undetermined coefficients\n",
3045 stats
->nb_undetermined
);
3046 fprintf (file
, "-----------------------------------------\n");
3047 fprintf (file
, "%d\tchrecs in the scev database\n",
3048 (int) scalar_evolution_info
->elements ());
3049 fprintf (file
, "%d\tsets in the scev database\n", nb_set_scev
);
3050 fprintf (file
, "%d\tgets in the scev database\n", nb_get_scev
);
3051 fprintf (file
, "-----------------------------------------\n");
3052 fprintf (file
, ")\n\n");
3055 /* Gather statistics about CHREC. */
3058 gather_chrec_stats (tree chrec
, struct chrec_stats
*stats
)
3060 if (dump_file
&& (dump_flags
& TDF_STATS
))
3062 fprintf (dump_file
, "(classify_chrec ");
3063 print_generic_expr (dump_file
, chrec
, 0);
3064 fprintf (dump_file
, "\n");
3069 if (chrec
== NULL_TREE
)
3071 stats
->nb_undetermined
++;
3075 switch (TREE_CODE (chrec
))
3077 case POLYNOMIAL_CHREC
:
3078 if (evolution_function_is_affine_p (chrec
))
3080 if (dump_file
&& (dump_flags
& TDF_STATS
))
3081 fprintf (dump_file
, " affine_univariate\n");
3084 else if (evolution_function_is_affine_multivariate_p (chrec
, 0))
3086 if (dump_file
&& (dump_flags
& TDF_STATS
))
3087 fprintf (dump_file
, " affine_multivariate\n");
3088 stats
->nb_affine_multivar
++;
3092 if (dump_file
&& (dump_flags
& TDF_STATS
))
3093 fprintf (dump_file
, " higher_degree_polynomial\n");
3094 stats
->nb_higher_poly
++;
3103 if (chrec_contains_undetermined (chrec
))
3105 if (dump_file
&& (dump_flags
& TDF_STATS
))
3106 fprintf (dump_file
, " undetermined\n");
3107 stats
->nb_undetermined
++;
3110 if (dump_file
&& (dump_flags
& TDF_STATS
))
3111 fprintf (dump_file
, ")\n");
3114 /* Classify the chrecs of the whole database. */
3117 gather_stats_on_scev_database (void)
3119 struct chrec_stats stats
;
3124 reset_chrecs_counters (&stats
);
3126 hash_table
<scev_info_hasher
>::iterator iter
;
3128 FOR_EACH_HASH_TABLE_ELEMENT (*scalar_evolution_info
, elt
, scev_info_str
*,
3130 gather_chrec_stats (elt
->chrec
, &stats
);
3132 dump_chrecs_stats (dump_file
, &stats
);
3140 initialize_scalar_evolutions_analyzer (void)
3142 /* The elements below are unique. */
3143 if (chrec_dont_know
== NULL_TREE
)
3145 chrec_not_analyzed_yet
= NULL_TREE
;
3146 chrec_dont_know
= make_node (SCEV_NOT_KNOWN
);
3147 chrec_known
= make_node (SCEV_KNOWN
);
3148 TREE_TYPE (chrec_dont_know
) = void_type_node
;
3149 TREE_TYPE (chrec_known
) = void_type_node
;
3153 /* Initialize the analysis of scalar evolutions for LOOPS. */
3156 scev_initialize (void)
3160 scalar_evolution_info
= hash_table
<scev_info_hasher
>::create_ggc (100);
3162 initialize_scalar_evolutions_analyzer ();
3164 FOR_EACH_LOOP (loop
, 0)
3166 loop
->nb_iterations
= NULL_TREE
;
3170 /* Return true if SCEV is initialized. */
3173 scev_initialized_p (void)
3175 return scalar_evolution_info
!= NULL
;
3178 /* Cleans up the information cached by the scalar evolutions analysis
3179 in the hash table. */
3182 scev_reset_htab (void)
3184 if (!scalar_evolution_info
)
3187 scalar_evolution_info
->empty ();
3190 /* Cleans up the information cached by the scalar evolutions analysis
3191 in the hash table and in the loop->nb_iterations. */
3200 FOR_EACH_LOOP (loop
, 0)
3202 loop
->nb_iterations
= NULL_TREE
;
3206 /* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with
3207 respect to WRTO_LOOP and returns its base and step in IV if possible
3208 (see analyze_scalar_evolution_in_loop for more details on USE_LOOP
3209 and WRTO_LOOP). If ALLOW_NONCONSTANT_STEP is true, we want step to be
3210 invariant in LOOP. Otherwise we require it to be an integer constant.
3212 IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g.
3213 because it is computed in signed arithmetics). Consequently, adding an
3216 for (i = IV->base; ; i += IV->step)
3218 is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is
3219 false for the type of the induction variable, or you can prove that i does
3220 not wrap by some other argument. Otherwise, this might introduce undefined
3223 for (i = iv->base; ; i = (type) ((unsigned type) i + (unsigned type) iv->step))
3225 must be used instead. */
3228 simple_iv (struct loop
*wrto_loop
, struct loop
*use_loop
, tree op
,
3229 affine_iv
*iv
, bool allow_nonconstant_step
)
3234 iv
->base
= NULL_TREE
;
3235 iv
->step
= NULL_TREE
;
3236 iv
->no_overflow
= false;
3238 type
= TREE_TYPE (op
);
3239 if (!POINTER_TYPE_P (type
)
3240 && !INTEGRAL_TYPE_P (type
))
3243 ev
= analyze_scalar_evolution_in_loop (wrto_loop
, use_loop
, op
,
3245 if (chrec_contains_undetermined (ev
)
3246 || chrec_contains_symbols_defined_in_loop (ev
, wrto_loop
->num
))
3249 if (tree_does_not_contain_chrecs (ev
))
3252 iv
->step
= build_int_cst (TREE_TYPE (ev
), 0);
3253 iv
->no_overflow
= true;
3257 if (TREE_CODE (ev
) != POLYNOMIAL_CHREC
3258 || CHREC_VARIABLE (ev
) != (unsigned) wrto_loop
->num
)
3261 iv
->step
= CHREC_RIGHT (ev
);
3262 if ((!allow_nonconstant_step
&& TREE_CODE (iv
->step
) != INTEGER_CST
)
3263 || tree_contains_chrecs (iv
->step
, NULL
))
3266 iv
->base
= CHREC_LEFT (ev
);
3267 if (tree_contains_chrecs (iv
->base
, NULL
))
3270 iv
->no_overflow
= !folded_casts
&& TYPE_OVERFLOW_UNDEFINED (type
);
3275 /* Finalize the scalar evolution analysis. */
3278 scev_finalize (void)
3280 if (!scalar_evolution_info
)
3282 scalar_evolution_info
->empty ();
3283 scalar_evolution_info
= NULL
;
3286 /* Returns true if the expression EXPR is considered to be too expensive
3287 for scev_const_prop. */
3290 expression_expensive_p (tree expr
)
3292 enum tree_code code
;
3294 if (is_gimple_val (expr
))
3297 code
= TREE_CODE (expr
);
3298 if (code
== TRUNC_DIV_EXPR
3299 || code
== CEIL_DIV_EXPR
3300 || code
== FLOOR_DIV_EXPR
3301 || code
== ROUND_DIV_EXPR
3302 || code
== TRUNC_MOD_EXPR
3303 || code
== CEIL_MOD_EXPR
3304 || code
== FLOOR_MOD_EXPR
3305 || code
== ROUND_MOD_EXPR
3306 || code
== EXACT_DIV_EXPR
)
3308 /* Division by power of two is usually cheap, so we allow it.
3309 Forbid anything else. */
3310 if (!integer_pow2p (TREE_OPERAND (expr
, 1)))
3314 switch (TREE_CODE_CLASS (code
))
3317 case tcc_comparison
:
3318 if (expression_expensive_p (TREE_OPERAND (expr
, 1)))
3323 return expression_expensive_p (TREE_OPERAND (expr
, 0));
3330 /* Replace ssa names for that scev can prove they are constant by the
3331 appropriate constants. Also perform final value replacement in loops,
3332 in case the replacement expressions are cheap.
3334 We only consider SSA names defined by phi nodes; rest is left to the
3335 ordinary constant propagation pass. */
3338 scev_const_prop (void)
3341 tree name
, type
, ev
;
3344 struct loop
*loop
, *ex_loop
;
3345 bitmap ssa_names_to_remove
= NULL
;
3349 if (number_of_loops (cfun
) <= 1)
3352 FOR_EACH_BB_FN (bb
, cfun
)
3354 loop
= bb
->loop_father
;
3356 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
3359 name
= PHI_RESULT (phi
);
3361 if (virtual_operand_p (name
))
3364 type
= TREE_TYPE (name
);
3366 if (!POINTER_TYPE_P (type
)
3367 && !INTEGRAL_TYPE_P (type
))
3370 ev
= resolve_mixers (loop
, analyze_scalar_evolution (loop
, name
));
3371 if (!is_gimple_min_invariant (ev
)
3372 || !may_propagate_copy (name
, ev
))
3375 /* Replace the uses of the name. */
3377 replace_uses_by (name
, ev
);
3379 if (!ssa_names_to_remove
)
3380 ssa_names_to_remove
= BITMAP_ALLOC (NULL
);
3381 bitmap_set_bit (ssa_names_to_remove
, SSA_NAME_VERSION (name
));
3385 /* Remove the ssa names that were replaced by constants. We do not
3386 remove them directly in the previous cycle, since this
3387 invalidates scev cache. */
3388 if (ssa_names_to_remove
)
3392 EXECUTE_IF_SET_IN_BITMAP (ssa_names_to_remove
, 0, i
, bi
)
3394 gimple_stmt_iterator psi
;
3395 name
= ssa_name (i
);
3396 phi
= as_a
<gphi
*> (SSA_NAME_DEF_STMT (name
));
3398 gcc_assert (gimple_code (phi
) == GIMPLE_PHI
);
3399 psi
= gsi_for_stmt (phi
);
3400 remove_phi_node (&psi
, true);
3403 BITMAP_FREE (ssa_names_to_remove
);
3407 /* Now the regular final value replacement. */
3408 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
3411 tree def
, rslt
, niter
;
3412 gimple_stmt_iterator gsi
;
3414 /* If we do not know exact number of iterations of the loop, we cannot
3415 replace the final value. */
3416 exit
= single_exit (loop
);
3420 niter
= number_of_latch_executions (loop
);
3421 if (niter
== chrec_dont_know
)
3424 /* Ensure that it is possible to insert new statements somewhere. */
3425 if (!single_pred_p (exit
->dest
))
3426 split_loop_exit_edge (exit
);
3427 gsi
= gsi_after_labels (exit
->dest
);
3429 ex_loop
= superloop_at_depth (loop
,
3430 loop_depth (exit
->dest
->loop_father
) + 1);
3432 for (psi
= gsi_start_phis (exit
->dest
); !gsi_end_p (psi
); )
3435 rslt
= PHI_RESULT (phi
);
3436 def
= PHI_ARG_DEF_FROM_EDGE (phi
, exit
);
3437 if (virtual_operand_p (def
))
3443 if (!POINTER_TYPE_P (TREE_TYPE (def
))
3444 && !INTEGRAL_TYPE_P (TREE_TYPE (def
)))
3451 def
= analyze_scalar_evolution_in_loop (ex_loop
, loop
, def
,
3453 def
= compute_overall_effect_of_inner_loop (ex_loop
, def
);
3454 if (!tree_does_not_contain_chrecs (def
)
3455 || chrec_contains_symbols_defined_in_loop (def
, ex_loop
->num
)
3456 /* Moving the computation from the loop may prolong life range
3457 of some ssa names, which may cause problems if they appear
3458 on abnormal edges. */
3459 || contains_abnormal_ssa_name_p (def
)
3460 /* Do not emit expensive expressions. The rationale is that
3461 when someone writes a code like
3463 while (n > 45) n -= 45;
3465 he probably knows that n is not large, and does not want it
3466 to be turned into n %= 45. */
3467 || expression_expensive_p (def
))
3469 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3471 fprintf (dump_file
, "not replacing:\n ");
3472 print_gimple_stmt (dump_file
, phi
, 0, 0);
3473 fprintf (dump_file
, "\n");
3479 /* Eliminate the PHI node and replace it by a computation outside
3483 fprintf (dump_file
, "\nfinal value replacement:\n ");
3484 print_gimple_stmt (dump_file
, phi
, 0, 0);
3485 fprintf (dump_file
, " with\n ");
3487 def
= unshare_expr (def
);
3488 remove_phi_node (&psi
, false);
3490 /* If def's type has undefined overflow and there were folded
3491 casts, rewrite all stmts added for def into arithmetics
3492 with defined overflow behavior. */
3493 if (folded_casts
&& TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (def
)))
3496 gimple_stmt_iterator gsi2
;
3497 def
= force_gimple_operand (def
, &stmts
, true, NULL_TREE
);
3498 gsi2
= gsi_start (stmts
);
3499 while (!gsi_end_p (gsi2
))
3501 gimple stmt
= gsi_stmt (gsi2
);
3502 gimple_stmt_iterator gsi3
= gsi2
;
3504 gsi_remove (&gsi3
, false);
3505 if (is_gimple_assign (stmt
)
3506 && arith_code_with_undefined_signed_overflow
3507 (gimple_assign_rhs_code (stmt
)))
3508 gsi_insert_seq_before (&gsi
,
3509 rewrite_to_defined_overflow (stmt
),
3512 gsi_insert_before (&gsi
, stmt
, GSI_SAME_STMT
);
3516 def
= force_gimple_operand_gsi (&gsi
, def
, false, NULL_TREE
,
3517 true, GSI_SAME_STMT
);
3519 ass
= gimple_build_assign (rslt
, def
);
3520 gsi_insert_before (&gsi
, ass
, GSI_SAME_STMT
);
3523 print_gimple_stmt (dump_file
, ass
, 0, 0);
3524 fprintf (dump_file
, "\n");
3531 #include "gt-tree-scalar-evolution.h"