2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / tree-scalar-evolution.c
blob0753bf3122ef8f6690f34b63075c8d4d35d235bb
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 "alias.h"
260 #include "backend.h"
261 #include "tree.h"
262 #include "gimple.h"
263 #include "rtl.h"
264 #include "ssa.h"
265 #include "options.h"
266 #include "fold-const.h"
267 #include "flags.h"
268 #include "insn-config.h"
269 #include "expmed.h"
270 #include "dojump.h"
271 #include "explow.h"
272 #include "calls.h"
273 #include "emit-rtl.h"
274 #include "varasm.h"
275 #include "stmt.h"
276 #include "expr.h"
277 #include "gimple-pretty-print.h"
278 #include "internal-fn.h"
279 #include "gimplify.h"
280 #include "gimple-iterator.h"
281 #include "gimplify-me.h"
282 #include "tree-cfg.h"
283 #include "tree-ssa-loop-ivopts.h"
284 #include "tree-ssa-loop-manip.h"
285 #include "tree-ssa-loop-niter.h"
286 #include "tree-ssa-loop.h"
287 #include "tree-ssa.h"
288 #include "cfgloop.h"
289 #include "tree-chrec.h"
290 #include "tree-affine.h"
291 #include "tree-scalar-evolution.h"
292 #include "dumpfile.h"
293 #include "params.h"
294 #include "tree-ssa-propagate.h"
295 #include "gimple-fold.h"
297 static tree analyze_scalar_evolution_1 (struct loop *, tree, tree);
298 static tree analyze_scalar_evolution_for_address_of (struct loop *loop,
299 tree var);
301 /* The cached information about an SSA name with version NAME_VERSION,
302 claiming that below basic block with index INSTANTIATED_BELOW, the
303 value of the SSA name can be expressed as CHREC. */
305 struct GTY((for_user)) scev_info_str {
306 unsigned int name_version;
307 int instantiated_below;
308 tree chrec;
311 /* Counters for the scev database. */
312 static unsigned nb_set_scev = 0;
313 static unsigned nb_get_scev = 0;
315 /* The following trees are unique elements. Thus the comparison of
316 another element to these elements should be done on the pointer to
317 these trees, and not on their value. */
319 /* The SSA_NAMEs that are not yet analyzed are qualified with NULL_TREE. */
320 tree chrec_not_analyzed_yet;
322 /* Reserved to the cases where the analyzer has detected an
323 undecidable property at compile time. */
324 tree chrec_dont_know;
326 /* When the analyzer has detected that a property will never
327 happen, then it qualifies it with chrec_known. */
328 tree chrec_known;
330 struct scev_info_hasher : ggc_ptr_hash<scev_info_str>
332 static hashval_t hash (scev_info_str *i);
333 static bool equal (const scev_info_str *a, const scev_info_str *b);
336 static GTY (()) hash_table<scev_info_hasher> *scalar_evolution_info;
339 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */
341 static inline struct scev_info_str *
342 new_scev_info_str (basic_block instantiated_below, tree var)
344 struct scev_info_str *res;
346 res = ggc_alloc<scev_info_str> ();
347 res->name_version = SSA_NAME_VERSION (var);
348 res->chrec = chrec_not_analyzed_yet;
349 res->instantiated_below = instantiated_below->index;
351 return res;
354 /* Computes a hash function for database element ELT. */
356 hashval_t
357 scev_info_hasher::hash (scev_info_str *elt)
359 return elt->name_version ^ elt->instantiated_below;
362 /* Compares database elements E1 and E2. */
364 bool
365 scev_info_hasher::equal (const scev_info_str *elt1, const scev_info_str *elt2)
367 return (elt1->name_version == elt2->name_version
368 && elt1->instantiated_below == elt2->instantiated_below);
371 /* Get the scalar evolution of VAR for INSTANTIATED_BELOW basic block.
372 A first query on VAR returns chrec_not_analyzed_yet. */
374 static tree *
375 find_var_scev_info (basic_block instantiated_below, tree var)
377 struct scev_info_str *res;
378 struct scev_info_str tmp;
380 tmp.name_version = SSA_NAME_VERSION (var);
381 tmp.instantiated_below = instantiated_below->index;
382 scev_info_str **slot = scalar_evolution_info->find_slot (&tmp, INSERT);
384 if (!*slot)
385 *slot = new_scev_info_str (instantiated_below, var);
386 res = *slot;
388 return &res->chrec;
391 /* Return true when CHREC contains symbolic names defined in
392 LOOP_NB. */
394 bool
395 chrec_contains_symbols_defined_in_loop (const_tree chrec, unsigned loop_nb)
397 int i, n;
399 if (chrec == NULL_TREE)
400 return false;
402 if (is_gimple_min_invariant (chrec))
403 return false;
405 if (TREE_CODE (chrec) == SSA_NAME)
407 gimple *def;
408 loop_p def_loop, loop;
410 if (SSA_NAME_IS_DEFAULT_DEF (chrec))
411 return false;
413 def = SSA_NAME_DEF_STMT (chrec);
414 def_loop = loop_containing_stmt (def);
415 loop = get_loop (cfun, loop_nb);
417 if (def_loop == NULL)
418 return false;
420 if (loop == def_loop || flow_loop_nested_p (loop, def_loop))
421 return true;
423 return false;
426 n = TREE_OPERAND_LENGTH (chrec);
427 for (i = 0; i < n; i++)
428 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec, i),
429 loop_nb))
430 return true;
431 return false;
434 /* Return true when PHI is a loop-phi-node. */
436 static bool
437 loop_phi_node_p (gimple *phi)
439 /* The implementation of this function is based on the following
440 property: "all the loop-phi-nodes of a loop are contained in the
441 loop's header basic block". */
443 return loop_containing_stmt (phi)->header == gimple_bb (phi);
446 /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
447 In general, in the case of multivariate evolutions we want to get
448 the evolution in different loops. LOOP specifies the level for
449 which to get the evolution.
451 Example:
453 | for (j = 0; j < 100; j++)
455 | for (k = 0; k < 100; k++)
457 | i = k + j; - Here the value of i is a function of j, k.
459 | ... = i - Here the value of i is a function of j.
461 | ... = i - Here the value of i is a scalar.
463 Example:
465 | i_0 = ...
466 | loop_1 10 times
467 | i_1 = phi (i_0, i_2)
468 | i_2 = i_1 + 2
469 | endloop
471 This loop has the same effect as:
472 LOOP_1 has the same effect as:
474 | i_1 = i_0 + 20
476 The overall effect of the loop, "i_0 + 20" in the previous example,
477 is obtained by passing in the parameters: LOOP = 1,
478 EVOLUTION_FN = {i_0, +, 2}_1.
481 tree
482 compute_overall_effect_of_inner_loop (struct loop *loop, tree evolution_fn)
484 bool val = false;
486 if (evolution_fn == chrec_dont_know)
487 return chrec_dont_know;
489 else if (TREE_CODE (evolution_fn) == POLYNOMIAL_CHREC)
491 struct loop *inner_loop = get_chrec_loop (evolution_fn);
493 if (inner_loop == loop
494 || flow_loop_nested_p (loop, inner_loop))
496 tree nb_iter = number_of_latch_executions (inner_loop);
498 if (nb_iter == chrec_dont_know)
499 return chrec_dont_know;
500 else
502 tree res;
504 /* evolution_fn is the evolution function in LOOP. Get
505 its value in the nb_iter-th iteration. */
506 res = chrec_apply (inner_loop->num, evolution_fn, nb_iter);
508 if (chrec_contains_symbols_defined_in_loop (res, loop->num))
509 res = instantiate_parameters (loop, res);
511 /* Continue the computation until ending on a parent of LOOP. */
512 return compute_overall_effect_of_inner_loop (loop, res);
515 else
516 return evolution_fn;
519 /* If the evolution function is an invariant, there is nothing to do. */
520 else if (no_evolution_in_loop_p (evolution_fn, loop->num, &val) && val)
521 return evolution_fn;
523 else
524 return chrec_dont_know;
527 /* Associate CHREC to SCALAR. */
529 static void
530 set_scalar_evolution (basic_block instantiated_below, tree scalar, tree chrec)
532 tree *scalar_info;
534 if (TREE_CODE (scalar) != SSA_NAME)
535 return;
537 scalar_info = find_var_scev_info (instantiated_below, scalar);
539 if (dump_file)
541 if (dump_flags & TDF_SCEV)
543 fprintf (dump_file, "(set_scalar_evolution \n");
544 fprintf (dump_file, " instantiated_below = %d \n",
545 instantiated_below->index);
546 fprintf (dump_file, " (scalar = ");
547 print_generic_expr (dump_file, scalar, 0);
548 fprintf (dump_file, ")\n (scalar_evolution = ");
549 print_generic_expr (dump_file, chrec, 0);
550 fprintf (dump_file, "))\n");
552 if (dump_flags & TDF_STATS)
553 nb_set_scev++;
556 *scalar_info = chrec;
559 /* Retrieve the chrec associated to SCALAR instantiated below
560 INSTANTIATED_BELOW block. */
562 static tree
563 get_scalar_evolution (basic_block instantiated_below, tree scalar)
565 tree res;
567 if (dump_file)
569 if (dump_flags & TDF_SCEV)
571 fprintf (dump_file, "(get_scalar_evolution \n");
572 fprintf (dump_file, " (scalar = ");
573 print_generic_expr (dump_file, scalar, 0);
574 fprintf (dump_file, ")\n");
576 if (dump_flags & TDF_STATS)
577 nb_get_scev++;
580 switch (TREE_CODE (scalar))
582 case SSA_NAME:
583 res = *find_var_scev_info (instantiated_below, scalar);
584 break;
586 case REAL_CST:
587 case FIXED_CST:
588 case INTEGER_CST:
589 res = scalar;
590 break;
592 default:
593 res = chrec_not_analyzed_yet;
594 break;
597 if (dump_file && (dump_flags & TDF_SCEV))
599 fprintf (dump_file, " (scalar_evolution = ");
600 print_generic_expr (dump_file, res, 0);
601 fprintf (dump_file, "))\n");
604 return res;
607 /* Helper function for add_to_evolution. Returns the evolution
608 function for an assignment of the form "a = b + c", where "a" and
609 "b" are on the strongly connected component. CHREC_BEFORE is the
610 information that we already have collected up to this point.
611 TO_ADD is the evolution of "c".
613 When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
614 evolution the expression TO_ADD, otherwise construct an evolution
615 part for this loop. */
617 static tree
618 add_to_evolution_1 (unsigned loop_nb, tree chrec_before, tree to_add,
619 gimple *at_stmt)
621 tree type, left, right;
622 struct loop *loop = get_loop (cfun, loop_nb), *chloop;
624 switch (TREE_CODE (chrec_before))
626 case POLYNOMIAL_CHREC:
627 chloop = get_chrec_loop (chrec_before);
628 if (chloop == loop
629 || flow_loop_nested_p (chloop, loop))
631 unsigned var;
633 type = chrec_type (chrec_before);
635 /* When there is no evolution part in this loop, build it. */
636 if (chloop != loop)
638 var = loop_nb;
639 left = chrec_before;
640 right = SCALAR_FLOAT_TYPE_P (type)
641 ? build_real (type, dconst0)
642 : build_int_cst (type, 0);
644 else
646 var = CHREC_VARIABLE (chrec_before);
647 left = CHREC_LEFT (chrec_before);
648 right = CHREC_RIGHT (chrec_before);
651 to_add = chrec_convert (type, to_add, at_stmt);
652 right = chrec_convert_rhs (type, right, at_stmt);
653 right = chrec_fold_plus (chrec_type (right), right, to_add);
654 return build_polynomial_chrec (var, left, right);
656 else
658 gcc_assert (flow_loop_nested_p (loop, chloop));
660 /* Search the evolution in LOOP_NB. */
661 left = add_to_evolution_1 (loop_nb, CHREC_LEFT (chrec_before),
662 to_add, at_stmt);
663 right = CHREC_RIGHT (chrec_before);
664 right = chrec_convert_rhs (chrec_type (left), right, at_stmt);
665 return build_polynomial_chrec (CHREC_VARIABLE (chrec_before),
666 left, right);
669 default:
670 /* These nodes do not depend on a loop. */
671 if (chrec_before == chrec_dont_know)
672 return chrec_dont_know;
674 left = chrec_before;
675 right = chrec_convert_rhs (chrec_type (left), to_add, at_stmt);
676 return build_polynomial_chrec (loop_nb, left, right);
680 /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
681 of LOOP_NB.
683 Description (provided for completeness, for those who read code in
684 a plane, and for my poor 62 bytes brain that would have forgotten
685 all this in the next two or three months):
687 The algorithm of translation of programs from the SSA representation
688 into the chrecs syntax is based on a pattern matching. After having
689 reconstructed the overall tree expression for a loop, there are only
690 two cases that can arise:
692 1. a = loop-phi (init, a + expr)
693 2. a = loop-phi (init, expr)
695 where EXPR is either a scalar constant with respect to the analyzed
696 loop (this is a degree 0 polynomial), or an expression containing
697 other loop-phi definitions (these are higher degree polynomials).
699 Examples:
702 | init = ...
703 | loop_1
704 | a = phi (init, a + 5)
705 | endloop
708 | inita = ...
709 | initb = ...
710 | loop_1
711 | a = phi (inita, 2 * b + 3)
712 | b = phi (initb, b + 1)
713 | endloop
715 For the first case, the semantics of the SSA representation is:
717 | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
719 that is, there is a loop index "x" that determines the scalar value
720 of the variable during the loop execution. During the first
721 iteration, the value is that of the initial condition INIT, while
722 during the subsequent iterations, it is the sum of the initial
723 condition with the sum of all the values of EXPR from the initial
724 iteration to the before last considered iteration.
726 For the second case, the semantics of the SSA program is:
728 | a (x) = init, if x = 0;
729 | expr (x - 1), otherwise.
731 The second case corresponds to the PEELED_CHREC, whose syntax is
732 close to the syntax of a loop-phi-node:
734 | phi (init, expr) vs. (init, expr)_x
736 The proof of the translation algorithm for the first case is a
737 proof by structural induction based on the degree of EXPR.
739 Degree 0:
740 When EXPR is a constant with respect to the analyzed loop, or in
741 other words when EXPR is a polynomial of degree 0, the evolution of
742 the variable A in the loop is an affine function with an initial
743 condition INIT, and a step EXPR. In order to show this, we start
744 from the semantics of the SSA representation:
746 f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
748 and since "expr (j)" is a constant with respect to "j",
750 f (x) = init + x * expr
752 Finally, based on the semantics of the pure sum chrecs, by
753 identification we get the corresponding chrecs syntax:
755 f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
756 f (x) -> {init, +, expr}_x
758 Higher degree:
759 Suppose that EXPR is a polynomial of degree N with respect to the
760 analyzed loop_x for which we have already determined that it is
761 written under the chrecs syntax:
763 | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
765 We start from the semantics of the SSA program:
767 | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
769 | f (x) = init + \sum_{j = 0}^{x - 1}
770 | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
772 | f (x) = init + \sum_{j = 0}^{x - 1}
773 | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
775 | f (x) = init + \sum_{k = 0}^{n - 1}
776 | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
778 | f (x) = init + \sum_{k = 0}^{n - 1}
779 | (b_k * \binom{x}{k + 1})
781 | f (x) = init + b_0 * \binom{x}{1} + ...
782 | + b_{n-1} * \binom{x}{n}
784 | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
785 | + b_{n-1} * \binom{x}{n}
788 And finally from the definition of the chrecs syntax, we identify:
789 | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x
791 This shows the mechanism that stands behind the add_to_evolution
792 function. An important point is that the use of symbolic
793 parameters avoids the need of an analysis schedule.
795 Example:
797 | inita = ...
798 | initb = ...
799 | loop_1
800 | a = phi (inita, a + 2 + b)
801 | b = phi (initb, b + 1)
802 | endloop
804 When analyzing "a", the algorithm keeps "b" symbolically:
806 | a -> {inita, +, 2 + b}_1
808 Then, after instantiation, the analyzer ends on the evolution:
810 | a -> {inita, +, 2 + initb, +, 1}_1
814 static tree
815 add_to_evolution (unsigned loop_nb, tree chrec_before, enum tree_code code,
816 tree to_add, gimple *at_stmt)
818 tree type = chrec_type (to_add);
819 tree res = NULL_TREE;
821 if (to_add == NULL_TREE)
822 return chrec_before;
824 /* TO_ADD is either a scalar, or a parameter. TO_ADD is not
825 instantiated at this point. */
826 if (TREE_CODE (to_add) == POLYNOMIAL_CHREC)
827 /* This should not happen. */
828 return chrec_dont_know;
830 if (dump_file && (dump_flags & TDF_SCEV))
832 fprintf (dump_file, "(add_to_evolution \n");
833 fprintf (dump_file, " (loop_nb = %d)\n", loop_nb);
834 fprintf (dump_file, " (chrec_before = ");
835 print_generic_expr (dump_file, chrec_before, 0);
836 fprintf (dump_file, ")\n (to_add = ");
837 print_generic_expr (dump_file, to_add, 0);
838 fprintf (dump_file, ")\n");
841 if (code == MINUS_EXPR)
842 to_add = chrec_fold_multiply (type, to_add, SCALAR_FLOAT_TYPE_P (type)
843 ? build_real (type, dconstm1)
844 : build_int_cst_type (type, -1));
846 res = add_to_evolution_1 (loop_nb, chrec_before, to_add, at_stmt);
848 if (dump_file && (dump_flags & TDF_SCEV))
850 fprintf (dump_file, " (res = ");
851 print_generic_expr (dump_file, res, 0);
852 fprintf (dump_file, "))\n");
855 return res;
860 /* This section selects the loops that will be good candidates for the
861 scalar evolution analysis. For the moment, greedily select all the
862 loop nests we could analyze. */
864 /* For a loop with a single exit edge, return the COND_EXPR that
865 guards the exit edge. If the expression is too difficult to
866 analyze, then give up. */
868 gcond *
869 get_loop_exit_condition (const struct loop *loop)
871 gcond *res = NULL;
872 edge exit_edge = single_exit (loop);
874 if (dump_file && (dump_flags & TDF_SCEV))
875 fprintf (dump_file, "(get_loop_exit_condition \n ");
877 if (exit_edge)
879 gimple *stmt;
881 stmt = last_stmt (exit_edge->src);
882 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
883 res = cond_stmt;
886 if (dump_file && (dump_flags & TDF_SCEV))
888 print_gimple_stmt (dump_file, res, 0, 0);
889 fprintf (dump_file, ")\n");
892 return res;
896 /* Depth first search algorithm. */
898 enum t_bool {
899 t_false,
900 t_true,
901 t_dont_know
905 static t_bool follow_ssa_edge (struct loop *loop, gimple *, gphi *,
906 tree *, int);
908 /* Follow the ssa edge into the binary expression RHS0 CODE RHS1.
909 Return true if the strongly connected component has been found. */
911 static t_bool
912 follow_ssa_edge_binary (struct loop *loop, gimple *at_stmt,
913 tree type, tree rhs0, enum tree_code code, tree rhs1,
914 gphi *halting_phi, tree *evolution_of_loop,
915 int limit)
917 t_bool res = t_false;
918 tree evol;
920 switch (code)
922 case POINTER_PLUS_EXPR:
923 case PLUS_EXPR:
924 if (TREE_CODE (rhs0) == SSA_NAME)
926 if (TREE_CODE (rhs1) == SSA_NAME)
928 /* Match an assignment under the form:
929 "a = b + c". */
931 /* We want only assignments of form "name + name" contribute to
932 LIMIT, as the other cases do not necessarily contribute to
933 the complexity of the expression. */
934 limit++;
936 evol = *evolution_of_loop;
937 evol = add_to_evolution
938 (loop->num,
939 chrec_convert (type, evol, at_stmt),
940 code, rhs1, at_stmt);
941 res = follow_ssa_edge
942 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi, &evol, limit);
943 if (res == t_true)
944 *evolution_of_loop = evol;
945 else if (res == t_false)
947 *evolution_of_loop = add_to_evolution
948 (loop->num,
949 chrec_convert (type, *evolution_of_loop, at_stmt),
950 code, rhs0, at_stmt);
951 res = follow_ssa_edge
952 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
953 evolution_of_loop, limit);
954 if (res == t_true)
956 else if (res == t_dont_know)
957 *evolution_of_loop = chrec_dont_know;
960 else if (res == t_dont_know)
961 *evolution_of_loop = chrec_dont_know;
964 else
966 /* Match an assignment under the form:
967 "a = b + ...". */
968 *evolution_of_loop = add_to_evolution
969 (loop->num, chrec_convert (type, *evolution_of_loop,
970 at_stmt),
971 code, rhs1, at_stmt);
972 res = follow_ssa_edge
973 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
974 evolution_of_loop, limit);
975 if (res == t_true)
977 else if (res == t_dont_know)
978 *evolution_of_loop = chrec_dont_know;
982 else if (TREE_CODE (rhs1) == SSA_NAME)
984 /* Match an assignment under the form:
985 "a = ... + c". */
986 *evolution_of_loop = add_to_evolution
987 (loop->num, chrec_convert (type, *evolution_of_loop,
988 at_stmt),
989 code, rhs0, at_stmt);
990 res = follow_ssa_edge
991 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
992 evolution_of_loop, limit);
993 if (res == t_true)
995 else if (res == t_dont_know)
996 *evolution_of_loop = chrec_dont_know;
999 else
1000 /* Otherwise, match an assignment under the form:
1001 "a = ... + ...". */
1002 /* And there is nothing to do. */
1003 res = t_false;
1004 break;
1006 case MINUS_EXPR:
1007 /* This case is under the form "opnd0 = rhs0 - rhs1". */
1008 if (TREE_CODE (rhs0) == SSA_NAME)
1010 /* Match an assignment under the form:
1011 "a = b - ...". */
1013 /* We want only assignments of form "name - name" contribute to
1014 LIMIT, as the other cases do not necessarily contribute to
1015 the complexity of the expression. */
1016 if (TREE_CODE (rhs1) == SSA_NAME)
1017 limit++;
1019 *evolution_of_loop = add_to_evolution
1020 (loop->num, chrec_convert (type, *evolution_of_loop, at_stmt),
1021 MINUS_EXPR, rhs1, at_stmt);
1022 res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
1023 evolution_of_loop, limit);
1024 if (res == t_true)
1026 else if (res == t_dont_know)
1027 *evolution_of_loop = chrec_dont_know;
1029 else
1030 /* Otherwise, match an assignment under the form:
1031 "a = ... - ...". */
1032 /* And there is nothing to do. */
1033 res = t_false;
1034 break;
1036 default:
1037 res = t_false;
1040 return res;
1043 /* Follow the ssa edge into the expression EXPR.
1044 Return true if the strongly connected component has been found. */
1046 static t_bool
1047 follow_ssa_edge_expr (struct loop *loop, gimple *at_stmt, tree expr,
1048 gphi *halting_phi, tree *evolution_of_loop,
1049 int limit)
1051 enum tree_code code = TREE_CODE (expr);
1052 tree type = TREE_TYPE (expr), rhs0, rhs1;
1053 t_bool res;
1055 /* The EXPR is one of the following cases:
1056 - an SSA_NAME,
1057 - an INTEGER_CST,
1058 - a PLUS_EXPR,
1059 - a POINTER_PLUS_EXPR,
1060 - a MINUS_EXPR,
1061 - an ASSERT_EXPR,
1062 - other cases are not yet handled. */
1064 switch (code)
1066 CASE_CONVERT:
1067 /* This assignment is under the form "a_1 = (cast) rhs. */
1068 res = follow_ssa_edge_expr (loop, at_stmt, TREE_OPERAND (expr, 0),
1069 halting_phi, evolution_of_loop, limit);
1070 *evolution_of_loop = chrec_convert (type, *evolution_of_loop, at_stmt);
1071 break;
1073 case INTEGER_CST:
1074 /* This assignment is under the form "a_1 = 7". */
1075 res = t_false;
1076 break;
1078 case SSA_NAME:
1079 /* This assignment is under the form: "a_1 = b_2". */
1080 res = follow_ssa_edge
1081 (loop, SSA_NAME_DEF_STMT (expr), halting_phi, evolution_of_loop, limit);
1082 break;
1084 case POINTER_PLUS_EXPR:
1085 case PLUS_EXPR:
1086 case MINUS_EXPR:
1087 /* This case is under the form "rhs0 +- rhs1". */
1088 rhs0 = TREE_OPERAND (expr, 0);
1089 rhs1 = TREE_OPERAND (expr, 1);
1090 type = TREE_TYPE (rhs0);
1091 STRIP_USELESS_TYPE_CONVERSION (rhs0);
1092 STRIP_USELESS_TYPE_CONVERSION (rhs1);
1093 res = follow_ssa_edge_binary (loop, at_stmt, type, rhs0, code, rhs1,
1094 halting_phi, evolution_of_loop, limit);
1095 break;
1097 case ADDR_EXPR:
1098 /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR. */
1099 if (TREE_CODE (TREE_OPERAND (expr, 0)) == MEM_REF)
1101 expr = TREE_OPERAND (expr, 0);
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,
1108 rhs0, POINTER_PLUS_EXPR, rhs1,
1109 halting_phi, evolution_of_loop, limit);
1111 else
1112 res = t_false;
1113 break;
1115 case ASSERT_EXPR:
1116 /* This assignment is of the form: "a_1 = ASSERT_EXPR <a_2, ...>"
1117 It must be handled as a copy assignment of the form a_1 = a_2. */
1118 rhs0 = ASSERT_EXPR_VAR (expr);
1119 if (TREE_CODE (rhs0) == SSA_NAME)
1120 res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0),
1121 halting_phi, evolution_of_loop, limit);
1122 else
1123 res = t_false;
1124 break;
1126 default:
1127 res = t_false;
1128 break;
1131 return res;
1134 /* Follow the ssa edge into the right hand side of an assignment STMT.
1135 Return true if the strongly connected component has been found. */
1137 static t_bool
1138 follow_ssa_edge_in_rhs (struct loop *loop, gimple *stmt,
1139 gphi *halting_phi, tree *evolution_of_loop,
1140 int limit)
1142 enum tree_code code = gimple_assign_rhs_code (stmt);
1143 tree type = gimple_expr_type (stmt), rhs1, rhs2;
1144 t_bool res;
1146 switch (code)
1148 CASE_CONVERT:
1149 /* This assignment is under the form "a_1 = (cast) rhs. */
1150 res = follow_ssa_edge_expr (loop, stmt, gimple_assign_rhs1 (stmt),
1151 halting_phi, evolution_of_loop, limit);
1152 *evolution_of_loop = chrec_convert (type, *evolution_of_loop, stmt);
1153 break;
1155 case POINTER_PLUS_EXPR:
1156 case PLUS_EXPR:
1157 case MINUS_EXPR:
1158 rhs1 = gimple_assign_rhs1 (stmt);
1159 rhs2 = gimple_assign_rhs2 (stmt);
1160 type = TREE_TYPE (rhs1);
1161 res = follow_ssa_edge_binary (loop, stmt, type, rhs1, code, rhs2,
1162 halting_phi, evolution_of_loop, limit);
1163 break;
1165 default:
1166 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1167 res = follow_ssa_edge_expr (loop, stmt, gimple_assign_rhs1 (stmt),
1168 halting_phi, evolution_of_loop, limit);
1169 else
1170 res = t_false;
1171 break;
1174 return res;
1177 /* Checks whether the I-th argument of a PHI comes from a backedge. */
1179 static bool
1180 backedge_phi_arg_p (gphi *phi, int i)
1182 const_edge e = gimple_phi_arg_edge (phi, i);
1184 /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
1185 about updating it anywhere, and this should work as well most of the
1186 time. */
1187 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1188 return true;
1190 return false;
1193 /* Helper function for one branch of the condition-phi-node. Return
1194 true if the strongly connected component has been found following
1195 this path. */
1197 static inline t_bool
1198 follow_ssa_edge_in_condition_phi_branch (int i,
1199 struct loop *loop,
1200 gphi *condition_phi,
1201 gphi *halting_phi,
1202 tree *evolution_of_branch,
1203 tree init_cond, int limit)
1205 tree branch = PHI_ARG_DEF (condition_phi, i);
1206 *evolution_of_branch = chrec_dont_know;
1208 /* Do not follow back edges (they must belong to an irreducible loop, which
1209 we really do not want to worry about). */
1210 if (backedge_phi_arg_p (condition_phi, i))
1211 return t_false;
1213 if (TREE_CODE (branch) == SSA_NAME)
1215 *evolution_of_branch = init_cond;
1216 return follow_ssa_edge (loop, SSA_NAME_DEF_STMT (branch), halting_phi,
1217 evolution_of_branch, limit);
1220 /* This case occurs when one of the condition branches sets
1221 the variable to a constant: i.e. a phi-node like
1222 "a_2 = PHI <a_7(5), 2(6)>;".
1224 FIXME: This case have to be refined correctly:
1225 in some cases it is possible to say something better than
1226 chrec_dont_know, for example using a wrap-around notation. */
1227 return t_false;
1230 /* This function merges the branches of a condition-phi-node in a
1231 loop. */
1233 static t_bool
1234 follow_ssa_edge_in_condition_phi (struct loop *loop,
1235 gphi *condition_phi,
1236 gphi *halting_phi,
1237 tree *evolution_of_loop, int limit)
1239 int i, n;
1240 tree init = *evolution_of_loop;
1241 tree evolution_of_branch;
1242 t_bool res = follow_ssa_edge_in_condition_phi_branch (0, loop, condition_phi,
1243 halting_phi,
1244 &evolution_of_branch,
1245 init, limit);
1246 if (res == t_false || res == t_dont_know)
1247 return res;
1249 *evolution_of_loop = evolution_of_branch;
1251 n = gimple_phi_num_args (condition_phi);
1252 for (i = 1; i < n; i++)
1254 /* Quickly give up when the evolution of one of the branches is
1255 not known. */
1256 if (*evolution_of_loop == chrec_dont_know)
1257 return t_true;
1259 /* Increase the limit by the PHI argument number to avoid exponential
1260 time and memory complexity. */
1261 res = follow_ssa_edge_in_condition_phi_branch (i, loop, condition_phi,
1262 halting_phi,
1263 &evolution_of_branch,
1264 init, limit + i);
1265 if (res == t_false || res == t_dont_know)
1266 return res;
1268 *evolution_of_loop = chrec_merge (*evolution_of_loop,
1269 evolution_of_branch);
1272 return t_true;
1275 /* Follow an SSA edge in an inner loop. It computes the overall
1276 effect of the loop, and following the symbolic initial conditions,
1277 it follows the edges in the parent loop. The inner loop is
1278 considered as a single statement. */
1280 static t_bool
1281 follow_ssa_edge_inner_loop_phi (struct loop *outer_loop,
1282 gphi *loop_phi_node,
1283 gphi *halting_phi,
1284 tree *evolution_of_loop, int limit)
1286 struct loop *loop = loop_containing_stmt (loop_phi_node);
1287 tree ev = analyze_scalar_evolution (loop, PHI_RESULT (loop_phi_node));
1289 /* Sometimes, the inner loop is too difficult to analyze, and the
1290 result of the analysis is a symbolic parameter. */
1291 if (ev == PHI_RESULT (loop_phi_node))
1293 t_bool res = t_false;
1294 int i, n = gimple_phi_num_args (loop_phi_node);
1296 for (i = 0; i < n; i++)
1298 tree arg = PHI_ARG_DEF (loop_phi_node, i);
1299 basic_block bb;
1301 /* Follow the edges that exit the inner loop. */
1302 bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1303 if (!flow_bb_inside_loop_p (loop, bb))
1304 res = follow_ssa_edge_expr (outer_loop, loop_phi_node,
1305 arg, halting_phi,
1306 evolution_of_loop, limit);
1307 if (res == t_true)
1308 break;
1311 /* If the path crosses this loop-phi, give up. */
1312 if (res == t_true)
1313 *evolution_of_loop = chrec_dont_know;
1315 return res;
1318 /* Otherwise, compute the overall effect of the inner loop. */
1319 ev = compute_overall_effect_of_inner_loop (loop, ev);
1320 return follow_ssa_edge_expr (outer_loop, loop_phi_node, ev, halting_phi,
1321 evolution_of_loop, limit);
1324 /* Follow an SSA edge from a loop-phi-node to itself, constructing a
1325 path that is analyzed on the return walk. */
1327 static t_bool
1328 follow_ssa_edge (struct loop *loop, gimple *def, gphi *halting_phi,
1329 tree *evolution_of_loop, int limit)
1331 struct loop *def_loop;
1333 if (gimple_nop_p (def))
1334 return t_false;
1336 /* Give up if the path is longer than the MAX that we allow. */
1337 if (limit > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_COMPLEXITY))
1338 return t_dont_know;
1340 def_loop = loop_containing_stmt (def);
1342 switch (gimple_code (def))
1344 case GIMPLE_PHI:
1345 if (!loop_phi_node_p (def))
1346 /* DEF is a condition-phi-node. Follow the branches, and
1347 record their evolutions. Finally, merge the collected
1348 information and set the approximation to the main
1349 variable. */
1350 return follow_ssa_edge_in_condition_phi
1351 (loop, as_a <gphi *> (def), halting_phi, evolution_of_loop,
1352 limit);
1354 /* When the analyzed phi is the halting_phi, the
1355 depth-first search is over: we have found a path from
1356 the halting_phi to itself in the loop. */
1357 if (def == halting_phi)
1358 return t_true;
1360 /* Otherwise, the evolution of the HALTING_PHI depends
1361 on the evolution of another loop-phi-node, i.e. the
1362 evolution function is a higher degree polynomial. */
1363 if (def_loop == loop)
1364 return t_false;
1366 /* Inner loop. */
1367 if (flow_loop_nested_p (loop, def_loop))
1368 return follow_ssa_edge_inner_loop_phi
1369 (loop, as_a <gphi *> (def), halting_phi, evolution_of_loop,
1370 limit + 1);
1372 /* Outer loop. */
1373 return t_false;
1375 case GIMPLE_ASSIGN:
1376 return follow_ssa_edge_in_rhs (loop, def, halting_phi,
1377 evolution_of_loop, limit);
1379 default:
1380 /* At this level of abstraction, the program is just a set
1381 of GIMPLE_ASSIGNs and PHI_NODEs. In principle there is no
1382 other node to be handled. */
1383 return t_false;
1388 /* Simplify PEELED_CHREC represented by (init_cond, arg) in LOOP.
1389 Handle below case and return the corresponding POLYNOMIAL_CHREC:
1391 # i_17 = PHI <i_13(5), 0(3)>
1392 # _20 = PHI <_5(5), start_4(D)(3)>
1394 i_13 = i_17 + 1;
1395 _5 = start_4(D) + i_13;
1397 Though variable _20 appears as a PEELED_CHREC in the form of
1398 (start_4, _5)_LOOP, it's a POLYNOMIAL_CHREC like {start_4, 1}_LOOP.
1400 See PR41488. */
1402 static tree
1403 simplify_peeled_chrec (struct loop *loop, tree arg, tree init_cond)
1405 aff_tree aff1, aff2;
1406 tree ev, left, right, type, step_val;
1407 hash_map<tree, name_expansion *> *peeled_chrec_map = NULL;
1409 ev = instantiate_parameters (loop, analyze_scalar_evolution (loop, arg));
1410 if (ev == NULL_TREE || TREE_CODE (ev) != POLYNOMIAL_CHREC)
1411 return chrec_dont_know;
1413 left = CHREC_LEFT (ev);
1414 right = CHREC_RIGHT (ev);
1415 type = TREE_TYPE (left);
1416 step_val = chrec_fold_plus (type, init_cond, right);
1418 /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1419 if "left" equals to "init + right". */
1420 if (operand_equal_p (left, step_val, 0))
1422 if (dump_file && (dump_flags & TDF_SCEV))
1423 fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1425 return build_polynomial_chrec (loop->num, init_cond, right);
1428 /* Try harder to check if they are equal. */
1429 tree_to_aff_combination_expand (left, type, &aff1, &peeled_chrec_map);
1430 tree_to_aff_combination_expand (step_val, type, &aff2, &peeled_chrec_map);
1431 free_affine_expand_cache (&peeled_chrec_map);
1432 aff_combination_scale (&aff2, -1);
1433 aff_combination_add (&aff1, &aff2);
1435 /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1436 if "left" equals to "init + right". */
1437 if (aff_combination_zero_p (&aff1))
1439 if (dump_file && (dump_flags & TDF_SCEV))
1440 fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1442 return build_polynomial_chrec (loop->num, init_cond, right);
1444 return chrec_dont_know;
1447 /* Given a LOOP_PHI_NODE, this function determines the evolution
1448 function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */
1450 static tree
1451 analyze_evolution_in_loop (gphi *loop_phi_node,
1452 tree init_cond)
1454 int i, n = gimple_phi_num_args (loop_phi_node);
1455 tree evolution_function = chrec_not_analyzed_yet;
1456 struct loop *loop = loop_containing_stmt (loop_phi_node);
1457 basic_block bb;
1458 static bool simplify_peeled_chrec_p = true;
1460 if (dump_file && (dump_flags & TDF_SCEV))
1462 fprintf (dump_file, "(analyze_evolution_in_loop \n");
1463 fprintf (dump_file, " (loop_phi_node = ");
1464 print_gimple_stmt (dump_file, loop_phi_node, 0, 0);
1465 fprintf (dump_file, ")\n");
1468 for (i = 0; i < n; i++)
1470 tree arg = PHI_ARG_DEF (loop_phi_node, i);
1471 gimple *ssa_chain;
1472 tree ev_fn;
1473 t_bool res;
1475 /* Select the edges that enter the loop body. */
1476 bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1477 if (!flow_bb_inside_loop_p (loop, bb))
1478 continue;
1480 if (TREE_CODE (arg) == SSA_NAME)
1482 bool val = false;
1484 ssa_chain = SSA_NAME_DEF_STMT (arg);
1486 /* Pass in the initial condition to the follow edge function. */
1487 ev_fn = init_cond;
1488 res = follow_ssa_edge (loop, ssa_chain, loop_phi_node, &ev_fn, 0);
1490 /* If ev_fn has no evolution in the inner loop, and the
1491 init_cond is not equal to ev_fn, then we have an
1492 ambiguity between two possible values, as we cannot know
1493 the number of iterations at this point. */
1494 if (TREE_CODE (ev_fn) != POLYNOMIAL_CHREC
1495 && no_evolution_in_loop_p (ev_fn, loop->num, &val) && val
1496 && !operand_equal_p (init_cond, ev_fn, 0))
1497 ev_fn = chrec_dont_know;
1499 else
1500 res = t_false;
1502 /* When it is impossible to go back on the same
1503 loop_phi_node by following the ssa edges, the
1504 evolution is represented by a peeled chrec, i.e. the
1505 first iteration, EV_FN has the value INIT_COND, then
1506 all the other iterations it has the value of ARG.
1507 For the moment, PEELED_CHREC nodes are not built. */
1508 if (res != t_true)
1510 ev_fn = chrec_dont_know;
1511 /* Try to recognize POLYNOMIAL_CHREC which appears in
1512 the form of PEELED_CHREC, but guard the process with
1513 a bool variable to keep the analyzer from infinite
1514 recurrence for real PEELED_RECs. */
1515 if (simplify_peeled_chrec_p && TREE_CODE (arg) == SSA_NAME)
1517 simplify_peeled_chrec_p = false;
1518 ev_fn = simplify_peeled_chrec (loop, arg, init_cond);
1519 simplify_peeled_chrec_p = true;
1523 /* When there are multiple back edges of the loop (which in fact never
1524 happens currently, but nevertheless), merge their evolutions. */
1525 evolution_function = chrec_merge (evolution_function, ev_fn);
1528 if (dump_file && (dump_flags & TDF_SCEV))
1530 fprintf (dump_file, " (evolution_function = ");
1531 print_generic_expr (dump_file, evolution_function, 0);
1532 fprintf (dump_file, "))\n");
1535 return evolution_function;
1538 /* Given a loop-phi-node, return the initial conditions of the
1539 variable on entry of the loop. When the CCP has propagated
1540 constants into the loop-phi-node, the initial condition is
1541 instantiated, otherwise the initial condition is kept symbolic.
1542 This analyzer does not analyze the evolution outside the current
1543 loop, and leaves this task to the on-demand tree reconstructor. */
1545 static tree
1546 analyze_initial_condition (gphi *loop_phi_node)
1548 int i, n;
1549 tree init_cond = chrec_not_analyzed_yet;
1550 struct loop *loop = loop_containing_stmt (loop_phi_node);
1552 if (dump_file && (dump_flags & TDF_SCEV))
1554 fprintf (dump_file, "(analyze_initial_condition \n");
1555 fprintf (dump_file, " (loop_phi_node = \n");
1556 print_gimple_stmt (dump_file, loop_phi_node, 0, 0);
1557 fprintf (dump_file, ")\n");
1560 n = gimple_phi_num_args (loop_phi_node);
1561 for (i = 0; i < n; i++)
1563 tree branch = PHI_ARG_DEF (loop_phi_node, i);
1564 basic_block bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1566 /* When the branch is oriented to the loop's body, it does
1567 not contribute to the initial condition. */
1568 if (flow_bb_inside_loop_p (loop, bb))
1569 continue;
1571 if (init_cond == chrec_not_analyzed_yet)
1573 init_cond = branch;
1574 continue;
1577 if (TREE_CODE (branch) == SSA_NAME)
1579 init_cond = chrec_dont_know;
1580 break;
1583 init_cond = chrec_merge (init_cond, branch);
1586 /* Ooops -- a loop without an entry??? */
1587 if (init_cond == chrec_not_analyzed_yet)
1588 init_cond = chrec_dont_know;
1590 /* During early loop unrolling we do not have fully constant propagated IL.
1591 Handle degenerate PHIs here to not miss important unrollings. */
1592 if (TREE_CODE (init_cond) == SSA_NAME)
1594 gimple *def = SSA_NAME_DEF_STMT (init_cond);
1595 if (gphi *phi = dyn_cast <gphi *> (def))
1597 tree res = degenerate_phi_result (phi);
1598 if (res != NULL_TREE
1599 /* Only allow invariants here, otherwise we may break
1600 loop-closed SSA form. */
1601 && is_gimple_min_invariant (res))
1602 init_cond = res;
1606 if (dump_file && (dump_flags & TDF_SCEV))
1608 fprintf (dump_file, " (init_cond = ");
1609 print_generic_expr (dump_file, init_cond, 0);
1610 fprintf (dump_file, "))\n");
1613 return init_cond;
1616 /* Analyze the scalar evolution for LOOP_PHI_NODE. */
1618 static tree
1619 interpret_loop_phi (struct loop *loop, gphi *loop_phi_node)
1621 tree res;
1622 struct loop *phi_loop = loop_containing_stmt (loop_phi_node);
1623 tree init_cond;
1625 if (phi_loop != loop)
1627 struct loop *subloop;
1628 tree evolution_fn = analyze_scalar_evolution
1629 (phi_loop, PHI_RESULT (loop_phi_node));
1631 /* Dive one level deeper. */
1632 subloop = superloop_at_depth (phi_loop, loop_depth (loop) + 1);
1634 /* Interpret the subloop. */
1635 res = compute_overall_effect_of_inner_loop (subloop, evolution_fn);
1636 return res;
1639 /* Otherwise really interpret the loop phi. */
1640 init_cond = analyze_initial_condition (loop_phi_node);
1641 res = analyze_evolution_in_loop (loop_phi_node, init_cond);
1643 /* Verify we maintained the correct initial condition throughout
1644 possible conversions in the SSA chain. */
1645 if (res != chrec_dont_know)
1647 tree new_init = res;
1648 if (CONVERT_EXPR_P (res)
1649 && TREE_CODE (TREE_OPERAND (res, 0)) == POLYNOMIAL_CHREC)
1650 new_init = fold_convert (TREE_TYPE (res),
1651 CHREC_LEFT (TREE_OPERAND (res, 0)));
1652 else if (TREE_CODE (res) == POLYNOMIAL_CHREC)
1653 new_init = CHREC_LEFT (res);
1654 STRIP_USELESS_TYPE_CONVERSION (new_init);
1655 if (TREE_CODE (new_init) == POLYNOMIAL_CHREC
1656 || !operand_equal_p (init_cond, new_init, 0))
1657 return chrec_dont_know;
1660 return res;
1663 /* This function merges the branches of a condition-phi-node,
1664 contained in the outermost loop, and whose arguments are already
1665 analyzed. */
1667 static tree
1668 interpret_condition_phi (struct loop *loop, gphi *condition_phi)
1670 int i, n = gimple_phi_num_args (condition_phi);
1671 tree res = chrec_not_analyzed_yet;
1673 for (i = 0; i < n; i++)
1675 tree branch_chrec;
1677 if (backedge_phi_arg_p (condition_phi, i))
1679 res = chrec_dont_know;
1680 break;
1683 branch_chrec = analyze_scalar_evolution
1684 (loop, PHI_ARG_DEF (condition_phi, i));
1686 res = chrec_merge (res, branch_chrec);
1689 return res;
1692 /* Interpret the operation RHS1 OP RHS2. If we didn't
1693 analyze this node before, follow the definitions until ending
1694 either on an analyzed GIMPLE_ASSIGN, or on a loop-phi-node. On the
1695 return path, this function propagates evolutions (ala constant copy
1696 propagation). OPND1 is not a GIMPLE expression because we could
1697 analyze the effect of an inner loop: see interpret_loop_phi. */
1699 static tree
1700 interpret_rhs_expr (struct loop *loop, gimple *at_stmt,
1701 tree type, tree rhs1, enum tree_code code, tree rhs2)
1703 tree res, chrec1, chrec2;
1704 gimple *def;
1706 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1708 if (is_gimple_min_invariant (rhs1))
1709 return chrec_convert (type, rhs1, at_stmt);
1711 if (code == SSA_NAME)
1712 return chrec_convert (type, analyze_scalar_evolution (loop, rhs1),
1713 at_stmt);
1715 if (code == ASSERT_EXPR)
1717 rhs1 = ASSERT_EXPR_VAR (rhs1);
1718 return chrec_convert (type, analyze_scalar_evolution (loop, rhs1),
1719 at_stmt);
1723 switch (code)
1725 case ADDR_EXPR:
1726 if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF
1727 || handled_component_p (TREE_OPERAND (rhs1, 0)))
1729 machine_mode mode;
1730 HOST_WIDE_INT bitsize, bitpos;
1731 int unsignedp;
1732 int volatilep = 0;
1733 tree base, offset;
1734 tree chrec3;
1735 tree unitpos;
1737 base = get_inner_reference (TREE_OPERAND (rhs1, 0),
1738 &bitsize, &bitpos, &offset,
1739 &mode, &unsignedp, &volatilep, false);
1741 if (TREE_CODE (base) == MEM_REF)
1743 rhs2 = TREE_OPERAND (base, 1);
1744 rhs1 = TREE_OPERAND (base, 0);
1746 chrec1 = analyze_scalar_evolution (loop, rhs1);
1747 chrec2 = analyze_scalar_evolution (loop, rhs2);
1748 chrec1 = chrec_convert (type, chrec1, at_stmt);
1749 chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt);
1750 chrec1 = instantiate_parameters (loop, chrec1);
1751 chrec2 = instantiate_parameters (loop, chrec2);
1752 res = chrec_fold_plus (type, chrec1, chrec2);
1754 else
1756 chrec1 = analyze_scalar_evolution_for_address_of (loop, base);
1757 chrec1 = chrec_convert (type, chrec1, at_stmt);
1758 res = chrec1;
1761 if (offset != NULL_TREE)
1763 chrec2 = analyze_scalar_evolution (loop, offset);
1764 chrec2 = chrec_convert (TREE_TYPE (offset), chrec2, at_stmt);
1765 chrec2 = instantiate_parameters (loop, chrec2);
1766 res = chrec_fold_plus (type, res, chrec2);
1769 if (bitpos != 0)
1771 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
1773 unitpos = size_int (bitpos / BITS_PER_UNIT);
1774 chrec3 = analyze_scalar_evolution (loop, unitpos);
1775 chrec3 = chrec_convert (TREE_TYPE (unitpos), chrec3, at_stmt);
1776 chrec3 = instantiate_parameters (loop, chrec3);
1777 res = chrec_fold_plus (type, res, chrec3);
1780 else
1781 res = chrec_dont_know;
1782 break;
1784 case POINTER_PLUS_EXPR:
1785 chrec1 = analyze_scalar_evolution (loop, rhs1);
1786 chrec2 = analyze_scalar_evolution (loop, rhs2);
1787 chrec1 = chrec_convert (type, chrec1, at_stmt);
1788 chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt);
1789 chrec1 = instantiate_parameters (loop, chrec1);
1790 chrec2 = instantiate_parameters (loop, chrec2);
1791 res = chrec_fold_plus (type, chrec1, chrec2);
1792 break;
1794 case PLUS_EXPR:
1795 chrec1 = analyze_scalar_evolution (loop, rhs1);
1796 chrec2 = analyze_scalar_evolution (loop, rhs2);
1797 chrec1 = chrec_convert (type, chrec1, at_stmt);
1798 chrec2 = chrec_convert (type, chrec2, at_stmt);
1799 chrec1 = instantiate_parameters (loop, chrec1);
1800 chrec2 = instantiate_parameters (loop, chrec2);
1801 res = chrec_fold_plus (type, chrec1, chrec2);
1802 break;
1804 case MINUS_EXPR:
1805 chrec1 = analyze_scalar_evolution (loop, rhs1);
1806 chrec2 = analyze_scalar_evolution (loop, rhs2);
1807 chrec1 = chrec_convert (type, chrec1, at_stmt);
1808 chrec2 = chrec_convert (type, chrec2, at_stmt);
1809 chrec1 = instantiate_parameters (loop, chrec1);
1810 chrec2 = instantiate_parameters (loop, chrec2);
1811 res = chrec_fold_minus (type, chrec1, chrec2);
1812 break;
1814 case NEGATE_EXPR:
1815 chrec1 = analyze_scalar_evolution (loop, rhs1);
1816 chrec1 = chrec_convert (type, chrec1, at_stmt);
1817 /* TYPE may be integer, real or complex, so use fold_convert. */
1818 chrec1 = instantiate_parameters (loop, chrec1);
1819 res = chrec_fold_multiply (type, chrec1,
1820 fold_convert (type, integer_minus_one_node));
1821 break;
1823 case BIT_NOT_EXPR:
1824 /* Handle ~X as -1 - X. */
1825 chrec1 = analyze_scalar_evolution (loop, rhs1);
1826 chrec1 = chrec_convert (type, chrec1, at_stmt);
1827 chrec1 = instantiate_parameters (loop, chrec1);
1828 res = chrec_fold_minus (type,
1829 fold_convert (type, integer_minus_one_node),
1830 chrec1);
1831 break;
1833 case MULT_EXPR:
1834 chrec1 = analyze_scalar_evolution (loop, rhs1);
1835 chrec2 = analyze_scalar_evolution (loop, rhs2);
1836 chrec1 = chrec_convert (type, chrec1, at_stmt);
1837 chrec2 = chrec_convert (type, chrec2, at_stmt);
1838 chrec1 = instantiate_parameters (loop, chrec1);
1839 chrec2 = instantiate_parameters (loop, chrec2);
1840 res = chrec_fold_multiply (type, chrec1, chrec2);
1841 break;
1843 CASE_CONVERT:
1844 /* In case we have a truncation of a widened operation that in
1845 the truncated type has undefined overflow behavior analyze
1846 the operation done in an unsigned type of the same precision
1847 as the final truncation. We cannot derive a scalar evolution
1848 for the widened operation but for the truncated result. */
1849 if (TREE_CODE (type) == INTEGER_TYPE
1850 && TREE_CODE (TREE_TYPE (rhs1)) == INTEGER_TYPE
1851 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (rhs1))
1852 && TYPE_OVERFLOW_UNDEFINED (type)
1853 && TREE_CODE (rhs1) == SSA_NAME
1854 && (def = SSA_NAME_DEF_STMT (rhs1))
1855 && is_gimple_assign (def)
1856 && TREE_CODE_CLASS (gimple_assign_rhs_code (def)) == tcc_binary
1857 && TREE_CODE (gimple_assign_rhs2 (def)) == INTEGER_CST)
1859 tree utype = unsigned_type_for (type);
1860 chrec1 = interpret_rhs_expr (loop, at_stmt, utype,
1861 gimple_assign_rhs1 (def),
1862 gimple_assign_rhs_code (def),
1863 gimple_assign_rhs2 (def));
1865 else
1866 chrec1 = analyze_scalar_evolution (loop, rhs1);
1867 res = chrec_convert (type, chrec1, at_stmt);
1868 break;
1870 default:
1871 res = chrec_dont_know;
1872 break;
1875 return res;
1878 /* Interpret the expression EXPR. */
1880 static tree
1881 interpret_expr (struct loop *loop, gimple *at_stmt, tree expr)
1883 enum tree_code code;
1884 tree type = TREE_TYPE (expr), op0, op1;
1886 if (automatically_generated_chrec_p (expr))
1887 return expr;
1889 if (TREE_CODE (expr) == POLYNOMIAL_CHREC
1890 || get_gimple_rhs_class (TREE_CODE (expr)) == GIMPLE_TERNARY_RHS)
1891 return chrec_dont_know;
1893 extract_ops_from_tree (expr, &code, &op0, &op1);
1895 return interpret_rhs_expr (loop, at_stmt, type,
1896 op0, code, op1);
1899 /* Interpret the rhs of the assignment STMT. */
1901 static tree
1902 interpret_gimple_assign (struct loop *loop, gimple *stmt)
1904 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
1905 enum tree_code code = gimple_assign_rhs_code (stmt);
1907 return interpret_rhs_expr (loop, stmt, type,
1908 gimple_assign_rhs1 (stmt), code,
1909 gimple_assign_rhs2 (stmt));
1914 /* This section contains all the entry points:
1915 - number_of_iterations_in_loop,
1916 - analyze_scalar_evolution,
1917 - instantiate_parameters.
1920 /* Compute and return the evolution function in WRTO_LOOP, the nearest
1921 common ancestor of DEF_LOOP and USE_LOOP. */
1923 static tree
1924 compute_scalar_evolution_in_loop (struct loop *wrto_loop,
1925 struct loop *def_loop,
1926 tree ev)
1928 bool val;
1929 tree res;
1931 if (def_loop == wrto_loop)
1932 return ev;
1934 def_loop = superloop_at_depth (def_loop, loop_depth (wrto_loop) + 1);
1935 res = compute_overall_effect_of_inner_loop (def_loop, ev);
1937 if (no_evolution_in_loop_p (res, wrto_loop->num, &val) && val)
1938 return res;
1940 return analyze_scalar_evolution_1 (wrto_loop, res, chrec_not_analyzed_yet);
1943 /* Helper recursive function. */
1945 static tree
1946 analyze_scalar_evolution_1 (struct loop *loop, tree var, tree res)
1948 tree type = TREE_TYPE (var);
1949 gimple *def;
1950 basic_block bb;
1951 struct loop *def_loop;
1953 if (loop == NULL || TREE_CODE (type) == VECTOR_TYPE)
1954 return chrec_dont_know;
1956 if (TREE_CODE (var) != SSA_NAME)
1957 return interpret_expr (loop, NULL, var);
1959 def = SSA_NAME_DEF_STMT (var);
1960 bb = gimple_bb (def);
1961 def_loop = bb ? bb->loop_father : NULL;
1963 if (bb == NULL
1964 || !flow_bb_inside_loop_p (loop, bb))
1966 /* Keep the symbolic form. */
1967 res = var;
1968 goto set_and_end;
1971 if (res != chrec_not_analyzed_yet)
1973 if (loop != bb->loop_father)
1974 res = compute_scalar_evolution_in_loop
1975 (find_common_loop (loop, bb->loop_father), bb->loop_father, res);
1977 goto set_and_end;
1980 if (loop != def_loop)
1982 res = analyze_scalar_evolution_1 (def_loop, var, chrec_not_analyzed_yet);
1983 res = compute_scalar_evolution_in_loop (loop, def_loop, res);
1985 goto set_and_end;
1988 switch (gimple_code (def))
1990 case GIMPLE_ASSIGN:
1991 res = interpret_gimple_assign (loop, def);
1992 break;
1994 case GIMPLE_PHI:
1995 if (loop_phi_node_p (def))
1996 res = interpret_loop_phi (loop, as_a <gphi *> (def));
1997 else
1998 res = interpret_condition_phi (loop, as_a <gphi *> (def));
1999 break;
2001 default:
2002 res = chrec_dont_know;
2003 break;
2006 set_and_end:
2008 /* Keep the symbolic form. */
2009 if (res == chrec_dont_know)
2010 res = var;
2012 if (loop == def_loop)
2013 set_scalar_evolution (block_before_loop (loop), var, res);
2015 return res;
2018 /* Analyzes and returns the scalar evolution of the ssa_name VAR in
2019 LOOP. LOOP is the loop in which the variable is used.
2021 Example of use: having a pointer VAR to a SSA_NAME node, STMT a
2022 pointer to the statement that uses this variable, in order to
2023 determine the evolution function of the variable, use the following
2024 calls:
2026 loop_p loop = loop_containing_stmt (stmt);
2027 tree chrec_with_symbols = analyze_scalar_evolution (loop, var);
2028 tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols);
2031 tree
2032 analyze_scalar_evolution (struct loop *loop, tree var)
2034 tree res;
2036 if (dump_file && (dump_flags & TDF_SCEV))
2038 fprintf (dump_file, "(analyze_scalar_evolution \n");
2039 fprintf (dump_file, " (loop_nb = %d)\n", loop->num);
2040 fprintf (dump_file, " (scalar = ");
2041 print_generic_expr (dump_file, var, 0);
2042 fprintf (dump_file, ")\n");
2045 res = get_scalar_evolution (block_before_loop (loop), var);
2046 res = analyze_scalar_evolution_1 (loop, var, res);
2048 if (dump_file && (dump_flags & TDF_SCEV))
2049 fprintf (dump_file, ")\n");
2051 return res;
2054 /* Analyzes and returns the scalar evolution of VAR address in LOOP. */
2056 static tree
2057 analyze_scalar_evolution_for_address_of (struct loop *loop, tree var)
2059 return analyze_scalar_evolution (loop, build_fold_addr_expr (var));
2062 /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
2063 WRTO_LOOP (which should be a superloop of USE_LOOP)
2065 FOLDED_CASTS is set to true if resolve_mixers used
2066 chrec_convert_aggressive (TODO -- not really, we are way too conservative
2067 at the moment in order to keep things simple).
2069 To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following
2070 example:
2072 for (i = 0; i < 100; i++) -- loop 1
2074 for (j = 0; j < 100; j++) -- loop 2
2076 k1 = i;
2077 k2 = j;
2079 use2 (k1, k2);
2081 for (t = 0; t < 100; t++) -- loop 3
2082 use3 (k1, k2);
2085 use1 (k1, k2);
2088 Both k1 and k2 are invariants in loop3, thus
2089 analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1
2090 analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2
2092 As they are invariant, it does not matter whether we consider their
2093 usage in loop 3 or loop 2, hence
2094 analyze_scalar_evolution_in_loop (loop2, loop3, k1) =
2095 analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i
2096 analyze_scalar_evolution_in_loop (loop2, loop3, k2) =
2097 analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2
2099 Similarly for their evolutions with respect to loop 1. The values of K2
2100 in the use in loop 2 vary independently on loop 1, thus we cannot express
2101 the evolution with respect to loop 1:
2102 analyze_scalar_evolution_in_loop (loop1, loop3, k1) =
2103 analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1
2104 analyze_scalar_evolution_in_loop (loop1, loop3, k2) =
2105 analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know
2107 The value of k2 in the use in loop 1 is known, though:
2108 analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1
2109 analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100
2112 static tree
2113 analyze_scalar_evolution_in_loop (struct loop *wrto_loop, struct loop *use_loop,
2114 tree version, bool *folded_casts)
2116 bool val = false;
2117 tree ev = version, tmp;
2119 /* We cannot just do
2121 tmp = analyze_scalar_evolution (use_loop, version);
2122 ev = resolve_mixers (wrto_loop, tmp, folded_casts);
2124 as resolve_mixers would query the scalar evolution with respect to
2125 wrto_loop. For example, in the situation described in the function
2126 comment, suppose that wrto_loop = loop1, use_loop = loop3 and
2127 version = k2. Then
2129 analyze_scalar_evolution (use_loop, version) = k2
2131 and resolve_mixers (loop1, k2, folded_casts) finds that the value of
2132 k2 in loop 1 is 100, which is a wrong result, since we are interested
2133 in the value in loop 3.
2135 Instead, we need to proceed from use_loop to wrto_loop loop by loop,
2136 each time checking that there is no evolution in the inner loop. */
2138 if (folded_casts)
2139 *folded_casts = false;
2140 while (1)
2142 tmp = analyze_scalar_evolution (use_loop, ev);
2143 ev = resolve_mixers (use_loop, tmp, folded_casts);
2145 if (use_loop == wrto_loop)
2146 return ev;
2148 /* If the value of the use changes in the inner loop, we cannot express
2149 its value in the outer loop (we might try to return interval chrec,
2150 but we do not have a user for it anyway) */
2151 if (!no_evolution_in_loop_p (ev, use_loop->num, &val)
2152 || !val)
2153 return chrec_dont_know;
2155 use_loop = loop_outer (use_loop);
2160 /* Hashtable helpers for a temporary hash-table used when
2161 instantiating a CHREC or resolving mixers. For this use
2162 instantiated_below is always the same. */
2164 struct instantiate_cache_type
2166 htab_t map;
2167 vec<scev_info_str> entries;
2169 instantiate_cache_type () : map (NULL), entries (vNULL) {}
2170 ~instantiate_cache_type ();
2171 tree get (unsigned slot) { return entries[slot].chrec; }
2172 void set (unsigned slot, tree chrec) { entries[slot].chrec = chrec; }
2175 instantiate_cache_type::~instantiate_cache_type ()
2177 if (map != NULL)
2179 htab_delete (map);
2180 entries.release ();
2184 /* Cache to avoid infinite recursion when instantiating an SSA name.
2185 Live during the outermost instantiate_scev or resolve_mixers call. */
2186 static instantiate_cache_type *global_cache;
2188 /* Computes a hash function for database element ELT. */
2190 static inline hashval_t
2191 hash_idx_scev_info (const void *elt_)
2193 unsigned idx = ((size_t) elt_) - 2;
2194 return scev_info_hasher::hash (&global_cache->entries[idx]);
2197 /* Compares database elements E1 and E2. */
2199 static inline int
2200 eq_idx_scev_info (const void *e1, const void *e2)
2202 unsigned idx1 = ((size_t) e1) - 2;
2203 return scev_info_hasher::equal (&global_cache->entries[idx1],
2204 (const scev_info_str *) e2);
2207 /* Returns from CACHE the slot number of the cached chrec for NAME. */
2209 static unsigned
2210 get_instantiated_value_entry (instantiate_cache_type &cache,
2211 tree name, basic_block instantiate_below)
2213 if (!cache.map)
2215 cache.map = htab_create (10, hash_idx_scev_info, eq_idx_scev_info, NULL);
2216 cache.entries.create (10);
2219 scev_info_str e;
2220 e.name_version = SSA_NAME_VERSION (name);
2221 e.instantiated_below = instantiate_below->index;
2222 void **slot = htab_find_slot_with_hash (cache.map, &e,
2223 scev_info_hasher::hash (&e), INSERT);
2224 if (!*slot)
2226 e.chrec = chrec_not_analyzed_yet;
2227 *slot = (void *)(size_t)(cache.entries.length () + 2);
2228 cache.entries.safe_push (e);
2231 return ((size_t)*slot) - 2;
2235 /* Return the closed_loop_phi node for VAR. If there is none, return
2236 NULL_TREE. */
2238 static tree
2239 loop_closed_phi_def (tree var)
2241 struct loop *loop;
2242 edge exit;
2243 gphi *phi;
2244 gphi_iterator psi;
2246 if (var == NULL_TREE
2247 || TREE_CODE (var) != SSA_NAME)
2248 return NULL_TREE;
2250 loop = loop_containing_stmt (SSA_NAME_DEF_STMT (var));
2251 exit = single_exit (loop);
2252 if (!exit)
2253 return NULL_TREE;
2255 for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi))
2257 phi = psi.phi ();
2258 if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == var)
2259 return PHI_RESULT (phi);
2262 return NULL_TREE;
2265 static tree instantiate_scev_r (basic_block, struct loop *, struct loop *,
2266 tree, bool *, int);
2268 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2269 and EVOLUTION_LOOP, that were left under a symbolic form.
2271 CHREC is an SSA_NAME to be instantiated.
2273 CACHE is the cache of already instantiated values.
2275 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2276 conversions that may wrap in signed/pointer type are folded, as long
2277 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2278 then we don't do such fold.
2280 SIZE_EXPR is used for computing the size of the expression to be
2281 instantiated, and to stop if it exceeds some limit. */
2283 static tree
2284 instantiate_scev_name (basic_block instantiate_below,
2285 struct loop *evolution_loop, struct loop *inner_loop,
2286 tree chrec,
2287 bool *fold_conversions,
2288 int size_expr)
2290 tree res;
2291 struct loop *def_loop;
2292 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (chrec));
2294 /* A parameter (or loop invariant and we do not want to include
2295 evolutions in outer loops), nothing to do. */
2296 if (!def_bb
2297 || loop_depth (def_bb->loop_father) == 0
2298 || dominated_by_p (CDI_DOMINATORS, instantiate_below, def_bb))
2299 return chrec;
2301 /* We cache the value of instantiated variable to avoid exponential
2302 time complexity due to reevaluations. We also store the convenient
2303 value in the cache in order to prevent infinite recursion -- we do
2304 not want to instantiate the SSA_NAME if it is in a mixer
2305 structure. This is used for avoiding the instantiation of
2306 recursively defined functions, such as:
2308 | a_2 -> {0, +, 1, +, a_2}_1 */
2310 unsigned si = get_instantiated_value_entry (*global_cache,
2311 chrec, instantiate_below);
2312 if (global_cache->get (si) != chrec_not_analyzed_yet)
2313 return global_cache->get (si);
2315 /* On recursion return chrec_dont_know. */
2316 global_cache->set (si, chrec_dont_know);
2318 def_loop = find_common_loop (evolution_loop, def_bb->loop_father);
2320 /* If the analysis yields a parametric chrec, instantiate the
2321 result again. */
2322 res = analyze_scalar_evolution (def_loop, chrec);
2324 /* Don't instantiate default definitions. */
2325 if (TREE_CODE (res) == SSA_NAME
2326 && SSA_NAME_IS_DEFAULT_DEF (res))
2329 /* Don't instantiate loop-closed-ssa phi nodes. */
2330 else if (TREE_CODE (res) == SSA_NAME
2331 && loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res)))
2332 > loop_depth (def_loop))
2334 if (res == chrec)
2335 res = loop_closed_phi_def (chrec);
2336 else
2337 res = chrec;
2339 /* When there is no loop_closed_phi_def, it means that the
2340 variable is not used after the loop: try to still compute the
2341 value of the variable when exiting the loop. */
2342 if (res == NULL_TREE)
2344 loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (chrec));
2345 res = analyze_scalar_evolution (loop, chrec);
2346 res = compute_overall_effect_of_inner_loop (loop, res);
2347 res = instantiate_scev_r (instantiate_below, evolution_loop,
2348 inner_loop, res,
2349 fold_conversions, size_expr);
2351 else if (!dominated_by_p (CDI_DOMINATORS, instantiate_below,
2352 gimple_bb (SSA_NAME_DEF_STMT (res))))
2353 res = chrec_dont_know;
2356 else if (res != chrec_dont_know)
2358 if (inner_loop
2359 && def_bb->loop_father != inner_loop
2360 && !flow_loop_nested_p (def_bb->loop_father, inner_loop))
2361 /* ??? We could try to compute the overall effect of the loop here. */
2362 res = chrec_dont_know;
2363 else
2364 res = instantiate_scev_r (instantiate_below, evolution_loop,
2365 inner_loop, res,
2366 fold_conversions, size_expr);
2369 /* Store the correct value to the cache. */
2370 global_cache->set (si, res);
2371 return res;
2374 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2375 and EVOLUTION_LOOP, that were left under a symbolic form.
2377 CHREC is a polynomial chain of recurrence to be instantiated.
2379 CACHE is the cache of already instantiated values.
2381 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2382 conversions that may wrap in signed/pointer type are folded, as long
2383 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2384 then we don't do such fold.
2386 SIZE_EXPR is used for computing the size of the expression to be
2387 instantiated, and to stop if it exceeds some limit. */
2389 static tree
2390 instantiate_scev_poly (basic_block instantiate_below,
2391 struct loop *evolution_loop, struct loop *,
2392 tree chrec, bool *fold_conversions, int size_expr)
2394 tree op1;
2395 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2396 get_chrec_loop (chrec),
2397 CHREC_LEFT (chrec), fold_conversions,
2398 size_expr);
2399 if (op0 == chrec_dont_know)
2400 return chrec_dont_know;
2402 op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2403 get_chrec_loop (chrec),
2404 CHREC_RIGHT (chrec), fold_conversions,
2405 size_expr);
2406 if (op1 == chrec_dont_know)
2407 return chrec_dont_know;
2409 if (CHREC_LEFT (chrec) != op0
2410 || CHREC_RIGHT (chrec) != op1)
2412 op1 = chrec_convert_rhs (chrec_type (op0), op1, NULL);
2413 chrec = build_polynomial_chrec (CHREC_VARIABLE (chrec), op0, op1);
2416 return chrec;
2419 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2420 and EVOLUTION_LOOP, that were left under a symbolic form.
2422 "C0 CODE C1" is a binary expression of type TYPE to be instantiated.
2424 CACHE is the cache of already instantiated values.
2426 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2427 conversions that may wrap in signed/pointer type are folded, as long
2428 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2429 then we don't do such fold.
2431 SIZE_EXPR is used for computing the size of the expression to be
2432 instantiated, and to stop if it exceeds some limit. */
2434 static tree
2435 instantiate_scev_binary (basic_block instantiate_below,
2436 struct loop *evolution_loop, struct loop *inner_loop,
2437 tree chrec, enum tree_code code,
2438 tree type, tree c0, tree c1,
2439 bool *fold_conversions, int size_expr)
2441 tree op1;
2442 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
2443 c0, fold_conversions, size_expr);
2444 if (op0 == chrec_dont_know)
2445 return chrec_dont_know;
2447 op1 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
2448 c1, fold_conversions, size_expr);
2449 if (op1 == chrec_dont_know)
2450 return chrec_dont_know;
2452 if (c0 != op0
2453 || c1 != op1)
2455 op0 = chrec_convert (type, op0, NULL);
2456 op1 = chrec_convert_rhs (type, op1, NULL);
2458 switch (code)
2460 case POINTER_PLUS_EXPR:
2461 case PLUS_EXPR:
2462 return chrec_fold_plus (type, op0, op1);
2464 case MINUS_EXPR:
2465 return chrec_fold_minus (type, op0, op1);
2467 case MULT_EXPR:
2468 return chrec_fold_multiply (type, op0, op1);
2470 default:
2471 gcc_unreachable ();
2475 return chrec ? chrec : fold_build2 (code, type, c0, c1);
2478 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2479 and EVOLUTION_LOOP, that were left under a symbolic form.
2481 "CHREC" is an array reference to be instantiated.
2483 CACHE is the cache of already instantiated values.
2485 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2486 conversions that may wrap in signed/pointer type are folded, as long
2487 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2488 then we don't do such fold.
2490 SIZE_EXPR is used for computing the size of the expression to be
2491 instantiated, and to stop if it exceeds some limit. */
2493 static tree
2494 instantiate_array_ref (basic_block instantiate_below,
2495 struct loop *evolution_loop, struct loop *inner_loop,
2496 tree chrec, bool *fold_conversions, int size_expr)
2498 tree res;
2499 tree index = TREE_OPERAND (chrec, 1);
2500 tree op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2501 inner_loop, index,
2502 fold_conversions, size_expr);
2504 if (op1 == chrec_dont_know)
2505 return chrec_dont_know;
2507 if (chrec && op1 == index)
2508 return chrec;
2510 res = unshare_expr (chrec);
2511 TREE_OPERAND (res, 1) = op1;
2512 return res;
2515 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2516 and EVOLUTION_LOOP, that were left under a symbolic form.
2518 "CHREC" that stands for a convert expression "(TYPE) OP" is to be
2519 instantiated.
2521 CACHE is the cache of already instantiated values.
2523 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2524 conversions that may wrap in signed/pointer type are folded, as long
2525 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2526 then we don't do such fold.
2528 SIZE_EXPR is used for computing the size of the expression to be
2529 instantiated, and to stop if it exceeds some limit. */
2531 static tree
2532 instantiate_scev_convert (basic_block instantiate_below,
2533 struct loop *evolution_loop, struct loop *inner_loop,
2534 tree chrec, tree type, tree op,
2535 bool *fold_conversions, int size_expr)
2537 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2538 inner_loop, op,
2539 fold_conversions, size_expr);
2541 if (op0 == chrec_dont_know)
2542 return chrec_dont_know;
2544 if (fold_conversions)
2546 tree tmp = chrec_convert_aggressive (type, op0, fold_conversions);
2547 if (tmp)
2548 return tmp;
2550 /* If we used chrec_convert_aggressive, we can no longer assume that
2551 signed chrecs do not overflow, as chrec_convert does, so avoid
2552 calling it in that case. */
2553 if (*fold_conversions)
2555 if (chrec && op0 == op)
2556 return chrec;
2558 return fold_convert (type, op0);
2562 return chrec_convert (type, op0, NULL);
2565 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2566 and EVOLUTION_LOOP, that were left under a symbolic form.
2568 CHREC is a BIT_NOT_EXPR or a NEGATE_EXPR expression to be instantiated.
2569 Handle ~X as -1 - X.
2570 Handle -X as -1 * X.
2572 CACHE is the cache of already instantiated values.
2574 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2575 conversions that may wrap in signed/pointer type are folded, as long
2576 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2577 then we don't do such fold.
2579 SIZE_EXPR is used for computing the size of the expression to be
2580 instantiated, and to stop if it exceeds some limit. */
2582 static tree
2583 instantiate_scev_not (basic_block instantiate_below,
2584 struct loop *evolution_loop, struct loop *inner_loop,
2585 tree chrec,
2586 enum tree_code code, tree type, tree op,
2587 bool *fold_conversions, int size_expr)
2589 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2590 inner_loop, op,
2591 fold_conversions, size_expr);
2593 if (op0 == chrec_dont_know)
2594 return chrec_dont_know;
2596 if (op != op0)
2598 op0 = chrec_convert (type, op0, NULL);
2600 switch (code)
2602 case BIT_NOT_EXPR:
2603 return chrec_fold_minus
2604 (type, fold_convert (type, integer_minus_one_node), op0);
2606 case NEGATE_EXPR:
2607 return chrec_fold_multiply
2608 (type, fold_convert (type, integer_minus_one_node), op0);
2610 default:
2611 gcc_unreachable ();
2615 return chrec ? chrec : fold_build1 (code, type, op0);
2618 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2619 and EVOLUTION_LOOP, that were left under a symbolic form.
2621 CHREC is an expression with 3 operands to be instantiated.
2623 CACHE is the cache of already instantiated values.
2625 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2626 conversions that may wrap in signed/pointer type are folded, as long
2627 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2628 then we don't do such fold.
2630 SIZE_EXPR is used for computing the size of the expression to be
2631 instantiated, and to stop if it exceeds some limit. */
2633 static tree
2634 instantiate_scev_3 (basic_block instantiate_below,
2635 struct loop *evolution_loop, struct loop *inner_loop,
2636 tree chrec,
2637 bool *fold_conversions, int size_expr)
2639 tree op1, op2;
2640 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2641 inner_loop, TREE_OPERAND (chrec, 0),
2642 fold_conversions, size_expr);
2643 if (op0 == chrec_dont_know)
2644 return chrec_dont_know;
2646 op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2647 inner_loop, TREE_OPERAND (chrec, 1),
2648 fold_conversions, size_expr);
2649 if (op1 == chrec_dont_know)
2650 return chrec_dont_know;
2652 op2 = instantiate_scev_r (instantiate_below, evolution_loop,
2653 inner_loop, TREE_OPERAND (chrec, 2),
2654 fold_conversions, size_expr);
2655 if (op2 == chrec_dont_know)
2656 return chrec_dont_know;
2658 if (op0 == TREE_OPERAND (chrec, 0)
2659 && op1 == TREE_OPERAND (chrec, 1)
2660 && op2 == TREE_OPERAND (chrec, 2))
2661 return chrec;
2663 return fold_build3 (TREE_CODE (chrec),
2664 TREE_TYPE (chrec), op0, op1, op2);
2667 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2668 and EVOLUTION_LOOP, that were left under a symbolic form.
2670 CHREC is an expression with 2 operands to be instantiated.
2672 CACHE is the cache of already instantiated values.
2674 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2675 conversions that may wrap in signed/pointer type are folded, as long
2676 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2677 then we don't do such fold.
2679 SIZE_EXPR is used for computing the size of the expression to be
2680 instantiated, and to stop if it exceeds some limit. */
2682 static tree
2683 instantiate_scev_2 (basic_block instantiate_below,
2684 struct loop *evolution_loop, struct loop *inner_loop,
2685 tree chrec,
2686 bool *fold_conversions, int size_expr)
2688 tree op1;
2689 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2690 inner_loop, TREE_OPERAND (chrec, 0),
2691 fold_conversions, size_expr);
2692 if (op0 == chrec_dont_know)
2693 return chrec_dont_know;
2695 op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2696 inner_loop, TREE_OPERAND (chrec, 1),
2697 fold_conversions, size_expr);
2698 if (op1 == chrec_dont_know)
2699 return chrec_dont_know;
2701 if (op0 == TREE_OPERAND (chrec, 0)
2702 && op1 == TREE_OPERAND (chrec, 1))
2703 return chrec;
2705 return fold_build2 (TREE_CODE (chrec), TREE_TYPE (chrec), op0, op1);
2708 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2709 and EVOLUTION_LOOP, that were left under a symbolic form.
2711 CHREC is an expression with 2 operands to be instantiated.
2713 CACHE is the cache of already instantiated values.
2715 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2716 conversions that may wrap in signed/pointer type are folded, as long
2717 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2718 then we don't do such fold.
2720 SIZE_EXPR is used for computing the size of the expression to be
2721 instantiated, and to stop if it exceeds some limit. */
2723 static tree
2724 instantiate_scev_1 (basic_block instantiate_below,
2725 struct loop *evolution_loop, struct loop *inner_loop,
2726 tree chrec,
2727 bool *fold_conversions, int size_expr)
2729 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2730 inner_loop, TREE_OPERAND (chrec, 0),
2731 fold_conversions, size_expr);
2733 if (op0 == chrec_dont_know)
2734 return chrec_dont_know;
2736 if (op0 == TREE_OPERAND (chrec, 0))
2737 return chrec;
2739 return fold_build1 (TREE_CODE (chrec), TREE_TYPE (chrec), op0);
2742 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2743 and EVOLUTION_LOOP, that were left under a symbolic form.
2745 CHREC is the scalar evolution to instantiate.
2747 CACHE is the cache of already instantiated values.
2749 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2750 conversions that may wrap in signed/pointer type are folded, as long
2751 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2752 then we don't do such fold.
2754 SIZE_EXPR is used for computing the size of the expression to be
2755 instantiated, and to stop if it exceeds some limit. */
2757 static tree
2758 instantiate_scev_r (basic_block instantiate_below,
2759 struct loop *evolution_loop, struct loop *inner_loop,
2760 tree chrec,
2761 bool *fold_conversions, int size_expr)
2763 /* Give up if the expression is larger than the MAX that we allow. */
2764 if (size_expr++ > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE))
2765 return chrec_dont_know;
2767 if (chrec == NULL_TREE
2768 || automatically_generated_chrec_p (chrec)
2769 || is_gimple_min_invariant (chrec))
2770 return chrec;
2772 switch (TREE_CODE (chrec))
2774 case SSA_NAME:
2775 return instantiate_scev_name (instantiate_below, evolution_loop,
2776 inner_loop, chrec,
2777 fold_conversions, size_expr);
2779 case POLYNOMIAL_CHREC:
2780 return instantiate_scev_poly (instantiate_below, evolution_loop,
2781 inner_loop, chrec,
2782 fold_conversions, size_expr);
2784 case POINTER_PLUS_EXPR:
2785 case PLUS_EXPR:
2786 case MINUS_EXPR:
2787 case MULT_EXPR:
2788 return instantiate_scev_binary (instantiate_below, evolution_loop,
2789 inner_loop, chrec,
2790 TREE_CODE (chrec), chrec_type (chrec),
2791 TREE_OPERAND (chrec, 0),
2792 TREE_OPERAND (chrec, 1),
2793 fold_conversions, size_expr);
2795 CASE_CONVERT:
2796 return instantiate_scev_convert (instantiate_below, evolution_loop,
2797 inner_loop, chrec,
2798 TREE_TYPE (chrec), TREE_OPERAND (chrec, 0),
2799 fold_conversions, size_expr);
2801 case NEGATE_EXPR:
2802 case BIT_NOT_EXPR:
2803 return instantiate_scev_not (instantiate_below, evolution_loop,
2804 inner_loop, chrec,
2805 TREE_CODE (chrec), TREE_TYPE (chrec),
2806 TREE_OPERAND (chrec, 0),
2807 fold_conversions, size_expr);
2809 case ADDR_EXPR:
2810 case SCEV_NOT_KNOWN:
2811 return chrec_dont_know;
2813 case SCEV_KNOWN:
2814 return chrec_known;
2816 case ARRAY_REF:
2817 return instantiate_array_ref (instantiate_below, evolution_loop,
2818 inner_loop, chrec,
2819 fold_conversions, size_expr);
2821 default:
2822 break;
2825 if (VL_EXP_CLASS_P (chrec))
2826 return chrec_dont_know;
2828 switch (TREE_CODE_LENGTH (TREE_CODE (chrec)))
2830 case 3:
2831 return instantiate_scev_3 (instantiate_below, evolution_loop,
2832 inner_loop, chrec,
2833 fold_conversions, size_expr);
2835 case 2:
2836 return instantiate_scev_2 (instantiate_below, evolution_loop,
2837 inner_loop, chrec,
2838 fold_conversions, size_expr);
2840 case 1:
2841 return instantiate_scev_1 (instantiate_below, evolution_loop,
2842 inner_loop, chrec,
2843 fold_conversions, size_expr);
2845 case 0:
2846 return chrec;
2848 default:
2849 break;
2852 /* Too complicated to handle. */
2853 return chrec_dont_know;
2856 /* Analyze all the parameters of the chrec that were left under a
2857 symbolic form. INSTANTIATE_BELOW is the basic block that stops the
2858 recursive instantiation of parameters: a parameter is a variable
2859 that is defined in a basic block that dominates INSTANTIATE_BELOW or
2860 a function parameter. */
2862 tree
2863 instantiate_scev (basic_block instantiate_below, struct loop *evolution_loop,
2864 tree chrec)
2866 tree res;
2868 if (dump_file && (dump_flags & TDF_SCEV))
2870 fprintf (dump_file, "(instantiate_scev \n");
2871 fprintf (dump_file, " (instantiate_below = %d)\n", instantiate_below->index);
2872 fprintf (dump_file, " (evolution_loop = %d)\n", evolution_loop->num);
2873 fprintf (dump_file, " (chrec = ");
2874 print_generic_expr (dump_file, chrec, 0);
2875 fprintf (dump_file, ")\n");
2878 bool destr = false;
2879 if (!global_cache)
2881 global_cache = new instantiate_cache_type;
2882 destr = true;
2885 res = instantiate_scev_r (instantiate_below, evolution_loop,
2886 NULL, chrec, NULL, 0);
2888 if (destr)
2890 delete global_cache;
2891 global_cache = NULL;
2894 if (dump_file && (dump_flags & TDF_SCEV))
2896 fprintf (dump_file, " (res = ");
2897 print_generic_expr (dump_file, res, 0);
2898 fprintf (dump_file, "))\n");
2901 return res;
2904 /* Similar to instantiate_parameters, but does not introduce the
2905 evolutions in outer loops for LOOP invariants in CHREC, and does not
2906 care about causing overflows, as long as they do not affect value
2907 of an expression. */
2909 tree
2910 resolve_mixers (struct loop *loop, tree chrec, bool *folded_casts)
2912 bool destr = false;
2913 bool fold_conversions = false;
2914 if (!global_cache)
2916 global_cache = new instantiate_cache_type;
2917 destr = true;
2920 tree ret = instantiate_scev_r (block_before_loop (loop), loop, NULL,
2921 chrec, &fold_conversions, 0);
2923 if (folded_casts && !*folded_casts)
2924 *folded_casts = fold_conversions;
2926 if (destr)
2928 delete global_cache;
2929 global_cache = NULL;
2932 return ret;
2935 /* Entry point for the analysis of the number of iterations pass.
2936 This function tries to safely approximate the number of iterations
2937 the loop will run. When this property is not decidable at compile
2938 time, the result is chrec_dont_know. Otherwise the result is a
2939 scalar or a symbolic parameter. When the number of iterations may
2940 be equal to zero and the property cannot be determined at compile
2941 time, the result is a COND_EXPR that represents in a symbolic form
2942 the conditions under which the number of iterations is not zero.
2944 Example of analysis: suppose that the loop has an exit condition:
2946 "if (b > 49) goto end_loop;"
2948 and that in a previous analysis we have determined that the
2949 variable 'b' has an evolution function:
2951 "EF = {23, +, 5}_2".
2953 When we evaluate the function at the point 5, i.e. the value of the
2954 variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
2955 and EF (6) = 53. In this case the value of 'b' on exit is '53' and
2956 the loop body has been executed 6 times. */
2958 tree
2959 number_of_latch_executions (struct loop *loop)
2961 edge exit;
2962 struct tree_niter_desc niter_desc;
2963 tree may_be_zero;
2964 tree res;
2966 /* Determine whether the number of iterations in loop has already
2967 been computed. */
2968 res = loop->nb_iterations;
2969 if (res)
2970 return res;
2972 may_be_zero = NULL_TREE;
2974 if (dump_file && (dump_flags & TDF_SCEV))
2975 fprintf (dump_file, "(number_of_iterations_in_loop = \n");
2977 res = chrec_dont_know;
2978 exit = single_exit (loop);
2980 if (exit && number_of_iterations_exit (loop, exit, &niter_desc, false))
2982 may_be_zero = niter_desc.may_be_zero;
2983 res = niter_desc.niter;
2986 if (res == chrec_dont_know
2987 || !may_be_zero
2988 || integer_zerop (may_be_zero))
2990 else if (integer_nonzerop (may_be_zero))
2991 res = build_int_cst (TREE_TYPE (res), 0);
2993 else if (COMPARISON_CLASS_P (may_be_zero))
2994 res = fold_build3 (COND_EXPR, TREE_TYPE (res), may_be_zero,
2995 build_int_cst (TREE_TYPE (res), 0), res);
2996 else
2997 res = chrec_dont_know;
2999 if (dump_file && (dump_flags & TDF_SCEV))
3001 fprintf (dump_file, " (set_nb_iterations_in_loop = ");
3002 print_generic_expr (dump_file, res, 0);
3003 fprintf (dump_file, "))\n");
3006 loop->nb_iterations = res;
3007 return res;
3011 /* Counters for the stats. */
3013 struct chrec_stats
3015 unsigned nb_chrecs;
3016 unsigned nb_affine;
3017 unsigned nb_affine_multivar;
3018 unsigned nb_higher_poly;
3019 unsigned nb_chrec_dont_know;
3020 unsigned nb_undetermined;
3023 /* Reset the counters. */
3025 static inline void
3026 reset_chrecs_counters (struct chrec_stats *stats)
3028 stats->nb_chrecs = 0;
3029 stats->nb_affine = 0;
3030 stats->nb_affine_multivar = 0;
3031 stats->nb_higher_poly = 0;
3032 stats->nb_chrec_dont_know = 0;
3033 stats->nb_undetermined = 0;
3036 /* Dump the contents of a CHREC_STATS structure. */
3038 static void
3039 dump_chrecs_stats (FILE *file, struct chrec_stats *stats)
3041 fprintf (file, "\n(\n");
3042 fprintf (file, "-----------------------------------------\n");
3043 fprintf (file, "%d\taffine univariate chrecs\n", stats->nb_affine);
3044 fprintf (file, "%d\taffine multivariate chrecs\n", stats->nb_affine_multivar);
3045 fprintf (file, "%d\tdegree greater than 2 polynomials\n",
3046 stats->nb_higher_poly);
3047 fprintf (file, "%d\tchrec_dont_know chrecs\n", stats->nb_chrec_dont_know);
3048 fprintf (file, "-----------------------------------------\n");
3049 fprintf (file, "%d\ttotal chrecs\n", stats->nb_chrecs);
3050 fprintf (file, "%d\twith undetermined coefficients\n",
3051 stats->nb_undetermined);
3052 fprintf (file, "-----------------------------------------\n");
3053 fprintf (file, "%d\tchrecs in the scev database\n",
3054 (int) scalar_evolution_info->elements ());
3055 fprintf (file, "%d\tsets in the scev database\n", nb_set_scev);
3056 fprintf (file, "%d\tgets in the scev database\n", nb_get_scev);
3057 fprintf (file, "-----------------------------------------\n");
3058 fprintf (file, ")\n\n");
3061 /* Gather statistics about CHREC. */
3063 static void
3064 gather_chrec_stats (tree chrec, struct chrec_stats *stats)
3066 if (dump_file && (dump_flags & TDF_STATS))
3068 fprintf (dump_file, "(classify_chrec ");
3069 print_generic_expr (dump_file, chrec, 0);
3070 fprintf (dump_file, "\n");
3073 stats->nb_chrecs++;
3075 if (chrec == NULL_TREE)
3077 stats->nb_undetermined++;
3078 return;
3081 switch (TREE_CODE (chrec))
3083 case POLYNOMIAL_CHREC:
3084 if (evolution_function_is_affine_p (chrec))
3086 if (dump_file && (dump_flags & TDF_STATS))
3087 fprintf (dump_file, " affine_univariate\n");
3088 stats->nb_affine++;
3090 else if (evolution_function_is_affine_multivariate_p (chrec, 0))
3092 if (dump_file && (dump_flags & TDF_STATS))
3093 fprintf (dump_file, " affine_multivariate\n");
3094 stats->nb_affine_multivar++;
3096 else
3098 if (dump_file && (dump_flags & TDF_STATS))
3099 fprintf (dump_file, " higher_degree_polynomial\n");
3100 stats->nb_higher_poly++;
3103 break;
3105 default:
3106 break;
3109 if (chrec_contains_undetermined (chrec))
3111 if (dump_file && (dump_flags & TDF_STATS))
3112 fprintf (dump_file, " undetermined\n");
3113 stats->nb_undetermined++;
3116 if (dump_file && (dump_flags & TDF_STATS))
3117 fprintf (dump_file, ")\n");
3120 /* Classify the chrecs of the whole database. */
3122 void
3123 gather_stats_on_scev_database (void)
3125 struct chrec_stats stats;
3127 if (!dump_file)
3128 return;
3130 reset_chrecs_counters (&stats);
3132 hash_table<scev_info_hasher>::iterator iter;
3133 scev_info_str *elt;
3134 FOR_EACH_HASH_TABLE_ELEMENT (*scalar_evolution_info, elt, scev_info_str *,
3135 iter)
3136 gather_chrec_stats (elt->chrec, &stats);
3138 dump_chrecs_stats (dump_file, &stats);
3143 /* Initializer. */
3145 static void
3146 initialize_scalar_evolutions_analyzer (void)
3148 /* The elements below are unique. */
3149 if (chrec_dont_know == NULL_TREE)
3151 chrec_not_analyzed_yet = NULL_TREE;
3152 chrec_dont_know = make_node (SCEV_NOT_KNOWN);
3153 chrec_known = make_node (SCEV_KNOWN);
3154 TREE_TYPE (chrec_dont_know) = void_type_node;
3155 TREE_TYPE (chrec_known) = void_type_node;
3159 /* Initialize the analysis of scalar evolutions for LOOPS. */
3161 void
3162 scev_initialize (void)
3164 struct loop *loop;
3166 scalar_evolution_info = hash_table<scev_info_hasher>::create_ggc (100);
3168 initialize_scalar_evolutions_analyzer ();
3170 FOR_EACH_LOOP (loop, 0)
3172 loop->nb_iterations = NULL_TREE;
3176 /* Return true if SCEV is initialized. */
3178 bool
3179 scev_initialized_p (void)
3181 return scalar_evolution_info != NULL;
3184 /* Cleans up the information cached by the scalar evolutions analysis
3185 in the hash table. */
3187 void
3188 scev_reset_htab (void)
3190 if (!scalar_evolution_info)
3191 return;
3193 scalar_evolution_info->empty ();
3196 /* Cleans up the information cached by the scalar evolutions analysis
3197 in the hash table and in the loop->nb_iterations. */
3199 void
3200 scev_reset (void)
3202 struct loop *loop;
3204 scev_reset_htab ();
3206 FOR_EACH_LOOP (loop, 0)
3208 loop->nb_iterations = NULL_TREE;
3212 /* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with
3213 respect to WRTO_LOOP and returns its base and step in IV if possible
3214 (see analyze_scalar_evolution_in_loop for more details on USE_LOOP
3215 and WRTO_LOOP). If ALLOW_NONCONSTANT_STEP is true, we want step to be
3216 invariant in LOOP. Otherwise we require it to be an integer constant.
3218 IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g.
3219 because it is computed in signed arithmetics). Consequently, adding an
3220 induction variable
3222 for (i = IV->base; ; i += IV->step)
3224 is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is
3225 false for the type of the induction variable, or you can prove that i does
3226 not wrap by some other argument. Otherwise, this might introduce undefined
3227 behavior, and
3229 for (i = iv->base; ; i = (type) ((unsigned type) i + (unsigned type) iv->step))
3231 must be used instead. */
3233 bool
3234 simple_iv (struct loop *wrto_loop, struct loop *use_loop, tree op,
3235 affine_iv *iv, bool allow_nonconstant_step)
3237 enum tree_code code;
3238 tree type, ev, base, e, stop;
3239 wide_int extreme;
3240 bool folded_casts, overflow;
3242 iv->base = NULL_TREE;
3243 iv->step = NULL_TREE;
3244 iv->no_overflow = false;
3246 type = TREE_TYPE (op);
3247 if (!POINTER_TYPE_P (type)
3248 && !INTEGRAL_TYPE_P (type))
3249 return false;
3251 ev = analyze_scalar_evolution_in_loop (wrto_loop, use_loop, op,
3252 &folded_casts);
3253 if (chrec_contains_undetermined (ev)
3254 || chrec_contains_symbols_defined_in_loop (ev, wrto_loop->num))
3255 return false;
3257 if (tree_does_not_contain_chrecs (ev))
3259 iv->base = ev;
3260 iv->step = build_int_cst (TREE_TYPE (ev), 0);
3261 iv->no_overflow = true;
3262 return true;
3265 if (TREE_CODE (ev) != POLYNOMIAL_CHREC
3266 || CHREC_VARIABLE (ev) != (unsigned) wrto_loop->num)
3267 return false;
3269 iv->step = CHREC_RIGHT (ev);
3270 if ((!allow_nonconstant_step && TREE_CODE (iv->step) != INTEGER_CST)
3271 || tree_contains_chrecs (iv->step, NULL))
3272 return false;
3274 iv->base = CHREC_LEFT (ev);
3275 if (tree_contains_chrecs (iv->base, NULL))
3276 return false;
3278 iv->no_overflow = (!folded_casts && ANY_INTEGRAL_TYPE_P (type)
3279 && TYPE_OVERFLOW_UNDEFINED (type));
3281 /* Try to simplify iv base:
3283 (signed T) ((unsigned T)base + step) ;; TREE_TYPE (base) == signed T
3284 == (signed T)(unsigned T)base + step
3285 == base + step
3287 If we can prove operation (base + step) doesn't overflow or underflow.
3288 Specifically, we try to prove below conditions are satisfied:
3290 base <= UPPER_BOUND (type) - step ;;step > 0
3291 base >= LOWER_BOUND (type) - step ;;step < 0
3293 This is done by proving the reverse conditions are false using loop's
3294 initial conditions.
3296 The is necessary to make loop niter, or iv overflow analysis easier
3297 for below example:
3299 int foo (int *a, signed char s, signed char l)
3301 signed char i;
3302 for (i = s; i < l; i++)
3303 a[i] = 0;
3304 return 0;
3307 Note variable I is firstly converted to type unsigned char, incremented,
3308 then converted back to type signed char. */
3310 if (wrto_loop->num != use_loop->num)
3311 return true;
3313 if (!CONVERT_EXPR_P (iv->base) || TREE_CODE (iv->step) != INTEGER_CST)
3314 return true;
3316 type = TREE_TYPE (iv->base);
3317 e = TREE_OPERAND (iv->base, 0);
3318 if (TREE_CODE (e) != PLUS_EXPR
3319 || TREE_CODE (TREE_OPERAND (e, 1)) != INTEGER_CST
3320 || !tree_int_cst_equal (iv->step,
3321 fold_convert (type, TREE_OPERAND (e, 1))))
3322 return true;
3323 e = TREE_OPERAND (e, 0);
3324 if (!CONVERT_EXPR_P (e))
3325 return true;
3326 base = TREE_OPERAND (e, 0);
3327 if (!useless_type_conversion_p (type, TREE_TYPE (base)))
3328 return true;
3330 if (tree_int_cst_sign_bit (iv->step))
3332 code = LT_EXPR;
3333 extreme = wi::min_value (type);
3335 else
3337 code = GT_EXPR;
3338 extreme = wi::max_value (type);
3340 overflow = false;
3341 extreme = wi::sub (extreme, iv->step, TYPE_SIGN (type), &overflow);
3342 if (overflow)
3343 return true;
3344 e = fold_build2 (code, boolean_type_node, base,
3345 wide_int_to_tree (type, extreme));
3346 stop = (TREE_CODE (base) == SSA_NAME) ? base : NULL;
3347 e = simplify_using_initial_conditions (use_loop, e, stop);
3348 if (!integer_zerop (e))
3349 return true;
3351 if (POINTER_TYPE_P (TREE_TYPE (base)))
3352 code = POINTER_PLUS_EXPR;
3353 else
3354 code = PLUS_EXPR;
3356 iv->base = fold_build2 (code, TREE_TYPE (base), base, iv->step);
3357 return true;
3360 /* Finalize the scalar evolution analysis. */
3362 void
3363 scev_finalize (void)
3365 if (!scalar_evolution_info)
3366 return;
3367 scalar_evolution_info->empty ();
3368 scalar_evolution_info = NULL;
3371 /* Returns true if the expression EXPR is considered to be too expensive
3372 for scev_const_prop. */
3374 bool
3375 expression_expensive_p (tree expr)
3377 enum tree_code code;
3379 if (is_gimple_val (expr))
3380 return false;
3382 code = TREE_CODE (expr);
3383 if (code == TRUNC_DIV_EXPR
3384 || code == CEIL_DIV_EXPR
3385 || code == FLOOR_DIV_EXPR
3386 || code == ROUND_DIV_EXPR
3387 || code == TRUNC_MOD_EXPR
3388 || code == CEIL_MOD_EXPR
3389 || code == FLOOR_MOD_EXPR
3390 || code == ROUND_MOD_EXPR
3391 || code == EXACT_DIV_EXPR)
3393 /* Division by power of two is usually cheap, so we allow it.
3394 Forbid anything else. */
3395 if (!integer_pow2p (TREE_OPERAND (expr, 1)))
3396 return true;
3399 switch (TREE_CODE_CLASS (code))
3401 case tcc_binary:
3402 case tcc_comparison:
3403 if (expression_expensive_p (TREE_OPERAND (expr, 1)))
3404 return true;
3406 /* Fallthru. */
3407 case tcc_unary:
3408 return expression_expensive_p (TREE_OPERAND (expr, 0));
3410 default:
3411 return true;
3415 /* Replace ssa names for that scev can prove they are constant by the
3416 appropriate constants. Also perform final value replacement in loops,
3417 in case the replacement expressions are cheap.
3419 We only consider SSA names defined by phi nodes; rest is left to the
3420 ordinary constant propagation pass. */
3422 unsigned int
3423 scev_const_prop (void)
3425 basic_block bb;
3426 tree name, type, ev;
3427 gphi *phi;
3428 gassign *ass;
3429 struct loop *loop, *ex_loop;
3430 bitmap ssa_names_to_remove = NULL;
3431 unsigned i;
3432 gphi_iterator psi;
3434 if (number_of_loops (cfun) <= 1)
3435 return 0;
3437 FOR_EACH_BB_FN (bb, cfun)
3439 loop = bb->loop_father;
3441 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
3443 phi = psi.phi ();
3444 name = PHI_RESULT (phi);
3446 if (virtual_operand_p (name))
3447 continue;
3449 type = TREE_TYPE (name);
3451 if (!POINTER_TYPE_P (type)
3452 && !INTEGRAL_TYPE_P (type))
3453 continue;
3455 ev = resolve_mixers (loop, analyze_scalar_evolution (loop, name),
3456 NULL);
3457 if (!is_gimple_min_invariant (ev)
3458 || !may_propagate_copy (name, ev))
3459 continue;
3461 /* Replace the uses of the name. */
3462 if (name != ev)
3463 replace_uses_by (name, ev);
3465 if (!ssa_names_to_remove)
3466 ssa_names_to_remove = BITMAP_ALLOC (NULL);
3467 bitmap_set_bit (ssa_names_to_remove, SSA_NAME_VERSION (name));
3471 /* Remove the ssa names that were replaced by constants. We do not
3472 remove them directly in the previous cycle, since this
3473 invalidates scev cache. */
3474 if (ssa_names_to_remove)
3476 bitmap_iterator bi;
3478 EXECUTE_IF_SET_IN_BITMAP (ssa_names_to_remove, 0, i, bi)
3480 gimple_stmt_iterator psi;
3481 name = ssa_name (i);
3482 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (name));
3484 gcc_assert (gimple_code (phi) == GIMPLE_PHI);
3485 psi = gsi_for_stmt (phi);
3486 remove_phi_node (&psi, true);
3489 BITMAP_FREE (ssa_names_to_remove);
3490 scev_reset ();
3493 /* Now the regular final value replacement. */
3494 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
3496 edge exit;
3497 tree def, rslt, niter;
3498 gimple_stmt_iterator gsi;
3500 /* If we do not know exact number of iterations of the loop, we cannot
3501 replace the final value. */
3502 exit = single_exit (loop);
3503 if (!exit)
3504 continue;
3506 niter = number_of_latch_executions (loop);
3507 if (niter == chrec_dont_know)
3508 continue;
3510 /* Ensure that it is possible to insert new statements somewhere. */
3511 if (!single_pred_p (exit->dest))
3512 split_loop_exit_edge (exit);
3513 gsi = gsi_after_labels (exit->dest);
3515 ex_loop = superloop_at_depth (loop,
3516 loop_depth (exit->dest->loop_father) + 1);
3518 for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); )
3520 phi = psi.phi ();
3521 rslt = PHI_RESULT (phi);
3522 def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
3523 if (virtual_operand_p (def))
3525 gsi_next (&psi);
3526 continue;
3529 if (!POINTER_TYPE_P (TREE_TYPE (def))
3530 && !INTEGRAL_TYPE_P (TREE_TYPE (def)))
3532 gsi_next (&psi);
3533 continue;
3536 bool folded_casts;
3537 def = analyze_scalar_evolution_in_loop (ex_loop, loop, def,
3538 &folded_casts);
3539 def = compute_overall_effect_of_inner_loop (ex_loop, def);
3540 if (!tree_does_not_contain_chrecs (def)
3541 || chrec_contains_symbols_defined_in_loop (def, ex_loop->num)
3542 /* Moving the computation from the loop may prolong life range
3543 of some ssa names, which may cause problems if they appear
3544 on abnormal edges. */
3545 || contains_abnormal_ssa_name_p (def)
3546 /* Do not emit expensive expressions. The rationale is that
3547 when someone writes a code like
3549 while (n > 45) n -= 45;
3551 he probably knows that n is not large, and does not want it
3552 to be turned into n %= 45. */
3553 || expression_expensive_p (def))
3555 if (dump_file && (dump_flags & TDF_DETAILS))
3557 fprintf (dump_file, "not replacing:\n ");
3558 print_gimple_stmt (dump_file, phi, 0, 0);
3559 fprintf (dump_file, "\n");
3561 gsi_next (&psi);
3562 continue;
3565 /* Eliminate the PHI node and replace it by a computation outside
3566 the loop. */
3567 if (dump_file)
3569 fprintf (dump_file, "\nfinal value replacement:\n ");
3570 print_gimple_stmt (dump_file, phi, 0, 0);
3571 fprintf (dump_file, " with\n ");
3573 def = unshare_expr (def);
3574 remove_phi_node (&psi, false);
3576 /* If def's type has undefined overflow and there were folded
3577 casts, rewrite all stmts added for def into arithmetics
3578 with defined overflow behavior. */
3579 if (folded_casts && ANY_INTEGRAL_TYPE_P (TREE_TYPE (def))
3580 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (def)))
3582 gimple_seq stmts;
3583 gimple_stmt_iterator gsi2;
3584 def = force_gimple_operand (def, &stmts, true, NULL_TREE);
3585 gsi2 = gsi_start (stmts);
3586 while (!gsi_end_p (gsi2))
3588 gimple *stmt = gsi_stmt (gsi2);
3589 gimple_stmt_iterator gsi3 = gsi2;
3590 gsi_next (&gsi2);
3591 gsi_remove (&gsi3, false);
3592 if (is_gimple_assign (stmt)
3593 && arith_code_with_undefined_signed_overflow
3594 (gimple_assign_rhs_code (stmt)))
3595 gsi_insert_seq_before (&gsi,
3596 rewrite_to_defined_overflow (stmt),
3597 GSI_SAME_STMT);
3598 else
3599 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
3602 else
3603 def = force_gimple_operand_gsi (&gsi, def, false, NULL_TREE,
3604 true, GSI_SAME_STMT);
3606 ass = gimple_build_assign (rslt, def);
3607 gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
3608 if (dump_file)
3610 print_gimple_stmt (dump_file, ass, 0, 0);
3611 fprintf (dump_file, "\n");
3615 return 0;
3618 #include "gt-tree-scalar-evolution.h"