PR jit/66539: Properly add testcase
[official-gcc.git] / gcc / tree-scalar-evolution.c
blob52d2c9dd464b3bcdf362039178fd28559bde326a
1 /* Scalar evolution detector.
2 Copyright (C) 2003-2015 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
10 version.
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
15 for more details.
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/>. */
22 Description:
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
48 its definition:
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.
73 Examples:
75 Example 1: Illustration of the basic algorithm.
77 | a = 3
78 | loop_1
79 | b = phi (a, c)
80 | c = b + 1
81 | if (c > 10) exit_loop
82 | endloop
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:
115 a -> 3
116 b -> {3, +, 1}_1
117 c -> {4, +, 1}_1
119 or in terms of a C program:
121 | a = 3
122 | for (x = 0; x <= 7; x++)
124 | b = x + 3
125 | c = x + 4
128 Example 2a: Illustration of the algorithm on nested loops.
130 | loop_1
131 | a = phi (1, b)
132 | c = a + 2
133 | loop_2 10 times
134 | b = phi (c, d)
135 | d = b + 3
136 | endloop
137 | endloop
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:
143 b -> {c, +, 3}_2
144 d -> {c + 3, +, 3}_2
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:
154 a -> {1, +, 32}_1
155 c -> {3, +, 32}_1
157 Example 2b: Multivariate chains of recurrences.
159 | loop_1
160 | k = phi (0, k + 1)
161 | loop_2 4 times
162 | j = phi (0, j + 1)
163 | loop_3 4 times
164 | i = phi (0, i + 1)
165 | A[j + k] = ...
166 | endloop
167 | endloop
168 | endloop
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.
182 | loop_1
183 | a = phi (2, b)
184 | c = phi (5, d)
185 | b = a + 1
186 | d = c + a
187 | endloop
189 a -> {2, +, 1}_1
190 b -> {3, +, 1}_1
191 c -> {5, +, a}_1
192 d -> {5 + a, +, a}_1
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.
199 | loop_1
200 | a = phi (1, b)
201 | c = phi (3, d)
202 | b = c
203 | d = c + a
204 | endloop
206 a -> (1, c)_1
207 c -> {3, +, a}_1
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.
220 | loop_1
221 | a = phi (1, b)
222 | c = phi (3, d)
223 | b = c
224 | d = a
225 | endloop
227 a -> (1, c)_1
228 c -> (3, a)_1
230 Based on these symbolic chrecs, it is possible to refine this
231 information into the more precise PERIODIC_CHRECs:
233 a -> |1, 3|_1
234 c -> |3, 1|_1
236 This transformation is not yet implemented.
238 Further readings:
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
256 #include "config.h"
257 #include "system.h"
258 #include "coretypes.h"
259 #include "input.h"
260 #include "alias.h"
261 #include "symtab.h"
262 #include "options.h"
263 #include "tree.h"
264 #include "fold-const.h"
265 #include "tm.h"
266 #include "hard-reg-set.h"
267 #include "function.h"
268 #include "rtl.h"
269 #include "flags.h"
270 #include "insn-config.h"
271 #include "expmed.h"
272 #include "dojump.h"
273 #include "explow.h"
274 #include "calls.h"
275 #include "emit-rtl.h"
276 #include "varasm.h"
277 #include "stmt.h"
278 #include "expr.h"
279 #include "gimple-pretty-print.h"
280 #include "predict.h"
281 #include "dominance.h"
282 #include "cfg.h"
283 #include "basic-block.h"
284 #include "tree-ssa-alias.h"
285 #include "internal-fn.h"
286 #include "gimple-expr.h"
287 #include "is-a.h"
288 #include "gimple.h"
289 #include "gimplify.h"
290 #include "gimple-iterator.h"
291 #include "gimplify-me.h"
292 #include "gimple-ssa.h"
293 #include "tree-cfg.h"
294 #include "tree-phinodes.h"
295 #include "stringpool.h"
296 #include "tree-ssanames.h"
297 #include "tree-ssa-loop-ivopts.h"
298 #include "tree-ssa-loop-manip.h"
299 #include "tree-ssa-loop-niter.h"
300 #include "tree-ssa-loop.h"
301 #include "tree-ssa.h"
302 #include "cfgloop.h"
303 #include "tree-chrec.h"
304 #include "tree-affine.h"
305 #include "tree-scalar-evolution.h"
306 #include "dumpfile.h"
307 #include "params.h"
308 #include "tree-ssa-propagate.h"
309 #include "gimple-fold.h"
311 static tree analyze_scalar_evolution_1 (struct loop *, tree, tree);
312 static tree analyze_scalar_evolution_for_address_of (struct loop *loop,
313 tree var);
315 /* The cached information about an SSA name with version NAME_VERSION,
316 claiming that below basic block with index INSTANTIATED_BELOW, the
317 value of the SSA name can be expressed as CHREC. */
319 struct GTY((for_user)) scev_info_str {
320 unsigned int name_version;
321 int instantiated_below;
322 tree chrec;
325 /* Counters for the scev database. */
326 static unsigned nb_set_scev = 0;
327 static unsigned nb_get_scev = 0;
329 /* The following trees are unique elements. Thus the comparison of
330 another element to these elements should be done on the pointer to
331 these trees, and not on their value. */
333 /* The SSA_NAMEs that are not yet analyzed are qualified with NULL_TREE. */
334 tree chrec_not_analyzed_yet;
336 /* Reserved to the cases where the analyzer has detected an
337 undecidable property at compile time. */
338 tree chrec_dont_know;
340 /* When the analyzer has detected that a property will never
341 happen, then it qualifies it with chrec_known. */
342 tree chrec_known;
344 struct scev_info_hasher : ggc_hasher<scev_info_str *>
346 static hashval_t hash (scev_info_str *i);
347 static bool equal (const scev_info_str *a, const scev_info_str *b);
350 static GTY (()) hash_table<scev_info_hasher> *scalar_evolution_info;
353 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */
355 static inline struct scev_info_str *
356 new_scev_info_str (basic_block instantiated_below, tree var)
358 struct scev_info_str *res;
360 res = ggc_alloc<scev_info_str> ();
361 res->name_version = SSA_NAME_VERSION (var);
362 res->chrec = chrec_not_analyzed_yet;
363 res->instantiated_below = instantiated_below->index;
365 return res;
368 /* Computes a hash function for database element ELT. */
370 hashval_t
371 scev_info_hasher::hash (scev_info_str *elt)
373 return elt->name_version ^ elt->instantiated_below;
376 /* Compares database elements E1 and E2. */
378 bool
379 scev_info_hasher::equal (const scev_info_str *elt1, const scev_info_str *elt2)
381 return (elt1->name_version == elt2->name_version
382 && elt1->instantiated_below == elt2->instantiated_below);
385 /* Get the scalar evolution of VAR for INSTANTIATED_BELOW basic block.
386 A first query on VAR returns chrec_not_analyzed_yet. */
388 static tree *
389 find_var_scev_info (basic_block instantiated_below, tree var)
391 struct scev_info_str *res;
392 struct scev_info_str tmp;
394 tmp.name_version = SSA_NAME_VERSION (var);
395 tmp.instantiated_below = instantiated_below->index;
396 scev_info_str **slot = scalar_evolution_info->find_slot (&tmp, INSERT);
398 if (!*slot)
399 *slot = new_scev_info_str (instantiated_below, var);
400 res = *slot;
402 return &res->chrec;
405 /* Return true when CHREC contains symbolic names defined in
406 LOOP_NB. */
408 bool
409 chrec_contains_symbols_defined_in_loop (const_tree chrec, unsigned loop_nb)
411 int i, n;
413 if (chrec == NULL_TREE)
414 return false;
416 if (is_gimple_min_invariant (chrec))
417 return false;
419 if (TREE_CODE (chrec) == SSA_NAME)
421 gimple def;
422 loop_p def_loop, loop;
424 if (SSA_NAME_IS_DEFAULT_DEF (chrec))
425 return false;
427 def = SSA_NAME_DEF_STMT (chrec);
428 def_loop = loop_containing_stmt (def);
429 loop = get_loop (cfun, loop_nb);
431 if (def_loop == NULL)
432 return false;
434 if (loop == def_loop || flow_loop_nested_p (loop, def_loop))
435 return true;
437 return false;
440 n = TREE_OPERAND_LENGTH (chrec);
441 for (i = 0; i < n; i++)
442 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec, i),
443 loop_nb))
444 return true;
445 return false;
448 /* Return true when PHI is a loop-phi-node. */
450 static bool
451 loop_phi_node_p (gimple phi)
453 /* The implementation of this function is based on the following
454 property: "all the loop-phi-nodes of a loop are contained in the
455 loop's header basic block". */
457 return loop_containing_stmt (phi)->header == gimple_bb (phi);
460 /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
461 In general, in the case of multivariate evolutions we want to get
462 the evolution in different loops. LOOP specifies the level for
463 which to get the evolution.
465 Example:
467 | for (j = 0; j < 100; j++)
469 | for (k = 0; k < 100; k++)
471 | i = k + j; - Here the value of i is a function of j, k.
473 | ... = i - Here the value of i is a function of j.
475 | ... = i - Here the value of i is a scalar.
477 Example:
479 | i_0 = ...
480 | loop_1 10 times
481 | i_1 = phi (i_0, i_2)
482 | i_2 = i_1 + 2
483 | endloop
485 This loop has the same effect as:
486 LOOP_1 has the same effect as:
488 | i_1 = i_0 + 20
490 The overall effect of the loop, "i_0 + 20" in the previous example,
491 is obtained by passing in the parameters: LOOP = 1,
492 EVOLUTION_FN = {i_0, +, 2}_1.
495 tree
496 compute_overall_effect_of_inner_loop (struct loop *loop, tree evolution_fn)
498 bool val = false;
500 if (evolution_fn == chrec_dont_know)
501 return chrec_dont_know;
503 else if (TREE_CODE (evolution_fn) == POLYNOMIAL_CHREC)
505 struct loop *inner_loop = get_chrec_loop (evolution_fn);
507 if (inner_loop == loop
508 || flow_loop_nested_p (loop, inner_loop))
510 tree nb_iter = number_of_latch_executions (inner_loop);
512 if (nb_iter == chrec_dont_know)
513 return chrec_dont_know;
514 else
516 tree res;
518 /* evolution_fn is the evolution function in LOOP. Get
519 its value in the nb_iter-th iteration. */
520 res = chrec_apply (inner_loop->num, evolution_fn, nb_iter);
522 if (chrec_contains_symbols_defined_in_loop (res, loop->num))
523 res = instantiate_parameters (loop, res);
525 /* Continue the computation until ending on a parent of LOOP. */
526 return compute_overall_effect_of_inner_loop (loop, res);
529 else
530 return evolution_fn;
533 /* If the evolution function is an invariant, there is nothing to do. */
534 else if (no_evolution_in_loop_p (evolution_fn, loop->num, &val) && val)
535 return evolution_fn;
537 else
538 return chrec_dont_know;
541 /* Associate CHREC to SCALAR. */
543 static void
544 set_scalar_evolution (basic_block instantiated_below, tree scalar, tree chrec)
546 tree *scalar_info;
548 if (TREE_CODE (scalar) != SSA_NAME)
549 return;
551 scalar_info = find_var_scev_info (instantiated_below, scalar);
553 if (dump_file)
555 if (dump_flags & TDF_SCEV)
557 fprintf (dump_file, "(set_scalar_evolution \n");
558 fprintf (dump_file, " instantiated_below = %d \n",
559 instantiated_below->index);
560 fprintf (dump_file, " (scalar = ");
561 print_generic_expr (dump_file, scalar, 0);
562 fprintf (dump_file, ")\n (scalar_evolution = ");
563 print_generic_expr (dump_file, chrec, 0);
564 fprintf (dump_file, "))\n");
566 if (dump_flags & TDF_STATS)
567 nb_set_scev++;
570 *scalar_info = chrec;
573 /* Retrieve the chrec associated to SCALAR instantiated below
574 INSTANTIATED_BELOW block. */
576 static tree
577 get_scalar_evolution (basic_block instantiated_below, tree scalar)
579 tree res;
581 if (dump_file)
583 if (dump_flags & TDF_SCEV)
585 fprintf (dump_file, "(get_scalar_evolution \n");
586 fprintf (dump_file, " (scalar = ");
587 print_generic_expr (dump_file, scalar, 0);
588 fprintf (dump_file, ")\n");
590 if (dump_flags & TDF_STATS)
591 nb_get_scev++;
594 switch (TREE_CODE (scalar))
596 case SSA_NAME:
597 res = *find_var_scev_info (instantiated_below, scalar);
598 break;
600 case REAL_CST:
601 case FIXED_CST:
602 case INTEGER_CST:
603 res = scalar;
604 break;
606 default:
607 res = chrec_not_analyzed_yet;
608 break;
611 if (dump_file && (dump_flags & TDF_SCEV))
613 fprintf (dump_file, " (scalar_evolution = ");
614 print_generic_expr (dump_file, res, 0);
615 fprintf (dump_file, "))\n");
618 return res;
621 /* Helper function for add_to_evolution. Returns the evolution
622 function for an assignment of the form "a = b + c", where "a" and
623 "b" are on the strongly connected component. CHREC_BEFORE is the
624 information that we already have collected up to this point.
625 TO_ADD is the evolution of "c".
627 When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
628 evolution the expression TO_ADD, otherwise construct an evolution
629 part for this loop. */
631 static tree
632 add_to_evolution_1 (unsigned loop_nb, tree chrec_before, tree to_add,
633 gimple at_stmt)
635 tree type, left, right;
636 struct loop *loop = get_loop (cfun, loop_nb), *chloop;
638 switch (TREE_CODE (chrec_before))
640 case POLYNOMIAL_CHREC:
641 chloop = get_chrec_loop (chrec_before);
642 if (chloop == loop
643 || flow_loop_nested_p (chloop, loop))
645 unsigned var;
647 type = chrec_type (chrec_before);
649 /* When there is no evolution part in this loop, build it. */
650 if (chloop != loop)
652 var = loop_nb;
653 left = chrec_before;
654 right = SCALAR_FLOAT_TYPE_P (type)
655 ? build_real (type, dconst0)
656 : build_int_cst (type, 0);
658 else
660 var = CHREC_VARIABLE (chrec_before);
661 left = CHREC_LEFT (chrec_before);
662 right = CHREC_RIGHT (chrec_before);
665 to_add = chrec_convert (type, to_add, at_stmt);
666 right = chrec_convert_rhs (type, right, at_stmt);
667 right = chrec_fold_plus (chrec_type (right), right, to_add);
668 return build_polynomial_chrec (var, left, right);
670 else
672 gcc_assert (flow_loop_nested_p (loop, chloop));
674 /* Search the evolution in LOOP_NB. */
675 left = add_to_evolution_1 (loop_nb, CHREC_LEFT (chrec_before),
676 to_add, at_stmt);
677 right = CHREC_RIGHT (chrec_before);
678 right = chrec_convert_rhs (chrec_type (left), right, at_stmt);
679 return build_polynomial_chrec (CHREC_VARIABLE (chrec_before),
680 left, right);
683 default:
684 /* These nodes do not depend on a loop. */
685 if (chrec_before == chrec_dont_know)
686 return chrec_dont_know;
688 left = chrec_before;
689 right = chrec_convert_rhs (chrec_type (left), to_add, at_stmt);
690 return build_polynomial_chrec (loop_nb, left, right);
694 /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
695 of LOOP_NB.
697 Description (provided for completeness, for those who read code in
698 a plane, and for my poor 62 bytes brain that would have forgotten
699 all this in the next two or three months):
701 The algorithm of translation of programs from the SSA representation
702 into the chrecs syntax is based on a pattern matching. After having
703 reconstructed the overall tree expression for a loop, there are only
704 two cases that can arise:
706 1. a = loop-phi (init, a + expr)
707 2. a = loop-phi (init, expr)
709 where EXPR is either a scalar constant with respect to the analyzed
710 loop (this is a degree 0 polynomial), or an expression containing
711 other loop-phi definitions (these are higher degree polynomials).
713 Examples:
716 | init = ...
717 | loop_1
718 | a = phi (init, a + 5)
719 | endloop
722 | inita = ...
723 | initb = ...
724 | loop_1
725 | a = phi (inita, 2 * b + 3)
726 | b = phi (initb, b + 1)
727 | endloop
729 For the first case, the semantics of the SSA representation is:
731 | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
733 that is, there is a loop index "x" that determines the scalar value
734 of the variable during the loop execution. During the first
735 iteration, the value is that of the initial condition INIT, while
736 during the subsequent iterations, it is the sum of the initial
737 condition with the sum of all the values of EXPR from the initial
738 iteration to the before last considered iteration.
740 For the second case, the semantics of the SSA program is:
742 | a (x) = init, if x = 0;
743 | expr (x - 1), otherwise.
745 The second case corresponds to the PEELED_CHREC, whose syntax is
746 close to the syntax of a loop-phi-node:
748 | phi (init, expr) vs. (init, expr)_x
750 The proof of the translation algorithm for the first case is a
751 proof by structural induction based on the degree of EXPR.
753 Degree 0:
754 When EXPR is a constant with respect to the analyzed loop, or in
755 other words when EXPR is a polynomial of degree 0, the evolution of
756 the variable A in the loop is an affine function with an initial
757 condition INIT, and a step EXPR. In order to show this, we start
758 from the semantics of the SSA representation:
760 f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
762 and since "expr (j)" is a constant with respect to "j",
764 f (x) = init + x * expr
766 Finally, based on the semantics of the pure sum chrecs, by
767 identification we get the corresponding chrecs syntax:
769 f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
770 f (x) -> {init, +, expr}_x
772 Higher degree:
773 Suppose that EXPR is a polynomial of degree N with respect to the
774 analyzed loop_x for which we have already determined that it is
775 written under the chrecs syntax:
777 | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
779 We start from the semantics of the SSA program:
781 | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
783 | f (x) = init + \sum_{j = 0}^{x - 1}
784 | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
786 | f (x) = init + \sum_{j = 0}^{x - 1}
787 | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
789 | f (x) = init + \sum_{k = 0}^{n - 1}
790 | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
792 | f (x) = init + \sum_{k = 0}^{n - 1}
793 | (b_k * \binom{x}{k + 1})
795 | f (x) = init + b_0 * \binom{x}{1} + ...
796 | + b_{n-1} * \binom{x}{n}
798 | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
799 | + b_{n-1} * \binom{x}{n}
802 And finally from the definition of the chrecs syntax, we identify:
803 | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x
805 This shows the mechanism that stands behind the add_to_evolution
806 function. An important point is that the use of symbolic
807 parameters avoids the need of an analysis schedule.
809 Example:
811 | inita = ...
812 | initb = ...
813 | loop_1
814 | a = phi (inita, a + 2 + b)
815 | b = phi (initb, b + 1)
816 | endloop
818 When analyzing "a", the algorithm keeps "b" symbolically:
820 | a -> {inita, +, 2 + b}_1
822 Then, after instantiation, the analyzer ends on the evolution:
824 | a -> {inita, +, 2 + initb, +, 1}_1
828 static tree
829 add_to_evolution (unsigned loop_nb, tree chrec_before, enum tree_code code,
830 tree to_add, gimple at_stmt)
832 tree type = chrec_type (to_add);
833 tree res = NULL_TREE;
835 if (to_add == NULL_TREE)
836 return chrec_before;
838 /* TO_ADD is either a scalar, or a parameter. TO_ADD is not
839 instantiated at this point. */
840 if (TREE_CODE (to_add) == POLYNOMIAL_CHREC)
841 /* This should not happen. */
842 return chrec_dont_know;
844 if (dump_file && (dump_flags & TDF_SCEV))
846 fprintf (dump_file, "(add_to_evolution \n");
847 fprintf (dump_file, " (loop_nb = %d)\n", loop_nb);
848 fprintf (dump_file, " (chrec_before = ");
849 print_generic_expr (dump_file, chrec_before, 0);
850 fprintf (dump_file, ")\n (to_add = ");
851 print_generic_expr (dump_file, to_add, 0);
852 fprintf (dump_file, ")\n");
855 if (code == MINUS_EXPR)
856 to_add = chrec_fold_multiply (type, to_add, SCALAR_FLOAT_TYPE_P (type)
857 ? build_real (type, dconstm1)
858 : build_int_cst_type (type, -1));
860 res = add_to_evolution_1 (loop_nb, chrec_before, to_add, at_stmt);
862 if (dump_file && (dump_flags & TDF_SCEV))
864 fprintf (dump_file, " (res = ");
865 print_generic_expr (dump_file, res, 0);
866 fprintf (dump_file, "))\n");
869 return res;
874 /* This section selects the loops that will be good candidates for the
875 scalar evolution analysis. For the moment, greedily select all the
876 loop nests we could analyze. */
878 /* For a loop with a single exit edge, return the COND_EXPR that
879 guards the exit edge. If the expression is too difficult to
880 analyze, then give up. */
882 gcond *
883 get_loop_exit_condition (const struct loop *loop)
885 gcond *res = NULL;
886 edge exit_edge = single_exit (loop);
888 if (dump_file && (dump_flags & TDF_SCEV))
889 fprintf (dump_file, "(get_loop_exit_condition \n ");
891 if (exit_edge)
893 gimple stmt;
895 stmt = last_stmt (exit_edge->src);
896 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
897 res = cond_stmt;
900 if (dump_file && (dump_flags & TDF_SCEV))
902 print_gimple_stmt (dump_file, res, 0, 0);
903 fprintf (dump_file, ")\n");
906 return res;
910 /* Depth first search algorithm. */
912 typedef enum t_bool {
913 t_false,
914 t_true,
915 t_dont_know
916 } t_bool;
919 static t_bool follow_ssa_edge (struct loop *loop, gimple, gphi *,
920 tree *, int);
922 /* Follow the ssa edge into the binary expression RHS0 CODE RHS1.
923 Return true if the strongly connected component has been found. */
925 static t_bool
926 follow_ssa_edge_binary (struct loop *loop, gimple at_stmt,
927 tree type, tree rhs0, enum tree_code code, tree rhs1,
928 gphi *halting_phi, tree *evolution_of_loop,
929 int limit)
931 t_bool res = t_false;
932 tree evol;
934 switch (code)
936 case POINTER_PLUS_EXPR:
937 case PLUS_EXPR:
938 if (TREE_CODE (rhs0) == SSA_NAME)
940 if (TREE_CODE (rhs1) == SSA_NAME)
942 /* Match an assignment under the form:
943 "a = b + c". */
945 /* We want only assignments of form "name + name" contribute to
946 LIMIT, as the other cases do not necessarily contribute to
947 the complexity of the expression. */
948 limit++;
950 evol = *evolution_of_loop;
951 evol = add_to_evolution
952 (loop->num,
953 chrec_convert (type, evol, at_stmt),
954 code, rhs1, at_stmt);
955 res = follow_ssa_edge
956 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi, &evol, limit);
957 if (res == t_true)
958 *evolution_of_loop = evol;
959 else if (res == t_false)
961 *evolution_of_loop = add_to_evolution
962 (loop->num,
963 chrec_convert (type, *evolution_of_loop, at_stmt),
964 code, rhs0, at_stmt);
965 res = follow_ssa_edge
966 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
967 evolution_of_loop, limit);
968 if (res == t_true)
970 else if (res == t_dont_know)
971 *evolution_of_loop = chrec_dont_know;
974 else if (res == t_dont_know)
975 *evolution_of_loop = chrec_dont_know;
978 else
980 /* Match an assignment under the form:
981 "a = b + ...". */
982 *evolution_of_loop = add_to_evolution
983 (loop->num, chrec_convert (type, *evolution_of_loop,
984 at_stmt),
985 code, rhs1, at_stmt);
986 res = follow_ssa_edge
987 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
988 evolution_of_loop, limit);
989 if (res == t_true)
991 else if (res == t_dont_know)
992 *evolution_of_loop = chrec_dont_know;
996 else if (TREE_CODE (rhs1) == SSA_NAME)
998 /* Match an assignment under the form:
999 "a = ... + c". */
1000 *evolution_of_loop = add_to_evolution
1001 (loop->num, chrec_convert (type, *evolution_of_loop,
1002 at_stmt),
1003 code, rhs0, at_stmt);
1004 res = follow_ssa_edge
1005 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
1006 evolution_of_loop, limit);
1007 if (res == t_true)
1009 else if (res == t_dont_know)
1010 *evolution_of_loop = chrec_dont_know;
1013 else
1014 /* Otherwise, match an assignment under the form:
1015 "a = ... + ...". */
1016 /* And there is nothing to do. */
1017 res = t_false;
1018 break;
1020 case MINUS_EXPR:
1021 /* This case is under the form "opnd0 = rhs0 - rhs1". */
1022 if (TREE_CODE (rhs0) == SSA_NAME)
1024 /* Match an assignment under the form:
1025 "a = b - ...". */
1027 /* We want only assignments of form "name - name" contribute to
1028 LIMIT, as the other cases do not necessarily contribute to
1029 the complexity of the expression. */
1030 if (TREE_CODE (rhs1) == SSA_NAME)
1031 limit++;
1033 *evolution_of_loop = add_to_evolution
1034 (loop->num, chrec_convert (type, *evolution_of_loop, at_stmt),
1035 MINUS_EXPR, rhs1, at_stmt);
1036 res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
1037 evolution_of_loop, limit);
1038 if (res == t_true)
1040 else if (res == t_dont_know)
1041 *evolution_of_loop = chrec_dont_know;
1043 else
1044 /* Otherwise, match an assignment under the form:
1045 "a = ... - ...". */
1046 /* And there is nothing to do. */
1047 res = t_false;
1048 break;
1050 default:
1051 res = t_false;
1054 return res;
1057 /* Follow the ssa edge into the expression EXPR.
1058 Return true if the strongly connected component has been found. */
1060 static t_bool
1061 follow_ssa_edge_expr (struct loop *loop, gimple at_stmt, tree expr,
1062 gphi *halting_phi, tree *evolution_of_loop,
1063 int limit)
1065 enum tree_code code = TREE_CODE (expr);
1066 tree type = TREE_TYPE (expr), rhs0, rhs1;
1067 t_bool res;
1069 /* The EXPR is one of the following cases:
1070 - an SSA_NAME,
1071 - an INTEGER_CST,
1072 - a PLUS_EXPR,
1073 - a POINTER_PLUS_EXPR,
1074 - a MINUS_EXPR,
1075 - an ASSERT_EXPR,
1076 - other cases are not yet handled. */
1078 switch (code)
1080 CASE_CONVERT:
1081 /* This assignment is under the form "a_1 = (cast) rhs. */
1082 res = follow_ssa_edge_expr (loop, at_stmt, TREE_OPERAND (expr, 0),
1083 halting_phi, evolution_of_loop, limit);
1084 *evolution_of_loop = chrec_convert (type, *evolution_of_loop, at_stmt);
1085 break;
1087 case INTEGER_CST:
1088 /* This assignment is under the form "a_1 = 7". */
1089 res = t_false;
1090 break;
1092 case SSA_NAME:
1093 /* This assignment is under the form: "a_1 = b_2". */
1094 res = follow_ssa_edge
1095 (loop, SSA_NAME_DEF_STMT (expr), halting_phi, evolution_of_loop, limit);
1096 break;
1098 case POINTER_PLUS_EXPR:
1099 case PLUS_EXPR:
1100 case MINUS_EXPR:
1101 /* This case is under the form "rhs0 +- rhs1". */
1102 rhs0 = TREE_OPERAND (expr, 0);
1103 rhs1 = TREE_OPERAND (expr, 1);
1104 type = TREE_TYPE (rhs0);
1105 STRIP_USELESS_TYPE_CONVERSION (rhs0);
1106 STRIP_USELESS_TYPE_CONVERSION (rhs1);
1107 res = follow_ssa_edge_binary (loop, at_stmt, type, rhs0, code, rhs1,
1108 halting_phi, evolution_of_loop, limit);
1109 break;
1111 case ADDR_EXPR:
1112 /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR. */
1113 if (TREE_CODE (TREE_OPERAND (expr, 0)) == MEM_REF)
1115 expr = TREE_OPERAND (expr, 0);
1116 rhs0 = TREE_OPERAND (expr, 0);
1117 rhs1 = TREE_OPERAND (expr, 1);
1118 type = TREE_TYPE (rhs0);
1119 STRIP_USELESS_TYPE_CONVERSION (rhs0);
1120 STRIP_USELESS_TYPE_CONVERSION (rhs1);
1121 res = follow_ssa_edge_binary (loop, at_stmt, type,
1122 rhs0, POINTER_PLUS_EXPR, rhs1,
1123 halting_phi, evolution_of_loop, limit);
1125 else
1126 res = t_false;
1127 break;
1129 case ASSERT_EXPR:
1130 /* This assignment is of the form: "a_1 = ASSERT_EXPR <a_2, ...>"
1131 It must be handled as a copy assignment of the form a_1 = a_2. */
1132 rhs0 = ASSERT_EXPR_VAR (expr);
1133 if (TREE_CODE (rhs0) == SSA_NAME)
1134 res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0),
1135 halting_phi, evolution_of_loop, limit);
1136 else
1137 res = t_false;
1138 break;
1140 default:
1141 res = t_false;
1142 break;
1145 return res;
1148 /* Follow the ssa edge into the right hand side of an assignment STMT.
1149 Return true if the strongly connected component has been found. */
1151 static t_bool
1152 follow_ssa_edge_in_rhs (struct loop *loop, gimple stmt,
1153 gphi *halting_phi, tree *evolution_of_loop,
1154 int limit)
1156 enum tree_code code = gimple_assign_rhs_code (stmt);
1157 tree type = gimple_expr_type (stmt), rhs1, rhs2;
1158 t_bool res;
1160 switch (code)
1162 CASE_CONVERT:
1163 /* This assignment is under the form "a_1 = (cast) rhs. */
1164 res = follow_ssa_edge_expr (loop, stmt, gimple_assign_rhs1 (stmt),
1165 halting_phi, evolution_of_loop, limit);
1166 *evolution_of_loop = chrec_convert (type, *evolution_of_loop, stmt);
1167 break;
1169 case POINTER_PLUS_EXPR:
1170 case PLUS_EXPR:
1171 case MINUS_EXPR:
1172 rhs1 = gimple_assign_rhs1 (stmt);
1173 rhs2 = gimple_assign_rhs2 (stmt);
1174 type = TREE_TYPE (rhs1);
1175 res = follow_ssa_edge_binary (loop, stmt, type, rhs1, code, rhs2,
1176 halting_phi, evolution_of_loop, limit);
1177 break;
1179 default:
1180 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1181 res = follow_ssa_edge_expr (loop, stmt, gimple_assign_rhs1 (stmt),
1182 halting_phi, evolution_of_loop, limit);
1183 else
1184 res = t_false;
1185 break;
1188 return res;
1191 /* Checks whether the I-th argument of a PHI comes from a backedge. */
1193 static bool
1194 backedge_phi_arg_p (gphi *phi, int i)
1196 const_edge e = gimple_phi_arg_edge (phi, i);
1198 /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
1199 about updating it anywhere, and this should work as well most of the
1200 time. */
1201 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1202 return true;
1204 return false;
1207 /* Helper function for one branch of the condition-phi-node. Return
1208 true if the strongly connected component has been found following
1209 this path. */
1211 static inline t_bool
1212 follow_ssa_edge_in_condition_phi_branch (int i,
1213 struct loop *loop,
1214 gphi *condition_phi,
1215 gphi *halting_phi,
1216 tree *evolution_of_branch,
1217 tree init_cond, int limit)
1219 tree branch = PHI_ARG_DEF (condition_phi, i);
1220 *evolution_of_branch = chrec_dont_know;
1222 /* Do not follow back edges (they must belong to an irreducible loop, which
1223 we really do not want to worry about). */
1224 if (backedge_phi_arg_p (condition_phi, i))
1225 return t_false;
1227 if (TREE_CODE (branch) == SSA_NAME)
1229 *evolution_of_branch = init_cond;
1230 return follow_ssa_edge (loop, SSA_NAME_DEF_STMT (branch), halting_phi,
1231 evolution_of_branch, limit);
1234 /* This case occurs when one of the condition branches sets
1235 the variable to a constant: i.e. a phi-node like
1236 "a_2 = PHI <a_7(5), 2(6)>;".
1238 FIXME: This case have to be refined correctly:
1239 in some cases it is possible to say something better than
1240 chrec_dont_know, for example using a wrap-around notation. */
1241 return t_false;
1244 /* This function merges the branches of a condition-phi-node in a
1245 loop. */
1247 static t_bool
1248 follow_ssa_edge_in_condition_phi (struct loop *loop,
1249 gphi *condition_phi,
1250 gphi *halting_phi,
1251 tree *evolution_of_loop, int limit)
1253 int i, n;
1254 tree init = *evolution_of_loop;
1255 tree evolution_of_branch;
1256 t_bool res = follow_ssa_edge_in_condition_phi_branch (0, loop, condition_phi,
1257 halting_phi,
1258 &evolution_of_branch,
1259 init, limit);
1260 if (res == t_false || res == t_dont_know)
1261 return res;
1263 *evolution_of_loop = evolution_of_branch;
1265 n = gimple_phi_num_args (condition_phi);
1266 for (i = 1; i < n; i++)
1268 /* Quickly give up when the evolution of one of the branches is
1269 not known. */
1270 if (*evolution_of_loop == chrec_dont_know)
1271 return t_true;
1273 /* Increase the limit by the PHI argument number to avoid exponential
1274 time and memory complexity. */
1275 res = follow_ssa_edge_in_condition_phi_branch (i, loop, condition_phi,
1276 halting_phi,
1277 &evolution_of_branch,
1278 init, limit + i);
1279 if (res == t_false || res == t_dont_know)
1280 return res;
1282 *evolution_of_loop = chrec_merge (*evolution_of_loop,
1283 evolution_of_branch);
1286 return t_true;
1289 /* Follow an SSA edge in an inner loop. It computes the overall
1290 effect of the loop, and following the symbolic initial conditions,
1291 it follows the edges in the parent loop. The inner loop is
1292 considered as a single statement. */
1294 static t_bool
1295 follow_ssa_edge_inner_loop_phi (struct loop *outer_loop,
1296 gphi *loop_phi_node,
1297 gphi *halting_phi,
1298 tree *evolution_of_loop, int limit)
1300 struct loop *loop = loop_containing_stmt (loop_phi_node);
1301 tree ev = analyze_scalar_evolution (loop, PHI_RESULT (loop_phi_node));
1303 /* Sometimes, the inner loop is too difficult to analyze, and the
1304 result of the analysis is a symbolic parameter. */
1305 if (ev == PHI_RESULT (loop_phi_node))
1307 t_bool res = t_false;
1308 int i, n = gimple_phi_num_args (loop_phi_node);
1310 for (i = 0; i < n; i++)
1312 tree arg = PHI_ARG_DEF (loop_phi_node, i);
1313 basic_block bb;
1315 /* Follow the edges that exit the inner loop. */
1316 bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1317 if (!flow_bb_inside_loop_p (loop, bb))
1318 res = follow_ssa_edge_expr (outer_loop, loop_phi_node,
1319 arg, halting_phi,
1320 evolution_of_loop, limit);
1321 if (res == t_true)
1322 break;
1325 /* If the path crosses this loop-phi, give up. */
1326 if (res == t_true)
1327 *evolution_of_loop = chrec_dont_know;
1329 return res;
1332 /* Otherwise, compute the overall effect of the inner loop. */
1333 ev = compute_overall_effect_of_inner_loop (loop, ev);
1334 return follow_ssa_edge_expr (outer_loop, loop_phi_node, ev, halting_phi,
1335 evolution_of_loop, limit);
1338 /* Follow an SSA edge from a loop-phi-node to itself, constructing a
1339 path that is analyzed on the return walk. */
1341 static t_bool
1342 follow_ssa_edge (struct loop *loop, gimple def, gphi *halting_phi,
1343 tree *evolution_of_loop, int limit)
1345 struct loop *def_loop;
1347 if (gimple_nop_p (def))
1348 return t_false;
1350 /* Give up if the path is longer than the MAX that we allow. */
1351 if (limit > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_COMPLEXITY))
1352 return t_dont_know;
1354 def_loop = loop_containing_stmt (def);
1356 switch (gimple_code (def))
1358 case GIMPLE_PHI:
1359 if (!loop_phi_node_p (def))
1360 /* DEF is a condition-phi-node. Follow the branches, and
1361 record their evolutions. Finally, merge the collected
1362 information and set the approximation to the main
1363 variable. */
1364 return follow_ssa_edge_in_condition_phi
1365 (loop, as_a <gphi *> (def), halting_phi, evolution_of_loop,
1366 limit);
1368 /* When the analyzed phi is the halting_phi, the
1369 depth-first search is over: we have found a path from
1370 the halting_phi to itself in the loop. */
1371 if (def == halting_phi)
1372 return t_true;
1374 /* Otherwise, the evolution of the HALTING_PHI depends
1375 on the evolution of another loop-phi-node, i.e. the
1376 evolution function is a higher degree polynomial. */
1377 if (def_loop == loop)
1378 return t_false;
1380 /* Inner loop. */
1381 if (flow_loop_nested_p (loop, def_loop))
1382 return follow_ssa_edge_inner_loop_phi
1383 (loop, as_a <gphi *> (def), halting_phi, evolution_of_loop,
1384 limit + 1);
1386 /* Outer loop. */
1387 return t_false;
1389 case GIMPLE_ASSIGN:
1390 return follow_ssa_edge_in_rhs (loop, def, halting_phi,
1391 evolution_of_loop, limit);
1393 default:
1394 /* At this level of abstraction, the program is just a set
1395 of GIMPLE_ASSIGNs and PHI_NODEs. In principle there is no
1396 other node to be handled. */
1397 return t_false;
1402 /* Simplify PEELED_CHREC represented by (init_cond, arg) in LOOP.
1403 Handle below case and return the corresponding POLYNOMIAL_CHREC:
1405 # i_17 = PHI <i_13(5), 0(3)>
1406 # _20 = PHI <_5(5), start_4(D)(3)>
1408 i_13 = i_17 + 1;
1409 _5 = start_4(D) + i_13;
1411 Though variable _20 appears as a PEELED_CHREC in the form of
1412 (start_4, _5)_LOOP, it's a POLYNOMIAL_CHREC like {start_4, 1}_LOOP.
1414 See PR41488. */
1416 static tree
1417 simplify_peeled_chrec (struct loop *loop, tree arg, tree init_cond)
1419 aff_tree aff1, aff2;
1420 tree ev, left, right, type, step_val;
1421 hash_map<tree, name_expansion *> *peeled_chrec_map = NULL;
1423 ev = instantiate_parameters (loop, analyze_scalar_evolution (loop, arg));
1424 if (ev == NULL_TREE || TREE_CODE (ev) != POLYNOMIAL_CHREC)
1425 return chrec_dont_know;
1427 left = CHREC_LEFT (ev);
1428 right = CHREC_RIGHT (ev);
1429 type = TREE_TYPE (left);
1430 step_val = chrec_fold_plus (type, init_cond, right);
1432 /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1433 if "left" equals to "init + right". */
1434 if (operand_equal_p (left, step_val, 0))
1436 if (dump_file && (dump_flags & TDF_SCEV))
1437 fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1439 return build_polynomial_chrec (loop->num, init_cond, right);
1442 /* Try harder to check if they are equal. */
1443 tree_to_aff_combination_expand (left, type, &aff1, &peeled_chrec_map);
1444 tree_to_aff_combination_expand (step_val, type, &aff2, &peeled_chrec_map);
1445 free_affine_expand_cache (&peeled_chrec_map);
1446 aff_combination_scale (&aff2, -1);
1447 aff_combination_add (&aff1, &aff2);
1449 /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1450 if "left" equals to "init + right". */
1451 if (aff_combination_zero_p (&aff1))
1453 if (dump_file && (dump_flags & TDF_SCEV))
1454 fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1456 return build_polynomial_chrec (loop->num, init_cond, right);
1458 return chrec_dont_know;
1461 /* Given a LOOP_PHI_NODE, this function determines the evolution
1462 function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */
1464 static tree
1465 analyze_evolution_in_loop (gphi *loop_phi_node,
1466 tree init_cond)
1468 int i, n = gimple_phi_num_args (loop_phi_node);
1469 tree evolution_function = chrec_not_analyzed_yet;
1470 struct loop *loop = loop_containing_stmt (loop_phi_node);
1471 basic_block bb;
1472 static bool simplify_peeled_chrec_p = true;
1474 if (dump_file && (dump_flags & TDF_SCEV))
1476 fprintf (dump_file, "(analyze_evolution_in_loop \n");
1477 fprintf (dump_file, " (loop_phi_node = ");
1478 print_gimple_stmt (dump_file, loop_phi_node, 0, 0);
1479 fprintf (dump_file, ")\n");
1482 for (i = 0; i < n; i++)
1484 tree arg = PHI_ARG_DEF (loop_phi_node, i);
1485 gimple ssa_chain;
1486 tree ev_fn;
1487 t_bool res;
1489 /* Select the edges that enter the loop body. */
1490 bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1491 if (!flow_bb_inside_loop_p (loop, bb))
1492 continue;
1494 if (TREE_CODE (arg) == SSA_NAME)
1496 bool val = false;
1498 ssa_chain = SSA_NAME_DEF_STMT (arg);
1500 /* Pass in the initial condition to the follow edge function. */
1501 ev_fn = init_cond;
1502 res = follow_ssa_edge (loop, ssa_chain, loop_phi_node, &ev_fn, 0);
1504 /* If ev_fn has no evolution in the inner loop, and the
1505 init_cond is not equal to ev_fn, then we have an
1506 ambiguity between two possible values, as we cannot know
1507 the number of iterations at this point. */
1508 if (TREE_CODE (ev_fn) != POLYNOMIAL_CHREC
1509 && no_evolution_in_loop_p (ev_fn, loop->num, &val) && val
1510 && !operand_equal_p (init_cond, ev_fn, 0))
1511 ev_fn = chrec_dont_know;
1513 else
1514 res = t_false;
1516 /* When it is impossible to go back on the same
1517 loop_phi_node by following the ssa edges, the
1518 evolution is represented by a peeled chrec, i.e. the
1519 first iteration, EV_FN has the value INIT_COND, then
1520 all the other iterations it has the value of ARG.
1521 For the moment, PEELED_CHREC nodes are not built. */
1522 if (res != t_true)
1524 ev_fn = chrec_dont_know;
1525 /* Try to recognize POLYNOMIAL_CHREC which appears in
1526 the form of PEELED_CHREC, but guard the process with
1527 a bool variable to keep the analyzer from infinite
1528 recurrence for real PEELED_RECs. */
1529 if (simplify_peeled_chrec_p && TREE_CODE (arg) == SSA_NAME)
1531 simplify_peeled_chrec_p = false;
1532 ev_fn = simplify_peeled_chrec (loop, arg, init_cond);
1533 simplify_peeled_chrec_p = true;
1537 /* When there are multiple back edges of the loop (which in fact never
1538 happens currently, but nevertheless), merge their evolutions. */
1539 evolution_function = chrec_merge (evolution_function, ev_fn);
1542 if (dump_file && (dump_flags & TDF_SCEV))
1544 fprintf (dump_file, " (evolution_function = ");
1545 print_generic_expr (dump_file, evolution_function, 0);
1546 fprintf (dump_file, "))\n");
1549 return evolution_function;
1552 /* Given a loop-phi-node, return the initial conditions of the
1553 variable on entry of the loop. When the CCP has propagated
1554 constants into the loop-phi-node, the initial condition is
1555 instantiated, otherwise the initial condition is kept symbolic.
1556 This analyzer does not analyze the evolution outside the current
1557 loop, and leaves this task to the on-demand tree reconstructor. */
1559 static tree
1560 analyze_initial_condition (gphi *loop_phi_node)
1562 int i, n;
1563 tree init_cond = chrec_not_analyzed_yet;
1564 struct loop *loop = loop_containing_stmt (loop_phi_node);
1566 if (dump_file && (dump_flags & TDF_SCEV))
1568 fprintf (dump_file, "(analyze_initial_condition \n");
1569 fprintf (dump_file, " (loop_phi_node = \n");
1570 print_gimple_stmt (dump_file, loop_phi_node, 0, 0);
1571 fprintf (dump_file, ")\n");
1574 n = gimple_phi_num_args (loop_phi_node);
1575 for (i = 0; i < n; i++)
1577 tree branch = PHI_ARG_DEF (loop_phi_node, i);
1578 basic_block bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1580 /* When the branch is oriented to the loop's body, it does
1581 not contribute to the initial condition. */
1582 if (flow_bb_inside_loop_p (loop, bb))
1583 continue;
1585 if (init_cond == chrec_not_analyzed_yet)
1587 init_cond = branch;
1588 continue;
1591 if (TREE_CODE (branch) == SSA_NAME)
1593 init_cond = chrec_dont_know;
1594 break;
1597 init_cond = chrec_merge (init_cond, branch);
1600 /* Ooops -- a loop without an entry??? */
1601 if (init_cond == chrec_not_analyzed_yet)
1602 init_cond = chrec_dont_know;
1604 /* During early loop unrolling we do not have fully constant propagated IL.
1605 Handle degenerate PHIs here to not miss important unrollings. */
1606 if (TREE_CODE (init_cond) == SSA_NAME)
1608 gimple def = SSA_NAME_DEF_STMT (init_cond);
1609 if (gphi *phi = dyn_cast <gphi *> (def))
1611 tree res = degenerate_phi_result (phi);
1612 if (res != NULL_TREE
1613 /* Only allow invariants here, otherwise we may break
1614 loop-closed SSA form. */
1615 && is_gimple_min_invariant (res))
1616 init_cond = res;
1620 if (dump_file && (dump_flags & TDF_SCEV))
1622 fprintf (dump_file, " (init_cond = ");
1623 print_generic_expr (dump_file, init_cond, 0);
1624 fprintf (dump_file, "))\n");
1627 return init_cond;
1630 /* Analyze the scalar evolution for LOOP_PHI_NODE. */
1632 static tree
1633 interpret_loop_phi (struct loop *loop, gphi *loop_phi_node)
1635 tree res;
1636 struct loop *phi_loop = loop_containing_stmt (loop_phi_node);
1637 tree init_cond;
1639 if (phi_loop != loop)
1641 struct loop *subloop;
1642 tree evolution_fn = analyze_scalar_evolution
1643 (phi_loop, PHI_RESULT (loop_phi_node));
1645 /* Dive one level deeper. */
1646 subloop = superloop_at_depth (phi_loop, loop_depth (loop) + 1);
1648 /* Interpret the subloop. */
1649 res = compute_overall_effect_of_inner_loop (subloop, evolution_fn);
1650 return res;
1653 /* Otherwise really interpret the loop phi. */
1654 init_cond = analyze_initial_condition (loop_phi_node);
1655 res = analyze_evolution_in_loop (loop_phi_node, init_cond);
1657 /* Verify we maintained the correct initial condition throughout
1658 possible conversions in the SSA chain. */
1659 if (res != chrec_dont_know)
1661 tree new_init = res;
1662 if (CONVERT_EXPR_P (res)
1663 && TREE_CODE (TREE_OPERAND (res, 0)) == POLYNOMIAL_CHREC)
1664 new_init = fold_convert (TREE_TYPE (res),
1665 CHREC_LEFT (TREE_OPERAND (res, 0)));
1666 else if (TREE_CODE (res) == POLYNOMIAL_CHREC)
1667 new_init = CHREC_LEFT (res);
1668 STRIP_USELESS_TYPE_CONVERSION (new_init);
1669 if (TREE_CODE (new_init) == POLYNOMIAL_CHREC
1670 || !operand_equal_p (init_cond, new_init, 0))
1671 return chrec_dont_know;
1674 return res;
1677 /* This function merges the branches of a condition-phi-node,
1678 contained in the outermost loop, and whose arguments are already
1679 analyzed. */
1681 static tree
1682 interpret_condition_phi (struct loop *loop, gphi *condition_phi)
1684 int i, n = gimple_phi_num_args (condition_phi);
1685 tree res = chrec_not_analyzed_yet;
1687 for (i = 0; i < n; i++)
1689 tree branch_chrec;
1691 if (backedge_phi_arg_p (condition_phi, i))
1693 res = chrec_dont_know;
1694 break;
1697 branch_chrec = analyze_scalar_evolution
1698 (loop, PHI_ARG_DEF (condition_phi, i));
1700 res = chrec_merge (res, branch_chrec);
1703 return res;
1706 /* Interpret the operation RHS1 OP RHS2. If we didn't
1707 analyze this node before, follow the definitions until ending
1708 either on an analyzed GIMPLE_ASSIGN, or on a loop-phi-node. On the
1709 return path, this function propagates evolutions (ala constant copy
1710 propagation). OPND1 is not a GIMPLE expression because we could
1711 analyze the effect of an inner loop: see interpret_loop_phi. */
1713 static tree
1714 interpret_rhs_expr (struct loop *loop, gimple at_stmt,
1715 tree type, tree rhs1, enum tree_code code, tree rhs2)
1717 tree res, chrec1, chrec2;
1718 gimple def;
1720 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1722 if (is_gimple_min_invariant (rhs1))
1723 return chrec_convert (type, rhs1, at_stmt);
1725 if (code == SSA_NAME)
1726 return chrec_convert (type, analyze_scalar_evolution (loop, rhs1),
1727 at_stmt);
1729 if (code == ASSERT_EXPR)
1731 rhs1 = ASSERT_EXPR_VAR (rhs1);
1732 return chrec_convert (type, analyze_scalar_evolution (loop, rhs1),
1733 at_stmt);
1737 switch (code)
1739 case ADDR_EXPR:
1740 if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF
1741 || handled_component_p (TREE_OPERAND (rhs1, 0)))
1743 machine_mode mode;
1744 HOST_WIDE_INT bitsize, bitpos;
1745 int unsignedp;
1746 int volatilep = 0;
1747 tree base, offset;
1748 tree chrec3;
1749 tree unitpos;
1751 base = get_inner_reference (TREE_OPERAND (rhs1, 0),
1752 &bitsize, &bitpos, &offset,
1753 &mode, &unsignedp, &volatilep, false);
1755 if (TREE_CODE (base) == MEM_REF)
1757 rhs2 = TREE_OPERAND (base, 1);
1758 rhs1 = TREE_OPERAND (base, 0);
1760 chrec1 = analyze_scalar_evolution (loop, rhs1);
1761 chrec2 = analyze_scalar_evolution (loop, rhs2);
1762 chrec1 = chrec_convert (type, chrec1, at_stmt);
1763 chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt);
1764 chrec1 = instantiate_parameters (loop, chrec1);
1765 chrec2 = instantiate_parameters (loop, chrec2);
1766 res = chrec_fold_plus (type, chrec1, chrec2);
1768 else
1770 chrec1 = analyze_scalar_evolution_for_address_of (loop, base);
1771 chrec1 = chrec_convert (type, chrec1, at_stmt);
1772 res = chrec1;
1775 if (offset != NULL_TREE)
1777 chrec2 = analyze_scalar_evolution (loop, offset);
1778 chrec2 = chrec_convert (TREE_TYPE (offset), chrec2, at_stmt);
1779 chrec2 = instantiate_parameters (loop, chrec2);
1780 res = chrec_fold_plus (type, res, chrec2);
1783 if (bitpos != 0)
1785 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
1787 unitpos = size_int (bitpos / BITS_PER_UNIT);
1788 chrec3 = analyze_scalar_evolution (loop, unitpos);
1789 chrec3 = chrec_convert (TREE_TYPE (unitpos), chrec3, at_stmt);
1790 chrec3 = instantiate_parameters (loop, chrec3);
1791 res = chrec_fold_plus (type, res, chrec3);
1794 else
1795 res = chrec_dont_know;
1796 break;
1798 case POINTER_PLUS_EXPR:
1799 chrec1 = analyze_scalar_evolution (loop, rhs1);
1800 chrec2 = analyze_scalar_evolution (loop, rhs2);
1801 chrec1 = chrec_convert (type, chrec1, at_stmt);
1802 chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt);
1803 chrec1 = instantiate_parameters (loop, chrec1);
1804 chrec2 = instantiate_parameters (loop, chrec2);
1805 res = chrec_fold_plus (type, chrec1, chrec2);
1806 break;
1808 case PLUS_EXPR:
1809 chrec1 = analyze_scalar_evolution (loop, rhs1);
1810 chrec2 = analyze_scalar_evolution (loop, rhs2);
1811 chrec1 = chrec_convert (type, chrec1, at_stmt);
1812 chrec2 = chrec_convert (type, chrec2, at_stmt);
1813 chrec1 = instantiate_parameters (loop, chrec1);
1814 chrec2 = instantiate_parameters (loop, chrec2);
1815 res = chrec_fold_plus (type, chrec1, chrec2);
1816 break;
1818 case MINUS_EXPR:
1819 chrec1 = analyze_scalar_evolution (loop, rhs1);
1820 chrec2 = analyze_scalar_evolution (loop, rhs2);
1821 chrec1 = chrec_convert (type, chrec1, at_stmt);
1822 chrec2 = chrec_convert (type, chrec2, at_stmt);
1823 chrec1 = instantiate_parameters (loop, chrec1);
1824 chrec2 = instantiate_parameters (loop, chrec2);
1825 res = chrec_fold_minus (type, chrec1, chrec2);
1826 break;
1828 case NEGATE_EXPR:
1829 chrec1 = analyze_scalar_evolution (loop, rhs1);
1830 chrec1 = chrec_convert (type, chrec1, at_stmt);
1831 /* TYPE may be integer, real or complex, so use fold_convert. */
1832 chrec1 = instantiate_parameters (loop, chrec1);
1833 res = chrec_fold_multiply (type, chrec1,
1834 fold_convert (type, integer_minus_one_node));
1835 break;
1837 case BIT_NOT_EXPR:
1838 /* Handle ~X as -1 - X. */
1839 chrec1 = analyze_scalar_evolution (loop, rhs1);
1840 chrec1 = chrec_convert (type, chrec1, at_stmt);
1841 chrec1 = instantiate_parameters (loop, chrec1);
1842 res = chrec_fold_minus (type,
1843 fold_convert (type, integer_minus_one_node),
1844 chrec1);
1845 break;
1847 case MULT_EXPR:
1848 chrec1 = analyze_scalar_evolution (loop, rhs1);
1849 chrec2 = analyze_scalar_evolution (loop, rhs2);
1850 chrec1 = chrec_convert (type, chrec1, at_stmt);
1851 chrec2 = chrec_convert (type, chrec2, at_stmt);
1852 chrec1 = instantiate_parameters (loop, chrec1);
1853 chrec2 = instantiate_parameters (loop, chrec2);
1854 res = chrec_fold_multiply (type, chrec1, chrec2);
1855 break;
1857 CASE_CONVERT:
1858 /* In case we have a truncation of a widened operation that in
1859 the truncated type has undefined overflow behavior analyze
1860 the operation done in an unsigned type of the same precision
1861 as the final truncation. We cannot derive a scalar evolution
1862 for the widened operation but for the truncated result. */
1863 if (TREE_CODE (type) == INTEGER_TYPE
1864 && TREE_CODE (TREE_TYPE (rhs1)) == INTEGER_TYPE
1865 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (rhs1))
1866 && TYPE_OVERFLOW_UNDEFINED (type)
1867 && TREE_CODE (rhs1) == SSA_NAME
1868 && (def = SSA_NAME_DEF_STMT (rhs1))
1869 && is_gimple_assign (def)
1870 && TREE_CODE_CLASS (gimple_assign_rhs_code (def)) == tcc_binary
1871 && TREE_CODE (gimple_assign_rhs2 (def)) == INTEGER_CST)
1873 tree utype = unsigned_type_for (type);
1874 chrec1 = interpret_rhs_expr (loop, at_stmt, utype,
1875 gimple_assign_rhs1 (def),
1876 gimple_assign_rhs_code (def),
1877 gimple_assign_rhs2 (def));
1879 else
1880 chrec1 = analyze_scalar_evolution (loop, rhs1);
1881 res = chrec_convert (type, chrec1, at_stmt);
1882 break;
1884 default:
1885 res = chrec_dont_know;
1886 break;
1889 return res;
1892 /* Interpret the expression EXPR. */
1894 static tree
1895 interpret_expr (struct loop *loop, gimple at_stmt, tree expr)
1897 enum tree_code code;
1898 tree type = TREE_TYPE (expr), op0, op1;
1900 if (automatically_generated_chrec_p (expr))
1901 return expr;
1903 if (TREE_CODE (expr) == POLYNOMIAL_CHREC
1904 || get_gimple_rhs_class (TREE_CODE (expr)) == GIMPLE_TERNARY_RHS)
1905 return chrec_dont_know;
1907 extract_ops_from_tree (expr, &code, &op0, &op1);
1909 return interpret_rhs_expr (loop, at_stmt, type,
1910 op0, code, op1);
1913 /* Interpret the rhs of the assignment STMT. */
1915 static tree
1916 interpret_gimple_assign (struct loop *loop, gimple stmt)
1918 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
1919 enum tree_code code = gimple_assign_rhs_code (stmt);
1921 return interpret_rhs_expr (loop, stmt, type,
1922 gimple_assign_rhs1 (stmt), code,
1923 gimple_assign_rhs2 (stmt));
1928 /* This section contains all the entry points:
1929 - number_of_iterations_in_loop,
1930 - analyze_scalar_evolution,
1931 - instantiate_parameters.
1934 /* Compute and return the evolution function in WRTO_LOOP, the nearest
1935 common ancestor of DEF_LOOP and USE_LOOP. */
1937 static tree
1938 compute_scalar_evolution_in_loop (struct loop *wrto_loop,
1939 struct loop *def_loop,
1940 tree ev)
1942 bool val;
1943 tree res;
1945 if (def_loop == wrto_loop)
1946 return ev;
1948 def_loop = superloop_at_depth (def_loop, loop_depth (wrto_loop) + 1);
1949 res = compute_overall_effect_of_inner_loop (def_loop, ev);
1951 if (no_evolution_in_loop_p (res, wrto_loop->num, &val) && val)
1952 return res;
1954 return analyze_scalar_evolution_1 (wrto_loop, res, chrec_not_analyzed_yet);
1957 /* Helper recursive function. */
1959 static tree
1960 analyze_scalar_evolution_1 (struct loop *loop, tree var, tree res)
1962 tree type = TREE_TYPE (var);
1963 gimple def;
1964 basic_block bb;
1965 struct loop *def_loop;
1967 if (loop == NULL || TREE_CODE (type) == VECTOR_TYPE)
1968 return chrec_dont_know;
1970 if (TREE_CODE (var) != SSA_NAME)
1971 return interpret_expr (loop, NULL, var);
1973 def = SSA_NAME_DEF_STMT (var);
1974 bb = gimple_bb (def);
1975 def_loop = bb ? bb->loop_father : NULL;
1977 if (bb == NULL
1978 || !flow_bb_inside_loop_p (loop, bb))
1980 /* Keep the symbolic form. */
1981 res = var;
1982 goto set_and_end;
1985 if (res != chrec_not_analyzed_yet)
1987 if (loop != bb->loop_father)
1988 res = compute_scalar_evolution_in_loop
1989 (find_common_loop (loop, bb->loop_father), bb->loop_father, res);
1991 goto set_and_end;
1994 if (loop != def_loop)
1996 res = analyze_scalar_evolution_1 (def_loop, var, chrec_not_analyzed_yet);
1997 res = compute_scalar_evolution_in_loop (loop, def_loop, res);
1999 goto set_and_end;
2002 switch (gimple_code (def))
2004 case GIMPLE_ASSIGN:
2005 res = interpret_gimple_assign (loop, def);
2006 break;
2008 case GIMPLE_PHI:
2009 if (loop_phi_node_p (def))
2010 res = interpret_loop_phi (loop, as_a <gphi *> (def));
2011 else
2012 res = interpret_condition_phi (loop, as_a <gphi *> (def));
2013 break;
2015 default:
2016 res = chrec_dont_know;
2017 break;
2020 set_and_end:
2022 /* Keep the symbolic form. */
2023 if (res == chrec_dont_know)
2024 res = var;
2026 if (loop == def_loop)
2027 set_scalar_evolution (block_before_loop (loop), var, res);
2029 return res;
2032 /* Analyzes and returns the scalar evolution of the ssa_name VAR in
2033 LOOP. LOOP is the loop in which the variable is used.
2035 Example of use: having a pointer VAR to a SSA_NAME node, STMT a
2036 pointer to the statement that uses this variable, in order to
2037 determine the evolution function of the variable, use the following
2038 calls:
2040 loop_p loop = loop_containing_stmt (stmt);
2041 tree chrec_with_symbols = analyze_scalar_evolution (loop, var);
2042 tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols);
2045 tree
2046 analyze_scalar_evolution (struct loop *loop, tree var)
2048 tree res;
2050 if (dump_file && (dump_flags & TDF_SCEV))
2052 fprintf (dump_file, "(analyze_scalar_evolution \n");
2053 fprintf (dump_file, " (loop_nb = %d)\n", loop->num);
2054 fprintf (dump_file, " (scalar = ");
2055 print_generic_expr (dump_file, var, 0);
2056 fprintf (dump_file, ")\n");
2059 res = get_scalar_evolution (block_before_loop (loop), var);
2060 res = analyze_scalar_evolution_1 (loop, var, res);
2062 if (dump_file && (dump_flags & TDF_SCEV))
2063 fprintf (dump_file, ")\n");
2065 return res;
2068 /* Analyzes and returns the scalar evolution of VAR address in LOOP. */
2070 static tree
2071 analyze_scalar_evolution_for_address_of (struct loop *loop, tree var)
2073 return analyze_scalar_evolution (loop, build_fold_addr_expr (var));
2076 /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
2077 WRTO_LOOP (which should be a superloop of USE_LOOP)
2079 FOLDED_CASTS is set to true if resolve_mixers used
2080 chrec_convert_aggressive (TODO -- not really, we are way too conservative
2081 at the moment in order to keep things simple).
2083 To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following
2084 example:
2086 for (i = 0; i < 100; i++) -- loop 1
2088 for (j = 0; j < 100; j++) -- loop 2
2090 k1 = i;
2091 k2 = j;
2093 use2 (k1, k2);
2095 for (t = 0; t < 100; t++) -- loop 3
2096 use3 (k1, k2);
2099 use1 (k1, k2);
2102 Both k1 and k2 are invariants in loop3, thus
2103 analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1
2104 analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2
2106 As they are invariant, it does not matter whether we consider their
2107 usage in loop 3 or loop 2, hence
2108 analyze_scalar_evolution_in_loop (loop2, loop3, k1) =
2109 analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i
2110 analyze_scalar_evolution_in_loop (loop2, loop3, k2) =
2111 analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2
2113 Similarly for their evolutions with respect to loop 1. The values of K2
2114 in the use in loop 2 vary independently on loop 1, thus we cannot express
2115 the evolution with respect to loop 1:
2116 analyze_scalar_evolution_in_loop (loop1, loop3, k1) =
2117 analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1
2118 analyze_scalar_evolution_in_loop (loop1, loop3, k2) =
2119 analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know
2121 The value of k2 in the use in loop 1 is known, though:
2122 analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1
2123 analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100
2126 static tree
2127 analyze_scalar_evolution_in_loop (struct loop *wrto_loop, struct loop *use_loop,
2128 tree version, bool *folded_casts)
2130 bool val = false;
2131 tree ev = version, tmp;
2133 /* We cannot just do
2135 tmp = analyze_scalar_evolution (use_loop, version);
2136 ev = resolve_mixers (wrto_loop, tmp, folded_casts);
2138 as resolve_mixers would query the scalar evolution with respect to
2139 wrto_loop. For example, in the situation described in the function
2140 comment, suppose that wrto_loop = loop1, use_loop = loop3 and
2141 version = k2. Then
2143 analyze_scalar_evolution (use_loop, version) = k2
2145 and resolve_mixers (loop1, k2, folded_casts) finds that the value of
2146 k2 in loop 1 is 100, which is a wrong result, since we are interested
2147 in the value in loop 3.
2149 Instead, we need to proceed from use_loop to wrto_loop loop by loop,
2150 each time checking that there is no evolution in the inner loop. */
2152 if (folded_casts)
2153 *folded_casts = false;
2154 while (1)
2156 tmp = analyze_scalar_evolution (use_loop, ev);
2157 ev = resolve_mixers (use_loop, tmp, folded_casts);
2159 if (use_loop == wrto_loop)
2160 return ev;
2162 /* If the value of the use changes in the inner loop, we cannot express
2163 its value in the outer loop (we might try to return interval chrec,
2164 but we do not have a user for it anyway) */
2165 if (!no_evolution_in_loop_p (ev, use_loop->num, &val)
2166 || !val)
2167 return chrec_dont_know;
2169 use_loop = loop_outer (use_loop);
2174 /* Hashtable helpers for a temporary hash-table used when
2175 instantiating a CHREC or resolving mixers. For this use
2176 instantiated_below is always the same. */
2178 struct instantiate_cache_type
2180 htab_t map;
2181 vec<scev_info_str> entries;
2183 instantiate_cache_type () : map (NULL), entries (vNULL) {}
2184 ~instantiate_cache_type ();
2185 tree get (unsigned slot) { return entries[slot].chrec; }
2186 void set (unsigned slot, tree chrec) { entries[slot].chrec = chrec; }
2189 instantiate_cache_type::~instantiate_cache_type ()
2191 if (map != NULL)
2193 htab_delete (map);
2194 entries.release ();
2198 /* Cache to avoid infinite recursion when instantiating an SSA name.
2199 Live during the outermost instantiate_scev or resolve_mixers call. */
2200 static instantiate_cache_type *global_cache;
2202 /* Computes a hash function for database element ELT. */
2204 static inline hashval_t
2205 hash_idx_scev_info (const void *elt_)
2207 unsigned idx = ((size_t) elt_) - 2;
2208 return scev_info_hasher::hash (&global_cache->entries[idx]);
2211 /* Compares database elements E1 and E2. */
2213 static inline int
2214 eq_idx_scev_info (const void *e1, const void *e2)
2216 unsigned idx1 = ((size_t) e1) - 2;
2217 return scev_info_hasher::equal (&global_cache->entries[idx1],
2218 (const scev_info_str *) e2);
2221 /* Returns from CACHE the slot number of the cached chrec for NAME. */
2223 static unsigned
2224 get_instantiated_value_entry (instantiate_cache_type &cache,
2225 tree name, basic_block instantiate_below)
2227 if (!cache.map)
2229 cache.map = htab_create (10, hash_idx_scev_info, eq_idx_scev_info, NULL);
2230 cache.entries.create (10);
2233 scev_info_str e;
2234 e.name_version = SSA_NAME_VERSION (name);
2235 e.instantiated_below = instantiate_below->index;
2236 void **slot = htab_find_slot_with_hash (cache.map, &e,
2237 scev_info_hasher::hash (&e), INSERT);
2238 if (!*slot)
2240 e.chrec = chrec_not_analyzed_yet;
2241 *slot = (void *)(size_t)(cache.entries.length () + 2);
2242 cache.entries.safe_push (e);
2245 return ((size_t)*slot) - 2;
2249 /* Return the closed_loop_phi node for VAR. If there is none, return
2250 NULL_TREE. */
2252 static tree
2253 loop_closed_phi_def (tree var)
2255 struct loop *loop;
2256 edge exit;
2257 gphi *phi;
2258 gphi_iterator psi;
2260 if (var == NULL_TREE
2261 || TREE_CODE (var) != SSA_NAME)
2262 return NULL_TREE;
2264 loop = loop_containing_stmt (SSA_NAME_DEF_STMT (var));
2265 exit = single_exit (loop);
2266 if (!exit)
2267 return NULL_TREE;
2269 for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi))
2271 phi = psi.phi ();
2272 if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == var)
2273 return PHI_RESULT (phi);
2276 return NULL_TREE;
2279 static tree instantiate_scev_r (basic_block, struct loop *, struct loop *,
2280 tree, bool *, int);
2282 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2283 and EVOLUTION_LOOP, that were left under a symbolic form.
2285 CHREC is an SSA_NAME to be instantiated.
2287 CACHE is the cache of already instantiated values.
2289 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2290 conversions that may wrap in signed/pointer type are folded, as long
2291 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2292 then we don't do such fold.
2294 SIZE_EXPR is used for computing the size of the expression to be
2295 instantiated, and to stop if it exceeds some limit. */
2297 static tree
2298 instantiate_scev_name (basic_block instantiate_below,
2299 struct loop *evolution_loop, struct loop *inner_loop,
2300 tree chrec,
2301 bool *fold_conversions,
2302 int size_expr)
2304 tree res;
2305 struct loop *def_loop;
2306 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (chrec));
2308 /* A parameter (or loop invariant and we do not want to include
2309 evolutions in outer loops), nothing to do. */
2310 if (!def_bb
2311 || loop_depth (def_bb->loop_father) == 0
2312 || dominated_by_p (CDI_DOMINATORS, instantiate_below, def_bb))
2313 return chrec;
2315 /* We cache the value of instantiated variable to avoid exponential
2316 time complexity due to reevaluations. We also store the convenient
2317 value in the cache in order to prevent infinite recursion -- we do
2318 not want to instantiate the SSA_NAME if it is in a mixer
2319 structure. This is used for avoiding the instantiation of
2320 recursively defined functions, such as:
2322 | a_2 -> {0, +, 1, +, a_2}_1 */
2324 unsigned si = get_instantiated_value_entry (*global_cache,
2325 chrec, instantiate_below);
2326 if (global_cache->get (si) != chrec_not_analyzed_yet)
2327 return global_cache->get (si);
2329 /* On recursion return chrec_dont_know. */
2330 global_cache->set (si, chrec_dont_know);
2332 def_loop = find_common_loop (evolution_loop, def_bb->loop_father);
2334 /* If the analysis yields a parametric chrec, instantiate the
2335 result again. */
2336 res = analyze_scalar_evolution (def_loop, chrec);
2338 /* Don't instantiate default definitions. */
2339 if (TREE_CODE (res) == SSA_NAME
2340 && SSA_NAME_IS_DEFAULT_DEF (res))
2343 /* Don't instantiate loop-closed-ssa phi nodes. */
2344 else if (TREE_CODE (res) == SSA_NAME
2345 && loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res)))
2346 > loop_depth (def_loop))
2348 if (res == chrec)
2349 res = loop_closed_phi_def (chrec);
2350 else
2351 res = chrec;
2353 /* When there is no loop_closed_phi_def, it means that the
2354 variable is not used after the loop: try to still compute the
2355 value of the variable when exiting the loop. */
2356 if (res == NULL_TREE)
2358 loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (chrec));
2359 res = analyze_scalar_evolution (loop, chrec);
2360 res = compute_overall_effect_of_inner_loop (loop, res);
2361 res = instantiate_scev_r (instantiate_below, evolution_loop,
2362 inner_loop, res,
2363 fold_conversions, size_expr);
2365 else if (!dominated_by_p (CDI_DOMINATORS, instantiate_below,
2366 gimple_bb (SSA_NAME_DEF_STMT (res))))
2367 res = chrec_dont_know;
2370 else if (res != chrec_dont_know)
2372 if (inner_loop
2373 && def_bb->loop_father != inner_loop
2374 && !flow_loop_nested_p (def_bb->loop_father, inner_loop))
2375 /* ??? We could try to compute the overall effect of the loop here. */
2376 res = chrec_dont_know;
2377 else
2378 res = instantiate_scev_r (instantiate_below, evolution_loop,
2379 inner_loop, res,
2380 fold_conversions, size_expr);
2383 /* Store the correct value to the cache. */
2384 global_cache->set (si, res);
2385 return res;
2388 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2389 and EVOLUTION_LOOP, that were left under a symbolic form.
2391 CHREC is a polynomial chain of recurrence to be instantiated.
2393 CACHE is the cache of already instantiated values.
2395 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2396 conversions that may wrap in signed/pointer type are folded, as long
2397 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2398 then we don't do such fold.
2400 SIZE_EXPR is used for computing the size of the expression to be
2401 instantiated, and to stop if it exceeds some limit. */
2403 static tree
2404 instantiate_scev_poly (basic_block instantiate_below,
2405 struct loop *evolution_loop, struct loop *,
2406 tree chrec, bool *fold_conversions, int size_expr)
2408 tree op1;
2409 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2410 get_chrec_loop (chrec),
2411 CHREC_LEFT (chrec), fold_conversions,
2412 size_expr);
2413 if (op0 == chrec_dont_know)
2414 return chrec_dont_know;
2416 op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2417 get_chrec_loop (chrec),
2418 CHREC_RIGHT (chrec), fold_conversions,
2419 size_expr);
2420 if (op1 == chrec_dont_know)
2421 return chrec_dont_know;
2423 if (CHREC_LEFT (chrec) != op0
2424 || CHREC_RIGHT (chrec) != op1)
2426 op1 = chrec_convert_rhs (chrec_type (op0), op1, NULL);
2427 chrec = build_polynomial_chrec (CHREC_VARIABLE (chrec), op0, op1);
2430 return chrec;
2433 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2434 and EVOLUTION_LOOP, that were left under a symbolic form.
2436 "C0 CODE C1" is a binary expression of type TYPE to be instantiated.
2438 CACHE is the cache of already instantiated values.
2440 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2441 conversions that may wrap in signed/pointer type are folded, as long
2442 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2443 then we don't do such fold.
2445 SIZE_EXPR is used for computing the size of the expression to be
2446 instantiated, and to stop if it exceeds some limit. */
2448 static tree
2449 instantiate_scev_binary (basic_block instantiate_below,
2450 struct loop *evolution_loop, struct loop *inner_loop,
2451 tree chrec, enum tree_code code,
2452 tree type, tree c0, tree c1,
2453 bool *fold_conversions, int size_expr)
2455 tree op1;
2456 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
2457 c0, fold_conversions, size_expr);
2458 if (op0 == chrec_dont_know)
2459 return chrec_dont_know;
2461 op1 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
2462 c1, fold_conversions, size_expr);
2463 if (op1 == chrec_dont_know)
2464 return chrec_dont_know;
2466 if (c0 != op0
2467 || c1 != op1)
2469 op0 = chrec_convert (type, op0, NULL);
2470 op1 = chrec_convert_rhs (type, op1, NULL);
2472 switch (code)
2474 case POINTER_PLUS_EXPR:
2475 case PLUS_EXPR:
2476 return chrec_fold_plus (type, op0, op1);
2478 case MINUS_EXPR:
2479 return chrec_fold_minus (type, op0, op1);
2481 case MULT_EXPR:
2482 return chrec_fold_multiply (type, op0, op1);
2484 default:
2485 gcc_unreachable ();
2489 return chrec ? chrec : fold_build2 (code, type, c0, c1);
2492 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2493 and EVOLUTION_LOOP, that were left under a symbolic form.
2495 "CHREC" is an array reference to be instantiated.
2497 CACHE is the cache of already instantiated values.
2499 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2500 conversions that may wrap in signed/pointer type are folded, as long
2501 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2502 then we don't do such fold.
2504 SIZE_EXPR is used for computing the size of the expression to be
2505 instantiated, and to stop if it exceeds some limit. */
2507 static tree
2508 instantiate_array_ref (basic_block instantiate_below,
2509 struct loop *evolution_loop, struct loop *inner_loop,
2510 tree chrec, bool *fold_conversions, int size_expr)
2512 tree res;
2513 tree index = TREE_OPERAND (chrec, 1);
2514 tree op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2515 inner_loop, index,
2516 fold_conversions, size_expr);
2518 if (op1 == chrec_dont_know)
2519 return chrec_dont_know;
2521 if (chrec && op1 == index)
2522 return chrec;
2524 res = unshare_expr (chrec);
2525 TREE_OPERAND (res, 1) = op1;
2526 return res;
2529 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2530 and EVOLUTION_LOOP, that were left under a symbolic form.
2532 "CHREC" that stands for a convert expression "(TYPE) OP" is to be
2533 instantiated.
2535 CACHE is the cache of already instantiated values.
2537 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2538 conversions that may wrap in signed/pointer type are folded, as long
2539 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2540 then we don't do such fold.
2542 SIZE_EXPR is used for computing the size of the expression to be
2543 instantiated, and to stop if it exceeds some limit. */
2545 static tree
2546 instantiate_scev_convert (basic_block instantiate_below,
2547 struct loop *evolution_loop, struct loop *inner_loop,
2548 tree chrec, tree type, tree op,
2549 bool *fold_conversions, int size_expr)
2551 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2552 inner_loop, op,
2553 fold_conversions, size_expr);
2555 if (op0 == chrec_dont_know)
2556 return chrec_dont_know;
2558 if (fold_conversions)
2560 tree tmp = chrec_convert_aggressive (type, op0, fold_conversions);
2561 if (tmp)
2562 return tmp;
2564 /* If we used chrec_convert_aggressive, we can no longer assume that
2565 signed chrecs do not overflow, as chrec_convert does, so avoid
2566 calling it in that case. */
2567 if (*fold_conversions)
2569 if (chrec && op0 == op)
2570 return chrec;
2572 return fold_convert (type, op0);
2576 return chrec_convert (type, op0, NULL);
2579 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2580 and EVOLUTION_LOOP, that were left under a symbolic form.
2582 CHREC is a BIT_NOT_EXPR or a NEGATE_EXPR expression to be instantiated.
2583 Handle ~X as -1 - X.
2584 Handle -X as -1 * X.
2586 CACHE is the cache of already instantiated values.
2588 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2589 conversions that may wrap in signed/pointer type are folded, as long
2590 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2591 then we don't do such fold.
2593 SIZE_EXPR is used for computing the size of the expression to be
2594 instantiated, and to stop if it exceeds some limit. */
2596 static tree
2597 instantiate_scev_not (basic_block instantiate_below,
2598 struct loop *evolution_loop, struct loop *inner_loop,
2599 tree chrec,
2600 enum tree_code code, tree type, tree op,
2601 bool *fold_conversions, int size_expr)
2603 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2604 inner_loop, op,
2605 fold_conversions, size_expr);
2607 if (op0 == chrec_dont_know)
2608 return chrec_dont_know;
2610 if (op != op0)
2612 op0 = chrec_convert (type, op0, NULL);
2614 switch (code)
2616 case BIT_NOT_EXPR:
2617 return chrec_fold_minus
2618 (type, fold_convert (type, integer_minus_one_node), op0);
2620 case NEGATE_EXPR:
2621 return chrec_fold_multiply
2622 (type, fold_convert (type, integer_minus_one_node), op0);
2624 default:
2625 gcc_unreachable ();
2629 return chrec ? chrec : fold_build1 (code, type, op0);
2632 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2633 and EVOLUTION_LOOP, that were left under a symbolic form.
2635 CHREC is an expression with 3 operands to be instantiated.
2637 CACHE is the cache of already instantiated values.
2639 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2640 conversions that may wrap in signed/pointer type are folded, as long
2641 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2642 then we don't do such fold.
2644 SIZE_EXPR is used for computing the size of the expression to be
2645 instantiated, and to stop if it exceeds some limit. */
2647 static tree
2648 instantiate_scev_3 (basic_block instantiate_below,
2649 struct loop *evolution_loop, struct loop *inner_loop,
2650 tree chrec,
2651 bool *fold_conversions, int size_expr)
2653 tree op1, op2;
2654 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2655 inner_loop, TREE_OPERAND (chrec, 0),
2656 fold_conversions, size_expr);
2657 if (op0 == chrec_dont_know)
2658 return chrec_dont_know;
2660 op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2661 inner_loop, TREE_OPERAND (chrec, 1),
2662 fold_conversions, size_expr);
2663 if (op1 == chrec_dont_know)
2664 return chrec_dont_know;
2666 op2 = instantiate_scev_r (instantiate_below, evolution_loop,
2667 inner_loop, TREE_OPERAND (chrec, 2),
2668 fold_conversions, size_expr);
2669 if (op2 == chrec_dont_know)
2670 return chrec_dont_know;
2672 if (op0 == TREE_OPERAND (chrec, 0)
2673 && op1 == TREE_OPERAND (chrec, 1)
2674 && op2 == TREE_OPERAND (chrec, 2))
2675 return chrec;
2677 return fold_build3 (TREE_CODE (chrec),
2678 TREE_TYPE (chrec), op0, op1, op2);
2681 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2682 and EVOLUTION_LOOP, that were left under a symbolic form.
2684 CHREC is an expression with 2 operands to be instantiated.
2686 CACHE is the cache of already instantiated values.
2688 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2689 conversions that may wrap in signed/pointer type are folded, as long
2690 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2691 then we don't do such fold.
2693 SIZE_EXPR is used for computing the size of the expression to be
2694 instantiated, and to stop if it exceeds some limit. */
2696 static tree
2697 instantiate_scev_2 (basic_block instantiate_below,
2698 struct loop *evolution_loop, struct loop *inner_loop,
2699 tree chrec,
2700 bool *fold_conversions, int size_expr)
2702 tree op1;
2703 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2704 inner_loop, TREE_OPERAND (chrec, 0),
2705 fold_conversions, size_expr);
2706 if (op0 == chrec_dont_know)
2707 return chrec_dont_know;
2709 op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2710 inner_loop, TREE_OPERAND (chrec, 1),
2711 fold_conversions, size_expr);
2712 if (op1 == chrec_dont_know)
2713 return chrec_dont_know;
2715 if (op0 == TREE_OPERAND (chrec, 0)
2716 && op1 == TREE_OPERAND (chrec, 1))
2717 return chrec;
2719 return fold_build2 (TREE_CODE (chrec), TREE_TYPE (chrec), op0, op1);
2722 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2723 and EVOLUTION_LOOP, that were left under a symbolic form.
2725 CHREC is an expression with 2 operands to be instantiated.
2727 CACHE is the cache of already instantiated values.
2729 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2730 conversions that may wrap in signed/pointer type are folded, as long
2731 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2732 then we don't do such fold.
2734 SIZE_EXPR is used for computing the size of the expression to be
2735 instantiated, and to stop if it exceeds some limit. */
2737 static tree
2738 instantiate_scev_1 (basic_block instantiate_below,
2739 struct loop *evolution_loop, struct loop *inner_loop,
2740 tree chrec,
2741 bool *fold_conversions, int size_expr)
2743 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2744 inner_loop, TREE_OPERAND (chrec, 0),
2745 fold_conversions, size_expr);
2747 if (op0 == chrec_dont_know)
2748 return chrec_dont_know;
2750 if (op0 == TREE_OPERAND (chrec, 0))
2751 return chrec;
2753 return fold_build1 (TREE_CODE (chrec), TREE_TYPE (chrec), op0);
2756 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2757 and EVOLUTION_LOOP, that were left under a symbolic form.
2759 CHREC is the scalar evolution to instantiate.
2761 CACHE is the cache of already instantiated values.
2763 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2764 conversions that may wrap in signed/pointer type are folded, as long
2765 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2766 then we don't do such fold.
2768 SIZE_EXPR is used for computing the size of the expression to be
2769 instantiated, and to stop if it exceeds some limit. */
2771 static tree
2772 instantiate_scev_r (basic_block instantiate_below,
2773 struct loop *evolution_loop, struct loop *inner_loop,
2774 tree chrec,
2775 bool *fold_conversions, int size_expr)
2777 /* Give up if the expression is larger than the MAX that we allow. */
2778 if (size_expr++ > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE))
2779 return chrec_dont_know;
2781 if (chrec == NULL_TREE
2782 || automatically_generated_chrec_p (chrec)
2783 || is_gimple_min_invariant (chrec))
2784 return chrec;
2786 switch (TREE_CODE (chrec))
2788 case SSA_NAME:
2789 return instantiate_scev_name (instantiate_below, evolution_loop,
2790 inner_loop, chrec,
2791 fold_conversions, size_expr);
2793 case POLYNOMIAL_CHREC:
2794 return instantiate_scev_poly (instantiate_below, evolution_loop,
2795 inner_loop, chrec,
2796 fold_conversions, size_expr);
2798 case POINTER_PLUS_EXPR:
2799 case PLUS_EXPR:
2800 case MINUS_EXPR:
2801 case MULT_EXPR:
2802 return instantiate_scev_binary (instantiate_below, evolution_loop,
2803 inner_loop, chrec,
2804 TREE_CODE (chrec), chrec_type (chrec),
2805 TREE_OPERAND (chrec, 0),
2806 TREE_OPERAND (chrec, 1),
2807 fold_conversions, size_expr);
2809 CASE_CONVERT:
2810 return instantiate_scev_convert (instantiate_below, evolution_loop,
2811 inner_loop, chrec,
2812 TREE_TYPE (chrec), TREE_OPERAND (chrec, 0),
2813 fold_conversions, size_expr);
2815 case NEGATE_EXPR:
2816 case BIT_NOT_EXPR:
2817 return instantiate_scev_not (instantiate_below, evolution_loop,
2818 inner_loop, chrec,
2819 TREE_CODE (chrec), TREE_TYPE (chrec),
2820 TREE_OPERAND (chrec, 0),
2821 fold_conversions, size_expr);
2823 case ADDR_EXPR:
2824 case SCEV_NOT_KNOWN:
2825 return chrec_dont_know;
2827 case SCEV_KNOWN:
2828 return chrec_known;
2830 case ARRAY_REF:
2831 return instantiate_array_ref (instantiate_below, evolution_loop,
2832 inner_loop, chrec,
2833 fold_conversions, size_expr);
2835 default:
2836 break;
2839 if (VL_EXP_CLASS_P (chrec))
2840 return chrec_dont_know;
2842 switch (TREE_CODE_LENGTH (TREE_CODE (chrec)))
2844 case 3:
2845 return instantiate_scev_3 (instantiate_below, evolution_loop,
2846 inner_loop, chrec,
2847 fold_conversions, size_expr);
2849 case 2:
2850 return instantiate_scev_2 (instantiate_below, evolution_loop,
2851 inner_loop, chrec,
2852 fold_conversions, size_expr);
2854 case 1:
2855 return instantiate_scev_1 (instantiate_below, evolution_loop,
2856 inner_loop, chrec,
2857 fold_conversions, size_expr);
2859 case 0:
2860 return chrec;
2862 default:
2863 break;
2866 /* Too complicated to handle. */
2867 return chrec_dont_know;
2870 /* Analyze all the parameters of the chrec that were left under a
2871 symbolic form. INSTANTIATE_BELOW is the basic block that stops the
2872 recursive instantiation of parameters: a parameter is a variable
2873 that is defined in a basic block that dominates INSTANTIATE_BELOW or
2874 a function parameter. */
2876 tree
2877 instantiate_scev (basic_block instantiate_below, struct loop *evolution_loop,
2878 tree chrec)
2880 tree res;
2882 if (dump_file && (dump_flags & TDF_SCEV))
2884 fprintf (dump_file, "(instantiate_scev \n");
2885 fprintf (dump_file, " (instantiate_below = %d)\n", instantiate_below->index);
2886 fprintf (dump_file, " (evolution_loop = %d)\n", evolution_loop->num);
2887 fprintf (dump_file, " (chrec = ");
2888 print_generic_expr (dump_file, chrec, 0);
2889 fprintf (dump_file, ")\n");
2892 bool destr = false;
2893 if (!global_cache)
2895 global_cache = new instantiate_cache_type;
2896 destr = true;
2899 res = instantiate_scev_r (instantiate_below, evolution_loop,
2900 NULL, chrec, NULL, 0);
2902 if (destr)
2904 delete global_cache;
2905 global_cache = NULL;
2908 if (dump_file && (dump_flags & TDF_SCEV))
2910 fprintf (dump_file, " (res = ");
2911 print_generic_expr (dump_file, res, 0);
2912 fprintf (dump_file, "))\n");
2915 return res;
2918 /* Similar to instantiate_parameters, but does not introduce the
2919 evolutions in outer loops for LOOP invariants in CHREC, and does not
2920 care about causing overflows, as long as they do not affect value
2921 of an expression. */
2923 tree
2924 resolve_mixers (struct loop *loop, tree chrec, bool *folded_casts)
2926 bool destr = false;
2927 bool fold_conversions = false;
2928 if (!global_cache)
2930 global_cache = new instantiate_cache_type;
2931 destr = true;
2934 tree ret = instantiate_scev_r (block_before_loop (loop), loop, NULL,
2935 chrec, &fold_conversions, 0);
2937 if (folded_casts && !*folded_casts)
2938 *folded_casts = fold_conversions;
2940 if (destr)
2942 delete global_cache;
2943 global_cache = NULL;
2946 return ret;
2949 /* Entry point for the analysis of the number of iterations pass.
2950 This function tries to safely approximate the number of iterations
2951 the loop will run. When this property is not decidable at compile
2952 time, the result is chrec_dont_know. Otherwise the result is a
2953 scalar or a symbolic parameter. When the number of iterations may
2954 be equal to zero and the property cannot be determined at compile
2955 time, the result is a COND_EXPR that represents in a symbolic form
2956 the conditions under which the number of iterations is not zero.
2958 Example of analysis: suppose that the loop has an exit condition:
2960 "if (b > 49) goto end_loop;"
2962 and that in a previous analysis we have determined that the
2963 variable 'b' has an evolution function:
2965 "EF = {23, +, 5}_2".
2967 When we evaluate the function at the point 5, i.e. the value of the
2968 variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
2969 and EF (6) = 53. In this case the value of 'b' on exit is '53' and
2970 the loop body has been executed 6 times. */
2972 tree
2973 number_of_latch_executions (struct loop *loop)
2975 edge exit;
2976 struct tree_niter_desc niter_desc;
2977 tree may_be_zero;
2978 tree res;
2980 /* Determine whether the number of iterations in loop has already
2981 been computed. */
2982 res = loop->nb_iterations;
2983 if (res)
2984 return res;
2986 may_be_zero = NULL_TREE;
2988 if (dump_file && (dump_flags & TDF_SCEV))
2989 fprintf (dump_file, "(number_of_iterations_in_loop = \n");
2991 res = chrec_dont_know;
2992 exit = single_exit (loop);
2994 if (exit && number_of_iterations_exit (loop, exit, &niter_desc, false))
2996 may_be_zero = niter_desc.may_be_zero;
2997 res = niter_desc.niter;
3000 if (res == chrec_dont_know
3001 || !may_be_zero
3002 || integer_zerop (may_be_zero))
3004 else if (integer_nonzerop (may_be_zero))
3005 res = build_int_cst (TREE_TYPE (res), 0);
3007 else if (COMPARISON_CLASS_P (may_be_zero))
3008 res = fold_build3 (COND_EXPR, TREE_TYPE (res), may_be_zero,
3009 build_int_cst (TREE_TYPE (res), 0), res);
3010 else
3011 res = chrec_dont_know;
3013 if (dump_file && (dump_flags & TDF_SCEV))
3015 fprintf (dump_file, " (set_nb_iterations_in_loop = ");
3016 print_generic_expr (dump_file, res, 0);
3017 fprintf (dump_file, "))\n");
3020 loop->nb_iterations = res;
3021 return res;
3025 /* Counters for the stats. */
3027 struct chrec_stats
3029 unsigned nb_chrecs;
3030 unsigned nb_affine;
3031 unsigned nb_affine_multivar;
3032 unsigned nb_higher_poly;
3033 unsigned nb_chrec_dont_know;
3034 unsigned nb_undetermined;
3037 /* Reset the counters. */
3039 static inline void
3040 reset_chrecs_counters (struct chrec_stats *stats)
3042 stats->nb_chrecs = 0;
3043 stats->nb_affine = 0;
3044 stats->nb_affine_multivar = 0;
3045 stats->nb_higher_poly = 0;
3046 stats->nb_chrec_dont_know = 0;
3047 stats->nb_undetermined = 0;
3050 /* Dump the contents of a CHREC_STATS structure. */
3052 static void
3053 dump_chrecs_stats (FILE *file, struct chrec_stats *stats)
3055 fprintf (file, "\n(\n");
3056 fprintf (file, "-----------------------------------------\n");
3057 fprintf (file, "%d\taffine univariate chrecs\n", stats->nb_affine);
3058 fprintf (file, "%d\taffine multivariate chrecs\n", stats->nb_affine_multivar);
3059 fprintf (file, "%d\tdegree greater than 2 polynomials\n",
3060 stats->nb_higher_poly);
3061 fprintf (file, "%d\tchrec_dont_know chrecs\n", stats->nb_chrec_dont_know);
3062 fprintf (file, "-----------------------------------------\n");
3063 fprintf (file, "%d\ttotal chrecs\n", stats->nb_chrecs);
3064 fprintf (file, "%d\twith undetermined coefficients\n",
3065 stats->nb_undetermined);
3066 fprintf (file, "-----------------------------------------\n");
3067 fprintf (file, "%d\tchrecs in the scev database\n",
3068 (int) scalar_evolution_info->elements ());
3069 fprintf (file, "%d\tsets in the scev database\n", nb_set_scev);
3070 fprintf (file, "%d\tgets in the scev database\n", nb_get_scev);
3071 fprintf (file, "-----------------------------------------\n");
3072 fprintf (file, ")\n\n");
3075 /* Gather statistics about CHREC. */
3077 static void
3078 gather_chrec_stats (tree chrec, struct chrec_stats *stats)
3080 if (dump_file && (dump_flags & TDF_STATS))
3082 fprintf (dump_file, "(classify_chrec ");
3083 print_generic_expr (dump_file, chrec, 0);
3084 fprintf (dump_file, "\n");
3087 stats->nb_chrecs++;
3089 if (chrec == NULL_TREE)
3091 stats->nb_undetermined++;
3092 return;
3095 switch (TREE_CODE (chrec))
3097 case POLYNOMIAL_CHREC:
3098 if (evolution_function_is_affine_p (chrec))
3100 if (dump_file && (dump_flags & TDF_STATS))
3101 fprintf (dump_file, " affine_univariate\n");
3102 stats->nb_affine++;
3104 else if (evolution_function_is_affine_multivariate_p (chrec, 0))
3106 if (dump_file && (dump_flags & TDF_STATS))
3107 fprintf (dump_file, " affine_multivariate\n");
3108 stats->nb_affine_multivar++;
3110 else
3112 if (dump_file && (dump_flags & TDF_STATS))
3113 fprintf (dump_file, " higher_degree_polynomial\n");
3114 stats->nb_higher_poly++;
3117 break;
3119 default:
3120 break;
3123 if (chrec_contains_undetermined (chrec))
3125 if (dump_file && (dump_flags & TDF_STATS))
3126 fprintf (dump_file, " undetermined\n");
3127 stats->nb_undetermined++;
3130 if (dump_file && (dump_flags & TDF_STATS))
3131 fprintf (dump_file, ")\n");
3134 /* Classify the chrecs of the whole database. */
3136 void
3137 gather_stats_on_scev_database (void)
3139 struct chrec_stats stats;
3141 if (!dump_file)
3142 return;
3144 reset_chrecs_counters (&stats);
3146 hash_table<scev_info_hasher>::iterator iter;
3147 scev_info_str *elt;
3148 FOR_EACH_HASH_TABLE_ELEMENT (*scalar_evolution_info, elt, scev_info_str *,
3149 iter)
3150 gather_chrec_stats (elt->chrec, &stats);
3152 dump_chrecs_stats (dump_file, &stats);
3157 /* Initializer. */
3159 static void
3160 initialize_scalar_evolutions_analyzer (void)
3162 /* The elements below are unique. */
3163 if (chrec_dont_know == NULL_TREE)
3165 chrec_not_analyzed_yet = NULL_TREE;
3166 chrec_dont_know = make_node (SCEV_NOT_KNOWN);
3167 chrec_known = make_node (SCEV_KNOWN);
3168 TREE_TYPE (chrec_dont_know) = void_type_node;
3169 TREE_TYPE (chrec_known) = void_type_node;
3173 /* Initialize the analysis of scalar evolutions for LOOPS. */
3175 void
3176 scev_initialize (void)
3178 struct loop *loop;
3180 scalar_evolution_info = hash_table<scev_info_hasher>::create_ggc (100);
3182 initialize_scalar_evolutions_analyzer ();
3184 FOR_EACH_LOOP (loop, 0)
3186 loop->nb_iterations = NULL_TREE;
3190 /* Return true if SCEV is initialized. */
3192 bool
3193 scev_initialized_p (void)
3195 return scalar_evolution_info != NULL;
3198 /* Cleans up the information cached by the scalar evolutions analysis
3199 in the hash table. */
3201 void
3202 scev_reset_htab (void)
3204 if (!scalar_evolution_info)
3205 return;
3207 scalar_evolution_info->empty ();
3210 /* Cleans up the information cached by the scalar evolutions analysis
3211 in the hash table and in the loop->nb_iterations. */
3213 void
3214 scev_reset (void)
3216 struct loop *loop;
3218 scev_reset_htab ();
3220 FOR_EACH_LOOP (loop, 0)
3222 loop->nb_iterations = NULL_TREE;
3226 /* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with
3227 respect to WRTO_LOOP and returns its base and step in IV if possible
3228 (see analyze_scalar_evolution_in_loop for more details on USE_LOOP
3229 and WRTO_LOOP). If ALLOW_NONCONSTANT_STEP is true, we want step to be
3230 invariant in LOOP. Otherwise we require it to be an integer constant.
3232 IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g.
3233 because it is computed in signed arithmetics). Consequently, adding an
3234 induction variable
3236 for (i = IV->base; ; i += IV->step)
3238 is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is
3239 false for the type of the induction variable, or you can prove that i does
3240 not wrap by some other argument. Otherwise, this might introduce undefined
3241 behavior, and
3243 for (i = iv->base; ; i = (type) ((unsigned type) i + (unsigned type) iv->step))
3245 must be used instead. */
3247 bool
3248 simple_iv (struct loop *wrto_loop, struct loop *use_loop, tree op,
3249 affine_iv *iv, bool allow_nonconstant_step)
3251 tree type, ev;
3252 bool folded_casts;
3254 iv->base = NULL_TREE;
3255 iv->step = NULL_TREE;
3256 iv->no_overflow = false;
3258 type = TREE_TYPE (op);
3259 if (!POINTER_TYPE_P (type)
3260 && !INTEGRAL_TYPE_P (type))
3261 return false;
3263 ev = analyze_scalar_evolution_in_loop (wrto_loop, use_loop, op,
3264 &folded_casts);
3265 if (chrec_contains_undetermined (ev)
3266 || chrec_contains_symbols_defined_in_loop (ev, wrto_loop->num))
3267 return false;
3269 if (tree_does_not_contain_chrecs (ev))
3271 iv->base = ev;
3272 iv->step = build_int_cst (TREE_TYPE (ev), 0);
3273 iv->no_overflow = true;
3274 return true;
3277 if (TREE_CODE (ev) != POLYNOMIAL_CHREC
3278 || CHREC_VARIABLE (ev) != (unsigned) wrto_loop->num)
3279 return false;
3281 iv->step = CHREC_RIGHT (ev);
3282 if ((!allow_nonconstant_step && TREE_CODE (iv->step) != INTEGER_CST)
3283 || tree_contains_chrecs (iv->step, NULL))
3284 return false;
3286 iv->base = CHREC_LEFT (ev);
3287 if (tree_contains_chrecs (iv->base, NULL))
3288 return false;
3290 iv->no_overflow = (!folded_casts && ANY_INTEGRAL_TYPE_P (type)
3291 && TYPE_OVERFLOW_UNDEFINED (type));
3293 return true;
3296 /* Finalize the scalar evolution analysis. */
3298 void
3299 scev_finalize (void)
3301 if (!scalar_evolution_info)
3302 return;
3303 scalar_evolution_info->empty ();
3304 scalar_evolution_info = NULL;
3307 /* Returns true if the expression EXPR is considered to be too expensive
3308 for scev_const_prop. */
3310 bool
3311 expression_expensive_p (tree expr)
3313 enum tree_code code;
3315 if (is_gimple_val (expr))
3316 return false;
3318 code = TREE_CODE (expr);
3319 if (code == TRUNC_DIV_EXPR
3320 || code == CEIL_DIV_EXPR
3321 || code == FLOOR_DIV_EXPR
3322 || code == ROUND_DIV_EXPR
3323 || code == TRUNC_MOD_EXPR
3324 || code == CEIL_MOD_EXPR
3325 || code == FLOOR_MOD_EXPR
3326 || code == ROUND_MOD_EXPR
3327 || code == EXACT_DIV_EXPR)
3329 /* Division by power of two is usually cheap, so we allow it.
3330 Forbid anything else. */
3331 if (!integer_pow2p (TREE_OPERAND (expr, 1)))
3332 return true;
3335 switch (TREE_CODE_CLASS (code))
3337 case tcc_binary:
3338 case tcc_comparison:
3339 if (expression_expensive_p (TREE_OPERAND (expr, 1)))
3340 return true;
3342 /* Fallthru. */
3343 case tcc_unary:
3344 return expression_expensive_p (TREE_OPERAND (expr, 0));
3346 default:
3347 return true;
3351 /* Replace ssa names for that scev can prove they are constant by the
3352 appropriate constants. Also perform final value replacement in loops,
3353 in case the replacement expressions are cheap.
3355 We only consider SSA names defined by phi nodes; rest is left to the
3356 ordinary constant propagation pass. */
3358 unsigned int
3359 scev_const_prop (void)
3361 basic_block bb;
3362 tree name, type, ev;
3363 gphi *phi;
3364 gassign *ass;
3365 struct loop *loop, *ex_loop;
3366 bitmap ssa_names_to_remove = NULL;
3367 unsigned i;
3368 gphi_iterator psi;
3370 if (number_of_loops (cfun) <= 1)
3371 return 0;
3373 FOR_EACH_BB_FN (bb, cfun)
3375 loop = bb->loop_father;
3377 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
3379 phi = psi.phi ();
3380 name = PHI_RESULT (phi);
3382 if (virtual_operand_p (name))
3383 continue;
3385 type = TREE_TYPE (name);
3387 if (!POINTER_TYPE_P (type)
3388 && !INTEGRAL_TYPE_P (type))
3389 continue;
3391 ev = resolve_mixers (loop, analyze_scalar_evolution (loop, name),
3392 NULL);
3393 if (!is_gimple_min_invariant (ev)
3394 || !may_propagate_copy (name, ev))
3395 continue;
3397 /* Replace the uses of the name. */
3398 if (name != ev)
3399 replace_uses_by (name, ev);
3401 if (!ssa_names_to_remove)
3402 ssa_names_to_remove = BITMAP_ALLOC (NULL);
3403 bitmap_set_bit (ssa_names_to_remove, SSA_NAME_VERSION (name));
3407 /* Remove the ssa names that were replaced by constants. We do not
3408 remove them directly in the previous cycle, since this
3409 invalidates scev cache. */
3410 if (ssa_names_to_remove)
3412 bitmap_iterator bi;
3414 EXECUTE_IF_SET_IN_BITMAP (ssa_names_to_remove, 0, i, bi)
3416 gimple_stmt_iterator psi;
3417 name = ssa_name (i);
3418 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (name));
3420 gcc_assert (gimple_code (phi) == GIMPLE_PHI);
3421 psi = gsi_for_stmt (phi);
3422 remove_phi_node (&psi, true);
3425 BITMAP_FREE (ssa_names_to_remove);
3426 scev_reset ();
3429 /* Now the regular final value replacement. */
3430 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
3432 edge exit;
3433 tree def, rslt, niter;
3434 gimple_stmt_iterator gsi;
3436 /* If we do not know exact number of iterations of the loop, we cannot
3437 replace the final value. */
3438 exit = single_exit (loop);
3439 if (!exit)
3440 continue;
3442 niter = number_of_latch_executions (loop);
3443 if (niter == chrec_dont_know)
3444 continue;
3446 /* Ensure that it is possible to insert new statements somewhere. */
3447 if (!single_pred_p (exit->dest))
3448 split_loop_exit_edge (exit);
3449 gsi = gsi_after_labels (exit->dest);
3451 ex_loop = superloop_at_depth (loop,
3452 loop_depth (exit->dest->loop_father) + 1);
3454 for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); )
3456 phi = psi.phi ();
3457 rslt = PHI_RESULT (phi);
3458 def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
3459 if (virtual_operand_p (def))
3461 gsi_next (&psi);
3462 continue;
3465 if (!POINTER_TYPE_P (TREE_TYPE (def))
3466 && !INTEGRAL_TYPE_P (TREE_TYPE (def)))
3468 gsi_next (&psi);
3469 continue;
3472 bool folded_casts;
3473 def = analyze_scalar_evolution_in_loop (ex_loop, loop, def,
3474 &folded_casts);
3475 def = compute_overall_effect_of_inner_loop (ex_loop, def);
3476 if (!tree_does_not_contain_chrecs (def)
3477 || chrec_contains_symbols_defined_in_loop (def, ex_loop->num)
3478 /* Moving the computation from the loop may prolong life range
3479 of some ssa names, which may cause problems if they appear
3480 on abnormal edges. */
3481 || contains_abnormal_ssa_name_p (def)
3482 /* Do not emit expensive expressions. The rationale is that
3483 when someone writes a code like
3485 while (n > 45) n -= 45;
3487 he probably knows that n is not large, and does not want it
3488 to be turned into n %= 45. */
3489 || expression_expensive_p (def))
3491 if (dump_file && (dump_flags & TDF_DETAILS))
3493 fprintf (dump_file, "not replacing:\n ");
3494 print_gimple_stmt (dump_file, phi, 0, 0);
3495 fprintf (dump_file, "\n");
3497 gsi_next (&psi);
3498 continue;
3501 /* Eliminate the PHI node and replace it by a computation outside
3502 the loop. */
3503 if (dump_file)
3505 fprintf (dump_file, "\nfinal value replacement:\n ");
3506 print_gimple_stmt (dump_file, phi, 0, 0);
3507 fprintf (dump_file, " with\n ");
3509 def = unshare_expr (def);
3510 remove_phi_node (&psi, false);
3512 /* If def's type has undefined overflow and there were folded
3513 casts, rewrite all stmts added for def into arithmetics
3514 with defined overflow behavior. */
3515 if (folded_casts && ANY_INTEGRAL_TYPE_P (TREE_TYPE (def))
3516 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (def)))
3518 gimple_seq stmts;
3519 gimple_stmt_iterator gsi2;
3520 def = force_gimple_operand (def, &stmts, true, NULL_TREE);
3521 gsi2 = gsi_start (stmts);
3522 while (!gsi_end_p (gsi2))
3524 gimple stmt = gsi_stmt (gsi2);
3525 gimple_stmt_iterator gsi3 = gsi2;
3526 gsi_next (&gsi2);
3527 gsi_remove (&gsi3, false);
3528 if (is_gimple_assign (stmt)
3529 && arith_code_with_undefined_signed_overflow
3530 (gimple_assign_rhs_code (stmt)))
3531 gsi_insert_seq_before (&gsi,
3532 rewrite_to_defined_overflow (stmt),
3533 GSI_SAME_STMT);
3534 else
3535 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
3538 else
3539 def = force_gimple_operand_gsi (&gsi, def, false, NULL_TREE,
3540 true, GSI_SAME_STMT);
3542 ass = gimple_build_assign (rslt, def);
3543 gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
3544 if (dump_file)
3546 print_gimple_stmt (dump_file, ass, 0, 0);
3547 fprintf (dump_file, "\n");
3551 return 0;
3554 #include "gt-tree-scalar-evolution.h"