re PR tree-optimization/52943 (likely wrong code bug caused by predictive commoning)
[gcc.git] / gcc / tree-scalar-evolution.c
1 /* Scalar evolution detector.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Sebastian Pop <s.pop@laposte.net>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /*
23 Description:
24
25 This pass analyzes the evolution of scalar variables in loop
26 structures. The algorithm is based on the SSA representation,
27 and on the loop hierarchy tree. This algorithm is not based on
28 the notion of versions of a variable, as it was the case for the
29 previous implementations of the scalar evolution algorithm, but
30 it assumes that each defined name is unique.
31
32 The notation used in this file is called "chains of recurrences",
33 and has been proposed by Eugene Zima, Robert Van Engelen, and
34 others for describing induction variables in programs. For example
35 "b -> {0, +, 2}_1" means that the scalar variable "b" is equal to 0
36 when entering in the loop_1 and has a step 2 in this loop, in other
37 words "for (b = 0; b < N; b+=2);". Note that the coefficients of
38 this chain of recurrence (or chrec [shrek]) can contain the name of
39 other variables, in which case they are called parametric chrecs.
40 For example, "b -> {a, +, 2}_1" means that the initial value of "b"
41 is the value of "a". In most of the cases these parametric chrecs
42 are fully instantiated before their use because symbolic names can
43 hide some difficult cases such as self-references described later
44 (see the Fibonacci example).
45
46 A short sketch of the algorithm is:
47
48 Given a scalar variable to be analyzed, follow the SSA edge to
49 its definition:
50
51 - When the definition is a GIMPLE_ASSIGN: if the right hand side
52 (RHS) of the definition cannot be statically analyzed, the answer
53 of the analyzer is: "don't know".
54 Otherwise, for all the variables that are not yet analyzed in the
55 RHS, try to determine their evolution, and finally try to
56 evaluate the operation of the RHS that gives the evolution
57 function of the analyzed variable.
58
59 - When the definition is a condition-phi-node: determine the
60 evolution function for all the branches of the phi node, and
61 finally merge these evolutions (see chrec_merge).
62
63 - When the definition is a loop-phi-node: determine its initial
64 condition, that is the SSA edge defined in an outer loop, and
65 keep it symbolic. Then determine the SSA edges that are defined
66 in the body of the loop. Follow the inner edges until ending on
67 another loop-phi-node of the same analyzed loop. If the reached
68 loop-phi-node is not the starting loop-phi-node, then we keep
69 this definition under a symbolic form. If the reached
70 loop-phi-node is the same as the starting one, then we compute a
71 symbolic stride on the return path. The result is then the
72 symbolic chrec {initial_condition, +, symbolic_stride}_loop.
73
74 Examples:
75
76 Example 1: Illustration of the basic algorithm.
77
78 | a = 3
79 | loop_1
80 | b = phi (a, c)
81 | c = b + 1
82 | if (c > 10) exit_loop
83 | endloop
84
85 Suppose that we want to know the number of iterations of the
86 loop_1. The exit_loop is controlled by a COND_EXPR (c > 10). We
87 ask the scalar evolution analyzer two questions: what's the
88 scalar evolution (scev) of "c", and what's the scev of "10". For
89 "10" the answer is "10" since it is a scalar constant. For the
90 scalar variable "c", it follows the SSA edge to its definition,
91 "c = b + 1", and then asks again what's the scev of "b".
92 Following the SSA edge, we end on a loop-phi-node "b = phi (a,
93 c)", where the initial condition is "a", and the inner loop edge
94 is "c". The initial condition is kept under a symbolic form (it
95 may be the case that the copy constant propagation has done its
96 work and we end with the constant "3" as one of the edges of the
97 loop-phi-node). The update edge is followed to the end of the
98 loop, and until reaching again the starting loop-phi-node: b -> c
99 -> b. At this point we have drawn a path from "b" to "b" from
100 which we compute the stride in the loop: in this example it is
101 "+1". The resulting scev for "b" is "b -> {a, +, 1}_1". Now
102 that the scev for "b" is known, it is possible to compute the
103 scev for "c", that is "c -> {a + 1, +, 1}_1". In order to
104 determine the number of iterations in the loop_1, we have to
105 instantiate_parameters (loop_1, {a + 1, +, 1}_1), that gives after some
106 more analysis the scev {4, +, 1}_1, or in other words, this is
107 the function "f (x) = x + 4", where x is the iteration count of
108 the loop_1. Now we have to solve the inequality "x + 4 > 10",
109 and take the smallest iteration number for which the loop is
110 exited: x = 7. This loop runs from x = 0 to x = 7, and in total
111 there are 8 iterations. In terms of loop normalization, we have
112 created a variable that is implicitly defined, "x" or just "_1",
113 and all the other analyzed scalars of the loop are defined in
114 function of this variable:
115
116 a -> 3
117 b -> {3, +, 1}_1
118 c -> {4, +, 1}_1
119
120 or in terms of a C program:
121
122 | a = 3
123 | for (x = 0; x <= 7; x++)
124 | {
125 | b = x + 3
126 | c = x + 4
127 | }
128
129 Example 2a: Illustration of the algorithm on nested loops.
130
131 | loop_1
132 | a = phi (1, b)
133 | c = a + 2
134 | loop_2 10 times
135 | b = phi (c, d)
136 | d = b + 3
137 | endloop
138 | endloop
139
140 For analyzing the scalar evolution of "a", the algorithm follows
141 the SSA edge into the loop's body: "a -> b". "b" is an inner
142 loop-phi-node, and its analysis as in Example 1, gives:
143
144 b -> {c, +, 3}_2
145 d -> {c + 3, +, 3}_2
146
147 Following the SSA edge for the initial condition, we end on "c = a
148 + 2", and then on the starting loop-phi-node "a". From this point,
149 the loop stride is computed: back on "c = a + 2" we get a "+2" in
150 the loop_1, then on the loop-phi-node "b" we compute the overall
151 effect of the inner loop that is "b = c + 30", and we get a "+30"
152 in the loop_1. That means that the overall stride in loop_1 is
153 equal to "+32", and the result is:
154
155 a -> {1, +, 32}_1
156 c -> {3, +, 32}_1
157
158 Example 2b: Multivariate chains of recurrences.
159
160 | loop_1
161 | k = phi (0, k + 1)
162 | loop_2 4 times
163 | j = phi (0, j + 1)
164 | loop_3 4 times
165 | i = phi (0, i + 1)
166 | A[j + k] = ...
167 | endloop
168 | endloop
169 | endloop
170
171 Analyzing the access function of array A with
172 instantiate_parameters (loop_1, "j + k"), we obtain the
173 instantiation and the analysis of the scalar variables "j" and "k"
174 in loop_1. This leads to the scalar evolution {4, +, 1}_1: the end
175 value of loop_2 for "j" is 4, and the evolution of "k" in loop_1 is
176 {0, +, 1}_1. To obtain the evolution function in loop_3 and
177 instantiate the scalar variables up to loop_1, one has to use:
178 instantiate_scev (block_before_loop (loop_1), loop_3, "j + k").
179 The result of this call is {{0, +, 1}_1, +, 1}_2.
180
181 Example 3: Higher degree polynomials.
182
183 | loop_1
184 | a = phi (2, b)
185 | c = phi (5, d)
186 | b = a + 1
187 | d = c + a
188 | endloop
189
190 a -> {2, +, 1}_1
191 b -> {3, +, 1}_1
192 c -> {5, +, a}_1
193 d -> {5 + a, +, a}_1
194
195 instantiate_parameters (loop_1, {5, +, a}_1) -> {5, +, 2, +, 1}_1
196 instantiate_parameters (loop_1, {5 + a, +, a}_1) -> {7, +, 3, +, 1}_1
197
198 Example 4: Lucas, Fibonacci, or mixers in general.
199
200 | loop_1
201 | a = phi (1, b)
202 | c = phi (3, d)
203 | b = c
204 | d = c + a
205 | endloop
206
207 a -> (1, c)_1
208 c -> {3, +, a}_1
209
210 The syntax "(1, c)_1" stands for a PEELED_CHREC that has the
211 following semantics: during the first iteration of the loop_1, the
212 variable contains the value 1, and then it contains the value "c".
213 Note that this syntax is close to the syntax of the loop-phi-node:
214 "a -> (1, c)_1" vs. "a = phi (1, c)".
215
216 The symbolic chrec representation contains all the semantics of the
217 original code. What is more difficult is to use this information.
218
219 Example 5: Flip-flops, or exchangers.
220
221 | loop_1
222 | a = phi (1, b)
223 | c = phi (3, d)
224 | b = c
225 | d = a
226 | endloop
227
228 a -> (1, c)_1
229 c -> (3, a)_1
230
231 Based on these symbolic chrecs, it is possible to refine this
232 information into the more precise PERIODIC_CHRECs:
233
234 a -> |1, 3|_1
235 c -> |3, 1|_1
236
237 This transformation is not yet implemented.
238
239 Further readings:
240
241 You can find a more detailed description of the algorithm in:
242 http://icps.u-strasbg.fr/~pop/DEA_03_Pop.pdf
243 http://icps.u-strasbg.fr/~pop/DEA_03_Pop.ps.gz. But note that
244 this is a preliminary report and some of the details of the
245 algorithm have changed. I'm working on a research report that
246 updates the description of the algorithms to reflect the design
247 choices used in this implementation.
248
249 A set of slides show a high level overview of the algorithm and run
250 an example through the scalar evolution analyzer:
251 http://cri.ensmp.fr/~pop/gcc/mar04/slides.pdf
252
253 The slides that I have presented at the GCC Summit'04 are available
254 at: http://cri.ensmp.fr/~pop/gcc/20040604/gccsummit-lno-spop.pdf
255 */
256
257 #include "config.h"
258 #include "system.h"
259 #include "coretypes.h"
260 #include "gimple-pretty-print.h"
261 #include "tree-flow.h"
262 #include "cfgloop.h"
263 #include "tree-chrec.h"
264 #include "tree-scalar-evolution.h"
265 #include "tree-pass.h"
266 #include "params.h"
267
268 static tree analyze_scalar_evolution_1 (struct loop *, tree, tree);
269 static tree analyze_scalar_evolution_for_address_of (struct loop *loop,
270 tree var);
271
272 /* The cached information about an SSA name VAR, claiming that below
273 basic block INSTANTIATED_BELOW, the value of VAR can be expressed
274 as CHREC. */
275
276 struct GTY(()) scev_info_str {
277 basic_block instantiated_below;
278 tree var;
279 tree chrec;
280 };
281
282 /* Counters for the scev database. */
283 static unsigned nb_set_scev = 0;
284 static unsigned nb_get_scev = 0;
285
286 /* The following trees are unique elements. Thus the comparison of
287 another element to these elements should be done on the pointer to
288 these trees, and not on their value. */
289
290 /* The SSA_NAMEs that are not yet analyzed are qualified with NULL_TREE. */
291 tree chrec_not_analyzed_yet;
292
293 /* Reserved to the cases where the analyzer has detected an
294 undecidable property at compile time. */
295 tree chrec_dont_know;
296
297 /* When the analyzer has detected that a property will never
298 happen, then it qualifies it with chrec_known. */
299 tree chrec_known;
300
301 static GTY ((param_is (struct scev_info_str))) htab_t scalar_evolution_info;
302
303 \f
304 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */
305
306 static inline struct scev_info_str *
307 new_scev_info_str (basic_block instantiated_below, tree var)
308 {
309 struct scev_info_str *res;
310
311 res = ggc_alloc_scev_info_str ();
312 res->var = var;
313 res->chrec = chrec_not_analyzed_yet;
314 res->instantiated_below = instantiated_below;
315
316 return res;
317 }
318
319 /* Computes a hash function for database element ELT. */
320
321 static hashval_t
322 hash_scev_info (const void *elt)
323 {
324 return SSA_NAME_VERSION (((const struct scev_info_str *) elt)->var);
325 }
326
327 /* Compares database elements E1 and E2. */
328
329 static int
330 eq_scev_info (const void *e1, const void *e2)
331 {
332 const struct scev_info_str *elt1 = (const struct scev_info_str *) e1;
333 const struct scev_info_str *elt2 = (const struct scev_info_str *) e2;
334
335 return (elt1->var == elt2->var
336 && elt1->instantiated_below == elt2->instantiated_below);
337 }
338
339 /* Deletes database element E. */
340
341 static void
342 del_scev_info (void *e)
343 {
344 ggc_free (e);
345 }
346
347 /* Get the scalar evolution of VAR for INSTANTIATED_BELOW basic block.
348 A first query on VAR returns chrec_not_analyzed_yet. */
349
350 static tree *
351 find_var_scev_info (basic_block instantiated_below, tree var)
352 {
353 struct scev_info_str *res;
354 struct scev_info_str tmp;
355 PTR *slot;
356
357 tmp.var = var;
358 tmp.instantiated_below = instantiated_below;
359 slot = htab_find_slot (scalar_evolution_info, &tmp, INSERT);
360
361 if (!*slot)
362 *slot = new_scev_info_str (instantiated_below, var);
363 res = (struct scev_info_str *) *slot;
364
365 return &res->chrec;
366 }
367
368 /* Return true when CHREC contains symbolic names defined in
369 LOOP_NB. */
370
371 bool
372 chrec_contains_symbols_defined_in_loop (const_tree chrec, unsigned loop_nb)
373 {
374 int i, n;
375
376 if (chrec == NULL_TREE)
377 return false;
378
379 if (is_gimple_min_invariant (chrec))
380 return false;
381
382 if (TREE_CODE (chrec) == SSA_NAME)
383 {
384 gimple def;
385 loop_p def_loop, loop;
386
387 if (SSA_NAME_IS_DEFAULT_DEF (chrec))
388 return false;
389
390 def = SSA_NAME_DEF_STMT (chrec);
391 def_loop = loop_containing_stmt (def);
392 loop = get_loop (loop_nb);
393
394 if (def_loop == NULL)
395 return false;
396
397 if (loop == def_loop || flow_loop_nested_p (loop, def_loop))
398 return true;
399
400 return false;
401 }
402
403 n = TREE_OPERAND_LENGTH (chrec);
404 for (i = 0; i < n; i++)
405 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec, i),
406 loop_nb))
407 return true;
408 return false;
409 }
410
411 /* Return true when PHI is a loop-phi-node. */
412
413 static bool
414 loop_phi_node_p (gimple phi)
415 {
416 /* The implementation of this function is based on the following
417 property: "all the loop-phi-nodes of a loop are contained in the
418 loop's header basic block". */
419
420 return loop_containing_stmt (phi)->header == gimple_bb (phi);
421 }
422
423 /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
424 In general, in the case of multivariate evolutions we want to get
425 the evolution in different loops. LOOP specifies the level for
426 which to get the evolution.
427
428 Example:
429
430 | for (j = 0; j < 100; j++)
431 | {
432 | for (k = 0; k < 100; k++)
433 | {
434 | i = k + j; - Here the value of i is a function of j, k.
435 | }
436 | ... = i - Here the value of i is a function of j.
437 | }
438 | ... = i - Here the value of i is a scalar.
439
440 Example:
441
442 | i_0 = ...
443 | loop_1 10 times
444 | i_1 = phi (i_0, i_2)
445 | i_2 = i_1 + 2
446 | endloop
447
448 This loop has the same effect as:
449 LOOP_1 has the same effect as:
450
451 | i_1 = i_0 + 20
452
453 The overall effect of the loop, "i_0 + 20" in the previous example,
454 is obtained by passing in the parameters: LOOP = 1,
455 EVOLUTION_FN = {i_0, +, 2}_1.
456 */
457
458 tree
459 compute_overall_effect_of_inner_loop (struct loop *loop, tree evolution_fn)
460 {
461 bool val = false;
462
463 if (evolution_fn == chrec_dont_know)
464 return chrec_dont_know;
465
466 else if (TREE_CODE (evolution_fn) == POLYNOMIAL_CHREC)
467 {
468 struct loop *inner_loop = get_chrec_loop (evolution_fn);
469
470 if (inner_loop == loop
471 || flow_loop_nested_p (loop, inner_loop))
472 {
473 tree nb_iter = number_of_latch_executions (inner_loop);
474
475 if (nb_iter == chrec_dont_know)
476 return chrec_dont_know;
477 else
478 {
479 tree res;
480
481 /* evolution_fn is the evolution function in LOOP. Get
482 its value in the nb_iter-th iteration. */
483 res = chrec_apply (inner_loop->num, evolution_fn, nb_iter);
484
485 if (chrec_contains_symbols_defined_in_loop (res, loop->num))
486 res = instantiate_parameters (loop, res);
487
488 /* Continue the computation until ending on a parent of LOOP. */
489 return compute_overall_effect_of_inner_loop (loop, res);
490 }
491 }
492 else
493 return evolution_fn;
494 }
495
496 /* If the evolution function is an invariant, there is nothing to do. */
497 else if (no_evolution_in_loop_p (evolution_fn, loop->num, &val) && val)
498 return evolution_fn;
499
500 else
501 return chrec_dont_know;
502 }
503
504 /* Associate CHREC to SCALAR. */
505
506 static void
507 set_scalar_evolution (basic_block instantiated_below, tree scalar, tree chrec)
508 {
509 tree *scalar_info;
510
511 if (TREE_CODE (scalar) != SSA_NAME)
512 return;
513
514 scalar_info = find_var_scev_info (instantiated_below, scalar);
515
516 if (dump_file)
517 {
518 if (dump_flags & TDF_SCEV)
519 {
520 fprintf (dump_file, "(set_scalar_evolution \n");
521 fprintf (dump_file, " instantiated_below = %d \n",
522 instantiated_below->index);
523 fprintf (dump_file, " (scalar = ");
524 print_generic_expr (dump_file, scalar, 0);
525 fprintf (dump_file, ")\n (scalar_evolution = ");
526 print_generic_expr (dump_file, chrec, 0);
527 fprintf (dump_file, "))\n");
528 }
529 if (dump_flags & TDF_STATS)
530 nb_set_scev++;
531 }
532
533 *scalar_info = chrec;
534 }
535
536 /* Retrieve the chrec associated to SCALAR instantiated below
537 INSTANTIATED_BELOW block. */
538
539 static tree
540 get_scalar_evolution (basic_block instantiated_below, tree scalar)
541 {
542 tree res;
543
544 if (dump_file)
545 {
546 if (dump_flags & TDF_SCEV)
547 {
548 fprintf (dump_file, "(get_scalar_evolution \n");
549 fprintf (dump_file, " (scalar = ");
550 print_generic_expr (dump_file, scalar, 0);
551 fprintf (dump_file, ")\n");
552 }
553 if (dump_flags & TDF_STATS)
554 nb_get_scev++;
555 }
556
557 switch (TREE_CODE (scalar))
558 {
559 case SSA_NAME:
560 res = *find_var_scev_info (instantiated_below, scalar);
561 break;
562
563 case REAL_CST:
564 case FIXED_CST:
565 case INTEGER_CST:
566 res = scalar;
567 break;
568
569 default:
570 res = chrec_not_analyzed_yet;
571 break;
572 }
573
574 if (dump_file && (dump_flags & TDF_SCEV))
575 {
576 fprintf (dump_file, " (scalar_evolution = ");
577 print_generic_expr (dump_file, res, 0);
578 fprintf (dump_file, "))\n");
579 }
580
581 return res;
582 }
583
584 /* Helper function for add_to_evolution. Returns the evolution
585 function for an assignment of the form "a = b + c", where "a" and
586 "b" are on the strongly connected component. CHREC_BEFORE is the
587 information that we already have collected up to this point.
588 TO_ADD is the evolution of "c".
589
590 When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
591 evolution the expression TO_ADD, otherwise construct an evolution
592 part for this loop. */
593
594 static tree
595 add_to_evolution_1 (unsigned loop_nb, tree chrec_before, tree to_add,
596 gimple at_stmt)
597 {
598 tree type, left, right;
599 struct loop *loop = get_loop (loop_nb), *chloop;
600
601 switch (TREE_CODE (chrec_before))
602 {
603 case POLYNOMIAL_CHREC:
604 chloop = get_chrec_loop (chrec_before);
605 if (chloop == loop
606 || flow_loop_nested_p (chloop, loop))
607 {
608 unsigned var;
609
610 type = chrec_type (chrec_before);
611
612 /* When there is no evolution part in this loop, build it. */
613 if (chloop != loop)
614 {
615 var = loop_nb;
616 left = chrec_before;
617 right = SCALAR_FLOAT_TYPE_P (type)
618 ? build_real (type, dconst0)
619 : build_int_cst (type, 0);
620 }
621 else
622 {
623 var = CHREC_VARIABLE (chrec_before);
624 left = CHREC_LEFT (chrec_before);
625 right = CHREC_RIGHT (chrec_before);
626 }
627
628 to_add = chrec_convert (type, to_add, at_stmt);
629 right = chrec_convert_rhs (type, right, at_stmt);
630 right = chrec_fold_plus (chrec_type (right), right, to_add);
631 return build_polynomial_chrec (var, left, right);
632 }
633 else
634 {
635 gcc_assert (flow_loop_nested_p (loop, chloop));
636
637 /* Search the evolution in LOOP_NB. */
638 left = add_to_evolution_1 (loop_nb, CHREC_LEFT (chrec_before),
639 to_add, at_stmt);
640 right = CHREC_RIGHT (chrec_before);
641 right = chrec_convert_rhs (chrec_type (left), right, at_stmt);
642 return build_polynomial_chrec (CHREC_VARIABLE (chrec_before),
643 left, right);
644 }
645
646 default:
647 /* These nodes do not depend on a loop. */
648 if (chrec_before == chrec_dont_know)
649 return chrec_dont_know;
650
651 left = chrec_before;
652 right = chrec_convert_rhs (chrec_type (left), to_add, at_stmt);
653 return build_polynomial_chrec (loop_nb, left, right);
654 }
655 }
656
657 /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
658 of LOOP_NB.
659
660 Description (provided for completeness, for those who read code in
661 a plane, and for my poor 62 bytes brain that would have forgotten
662 all this in the next two or three months):
663
664 The algorithm of translation of programs from the SSA representation
665 into the chrecs syntax is based on a pattern matching. After having
666 reconstructed the overall tree expression for a loop, there are only
667 two cases that can arise:
668
669 1. a = loop-phi (init, a + expr)
670 2. a = loop-phi (init, expr)
671
672 where EXPR is either a scalar constant with respect to the analyzed
673 loop (this is a degree 0 polynomial), or an expression containing
674 other loop-phi definitions (these are higher degree polynomials).
675
676 Examples:
677
678 1.
679 | init = ...
680 | loop_1
681 | a = phi (init, a + 5)
682 | endloop
683
684 2.
685 | inita = ...
686 | initb = ...
687 | loop_1
688 | a = phi (inita, 2 * b + 3)
689 | b = phi (initb, b + 1)
690 | endloop
691
692 For the first case, the semantics of the SSA representation is:
693
694 | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
695
696 that is, there is a loop index "x" that determines the scalar value
697 of the variable during the loop execution. During the first
698 iteration, the value is that of the initial condition INIT, while
699 during the subsequent iterations, it is the sum of the initial
700 condition with the sum of all the values of EXPR from the initial
701 iteration to the before last considered iteration.
702
703 For the second case, the semantics of the SSA program is:
704
705 | a (x) = init, if x = 0;
706 | expr (x - 1), otherwise.
707
708 The second case corresponds to the PEELED_CHREC, whose syntax is
709 close to the syntax of a loop-phi-node:
710
711 | phi (init, expr) vs. (init, expr)_x
712
713 The proof of the translation algorithm for the first case is a
714 proof by structural induction based on the degree of EXPR.
715
716 Degree 0:
717 When EXPR is a constant with respect to the analyzed loop, or in
718 other words when EXPR is a polynomial of degree 0, the evolution of
719 the variable A in the loop is an affine function with an initial
720 condition INIT, and a step EXPR. In order to show this, we start
721 from the semantics of the SSA representation:
722
723 f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
724
725 and since "expr (j)" is a constant with respect to "j",
726
727 f (x) = init + x * expr
728
729 Finally, based on the semantics of the pure sum chrecs, by
730 identification we get the corresponding chrecs syntax:
731
732 f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
733 f (x) -> {init, +, expr}_x
734
735 Higher degree:
736 Suppose that EXPR is a polynomial of degree N with respect to the
737 analyzed loop_x for which we have already determined that it is
738 written under the chrecs syntax:
739
740 | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
741
742 We start from the semantics of the SSA program:
743
744 | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
745 |
746 | f (x) = init + \sum_{j = 0}^{x - 1}
747 | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
748 |
749 | f (x) = init + \sum_{j = 0}^{x - 1}
750 | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
751 |
752 | f (x) = init + \sum_{k = 0}^{n - 1}
753 | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
754 |
755 | f (x) = init + \sum_{k = 0}^{n - 1}
756 | (b_k * \binom{x}{k + 1})
757 |
758 | f (x) = init + b_0 * \binom{x}{1} + ...
759 | + b_{n-1} * \binom{x}{n}
760 |
761 | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
762 | + b_{n-1} * \binom{x}{n}
763 |
764
765 And finally from the definition of the chrecs syntax, we identify:
766 | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x
767
768 This shows the mechanism that stands behind the add_to_evolution
769 function. An important point is that the use of symbolic
770 parameters avoids the need of an analysis schedule.
771
772 Example:
773
774 | inita = ...
775 | initb = ...
776 | loop_1
777 | a = phi (inita, a + 2 + b)
778 | b = phi (initb, b + 1)
779 | endloop
780
781 When analyzing "a", the algorithm keeps "b" symbolically:
782
783 | a -> {inita, +, 2 + b}_1
784
785 Then, after instantiation, the analyzer ends on the evolution:
786
787 | a -> {inita, +, 2 + initb, +, 1}_1
788
789 */
790
791 static tree
792 add_to_evolution (unsigned loop_nb, tree chrec_before, enum tree_code code,
793 tree to_add, gimple at_stmt)
794 {
795 tree type = chrec_type (to_add);
796 tree res = NULL_TREE;
797
798 if (to_add == NULL_TREE)
799 return chrec_before;
800
801 /* TO_ADD is either a scalar, or a parameter. TO_ADD is not
802 instantiated at this point. */
803 if (TREE_CODE (to_add) == POLYNOMIAL_CHREC)
804 /* This should not happen. */
805 return chrec_dont_know;
806
807 if (dump_file && (dump_flags & TDF_SCEV))
808 {
809 fprintf (dump_file, "(add_to_evolution \n");
810 fprintf (dump_file, " (loop_nb = %d)\n", loop_nb);
811 fprintf (dump_file, " (chrec_before = ");
812 print_generic_expr (dump_file, chrec_before, 0);
813 fprintf (dump_file, ")\n (to_add = ");
814 print_generic_expr (dump_file, to_add, 0);
815 fprintf (dump_file, ")\n");
816 }
817
818 if (code == MINUS_EXPR)
819 to_add = chrec_fold_multiply (type, to_add, SCALAR_FLOAT_TYPE_P (type)
820 ? build_real (type, dconstm1)
821 : build_int_cst_type (type, -1));
822
823 res = add_to_evolution_1 (loop_nb, chrec_before, to_add, at_stmt);
824
825 if (dump_file && (dump_flags & TDF_SCEV))
826 {
827 fprintf (dump_file, " (res = ");
828 print_generic_expr (dump_file, res, 0);
829 fprintf (dump_file, "))\n");
830 }
831
832 return res;
833 }
834
835 \f
836
837 /* This section selects the loops that will be good candidates for the
838 scalar evolution analysis. For the moment, greedily select all the
839 loop nests we could analyze. */
840
841 /* For a loop with a single exit edge, return the COND_EXPR that
842 guards the exit edge. If the expression is too difficult to
843 analyze, then give up. */
844
845 gimple
846 get_loop_exit_condition (const struct loop *loop)
847 {
848 gimple res = NULL;
849 edge exit_edge = single_exit (loop);
850
851 if (dump_file && (dump_flags & TDF_SCEV))
852 fprintf (dump_file, "(get_loop_exit_condition \n ");
853
854 if (exit_edge)
855 {
856 gimple stmt;
857
858 stmt = last_stmt (exit_edge->src);
859 if (gimple_code (stmt) == GIMPLE_COND)
860 res = stmt;
861 }
862
863 if (dump_file && (dump_flags & TDF_SCEV))
864 {
865 print_gimple_stmt (dump_file, res, 0, 0);
866 fprintf (dump_file, ")\n");
867 }
868
869 return res;
870 }
871
872 /* Recursively determine and enqueue the exit conditions for a loop. */
873
874 static void
875 get_exit_conditions_rec (struct loop *loop,
876 VEC(gimple,heap) **exit_conditions)
877 {
878 if (!loop)
879 return;
880
881 /* Recurse on the inner loops, then on the next (sibling) loops. */
882 get_exit_conditions_rec (loop->inner, exit_conditions);
883 get_exit_conditions_rec (loop->next, exit_conditions);
884
885 if (single_exit (loop))
886 {
887 gimple loop_condition = get_loop_exit_condition (loop);
888
889 if (loop_condition)
890 VEC_safe_push (gimple, heap, *exit_conditions, loop_condition);
891 }
892 }
893
894 /* Select the candidate loop nests for the analysis. This function
895 initializes the EXIT_CONDITIONS array. */
896
897 static void
898 select_loops_exit_conditions (VEC(gimple,heap) **exit_conditions)
899 {
900 struct loop *function_body = current_loops->tree_root;
901
902 get_exit_conditions_rec (function_body->inner, exit_conditions);
903 }
904
905 \f
906 /* Depth first search algorithm. */
907
908 typedef enum t_bool {
909 t_false,
910 t_true,
911 t_dont_know
912 } t_bool;
913
914
915 static t_bool follow_ssa_edge (struct loop *loop, gimple, gimple, tree *, int);
916
917 /* Follow the ssa edge into the binary expression RHS0 CODE RHS1.
918 Return true if the strongly connected component has been found. */
919
920 static t_bool
921 follow_ssa_edge_binary (struct loop *loop, gimple at_stmt,
922 tree type, tree rhs0, enum tree_code code, tree rhs1,
923 gimple halting_phi, tree *evolution_of_loop, int limit)
924 {
925 t_bool res = t_false;
926 tree evol;
927
928 switch (code)
929 {
930 case POINTER_PLUS_EXPR:
931 case PLUS_EXPR:
932 if (TREE_CODE (rhs0) == SSA_NAME)
933 {
934 if (TREE_CODE (rhs1) == SSA_NAME)
935 {
936 /* Match an assignment under the form:
937 "a = b + c". */
938
939 /* We want only assignments of form "name + name" contribute to
940 LIMIT, as the other cases do not necessarily contribute to
941 the complexity of the expression. */
942 limit++;
943
944 evol = *evolution_of_loop;
945 res = follow_ssa_edge
946 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi, &evol, limit);
947
948 if (res == t_true)
949 *evolution_of_loop = add_to_evolution
950 (loop->num,
951 chrec_convert (type, evol, at_stmt),
952 code, rhs1, at_stmt);
953
954 else if (res == t_false)
955 {
956 res = follow_ssa_edge
957 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
958 evolution_of_loop, limit);
959
960 if (res == t_true)
961 *evolution_of_loop = add_to_evolution
962 (loop->num,
963 chrec_convert (type, *evolution_of_loop, at_stmt),
964 code, rhs0, at_stmt);
965
966 else if (res == t_dont_know)
967 *evolution_of_loop = chrec_dont_know;
968 }
969
970 else if (res == t_dont_know)
971 *evolution_of_loop = chrec_dont_know;
972 }
973
974 else
975 {
976 /* Match an assignment under the form:
977 "a = b + ...". */
978 res = follow_ssa_edge
979 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
980 evolution_of_loop, limit);
981 if (res == t_true)
982 *evolution_of_loop = add_to_evolution
983 (loop->num, chrec_convert (type, *evolution_of_loop,
984 at_stmt),
985 code, rhs1, at_stmt);
986
987 else if (res == t_dont_know)
988 *evolution_of_loop = chrec_dont_know;
989 }
990 }
991
992 else if (TREE_CODE (rhs1) == SSA_NAME)
993 {
994 /* Match an assignment under the form:
995 "a = ... + c". */
996 res = follow_ssa_edge
997 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
998 evolution_of_loop, limit);
999 if (res == t_true)
1000 *evolution_of_loop = add_to_evolution
1001 (loop->num, chrec_convert (type, *evolution_of_loop,
1002 at_stmt),
1003 code, rhs0, at_stmt);
1004
1005 else if (res == t_dont_know)
1006 *evolution_of_loop = chrec_dont_know;
1007 }
1008
1009 else
1010 /* Otherwise, match an assignment under the form:
1011 "a = ... + ...". */
1012 /* And there is nothing to do. */
1013 res = t_false;
1014 break;
1015
1016 case MINUS_EXPR:
1017 /* This case is under the form "opnd0 = rhs0 - rhs1". */
1018 if (TREE_CODE (rhs0) == SSA_NAME)
1019 {
1020 /* Match an assignment under the form:
1021 "a = b - ...". */
1022
1023 /* We want only assignments of form "name - name" contribute to
1024 LIMIT, as the other cases do not necessarily contribute to
1025 the complexity of the expression. */
1026 if (TREE_CODE (rhs1) == SSA_NAME)
1027 limit++;
1028
1029 res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
1030 evolution_of_loop, limit);
1031 if (res == t_true)
1032 *evolution_of_loop = add_to_evolution
1033 (loop->num, chrec_convert (type, *evolution_of_loop, at_stmt),
1034 MINUS_EXPR, rhs1, at_stmt);
1035
1036 else if (res == t_dont_know)
1037 *evolution_of_loop = chrec_dont_know;
1038 }
1039 else
1040 /* Otherwise, match an assignment under the form:
1041 "a = ... - ...". */
1042 /* And there is nothing to do. */
1043 res = t_false;
1044 break;
1045
1046 default:
1047 res = t_false;
1048 }
1049
1050 return res;
1051 }
1052
1053 /* Follow the ssa edge into the expression EXPR.
1054 Return true if the strongly connected component has been found. */
1055
1056 static t_bool
1057 follow_ssa_edge_expr (struct loop *loop, gimple at_stmt, tree expr,
1058 gimple halting_phi, tree *evolution_of_loop, int limit)
1059 {
1060 enum tree_code code = TREE_CODE (expr);
1061 tree type = TREE_TYPE (expr), rhs0, rhs1;
1062 t_bool res;
1063
1064 /* The EXPR is one of the following cases:
1065 - an SSA_NAME,
1066 - an INTEGER_CST,
1067 - a PLUS_EXPR,
1068 - a POINTER_PLUS_EXPR,
1069 - a MINUS_EXPR,
1070 - an ASSERT_EXPR,
1071 - other cases are not yet handled. */
1072
1073 switch (code)
1074 {
1075 CASE_CONVERT:
1076 /* This assignment is under the form "a_1 = (cast) rhs. */
1077 res = follow_ssa_edge_expr (loop, at_stmt, TREE_OPERAND (expr, 0),
1078 halting_phi, evolution_of_loop, limit);
1079 *evolution_of_loop = chrec_convert (type, *evolution_of_loop, at_stmt);
1080 break;
1081
1082 case INTEGER_CST:
1083 /* This assignment is under the form "a_1 = 7". */
1084 res = t_false;
1085 break;
1086
1087 case SSA_NAME:
1088 /* This assignment is under the form: "a_1 = b_2". */
1089 res = follow_ssa_edge
1090 (loop, SSA_NAME_DEF_STMT (expr), halting_phi, evolution_of_loop, limit);
1091 break;
1092
1093 case POINTER_PLUS_EXPR:
1094 case PLUS_EXPR:
1095 case MINUS_EXPR:
1096 /* This case is under the form "rhs0 +- rhs1". */
1097 rhs0 = TREE_OPERAND (expr, 0);
1098 rhs1 = TREE_OPERAND (expr, 1);
1099 type = TREE_TYPE (rhs0);
1100 STRIP_USELESS_TYPE_CONVERSION (rhs0);
1101 STRIP_USELESS_TYPE_CONVERSION (rhs1);
1102 res = follow_ssa_edge_binary (loop, at_stmt, type, rhs0, code, rhs1,
1103 halting_phi, evolution_of_loop, limit);
1104 break;
1105
1106 case ADDR_EXPR:
1107 /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR. */
1108 if (TREE_CODE (TREE_OPERAND (expr, 0)) == MEM_REF)
1109 {
1110 expr = TREE_OPERAND (expr, 0);
1111 rhs0 = TREE_OPERAND (expr, 0);
1112 rhs1 = TREE_OPERAND (expr, 1);
1113 type = TREE_TYPE (rhs0);
1114 STRIP_USELESS_TYPE_CONVERSION (rhs0);
1115 STRIP_USELESS_TYPE_CONVERSION (rhs1);
1116 res = follow_ssa_edge_binary (loop, at_stmt, type,
1117 rhs0, POINTER_PLUS_EXPR, rhs1,
1118 halting_phi, evolution_of_loop, limit);
1119 }
1120 else
1121 res = t_false;
1122 break;
1123
1124 case ASSERT_EXPR:
1125 /* This assignment is of the form: "a_1 = ASSERT_EXPR <a_2, ...>"
1126 It must be handled as a copy assignment of the form a_1 = a_2. */
1127 rhs0 = ASSERT_EXPR_VAR (expr);
1128 if (TREE_CODE (rhs0) == SSA_NAME)
1129 res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0),
1130 halting_phi, evolution_of_loop, limit);
1131 else
1132 res = t_false;
1133 break;
1134
1135 default:
1136 res = t_false;
1137 break;
1138 }
1139
1140 return res;
1141 }
1142
1143 /* Follow the ssa edge into the right hand side of an assignment STMT.
1144 Return true if the strongly connected component has been found. */
1145
1146 static t_bool
1147 follow_ssa_edge_in_rhs (struct loop *loop, gimple stmt,
1148 gimple halting_phi, tree *evolution_of_loop, int limit)
1149 {
1150 enum tree_code code = gimple_assign_rhs_code (stmt);
1151 tree type = gimple_expr_type (stmt), rhs1, rhs2;
1152 t_bool res;
1153
1154 switch (code)
1155 {
1156 CASE_CONVERT:
1157 /* This assignment is under the form "a_1 = (cast) rhs. */
1158 res = follow_ssa_edge_expr (loop, stmt, gimple_assign_rhs1 (stmt),
1159 halting_phi, evolution_of_loop, limit);
1160 *evolution_of_loop = chrec_convert (type, *evolution_of_loop, stmt);
1161 break;
1162
1163 case POINTER_PLUS_EXPR:
1164 case PLUS_EXPR:
1165 case MINUS_EXPR:
1166 rhs1 = gimple_assign_rhs1 (stmt);
1167 rhs2 = gimple_assign_rhs2 (stmt);
1168 type = TREE_TYPE (rhs1);
1169 res = follow_ssa_edge_binary (loop, stmt, type, rhs1, code, rhs2,
1170 halting_phi, evolution_of_loop, limit);
1171 break;
1172
1173 default:
1174 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1175 res = follow_ssa_edge_expr (loop, stmt, gimple_assign_rhs1 (stmt),
1176 halting_phi, evolution_of_loop, limit);
1177 else
1178 res = t_false;
1179 break;
1180 }
1181
1182 return res;
1183 }
1184
1185 /* Checks whether the I-th argument of a PHI comes from a backedge. */
1186
1187 static bool
1188 backedge_phi_arg_p (gimple phi, int i)
1189 {
1190 const_edge e = gimple_phi_arg_edge (phi, i);
1191
1192 /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
1193 about updating it anywhere, and this should work as well most of the
1194 time. */
1195 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1196 return true;
1197
1198 return false;
1199 }
1200
1201 /* Helper function for one branch of the condition-phi-node. Return
1202 true if the strongly connected component has been found following
1203 this path. */
1204
1205 static inline t_bool
1206 follow_ssa_edge_in_condition_phi_branch (int i,
1207 struct loop *loop,
1208 gimple condition_phi,
1209 gimple halting_phi,
1210 tree *evolution_of_branch,
1211 tree init_cond, int limit)
1212 {
1213 tree branch = PHI_ARG_DEF (condition_phi, i);
1214 *evolution_of_branch = chrec_dont_know;
1215
1216 /* Do not follow back edges (they must belong to an irreducible loop, which
1217 we really do not want to worry about). */
1218 if (backedge_phi_arg_p (condition_phi, i))
1219 return t_false;
1220
1221 if (TREE_CODE (branch) == SSA_NAME)
1222 {
1223 *evolution_of_branch = init_cond;
1224 return follow_ssa_edge (loop, SSA_NAME_DEF_STMT (branch), halting_phi,
1225 evolution_of_branch, limit);
1226 }
1227
1228 /* This case occurs when one of the condition branches sets
1229 the variable to a constant: i.e. a phi-node like
1230 "a_2 = PHI <a_7(5), 2(6)>;".
1231
1232 FIXME: This case have to be refined correctly:
1233 in some cases it is possible to say something better than
1234 chrec_dont_know, for example using a wrap-around notation. */
1235 return t_false;
1236 }
1237
1238 /* This function merges the branches of a condition-phi-node in a
1239 loop. */
1240
1241 static t_bool
1242 follow_ssa_edge_in_condition_phi (struct loop *loop,
1243 gimple condition_phi,
1244 gimple halting_phi,
1245 tree *evolution_of_loop, int limit)
1246 {
1247 int i, n;
1248 tree init = *evolution_of_loop;
1249 tree evolution_of_branch;
1250 t_bool res = follow_ssa_edge_in_condition_phi_branch (0, loop, condition_phi,
1251 halting_phi,
1252 &evolution_of_branch,
1253 init, limit);
1254 if (res == t_false || res == t_dont_know)
1255 return res;
1256
1257 *evolution_of_loop = evolution_of_branch;
1258
1259 n = gimple_phi_num_args (condition_phi);
1260 for (i = 1; i < n; i++)
1261 {
1262 /* Quickly give up when the evolution of one of the branches is
1263 not known. */
1264 if (*evolution_of_loop == chrec_dont_know)
1265 return t_true;
1266
1267 /* Increase the limit by the PHI argument number to avoid exponential
1268 time and memory complexity. */
1269 res = follow_ssa_edge_in_condition_phi_branch (i, loop, condition_phi,
1270 halting_phi,
1271 &evolution_of_branch,
1272 init, limit + i);
1273 if (res == t_false || res == t_dont_know)
1274 return res;
1275
1276 *evolution_of_loop = chrec_merge (*evolution_of_loop,
1277 evolution_of_branch);
1278 }
1279
1280 return t_true;
1281 }
1282
1283 /* Follow an SSA edge in an inner loop. It computes the overall
1284 effect of the loop, and following the symbolic initial conditions,
1285 it follows the edges in the parent loop. The inner loop is
1286 considered as a single statement. */
1287
1288 static t_bool
1289 follow_ssa_edge_inner_loop_phi (struct loop *outer_loop,
1290 gimple loop_phi_node,
1291 gimple halting_phi,
1292 tree *evolution_of_loop, int limit)
1293 {
1294 struct loop *loop = loop_containing_stmt (loop_phi_node);
1295 tree ev = analyze_scalar_evolution (loop, PHI_RESULT (loop_phi_node));
1296
1297 /* Sometimes, the inner loop is too difficult to analyze, and the
1298 result of the analysis is a symbolic parameter. */
1299 if (ev == PHI_RESULT (loop_phi_node))
1300 {
1301 t_bool res = t_false;
1302 int i, n = gimple_phi_num_args (loop_phi_node);
1303
1304 for (i = 0; i < n; i++)
1305 {
1306 tree arg = PHI_ARG_DEF (loop_phi_node, i);
1307 basic_block bb;
1308
1309 /* Follow the edges that exit the inner loop. */
1310 bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1311 if (!flow_bb_inside_loop_p (loop, bb))
1312 res = follow_ssa_edge_expr (outer_loop, loop_phi_node,
1313 arg, halting_phi,
1314 evolution_of_loop, limit);
1315 if (res == t_true)
1316 break;
1317 }
1318
1319 /* If the path crosses this loop-phi, give up. */
1320 if (res == t_true)
1321 *evolution_of_loop = chrec_dont_know;
1322
1323 return res;
1324 }
1325
1326 /* Otherwise, compute the overall effect of the inner loop. */
1327 ev = compute_overall_effect_of_inner_loop (loop, ev);
1328 return follow_ssa_edge_expr (outer_loop, loop_phi_node, ev, halting_phi,
1329 evolution_of_loop, limit);
1330 }
1331
1332 /* Follow an SSA edge from a loop-phi-node to itself, constructing a
1333 path that is analyzed on the return walk. */
1334
1335 static t_bool
1336 follow_ssa_edge (struct loop *loop, gimple def, gimple halting_phi,
1337 tree *evolution_of_loop, int limit)
1338 {
1339 struct loop *def_loop;
1340
1341 if (gimple_nop_p (def))
1342 return t_false;
1343
1344 /* Give up if the path is longer than the MAX that we allow. */
1345 if (limit > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_COMPLEXITY))
1346 return t_dont_know;
1347
1348 def_loop = loop_containing_stmt (def);
1349
1350 switch (gimple_code (def))
1351 {
1352 case GIMPLE_PHI:
1353 if (!loop_phi_node_p (def))
1354 /* DEF is a condition-phi-node. Follow the branches, and
1355 record their evolutions. Finally, merge the collected
1356 information and set the approximation to the main
1357 variable. */
1358 return follow_ssa_edge_in_condition_phi
1359 (loop, def, halting_phi, evolution_of_loop, limit);
1360
1361 /* When the analyzed phi is the halting_phi, the
1362 depth-first search is over: we have found a path from
1363 the halting_phi to itself in the loop. */
1364 if (def == halting_phi)
1365 return t_true;
1366
1367 /* Otherwise, the evolution of the HALTING_PHI depends
1368 on the evolution of another loop-phi-node, i.e. the
1369 evolution function is a higher degree polynomial. */
1370 if (def_loop == loop)
1371 return t_false;
1372
1373 /* Inner loop. */
1374 if (flow_loop_nested_p (loop, def_loop))
1375 return follow_ssa_edge_inner_loop_phi
1376 (loop, def, halting_phi, evolution_of_loop, limit + 1);
1377
1378 /* Outer loop. */
1379 return t_false;
1380
1381 case GIMPLE_ASSIGN:
1382 return follow_ssa_edge_in_rhs (loop, def, halting_phi,
1383 evolution_of_loop, limit);
1384
1385 default:
1386 /* At this level of abstraction, the program is just a set
1387 of GIMPLE_ASSIGNs and PHI_NODEs. In principle there is no
1388 other node to be handled. */
1389 return t_false;
1390 }
1391 }
1392
1393 \f
1394
1395 /* Given a LOOP_PHI_NODE, this function determines the evolution
1396 function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */
1397
1398 static tree
1399 analyze_evolution_in_loop (gimple loop_phi_node,
1400 tree init_cond)
1401 {
1402 int i, n = gimple_phi_num_args (loop_phi_node);
1403 tree evolution_function = chrec_not_analyzed_yet;
1404 struct loop *loop = loop_containing_stmt (loop_phi_node);
1405 basic_block bb;
1406
1407 if (dump_file && (dump_flags & TDF_SCEV))
1408 {
1409 fprintf (dump_file, "(analyze_evolution_in_loop \n");
1410 fprintf (dump_file, " (loop_phi_node = ");
1411 print_gimple_stmt (dump_file, loop_phi_node, 0, 0);
1412 fprintf (dump_file, ")\n");
1413 }
1414
1415 for (i = 0; i < n; i++)
1416 {
1417 tree arg = PHI_ARG_DEF (loop_phi_node, i);
1418 gimple ssa_chain;
1419 tree ev_fn;
1420 t_bool res;
1421
1422 /* Select the edges that enter the loop body. */
1423 bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1424 if (!flow_bb_inside_loop_p (loop, bb))
1425 continue;
1426
1427 if (TREE_CODE (arg) == SSA_NAME)
1428 {
1429 bool val = false;
1430
1431 ssa_chain = SSA_NAME_DEF_STMT (arg);
1432
1433 /* Pass in the initial condition to the follow edge function. */
1434 ev_fn = init_cond;
1435 res = follow_ssa_edge (loop, ssa_chain, loop_phi_node, &ev_fn, 0);
1436
1437 /* If ev_fn has no evolution in the inner loop, and the
1438 init_cond is not equal to ev_fn, then we have an
1439 ambiguity between two possible values, as we cannot know
1440 the number of iterations at this point. */
1441 if (TREE_CODE (ev_fn) != POLYNOMIAL_CHREC
1442 && no_evolution_in_loop_p (ev_fn, loop->num, &val) && val
1443 && !operand_equal_p (init_cond, ev_fn, 0))
1444 ev_fn = chrec_dont_know;
1445 }
1446 else
1447 res = t_false;
1448
1449 /* When it is impossible to go back on the same
1450 loop_phi_node by following the ssa edges, the
1451 evolution is represented by a peeled chrec, i.e. the
1452 first iteration, EV_FN has the value INIT_COND, then
1453 all the other iterations it has the value of ARG.
1454 For the moment, PEELED_CHREC nodes are not built. */
1455 if (res != t_true)
1456 ev_fn = chrec_dont_know;
1457
1458 /* When there are multiple back edges of the loop (which in fact never
1459 happens currently, but nevertheless), merge their evolutions. */
1460 evolution_function = chrec_merge (evolution_function, ev_fn);
1461 }
1462
1463 if (dump_file && (dump_flags & TDF_SCEV))
1464 {
1465 fprintf (dump_file, " (evolution_function = ");
1466 print_generic_expr (dump_file, evolution_function, 0);
1467 fprintf (dump_file, "))\n");
1468 }
1469
1470 return evolution_function;
1471 }
1472
1473 /* Given a loop-phi-node, return the initial conditions of the
1474 variable on entry of the loop. When the CCP has propagated
1475 constants into the loop-phi-node, the initial condition is
1476 instantiated, otherwise the initial condition is kept symbolic.
1477 This analyzer does not analyze the evolution outside the current
1478 loop, and leaves this task to the on-demand tree reconstructor. */
1479
1480 static tree
1481 analyze_initial_condition (gimple loop_phi_node)
1482 {
1483 int i, n;
1484 tree init_cond = chrec_not_analyzed_yet;
1485 struct loop *loop = loop_containing_stmt (loop_phi_node);
1486
1487 if (dump_file && (dump_flags & TDF_SCEV))
1488 {
1489 fprintf (dump_file, "(analyze_initial_condition \n");
1490 fprintf (dump_file, " (loop_phi_node = \n");
1491 print_gimple_stmt (dump_file, loop_phi_node, 0, 0);
1492 fprintf (dump_file, ")\n");
1493 }
1494
1495 n = gimple_phi_num_args (loop_phi_node);
1496 for (i = 0; i < n; i++)
1497 {
1498 tree branch = PHI_ARG_DEF (loop_phi_node, i);
1499 basic_block bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1500
1501 /* When the branch is oriented to the loop's body, it does
1502 not contribute to the initial condition. */
1503 if (flow_bb_inside_loop_p (loop, bb))
1504 continue;
1505
1506 if (init_cond == chrec_not_analyzed_yet)
1507 {
1508 init_cond = branch;
1509 continue;
1510 }
1511
1512 if (TREE_CODE (branch) == SSA_NAME)
1513 {
1514 init_cond = chrec_dont_know;
1515 break;
1516 }
1517
1518 init_cond = chrec_merge (init_cond, branch);
1519 }
1520
1521 /* Ooops -- a loop without an entry??? */
1522 if (init_cond == chrec_not_analyzed_yet)
1523 init_cond = chrec_dont_know;
1524
1525 /* During early loop unrolling we do not have fully constant propagated IL.
1526 Handle degenerate PHIs here to not miss important unrollings. */
1527 if (TREE_CODE (init_cond) == SSA_NAME)
1528 {
1529 gimple def = SSA_NAME_DEF_STMT (init_cond);
1530 tree res;
1531 if (gimple_code (def) == GIMPLE_PHI
1532 && (res = degenerate_phi_result (def)) != NULL_TREE
1533 /* Only allow invariants here, otherwise we may break
1534 loop-closed SSA form. */
1535 && is_gimple_min_invariant (res))
1536 init_cond = res;
1537 }
1538
1539 if (dump_file && (dump_flags & TDF_SCEV))
1540 {
1541 fprintf (dump_file, " (init_cond = ");
1542 print_generic_expr (dump_file, init_cond, 0);
1543 fprintf (dump_file, "))\n");
1544 }
1545
1546 return init_cond;
1547 }
1548
1549 /* Analyze the scalar evolution for LOOP_PHI_NODE. */
1550
1551 static tree
1552 interpret_loop_phi (struct loop *loop, gimple loop_phi_node)
1553 {
1554 tree res;
1555 struct loop *phi_loop = loop_containing_stmt (loop_phi_node);
1556 tree init_cond;
1557
1558 if (phi_loop != loop)
1559 {
1560 struct loop *subloop;
1561 tree evolution_fn = analyze_scalar_evolution
1562 (phi_loop, PHI_RESULT (loop_phi_node));
1563
1564 /* Dive one level deeper. */
1565 subloop = superloop_at_depth (phi_loop, loop_depth (loop) + 1);
1566
1567 /* Interpret the subloop. */
1568 res = compute_overall_effect_of_inner_loop (subloop, evolution_fn);
1569 return res;
1570 }
1571
1572 /* Otherwise really interpret the loop phi. */
1573 init_cond = analyze_initial_condition (loop_phi_node);
1574 res = analyze_evolution_in_loop (loop_phi_node, init_cond);
1575
1576 /* Verify we maintained the correct initial condition throughout
1577 possible conversions in the SSA chain. */
1578 if (res != chrec_dont_know)
1579 {
1580 tree new_init = res;
1581 if (CONVERT_EXPR_P (res)
1582 && TREE_CODE (TREE_OPERAND (res, 0)) == POLYNOMIAL_CHREC)
1583 new_init = fold_convert (TREE_TYPE (res),
1584 CHREC_LEFT (TREE_OPERAND (res, 0)));
1585 else if (TREE_CODE (res) == POLYNOMIAL_CHREC)
1586 new_init = CHREC_LEFT (res);
1587 STRIP_USELESS_TYPE_CONVERSION (new_init);
1588 if (TREE_CODE (new_init) == POLYNOMIAL_CHREC
1589 || !operand_equal_p (init_cond, new_init, 0))
1590 return chrec_dont_know;
1591 }
1592
1593 return res;
1594 }
1595
1596 /* This function merges the branches of a condition-phi-node,
1597 contained in the outermost loop, and whose arguments are already
1598 analyzed. */
1599
1600 static tree
1601 interpret_condition_phi (struct loop *loop, gimple condition_phi)
1602 {
1603 int i, n = gimple_phi_num_args (condition_phi);
1604 tree res = chrec_not_analyzed_yet;
1605
1606 for (i = 0; i < n; i++)
1607 {
1608 tree branch_chrec;
1609
1610 if (backedge_phi_arg_p (condition_phi, i))
1611 {
1612 res = chrec_dont_know;
1613 break;
1614 }
1615
1616 branch_chrec = analyze_scalar_evolution
1617 (loop, PHI_ARG_DEF (condition_phi, i));
1618
1619 res = chrec_merge (res, branch_chrec);
1620 }
1621
1622 return res;
1623 }
1624
1625 /* Interpret the operation RHS1 OP RHS2. If we didn't
1626 analyze this node before, follow the definitions until ending
1627 either on an analyzed GIMPLE_ASSIGN, or on a loop-phi-node. On the
1628 return path, this function propagates evolutions (ala constant copy
1629 propagation). OPND1 is not a GIMPLE expression because we could
1630 analyze the effect of an inner loop: see interpret_loop_phi. */
1631
1632 static tree
1633 interpret_rhs_expr (struct loop *loop, gimple at_stmt,
1634 tree type, tree rhs1, enum tree_code code, tree rhs2)
1635 {
1636 tree res, chrec1, chrec2;
1637
1638 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1639 {
1640 if (is_gimple_min_invariant (rhs1))
1641 return chrec_convert (type, rhs1, at_stmt);
1642
1643 if (code == SSA_NAME)
1644 return chrec_convert (type, analyze_scalar_evolution (loop, rhs1),
1645 at_stmt);
1646
1647 if (code == ASSERT_EXPR)
1648 {
1649 rhs1 = ASSERT_EXPR_VAR (rhs1);
1650 return chrec_convert (type, analyze_scalar_evolution (loop, rhs1),
1651 at_stmt);
1652 }
1653 }
1654
1655 switch (code)
1656 {
1657 case ADDR_EXPR:
1658 if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF
1659 || handled_component_p (TREE_OPERAND (rhs1, 0)))
1660 {
1661 enum machine_mode mode;
1662 HOST_WIDE_INT bitsize, bitpos;
1663 int unsignedp;
1664 int volatilep = 0;
1665 tree base, offset;
1666 tree chrec3;
1667 tree unitpos;
1668
1669 base = get_inner_reference (TREE_OPERAND (rhs1, 0),
1670 &bitsize, &bitpos, &offset,
1671 &mode, &unsignedp, &volatilep, false);
1672
1673 if (TREE_CODE (base) == MEM_REF)
1674 {
1675 rhs2 = TREE_OPERAND (base, 1);
1676 rhs1 = TREE_OPERAND (base, 0);
1677
1678 chrec1 = analyze_scalar_evolution (loop, rhs1);
1679 chrec2 = analyze_scalar_evolution (loop, rhs2);
1680 chrec1 = chrec_convert (type, chrec1, at_stmt);
1681 chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt);
1682 res = chrec_fold_plus (type, chrec1, chrec2);
1683 }
1684 else
1685 {
1686 chrec1 = analyze_scalar_evolution_for_address_of (loop, base);
1687 chrec1 = chrec_convert (type, chrec1, at_stmt);
1688 res = chrec1;
1689 }
1690
1691 if (offset != NULL_TREE)
1692 {
1693 chrec2 = analyze_scalar_evolution (loop, offset);
1694 chrec2 = chrec_convert (TREE_TYPE (offset), chrec2, at_stmt);
1695 res = chrec_fold_plus (type, res, chrec2);
1696 }
1697
1698 if (bitpos != 0)
1699 {
1700 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
1701
1702 unitpos = size_int (bitpos / BITS_PER_UNIT);
1703 chrec3 = analyze_scalar_evolution (loop, unitpos);
1704 chrec3 = chrec_convert (TREE_TYPE (unitpos), chrec3, at_stmt);
1705 res = chrec_fold_plus (type, res, chrec3);
1706 }
1707 }
1708 else
1709 res = chrec_dont_know;
1710 break;
1711
1712 case POINTER_PLUS_EXPR:
1713 chrec1 = analyze_scalar_evolution (loop, rhs1);
1714 chrec2 = analyze_scalar_evolution (loop, rhs2);
1715 chrec1 = chrec_convert (type, chrec1, at_stmt);
1716 chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt);
1717 res = chrec_fold_plus (type, chrec1, chrec2);
1718 break;
1719
1720 case PLUS_EXPR:
1721 chrec1 = analyze_scalar_evolution (loop, rhs1);
1722 chrec2 = analyze_scalar_evolution (loop, rhs2);
1723 chrec1 = chrec_convert (type, chrec1, at_stmt);
1724 chrec2 = chrec_convert (type, chrec2, at_stmt);
1725 res = chrec_fold_plus (type, chrec1, chrec2);
1726 break;
1727
1728 case MINUS_EXPR:
1729 chrec1 = analyze_scalar_evolution (loop, rhs1);
1730 chrec2 = analyze_scalar_evolution (loop, rhs2);
1731 chrec1 = chrec_convert (type, chrec1, at_stmt);
1732 chrec2 = chrec_convert (type, chrec2, at_stmt);
1733 res = chrec_fold_minus (type, chrec1, chrec2);
1734 break;
1735
1736 case NEGATE_EXPR:
1737 chrec1 = analyze_scalar_evolution (loop, rhs1);
1738 chrec1 = chrec_convert (type, chrec1, at_stmt);
1739 /* TYPE may be integer, real or complex, so use fold_convert. */
1740 res = chrec_fold_multiply (type, chrec1,
1741 fold_convert (type, integer_minus_one_node));
1742 break;
1743
1744 case BIT_NOT_EXPR:
1745 /* Handle ~X as -1 - X. */
1746 chrec1 = analyze_scalar_evolution (loop, rhs1);
1747 chrec1 = chrec_convert (type, chrec1, at_stmt);
1748 res = chrec_fold_minus (type,
1749 fold_convert (type, integer_minus_one_node),
1750 chrec1);
1751 break;
1752
1753 case MULT_EXPR:
1754 chrec1 = analyze_scalar_evolution (loop, rhs1);
1755 chrec2 = analyze_scalar_evolution (loop, rhs2);
1756 chrec1 = chrec_convert (type, chrec1, at_stmt);
1757 chrec2 = chrec_convert (type, chrec2, at_stmt);
1758 res = chrec_fold_multiply (type, chrec1, chrec2);
1759 break;
1760
1761 CASE_CONVERT:
1762 chrec1 = analyze_scalar_evolution (loop, rhs1);
1763 res = chrec_convert (type, chrec1, at_stmt);
1764 break;
1765
1766 default:
1767 res = chrec_dont_know;
1768 break;
1769 }
1770
1771 return res;
1772 }
1773
1774 /* Interpret the expression EXPR. */
1775
1776 static tree
1777 interpret_expr (struct loop *loop, gimple at_stmt, tree expr)
1778 {
1779 enum tree_code code;
1780 tree type = TREE_TYPE (expr), op0, op1;
1781
1782 if (automatically_generated_chrec_p (expr))
1783 return expr;
1784
1785 if (TREE_CODE (expr) == POLYNOMIAL_CHREC
1786 || get_gimple_rhs_class (TREE_CODE (expr)) == GIMPLE_TERNARY_RHS)
1787 return chrec_dont_know;
1788
1789 extract_ops_from_tree (expr, &code, &op0, &op1);
1790
1791 return interpret_rhs_expr (loop, at_stmt, type,
1792 op0, code, op1);
1793 }
1794
1795 /* Interpret the rhs of the assignment STMT. */
1796
1797 static tree
1798 interpret_gimple_assign (struct loop *loop, gimple stmt)
1799 {
1800 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
1801 enum tree_code code = gimple_assign_rhs_code (stmt);
1802
1803 return interpret_rhs_expr (loop, stmt, type,
1804 gimple_assign_rhs1 (stmt), code,
1805 gimple_assign_rhs2 (stmt));
1806 }
1807
1808 \f
1809
1810 /* This section contains all the entry points:
1811 - number_of_iterations_in_loop,
1812 - analyze_scalar_evolution,
1813 - instantiate_parameters.
1814 */
1815
1816 /* Compute and return the evolution function in WRTO_LOOP, the nearest
1817 common ancestor of DEF_LOOP and USE_LOOP. */
1818
1819 static tree
1820 compute_scalar_evolution_in_loop (struct loop *wrto_loop,
1821 struct loop *def_loop,
1822 tree ev)
1823 {
1824 bool val;
1825 tree res;
1826
1827 if (def_loop == wrto_loop)
1828 return ev;
1829
1830 def_loop = superloop_at_depth (def_loop, loop_depth (wrto_loop) + 1);
1831 res = compute_overall_effect_of_inner_loop (def_loop, ev);
1832
1833 if (no_evolution_in_loop_p (res, wrto_loop->num, &val) && val)
1834 return res;
1835
1836 return analyze_scalar_evolution_1 (wrto_loop, res, chrec_not_analyzed_yet);
1837 }
1838
1839 /* Helper recursive function. */
1840
1841 static tree
1842 analyze_scalar_evolution_1 (struct loop *loop, tree var, tree res)
1843 {
1844 tree type = TREE_TYPE (var);
1845 gimple def;
1846 basic_block bb;
1847 struct loop *def_loop;
1848
1849 if (loop == NULL || TREE_CODE (type) == VECTOR_TYPE)
1850 return chrec_dont_know;
1851
1852 if (TREE_CODE (var) != SSA_NAME)
1853 return interpret_expr (loop, NULL, var);
1854
1855 def = SSA_NAME_DEF_STMT (var);
1856 bb = gimple_bb (def);
1857 def_loop = bb ? bb->loop_father : NULL;
1858
1859 if (bb == NULL
1860 || !flow_bb_inside_loop_p (loop, bb))
1861 {
1862 /* Keep the symbolic form. */
1863 res = var;
1864 goto set_and_end;
1865 }
1866
1867 if (res != chrec_not_analyzed_yet)
1868 {
1869 if (loop != bb->loop_father)
1870 res = compute_scalar_evolution_in_loop
1871 (find_common_loop (loop, bb->loop_father), bb->loop_father, res);
1872
1873 goto set_and_end;
1874 }
1875
1876 if (loop != def_loop)
1877 {
1878 res = analyze_scalar_evolution_1 (def_loop, var, chrec_not_analyzed_yet);
1879 res = compute_scalar_evolution_in_loop (loop, def_loop, res);
1880
1881 goto set_and_end;
1882 }
1883
1884 switch (gimple_code (def))
1885 {
1886 case GIMPLE_ASSIGN:
1887 res = interpret_gimple_assign (loop, def);
1888 break;
1889
1890 case GIMPLE_PHI:
1891 if (loop_phi_node_p (def))
1892 res = interpret_loop_phi (loop, def);
1893 else
1894 res = interpret_condition_phi (loop, def);
1895 break;
1896
1897 default:
1898 res = chrec_dont_know;
1899 break;
1900 }
1901
1902 set_and_end:
1903
1904 /* Keep the symbolic form. */
1905 if (res == chrec_dont_know)
1906 res = var;
1907
1908 if (loop == def_loop)
1909 set_scalar_evolution (block_before_loop (loop), var, res);
1910
1911 return res;
1912 }
1913
1914 /* Analyzes and returns the scalar evolution of the ssa_name VAR in
1915 LOOP. LOOP is the loop in which the variable is used.
1916
1917 Example of use: having a pointer VAR to a SSA_NAME node, STMT a
1918 pointer to the statement that uses this variable, in order to
1919 determine the evolution function of the variable, use the following
1920 calls:
1921
1922 loop_p loop = loop_containing_stmt (stmt);
1923 tree chrec_with_symbols = analyze_scalar_evolution (loop, var);
1924 tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols);
1925 */
1926
1927 tree
1928 analyze_scalar_evolution (struct loop *loop, tree var)
1929 {
1930 tree res;
1931
1932 if (dump_file && (dump_flags & TDF_SCEV))
1933 {
1934 fprintf (dump_file, "(analyze_scalar_evolution \n");
1935 fprintf (dump_file, " (loop_nb = %d)\n", loop->num);
1936 fprintf (dump_file, " (scalar = ");
1937 print_generic_expr (dump_file, var, 0);
1938 fprintf (dump_file, ")\n");
1939 }
1940
1941 res = get_scalar_evolution (block_before_loop (loop), var);
1942 res = analyze_scalar_evolution_1 (loop, var, res);
1943
1944 if (dump_file && (dump_flags & TDF_SCEV))
1945 fprintf (dump_file, ")\n");
1946
1947 return res;
1948 }
1949
1950 /* Analyzes and returns the scalar evolution of VAR address in LOOP. */
1951
1952 static tree
1953 analyze_scalar_evolution_for_address_of (struct loop *loop, tree var)
1954 {
1955 return analyze_scalar_evolution (loop, build_fold_addr_expr (var));
1956 }
1957
1958 /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
1959 WRTO_LOOP (which should be a superloop of USE_LOOP)
1960
1961 FOLDED_CASTS is set to true if resolve_mixers used
1962 chrec_convert_aggressive (TODO -- not really, we are way too conservative
1963 at the moment in order to keep things simple).
1964
1965 To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following
1966 example:
1967
1968 for (i = 0; i < 100; i++) -- loop 1
1969 {
1970 for (j = 0; j < 100; j++) -- loop 2
1971 {
1972 k1 = i;
1973 k2 = j;
1974
1975 use2 (k1, k2);
1976
1977 for (t = 0; t < 100; t++) -- loop 3
1978 use3 (k1, k2);
1979
1980 }
1981 use1 (k1, k2);
1982 }
1983
1984 Both k1 and k2 are invariants in loop3, thus
1985 analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1
1986 analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2
1987
1988 As they are invariant, it does not matter whether we consider their
1989 usage in loop 3 or loop 2, hence
1990 analyze_scalar_evolution_in_loop (loop2, loop3, k1) =
1991 analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i
1992 analyze_scalar_evolution_in_loop (loop2, loop3, k2) =
1993 analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2
1994
1995 Similarly for their evolutions with respect to loop 1. The values of K2
1996 in the use in loop 2 vary independently on loop 1, thus we cannot express
1997 the evolution with respect to loop 1:
1998 analyze_scalar_evolution_in_loop (loop1, loop3, k1) =
1999 analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1
2000 analyze_scalar_evolution_in_loop (loop1, loop3, k2) =
2001 analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know
2002
2003 The value of k2 in the use in loop 1 is known, though:
2004 analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1
2005 analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100
2006 */
2007
2008 static tree
2009 analyze_scalar_evolution_in_loop (struct loop *wrto_loop, struct loop *use_loop,
2010 tree version, bool *folded_casts)
2011 {
2012 bool val = false;
2013 tree ev = version, tmp;
2014
2015 /* We cannot just do
2016
2017 tmp = analyze_scalar_evolution (use_loop, version);
2018 ev = resolve_mixers (wrto_loop, tmp);
2019
2020 as resolve_mixers would query the scalar evolution with respect to
2021 wrto_loop. For example, in the situation described in the function
2022 comment, suppose that wrto_loop = loop1, use_loop = loop3 and
2023 version = k2. Then
2024
2025 analyze_scalar_evolution (use_loop, version) = k2
2026
2027 and resolve_mixers (loop1, k2) finds that the value of k2 in loop 1
2028 is 100, which is a wrong result, since we are interested in the
2029 value in loop 3.
2030
2031 Instead, we need to proceed from use_loop to wrto_loop loop by loop,
2032 each time checking that there is no evolution in the inner loop. */
2033
2034 if (folded_casts)
2035 *folded_casts = false;
2036 while (1)
2037 {
2038 tmp = analyze_scalar_evolution (use_loop, ev);
2039 ev = resolve_mixers (use_loop, tmp);
2040
2041 if (folded_casts && tmp != ev)
2042 *folded_casts = true;
2043
2044 if (use_loop == wrto_loop)
2045 return ev;
2046
2047 /* If the value of the use changes in the inner loop, we cannot express
2048 its value in the outer loop (we might try to return interval chrec,
2049 but we do not have a user for it anyway) */
2050 if (!no_evolution_in_loop_p (ev, use_loop->num, &val)
2051 || !val)
2052 return chrec_dont_know;
2053
2054 use_loop = loop_outer (use_loop);
2055 }
2056 }
2057
2058 /* Returns from CACHE the value for VERSION instantiated below
2059 INSTANTIATED_BELOW block. */
2060
2061 static tree
2062 get_instantiated_value (htab_t cache, basic_block instantiated_below,
2063 tree version)
2064 {
2065 struct scev_info_str *info, pattern;
2066
2067 pattern.var = version;
2068 pattern.instantiated_below = instantiated_below;
2069 info = (struct scev_info_str *) htab_find (cache, &pattern);
2070
2071 if (info)
2072 return info->chrec;
2073 else
2074 return NULL_TREE;
2075 }
2076
2077 /* Sets in CACHE the value of VERSION instantiated below basic block
2078 INSTANTIATED_BELOW to VAL. */
2079
2080 static void
2081 set_instantiated_value (htab_t cache, basic_block instantiated_below,
2082 tree version, tree val)
2083 {
2084 struct scev_info_str *info, pattern;
2085 PTR *slot;
2086
2087 pattern.var = version;
2088 pattern.instantiated_below = instantiated_below;
2089 slot = htab_find_slot (cache, &pattern, INSERT);
2090
2091 if (!*slot)
2092 *slot = new_scev_info_str (instantiated_below, version);
2093 info = (struct scev_info_str *) *slot;
2094 info->chrec = val;
2095 }
2096
2097 /* Return the closed_loop_phi node for VAR. If there is none, return
2098 NULL_TREE. */
2099
2100 static tree
2101 loop_closed_phi_def (tree var)
2102 {
2103 struct loop *loop;
2104 edge exit;
2105 gimple phi;
2106 gimple_stmt_iterator psi;
2107
2108 if (var == NULL_TREE
2109 || TREE_CODE (var) != SSA_NAME)
2110 return NULL_TREE;
2111
2112 loop = loop_containing_stmt (SSA_NAME_DEF_STMT (var));
2113 exit = single_exit (loop);
2114 if (!exit)
2115 return NULL_TREE;
2116
2117 for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi))
2118 {
2119 phi = gsi_stmt (psi);
2120 if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == var)
2121 return PHI_RESULT (phi);
2122 }
2123
2124 return NULL_TREE;
2125 }
2126
2127 static tree instantiate_scev_r (basic_block, struct loop *, tree, bool,
2128 htab_t, int);
2129
2130 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2131 and EVOLUTION_LOOP, that were left under a symbolic form.
2132
2133 CHREC is an SSA_NAME to be instantiated.
2134
2135 CACHE is the cache of already instantiated values.
2136
2137 FOLD_CONVERSIONS should be set to true when the conversions that
2138 may wrap in signed/pointer type are folded, as long as the value of
2139 the chrec is preserved.
2140
2141 SIZE_EXPR is used for computing the size of the expression to be
2142 instantiated, and to stop if it exceeds some limit. */
2143
2144 static tree
2145 instantiate_scev_name (basic_block instantiate_below,
2146 struct loop *evolution_loop, tree chrec,
2147 bool fold_conversions, htab_t cache, int size_expr)
2148 {
2149 tree res;
2150 struct loop *def_loop;
2151 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (chrec));
2152
2153 /* A parameter (or loop invariant and we do not want to include
2154 evolutions in outer loops), nothing to do. */
2155 if (!def_bb
2156 || loop_depth (def_bb->loop_father) == 0
2157 || dominated_by_p (CDI_DOMINATORS, instantiate_below, def_bb))
2158 return chrec;
2159
2160 /* We cache the value of instantiated variable to avoid exponential
2161 time complexity due to reevaluations. We also store the convenient
2162 value in the cache in order to prevent infinite recursion -- we do
2163 not want to instantiate the SSA_NAME if it is in a mixer
2164 structure. This is used for avoiding the instantiation of
2165 recursively defined functions, such as:
2166
2167 | a_2 -> {0, +, 1, +, a_2}_1 */
2168
2169 res = get_instantiated_value (cache, instantiate_below, chrec);
2170 if (res)
2171 return res;
2172
2173 res = chrec_dont_know;
2174 set_instantiated_value (cache, instantiate_below, chrec, res);
2175
2176 def_loop = find_common_loop (evolution_loop, def_bb->loop_father);
2177
2178 /* If the analysis yields a parametric chrec, instantiate the
2179 result again. */
2180 res = analyze_scalar_evolution (def_loop, chrec);
2181
2182 /* Don't instantiate default definitions. */
2183 if (TREE_CODE (res) == SSA_NAME
2184 && SSA_NAME_IS_DEFAULT_DEF (res))
2185 ;
2186
2187 /* Don't instantiate loop-closed-ssa phi nodes. */
2188 else if (TREE_CODE (res) == SSA_NAME
2189 && loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res)))
2190 > loop_depth (def_loop))
2191 {
2192 if (res == chrec)
2193 res = loop_closed_phi_def (chrec);
2194 else
2195 res = chrec;
2196
2197 /* When there is no loop_closed_phi_def, it means that the
2198 variable is not used after the loop: try to still compute the
2199 value of the variable when exiting the loop. */
2200 if (res == NULL_TREE)
2201 {
2202 loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (chrec));
2203 res = analyze_scalar_evolution (loop, chrec);
2204 res = compute_overall_effect_of_inner_loop (loop, res);
2205 res = instantiate_scev_r (instantiate_below, evolution_loop, res,
2206 fold_conversions, cache, size_expr);
2207 }
2208 else if (!dominated_by_p (CDI_DOMINATORS, instantiate_below,
2209 gimple_bb (SSA_NAME_DEF_STMT (res))))
2210 res = chrec_dont_know;
2211 }
2212
2213 else if (res != chrec_dont_know)
2214 res = instantiate_scev_r (instantiate_below, evolution_loop, res,
2215 fold_conversions, cache, size_expr);
2216
2217 /* Store the correct value to the cache. */
2218 set_instantiated_value (cache, instantiate_below, chrec, res);
2219 return res;
2220 }
2221
2222 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2223 and EVOLUTION_LOOP, that were left under a symbolic form.
2224
2225 CHREC is a polynomial chain of recurrence to be instantiated.
2226
2227 CACHE is the cache of already instantiated values.
2228
2229 FOLD_CONVERSIONS should be set to true when the conversions that
2230 may wrap in signed/pointer type are folded, as long as the value of
2231 the chrec is preserved.
2232
2233 SIZE_EXPR is used for computing the size of the expression to be
2234 instantiated, and to stop if it exceeds some limit. */
2235
2236 static tree
2237 instantiate_scev_poly (basic_block instantiate_below,
2238 struct loop *evolution_loop, tree chrec,
2239 bool fold_conversions, htab_t cache, int size_expr)
2240 {
2241 tree op1;
2242 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2243 CHREC_LEFT (chrec), fold_conversions, cache,
2244 size_expr);
2245 if (op0 == chrec_dont_know)
2246 return chrec_dont_know;
2247
2248 op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2249 CHREC_RIGHT (chrec), fold_conversions, cache,
2250 size_expr);
2251 if (op1 == chrec_dont_know)
2252 return chrec_dont_know;
2253
2254 if (CHREC_LEFT (chrec) != op0
2255 || CHREC_RIGHT (chrec) != op1)
2256 {
2257 unsigned var = CHREC_VARIABLE (chrec);
2258
2259 /* When the instantiated stride or base has an evolution in an
2260 innermost loop, return chrec_dont_know, as this is not a
2261 valid SCEV representation. In the reduced testcase for
2262 PR40281 we would have {0, +, {1, +, 1}_2}_1 that has no
2263 meaning. */
2264 if ((tree_is_chrec (op0) && CHREC_VARIABLE (op0) > var)
2265 || (tree_is_chrec (op1) && CHREC_VARIABLE (op1) > var))
2266 return chrec_dont_know;
2267
2268 op1 = chrec_convert_rhs (chrec_type (op0), op1, NULL);
2269 chrec = build_polynomial_chrec (var, op0, op1);
2270 }
2271
2272 return chrec;
2273 }
2274
2275 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2276 and EVOLUTION_LOOP, that were left under a symbolic form.
2277
2278 "C0 CODE C1" is a binary expression of type TYPE to be instantiated.
2279
2280 CACHE is the cache of already instantiated values.
2281
2282 FOLD_CONVERSIONS should be set to true when the conversions that
2283 may wrap in signed/pointer type are folded, as long as the value of
2284 the chrec is preserved.
2285
2286 SIZE_EXPR is used for computing the size of the expression to be
2287 instantiated, and to stop if it exceeds some limit. */
2288
2289 static tree
2290 instantiate_scev_binary (basic_block instantiate_below,
2291 struct loop *evolution_loop, tree chrec, enum tree_code code,
2292 tree type, tree c0, tree c1,
2293 bool fold_conversions, htab_t cache, int size_expr)
2294 {
2295 tree op1;
2296 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2297 c0, fold_conversions, cache,
2298 size_expr);
2299 if (op0 == chrec_dont_know)
2300 return chrec_dont_know;
2301
2302 op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2303 c1, fold_conversions, cache,
2304 size_expr);
2305 if (op1 == chrec_dont_know)
2306 return chrec_dont_know;
2307
2308 if (c0 != op0
2309 || c1 != op1)
2310 {
2311 op0 = chrec_convert (type, op0, NULL);
2312 op1 = chrec_convert_rhs (type, op1, NULL);
2313
2314 switch (code)
2315 {
2316 case POINTER_PLUS_EXPR:
2317 case PLUS_EXPR:
2318 return chrec_fold_plus (type, op0, op1);
2319
2320 case MINUS_EXPR:
2321 return chrec_fold_minus (type, op0, op1);
2322
2323 case MULT_EXPR:
2324 return chrec_fold_multiply (type, op0, op1);
2325
2326 default:
2327 gcc_unreachable ();
2328 }
2329 }
2330
2331 return chrec ? chrec : fold_build2 (code, type, c0, c1);
2332 }
2333
2334 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2335 and EVOLUTION_LOOP, that were left under a symbolic form.
2336
2337 "CHREC" is an array reference to be instantiated.
2338
2339 CACHE is the cache of already instantiated values.
2340
2341 FOLD_CONVERSIONS should be set to true when the conversions that
2342 may wrap in signed/pointer type are folded, as long as the value of
2343 the chrec is preserved.
2344
2345 SIZE_EXPR is used for computing the size of the expression to be
2346 instantiated, and to stop if it exceeds some limit. */
2347
2348 static tree
2349 instantiate_array_ref (basic_block instantiate_below,
2350 struct loop *evolution_loop, tree chrec,
2351 bool fold_conversions, htab_t cache, int size_expr)
2352 {
2353 tree res;
2354 tree index = TREE_OPERAND (chrec, 1);
2355 tree op1 = instantiate_scev_r (instantiate_below, evolution_loop, index,
2356 fold_conversions, cache, size_expr);
2357
2358 if (op1 == chrec_dont_know)
2359 return chrec_dont_know;
2360
2361 if (chrec && op1 == index)
2362 return chrec;
2363
2364 res = unshare_expr (chrec);
2365 TREE_OPERAND (res, 1) = op1;
2366 return res;
2367 }
2368
2369 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2370 and EVOLUTION_LOOP, that were left under a symbolic form.
2371
2372 "CHREC" that stands for a convert expression "(TYPE) OP" is to be
2373 instantiated.
2374
2375 CACHE is the cache of already instantiated values.
2376
2377 FOLD_CONVERSIONS should be set to true when the conversions that
2378 may wrap in signed/pointer type are folded, as long as the value of
2379 the chrec is preserved.
2380
2381 SIZE_EXPR is used for computing the size of the expression to be
2382 instantiated, and to stop if it exceeds some limit. */
2383
2384 static tree
2385 instantiate_scev_convert (basic_block instantiate_below,
2386 struct loop *evolution_loop, tree chrec,
2387 tree type, tree op,
2388 bool fold_conversions, htab_t cache, int size_expr)
2389 {
2390 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, op,
2391 fold_conversions, cache, size_expr);
2392
2393 if (op0 == chrec_dont_know)
2394 return chrec_dont_know;
2395
2396 if (fold_conversions)
2397 {
2398 tree tmp = chrec_convert_aggressive (type, op0);
2399 if (tmp)
2400 return tmp;
2401 }
2402
2403 if (chrec && op0 == op)
2404 return chrec;
2405
2406 /* If we used chrec_convert_aggressive, we can no longer assume that
2407 signed chrecs do not overflow, as chrec_convert does, so avoid
2408 calling it in that case. */
2409 if (fold_conversions)
2410 return fold_convert (type, op0);
2411
2412 return chrec_convert (type, op0, NULL);
2413 }
2414
2415 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2416 and EVOLUTION_LOOP, that were left under a symbolic form.
2417
2418 CHREC is a BIT_NOT_EXPR or a NEGATE_EXPR expression to be instantiated.
2419 Handle ~X as -1 - X.
2420 Handle -X as -1 * X.
2421
2422 CACHE is the cache of already instantiated values.
2423
2424 FOLD_CONVERSIONS should be set to true when the conversions that
2425 may wrap in signed/pointer type are folded, as long as the value of
2426 the chrec is preserved.
2427
2428 SIZE_EXPR is used for computing the size of the expression to be
2429 instantiated, and to stop if it exceeds some limit. */
2430
2431 static tree
2432 instantiate_scev_not (basic_block instantiate_below,
2433 struct loop *evolution_loop, tree chrec,
2434 enum tree_code code, tree type, tree op,
2435 bool fold_conversions, htab_t cache, int size_expr)
2436 {
2437 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, op,
2438 fold_conversions, cache, size_expr);
2439
2440 if (op0 == chrec_dont_know)
2441 return chrec_dont_know;
2442
2443 if (op != op0)
2444 {
2445 op0 = chrec_convert (type, op0, NULL);
2446
2447 switch (code)
2448 {
2449 case BIT_NOT_EXPR:
2450 return chrec_fold_minus
2451 (type, fold_convert (type, integer_minus_one_node), op0);
2452
2453 case NEGATE_EXPR:
2454 return chrec_fold_multiply
2455 (type, fold_convert (type, integer_minus_one_node), op0);
2456
2457 default:
2458 gcc_unreachable ();
2459 }
2460 }
2461
2462 return chrec ? chrec : fold_build1 (code, type, op0);
2463 }
2464
2465 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2466 and EVOLUTION_LOOP, that were left under a symbolic form.
2467
2468 CHREC is an expression with 3 operands to be instantiated.
2469
2470 CACHE is the cache of already instantiated values.
2471
2472 FOLD_CONVERSIONS should be set to true when the conversions that
2473 may wrap in signed/pointer type are folded, as long as the value of
2474 the chrec is preserved.
2475
2476 SIZE_EXPR is used for computing the size of the expression to be
2477 instantiated, and to stop if it exceeds some limit. */
2478
2479 static tree
2480 instantiate_scev_3 (basic_block instantiate_below,
2481 struct loop *evolution_loop, tree chrec,
2482 bool fold_conversions, htab_t cache, int size_expr)
2483 {
2484 tree op1, op2;
2485 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2486 TREE_OPERAND (chrec, 0),
2487 fold_conversions, cache, size_expr);
2488 if (op0 == chrec_dont_know)
2489 return chrec_dont_know;
2490
2491 op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2492 TREE_OPERAND (chrec, 1),
2493 fold_conversions, cache, size_expr);
2494 if (op1 == chrec_dont_know)
2495 return chrec_dont_know;
2496
2497 op2 = instantiate_scev_r (instantiate_below, evolution_loop,
2498 TREE_OPERAND (chrec, 2),
2499 fold_conversions, cache, size_expr);
2500 if (op2 == chrec_dont_know)
2501 return chrec_dont_know;
2502
2503 if (op0 == TREE_OPERAND (chrec, 0)
2504 && op1 == TREE_OPERAND (chrec, 1)
2505 && op2 == TREE_OPERAND (chrec, 2))
2506 return chrec;
2507
2508 return fold_build3 (TREE_CODE (chrec),
2509 TREE_TYPE (chrec), op0, op1, op2);
2510 }
2511
2512 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2513 and EVOLUTION_LOOP, that were left under a symbolic form.
2514
2515 CHREC is an expression with 2 operands to be instantiated.
2516
2517 CACHE is the cache of already instantiated values.
2518
2519 FOLD_CONVERSIONS should be set to true when the conversions that
2520 may wrap in signed/pointer type are folded, as long as the value of
2521 the chrec is preserved.
2522
2523 SIZE_EXPR is used for computing the size of the expression to be
2524 instantiated, and to stop if it exceeds some limit. */
2525
2526 static tree
2527 instantiate_scev_2 (basic_block instantiate_below,
2528 struct loop *evolution_loop, tree chrec,
2529 bool fold_conversions, htab_t cache, int size_expr)
2530 {
2531 tree op1;
2532 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2533 TREE_OPERAND (chrec, 0),
2534 fold_conversions, cache, size_expr);
2535 if (op0 == chrec_dont_know)
2536 return chrec_dont_know;
2537
2538 op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2539 TREE_OPERAND (chrec, 1),
2540 fold_conversions, cache, size_expr);
2541 if (op1 == chrec_dont_know)
2542 return chrec_dont_know;
2543
2544 if (op0 == TREE_OPERAND (chrec, 0)
2545 && op1 == TREE_OPERAND (chrec, 1))
2546 return chrec;
2547
2548 return fold_build2 (TREE_CODE (chrec), TREE_TYPE (chrec), op0, op1);
2549 }
2550
2551 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2552 and EVOLUTION_LOOP, that were left under a symbolic form.
2553
2554 CHREC is an expression with 2 operands to be instantiated.
2555
2556 CACHE is the cache of already instantiated values.
2557
2558 FOLD_CONVERSIONS should be set to true when the conversions that
2559 may wrap in signed/pointer type are folded, as long as the value of
2560 the chrec is preserved.
2561
2562 SIZE_EXPR is used for computing the size of the expression to be
2563 instantiated, and to stop if it exceeds some limit. */
2564
2565 static tree
2566 instantiate_scev_1 (basic_block instantiate_below,
2567 struct loop *evolution_loop, tree chrec,
2568 bool fold_conversions, htab_t cache, int size_expr)
2569 {
2570 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2571 TREE_OPERAND (chrec, 0),
2572 fold_conversions, cache, size_expr);
2573
2574 if (op0 == chrec_dont_know)
2575 return chrec_dont_know;
2576
2577 if (op0 == TREE_OPERAND (chrec, 0))
2578 return chrec;
2579
2580 return fold_build1 (TREE_CODE (chrec), TREE_TYPE (chrec), op0);
2581 }
2582
2583 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2584 and EVOLUTION_LOOP, that were left under a symbolic form.
2585
2586 CHREC is the scalar evolution to instantiate.
2587
2588 CACHE is the cache of already instantiated values.
2589
2590 FOLD_CONVERSIONS should be set to true when the conversions that
2591 may wrap in signed/pointer type are folded, as long as the value of
2592 the chrec is preserved.
2593
2594 SIZE_EXPR is used for computing the size of the expression to be
2595 instantiated, and to stop if it exceeds some limit. */
2596
2597 static tree
2598 instantiate_scev_r (basic_block instantiate_below,
2599 struct loop *evolution_loop, tree chrec,
2600 bool fold_conversions, htab_t cache, int size_expr)
2601 {
2602 /* Give up if the expression is larger than the MAX that we allow. */
2603 if (size_expr++ > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE))
2604 return chrec_dont_know;
2605
2606 if (chrec == NULL_TREE
2607 || automatically_generated_chrec_p (chrec)
2608 || is_gimple_min_invariant (chrec))
2609 return chrec;
2610
2611 switch (TREE_CODE (chrec))
2612 {
2613 case SSA_NAME:
2614 return instantiate_scev_name (instantiate_below, evolution_loop, chrec,
2615 fold_conversions, cache, size_expr);
2616
2617 case POLYNOMIAL_CHREC:
2618 return instantiate_scev_poly (instantiate_below, evolution_loop, chrec,
2619 fold_conversions, cache, size_expr);
2620
2621 case POINTER_PLUS_EXPR:
2622 case PLUS_EXPR:
2623 case MINUS_EXPR:
2624 case MULT_EXPR:
2625 return instantiate_scev_binary (instantiate_below, evolution_loop, chrec,
2626 TREE_CODE (chrec), chrec_type (chrec),
2627 TREE_OPERAND (chrec, 0),
2628 TREE_OPERAND (chrec, 1),
2629 fold_conversions, cache, size_expr);
2630
2631 CASE_CONVERT:
2632 return instantiate_scev_convert (instantiate_below, evolution_loop, chrec,
2633 TREE_TYPE (chrec), TREE_OPERAND (chrec, 0),
2634 fold_conversions, cache, size_expr);
2635
2636 case NEGATE_EXPR:
2637 case BIT_NOT_EXPR:
2638 return instantiate_scev_not (instantiate_below, evolution_loop, chrec,
2639 TREE_CODE (chrec), TREE_TYPE (chrec),
2640 TREE_OPERAND (chrec, 0),
2641 fold_conversions, cache, size_expr);
2642
2643 case ADDR_EXPR:
2644 case SCEV_NOT_KNOWN:
2645 return chrec_dont_know;
2646
2647 case SCEV_KNOWN:
2648 return chrec_known;
2649
2650 case ARRAY_REF:
2651 return instantiate_array_ref (instantiate_below, evolution_loop, chrec,
2652 fold_conversions, cache, size_expr);
2653
2654 default:
2655 break;
2656 }
2657
2658 if (VL_EXP_CLASS_P (chrec))
2659 return chrec_dont_know;
2660
2661 switch (TREE_CODE_LENGTH (TREE_CODE (chrec)))
2662 {
2663 case 3:
2664 return instantiate_scev_3 (instantiate_below, evolution_loop, chrec,
2665 fold_conversions, cache, size_expr);
2666
2667 case 2:
2668 return instantiate_scev_2 (instantiate_below, evolution_loop, chrec,
2669 fold_conversions, cache, size_expr);
2670
2671 case 1:
2672 return instantiate_scev_1 (instantiate_below, evolution_loop, chrec,
2673 fold_conversions, cache, size_expr);
2674
2675 case 0:
2676 return chrec;
2677
2678 default:
2679 break;
2680 }
2681
2682 /* Too complicated to handle. */
2683 return chrec_dont_know;
2684 }
2685
2686 /* Analyze all the parameters of the chrec that were left under a
2687 symbolic form. INSTANTIATE_BELOW is the basic block that stops the
2688 recursive instantiation of parameters: a parameter is a variable
2689 that is defined in a basic block that dominates INSTANTIATE_BELOW or
2690 a function parameter. */
2691
2692 tree
2693 instantiate_scev (basic_block instantiate_below, struct loop *evolution_loop,
2694 tree chrec)
2695 {
2696 tree res;
2697 htab_t cache = htab_create (10, hash_scev_info, eq_scev_info, del_scev_info);
2698
2699 if (dump_file && (dump_flags & TDF_SCEV))
2700 {
2701 fprintf (dump_file, "(instantiate_scev \n");
2702 fprintf (dump_file, " (instantiate_below = %d)\n", instantiate_below->index);
2703 fprintf (dump_file, " (evolution_loop = %d)\n", evolution_loop->num);
2704 fprintf (dump_file, " (chrec = ");
2705 print_generic_expr (dump_file, chrec, 0);
2706 fprintf (dump_file, ")\n");
2707 }
2708
2709 res = instantiate_scev_r (instantiate_below, evolution_loop, chrec, false,
2710 cache, 0);
2711
2712 if (dump_file && (dump_flags & TDF_SCEV))
2713 {
2714 fprintf (dump_file, " (res = ");
2715 print_generic_expr (dump_file, res, 0);
2716 fprintf (dump_file, "))\n");
2717 }
2718
2719 htab_delete (cache);
2720
2721 return res;
2722 }
2723
2724 /* Similar to instantiate_parameters, but does not introduce the
2725 evolutions in outer loops for LOOP invariants in CHREC, and does not
2726 care about causing overflows, as long as they do not affect value
2727 of an expression. */
2728
2729 tree
2730 resolve_mixers (struct loop *loop, tree chrec)
2731 {
2732 htab_t cache = htab_create (10, hash_scev_info, eq_scev_info, del_scev_info);
2733 tree ret = instantiate_scev_r (block_before_loop (loop), loop, chrec, true,
2734 cache, 0);
2735 htab_delete (cache);
2736 return ret;
2737 }
2738
2739 /* Entry point for the analysis of the number of iterations pass.
2740 This function tries to safely approximate the number of iterations
2741 the loop will run. When this property is not decidable at compile
2742 time, the result is chrec_dont_know. Otherwise the result is a
2743 scalar or a symbolic parameter. When the number of iterations may
2744 be equal to zero and the property cannot be determined at compile
2745 time, the result is a COND_EXPR that represents in a symbolic form
2746 the conditions under which the number of iterations is not zero.
2747
2748 Example of analysis: suppose that the loop has an exit condition:
2749
2750 "if (b > 49) goto end_loop;"
2751
2752 and that in a previous analysis we have determined that the
2753 variable 'b' has an evolution function:
2754
2755 "EF = {23, +, 5}_2".
2756
2757 When we evaluate the function at the point 5, i.e. the value of the
2758 variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
2759 and EF (6) = 53. In this case the value of 'b' on exit is '53' and
2760 the loop body has been executed 6 times. */
2761
2762 tree
2763 number_of_latch_executions (struct loop *loop)
2764 {
2765 edge exit;
2766 struct tree_niter_desc niter_desc;
2767 tree may_be_zero;
2768 tree res;
2769
2770 /* Determine whether the number of iterations in loop has already
2771 been computed. */
2772 res = loop->nb_iterations;
2773 if (res)
2774 return res;
2775
2776 may_be_zero = NULL_TREE;
2777
2778 if (dump_file && (dump_flags & TDF_SCEV))
2779 fprintf (dump_file, "(number_of_iterations_in_loop = \n");
2780
2781 res = chrec_dont_know;
2782 exit = single_exit (loop);
2783
2784 if (exit && number_of_iterations_exit (loop, exit, &niter_desc, false))
2785 {
2786 may_be_zero = niter_desc.may_be_zero;
2787 res = niter_desc.niter;
2788 }
2789
2790 if (res == chrec_dont_know
2791 || !may_be_zero
2792 || integer_zerop (may_be_zero))
2793 ;
2794 else if (integer_nonzerop (may_be_zero))
2795 res = build_int_cst (TREE_TYPE (res), 0);
2796
2797 else if (COMPARISON_CLASS_P (may_be_zero))
2798 res = fold_build3 (COND_EXPR, TREE_TYPE (res), may_be_zero,
2799 build_int_cst (TREE_TYPE (res), 0), res);
2800 else
2801 res = chrec_dont_know;
2802
2803 if (dump_file && (dump_flags & TDF_SCEV))
2804 {
2805 fprintf (dump_file, " (set_nb_iterations_in_loop = ");
2806 print_generic_expr (dump_file, res, 0);
2807 fprintf (dump_file, "))\n");
2808 }
2809
2810 loop->nb_iterations = res;
2811 return res;
2812 }
2813
2814 /* Returns the number of executions of the exit condition of LOOP,
2815 i.e., the number by one higher than number_of_latch_executions.
2816 Note that unlike number_of_latch_executions, this number does
2817 not necessarily fit in the unsigned variant of the type of
2818 the control variable -- if the number of iterations is a constant,
2819 we return chrec_dont_know if adding one to number_of_latch_executions
2820 overflows; however, in case the number of iterations is symbolic
2821 expression, the caller is responsible for dealing with this
2822 the possible overflow. */
2823
2824 tree
2825 number_of_exit_cond_executions (struct loop *loop)
2826 {
2827 tree ret = number_of_latch_executions (loop);
2828 tree type = chrec_type (ret);
2829
2830 if (chrec_contains_undetermined (ret))
2831 return ret;
2832
2833 ret = chrec_fold_plus (type, ret, build_int_cst (type, 1));
2834 if (TREE_CODE (ret) == INTEGER_CST
2835 && TREE_OVERFLOW (ret))
2836 return chrec_dont_know;
2837
2838 return ret;
2839 }
2840
2841 /* One of the drivers for testing the scalar evolutions analysis.
2842 This function computes the number of iterations for all the loops
2843 from the EXIT_CONDITIONS array. */
2844
2845 static void
2846 number_of_iterations_for_all_loops (VEC(gimple,heap) **exit_conditions)
2847 {
2848 unsigned int i;
2849 unsigned nb_chrec_dont_know_loops = 0;
2850 unsigned nb_static_loops = 0;
2851 gimple cond;
2852
2853 FOR_EACH_VEC_ELT (gimple, *exit_conditions, i, cond)
2854 {
2855 tree res = number_of_latch_executions (loop_containing_stmt (cond));
2856 if (chrec_contains_undetermined (res))
2857 nb_chrec_dont_know_loops++;
2858 else
2859 nb_static_loops++;
2860 }
2861
2862 if (dump_file)
2863 {
2864 fprintf (dump_file, "\n(\n");
2865 fprintf (dump_file, "-----------------------------------------\n");
2866 fprintf (dump_file, "%d\tnb_chrec_dont_know_loops\n", nb_chrec_dont_know_loops);
2867 fprintf (dump_file, "%d\tnb_static_loops\n", nb_static_loops);
2868 fprintf (dump_file, "%d\tnb_total_loops\n", number_of_loops ());
2869 fprintf (dump_file, "-----------------------------------------\n");
2870 fprintf (dump_file, ")\n\n");
2871
2872 print_loops (dump_file, 3);
2873 }
2874 }
2875
2876 \f
2877
2878 /* Counters for the stats. */
2879
2880 struct chrec_stats
2881 {
2882 unsigned nb_chrecs;
2883 unsigned nb_affine;
2884 unsigned nb_affine_multivar;
2885 unsigned nb_higher_poly;
2886 unsigned nb_chrec_dont_know;
2887 unsigned nb_undetermined;
2888 };
2889
2890 /* Reset the counters. */
2891
2892 static inline void
2893 reset_chrecs_counters (struct chrec_stats *stats)
2894 {
2895 stats->nb_chrecs = 0;
2896 stats->nb_affine = 0;
2897 stats->nb_affine_multivar = 0;
2898 stats->nb_higher_poly = 0;
2899 stats->nb_chrec_dont_know = 0;
2900 stats->nb_undetermined = 0;
2901 }
2902
2903 /* Dump the contents of a CHREC_STATS structure. */
2904
2905 static void
2906 dump_chrecs_stats (FILE *file, struct chrec_stats *stats)
2907 {
2908 fprintf (file, "\n(\n");
2909 fprintf (file, "-----------------------------------------\n");
2910 fprintf (file, "%d\taffine univariate chrecs\n", stats->nb_affine);
2911 fprintf (file, "%d\taffine multivariate chrecs\n", stats->nb_affine_multivar);
2912 fprintf (file, "%d\tdegree greater than 2 polynomials\n",
2913 stats->nb_higher_poly);
2914 fprintf (file, "%d\tchrec_dont_know chrecs\n", stats->nb_chrec_dont_know);
2915 fprintf (file, "-----------------------------------------\n");
2916 fprintf (file, "%d\ttotal chrecs\n", stats->nb_chrecs);
2917 fprintf (file, "%d\twith undetermined coefficients\n",
2918 stats->nb_undetermined);
2919 fprintf (file, "-----------------------------------------\n");
2920 fprintf (file, "%d\tchrecs in the scev database\n",
2921 (int) htab_elements (scalar_evolution_info));
2922 fprintf (file, "%d\tsets in the scev database\n", nb_set_scev);
2923 fprintf (file, "%d\tgets in the scev database\n", nb_get_scev);
2924 fprintf (file, "-----------------------------------------\n");
2925 fprintf (file, ")\n\n");
2926 }
2927
2928 /* Gather statistics about CHREC. */
2929
2930 static void
2931 gather_chrec_stats (tree chrec, struct chrec_stats *stats)
2932 {
2933 if (dump_file && (dump_flags & TDF_STATS))
2934 {
2935 fprintf (dump_file, "(classify_chrec ");
2936 print_generic_expr (dump_file, chrec, 0);
2937 fprintf (dump_file, "\n");
2938 }
2939
2940 stats->nb_chrecs++;
2941
2942 if (chrec == NULL_TREE)
2943 {
2944 stats->nb_undetermined++;
2945 return;
2946 }
2947
2948 switch (TREE_CODE (chrec))
2949 {
2950 case POLYNOMIAL_CHREC:
2951 if (evolution_function_is_affine_p (chrec))
2952 {
2953 if (dump_file && (dump_flags & TDF_STATS))
2954 fprintf (dump_file, " affine_univariate\n");
2955 stats->nb_affine++;
2956 }
2957 else if (evolution_function_is_affine_multivariate_p (chrec, 0))
2958 {
2959 if (dump_file && (dump_flags & TDF_STATS))
2960 fprintf (dump_file, " affine_multivariate\n");
2961 stats->nb_affine_multivar++;
2962 }
2963 else
2964 {
2965 if (dump_file && (dump_flags & TDF_STATS))
2966 fprintf (dump_file, " higher_degree_polynomial\n");
2967 stats->nb_higher_poly++;
2968 }
2969
2970 break;
2971
2972 default:
2973 break;
2974 }
2975
2976 if (chrec_contains_undetermined (chrec))
2977 {
2978 if (dump_file && (dump_flags & TDF_STATS))
2979 fprintf (dump_file, " undetermined\n");
2980 stats->nb_undetermined++;
2981 }
2982
2983 if (dump_file && (dump_flags & TDF_STATS))
2984 fprintf (dump_file, ")\n");
2985 }
2986
2987 /* One of the drivers for testing the scalar evolutions analysis.
2988 This function analyzes the scalar evolution of all the scalars
2989 defined as loop phi nodes in one of the loops from the
2990 EXIT_CONDITIONS array.
2991
2992 TODO Optimization: A loop is in canonical form if it contains only
2993 a single scalar loop phi node. All the other scalars that have an
2994 evolution in the loop are rewritten in function of this single
2995 index. This allows the parallelization of the loop. */
2996
2997 static void
2998 analyze_scalar_evolution_for_all_loop_phi_nodes (VEC(gimple,heap) **exit_conditions)
2999 {
3000 unsigned int i;
3001 struct chrec_stats stats;
3002 gimple cond, phi;
3003 gimple_stmt_iterator psi;
3004
3005 reset_chrecs_counters (&stats);
3006
3007 FOR_EACH_VEC_ELT (gimple, *exit_conditions, i, cond)
3008 {
3009 struct loop *loop;
3010 basic_block bb;
3011 tree chrec;
3012
3013 loop = loop_containing_stmt (cond);
3014 bb = loop->header;
3015
3016 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
3017 {
3018 phi = gsi_stmt (psi);
3019 if (is_gimple_reg (PHI_RESULT (phi)))
3020 {
3021 chrec = instantiate_parameters
3022 (loop,
3023 analyze_scalar_evolution (loop, PHI_RESULT (phi)));
3024
3025 if (dump_file && (dump_flags & TDF_STATS))
3026 gather_chrec_stats (chrec, &stats);
3027 }
3028 }
3029 }
3030
3031 if (dump_file && (dump_flags & TDF_STATS))
3032 dump_chrecs_stats (dump_file, &stats);
3033 }
3034
3035 /* Callback for htab_traverse, gathers information on chrecs in the
3036 hashtable. */
3037
3038 static int
3039 gather_stats_on_scev_database_1 (void **slot, void *stats)
3040 {
3041 struct scev_info_str *entry = (struct scev_info_str *) *slot;
3042
3043 gather_chrec_stats (entry->chrec, (struct chrec_stats *) stats);
3044
3045 return 1;
3046 }
3047
3048 /* Classify the chrecs of the whole database. */
3049
3050 void
3051 gather_stats_on_scev_database (void)
3052 {
3053 struct chrec_stats stats;
3054
3055 if (!dump_file)
3056 return;
3057
3058 reset_chrecs_counters (&stats);
3059
3060 htab_traverse (scalar_evolution_info, gather_stats_on_scev_database_1,
3061 &stats);
3062
3063 dump_chrecs_stats (dump_file, &stats);
3064 }
3065
3066 \f
3067
3068 /* Initializer. */
3069
3070 static void
3071 initialize_scalar_evolutions_analyzer (void)
3072 {
3073 /* The elements below are unique. */
3074 if (chrec_dont_know == NULL_TREE)
3075 {
3076 chrec_not_analyzed_yet = NULL_TREE;
3077 chrec_dont_know = make_node (SCEV_NOT_KNOWN);
3078 chrec_known = make_node (SCEV_KNOWN);
3079 TREE_TYPE (chrec_dont_know) = void_type_node;
3080 TREE_TYPE (chrec_known) = void_type_node;
3081 }
3082 }
3083
3084 /* Initialize the analysis of scalar evolutions for LOOPS. */
3085
3086 void
3087 scev_initialize (void)
3088 {
3089 loop_iterator li;
3090 struct loop *loop;
3091
3092
3093 scalar_evolution_info = htab_create_ggc (100, hash_scev_info, eq_scev_info,
3094 del_scev_info);
3095
3096 initialize_scalar_evolutions_analyzer ();
3097
3098 FOR_EACH_LOOP (li, loop, 0)
3099 {
3100 loop->nb_iterations = NULL_TREE;
3101 }
3102 }
3103
3104 /* Cleans up the information cached by the scalar evolutions analysis
3105 in the hash table. */
3106
3107 void
3108 scev_reset_htab (void)
3109 {
3110 if (!scalar_evolution_info)
3111 return;
3112
3113 htab_empty (scalar_evolution_info);
3114 }
3115
3116 /* Cleans up the information cached by the scalar evolutions analysis
3117 in the hash table and in the loop->nb_iterations. */
3118
3119 void
3120 scev_reset (void)
3121 {
3122 loop_iterator li;
3123 struct loop *loop;
3124
3125 scev_reset_htab ();
3126
3127 if (!current_loops)
3128 return;
3129
3130 FOR_EACH_LOOP (li, loop, 0)
3131 {
3132 loop->nb_iterations = NULL_TREE;
3133 }
3134 }
3135
3136 /* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with
3137 respect to WRTO_LOOP and returns its base and step in IV if possible
3138 (see analyze_scalar_evolution_in_loop for more details on USE_LOOP
3139 and WRTO_LOOP). If ALLOW_NONCONSTANT_STEP is true, we want step to be
3140 invariant in LOOP. Otherwise we require it to be an integer constant.
3141
3142 IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g.
3143 because it is computed in signed arithmetics). Consequently, adding an
3144 induction variable
3145
3146 for (i = IV->base; ; i += IV->step)
3147
3148 is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is
3149 false for the type of the induction variable, or you can prove that i does
3150 not wrap by some other argument. Otherwise, this might introduce undefined
3151 behavior, and
3152
3153 for (i = iv->base; ; i = (type) ((unsigned type) i + (unsigned type) iv->step))
3154
3155 must be used instead. */
3156
3157 bool
3158 simple_iv (struct loop *wrto_loop, struct loop *use_loop, tree op,
3159 affine_iv *iv, bool allow_nonconstant_step)
3160 {
3161 tree type, ev;
3162 bool folded_casts;
3163
3164 iv->base = NULL_TREE;
3165 iv->step = NULL_TREE;
3166 iv->no_overflow = false;
3167
3168 type = TREE_TYPE (op);
3169 if (!POINTER_TYPE_P (type)
3170 && !INTEGRAL_TYPE_P (type))
3171 return false;
3172
3173 ev = analyze_scalar_evolution_in_loop (wrto_loop, use_loop, op,
3174 &folded_casts);
3175 if (chrec_contains_undetermined (ev)
3176 || chrec_contains_symbols_defined_in_loop (ev, wrto_loop->num))
3177 return false;
3178
3179 if (tree_does_not_contain_chrecs (ev))
3180 {
3181 iv->base = ev;
3182 iv->step = build_int_cst (TREE_TYPE (ev), 0);
3183 iv->no_overflow = true;
3184 return true;
3185 }
3186
3187 if (TREE_CODE (ev) != POLYNOMIAL_CHREC
3188 || CHREC_VARIABLE (ev) != (unsigned) wrto_loop->num)
3189 return false;
3190
3191 iv->step = CHREC_RIGHT (ev);
3192 if ((!allow_nonconstant_step && TREE_CODE (iv->step) != INTEGER_CST)
3193 || tree_contains_chrecs (iv->step, NULL))
3194 return false;
3195
3196 iv->base = CHREC_LEFT (ev);
3197 if (tree_contains_chrecs (iv->base, NULL))
3198 return false;
3199
3200 iv->no_overflow = !folded_casts && TYPE_OVERFLOW_UNDEFINED (type);
3201
3202 return true;
3203 }
3204
3205 /* Runs the analysis of scalar evolutions. */
3206
3207 void
3208 scev_analysis (void)
3209 {
3210 VEC(gimple,heap) *exit_conditions;
3211
3212 exit_conditions = VEC_alloc (gimple, heap, 37);
3213 select_loops_exit_conditions (&exit_conditions);
3214
3215 if (dump_file && (dump_flags & TDF_STATS))
3216 analyze_scalar_evolution_for_all_loop_phi_nodes (&exit_conditions);
3217
3218 number_of_iterations_for_all_loops (&exit_conditions);
3219 VEC_free (gimple, heap, exit_conditions);
3220 }
3221
3222 /* Finalize the scalar evolution analysis. */
3223
3224 void
3225 scev_finalize (void)
3226 {
3227 if (!scalar_evolution_info)
3228 return;
3229 htab_delete (scalar_evolution_info);
3230 scalar_evolution_info = NULL;
3231 }
3232
3233 /* Returns true if the expression EXPR is considered to be too expensive
3234 for scev_const_prop. */
3235
3236 bool
3237 expression_expensive_p (tree expr)
3238 {
3239 enum tree_code code;
3240
3241 if (is_gimple_val (expr))
3242 return false;
3243
3244 code = TREE_CODE (expr);
3245 if (code == TRUNC_DIV_EXPR
3246 || code == CEIL_DIV_EXPR
3247 || code == FLOOR_DIV_EXPR
3248 || code == ROUND_DIV_EXPR
3249 || code == TRUNC_MOD_EXPR
3250 || code == CEIL_MOD_EXPR
3251 || code == FLOOR_MOD_EXPR
3252 || code == ROUND_MOD_EXPR
3253 || code == EXACT_DIV_EXPR)
3254 {
3255 /* Division by power of two is usually cheap, so we allow it.
3256 Forbid anything else. */
3257 if (!integer_pow2p (TREE_OPERAND (expr, 1)))
3258 return true;
3259 }
3260
3261 switch (TREE_CODE_CLASS (code))
3262 {
3263 case tcc_binary:
3264 case tcc_comparison:
3265 if (expression_expensive_p (TREE_OPERAND (expr, 1)))
3266 return true;
3267
3268 /* Fallthru. */
3269 case tcc_unary:
3270 return expression_expensive_p (TREE_OPERAND (expr, 0));
3271
3272 default:
3273 return true;
3274 }
3275 }
3276
3277 /* Replace ssa names for that scev can prove they are constant by the
3278 appropriate constants. Also perform final value replacement in loops,
3279 in case the replacement expressions are cheap.
3280
3281 We only consider SSA names defined by phi nodes; rest is left to the
3282 ordinary constant propagation pass. */
3283
3284 unsigned int
3285 scev_const_prop (void)
3286 {
3287 basic_block bb;
3288 tree name, type, ev;
3289 gimple phi, ass;
3290 struct loop *loop, *ex_loop;
3291 bitmap ssa_names_to_remove = NULL;
3292 unsigned i;
3293 loop_iterator li;
3294 gimple_stmt_iterator psi;
3295
3296 if (number_of_loops () <= 1)
3297 return 0;
3298
3299 FOR_EACH_BB (bb)
3300 {
3301 loop = bb->loop_father;
3302
3303 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
3304 {
3305 phi = gsi_stmt (psi);
3306 name = PHI_RESULT (phi);
3307
3308 if (!is_gimple_reg (name))
3309 continue;
3310
3311 type = TREE_TYPE (name);
3312
3313 if (!POINTER_TYPE_P (type)
3314 && !INTEGRAL_TYPE_P (type))
3315 continue;
3316
3317 ev = resolve_mixers (loop, analyze_scalar_evolution (loop, name));
3318 if (!is_gimple_min_invariant (ev)
3319 || !may_propagate_copy (name, ev))
3320 continue;
3321
3322 /* Replace the uses of the name. */
3323 if (name != ev)
3324 replace_uses_by (name, ev);
3325
3326 if (!ssa_names_to_remove)
3327 ssa_names_to_remove = BITMAP_ALLOC (NULL);
3328 bitmap_set_bit (ssa_names_to_remove, SSA_NAME_VERSION (name));
3329 }
3330 }
3331
3332 /* Remove the ssa names that were replaced by constants. We do not
3333 remove them directly in the previous cycle, since this
3334 invalidates scev cache. */
3335 if (ssa_names_to_remove)
3336 {
3337 bitmap_iterator bi;
3338
3339 EXECUTE_IF_SET_IN_BITMAP (ssa_names_to_remove, 0, i, bi)
3340 {
3341 gimple_stmt_iterator psi;
3342 name = ssa_name (i);
3343 phi = SSA_NAME_DEF_STMT (name);
3344
3345 gcc_assert (gimple_code (phi) == GIMPLE_PHI);
3346 psi = gsi_for_stmt (phi);
3347 remove_phi_node (&psi, true);
3348 }
3349
3350 BITMAP_FREE (ssa_names_to_remove);
3351 scev_reset ();
3352 }
3353
3354 /* Now the regular final value replacement. */
3355 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
3356 {
3357 edge exit;
3358 tree def, rslt, niter;
3359 gimple_stmt_iterator bsi;
3360
3361 /* If we do not know exact number of iterations of the loop, we cannot
3362 replace the final value. */
3363 exit = single_exit (loop);
3364 if (!exit)
3365 continue;
3366
3367 niter = number_of_latch_executions (loop);
3368 if (niter == chrec_dont_know)
3369 continue;
3370
3371 /* Ensure that it is possible to insert new statements somewhere. */
3372 if (!single_pred_p (exit->dest))
3373 split_loop_exit_edge (exit);
3374 bsi = gsi_after_labels (exit->dest);
3375
3376 ex_loop = superloop_at_depth (loop,
3377 loop_depth (exit->dest->loop_father) + 1);
3378
3379 for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); )
3380 {
3381 phi = gsi_stmt (psi);
3382 rslt = PHI_RESULT (phi);
3383 def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
3384 if (!is_gimple_reg (def))
3385 {
3386 gsi_next (&psi);
3387 continue;
3388 }
3389
3390 if (!POINTER_TYPE_P (TREE_TYPE (def))
3391 && !INTEGRAL_TYPE_P (TREE_TYPE (def)))
3392 {
3393 gsi_next (&psi);
3394 continue;
3395 }
3396
3397 def = analyze_scalar_evolution_in_loop (ex_loop, loop, def, NULL);
3398 def = compute_overall_effect_of_inner_loop (ex_loop, def);
3399 if (!tree_does_not_contain_chrecs (def)
3400 || chrec_contains_symbols_defined_in_loop (def, ex_loop->num)
3401 /* Moving the computation from the loop may prolong life range
3402 of some ssa names, which may cause problems if they appear
3403 on abnormal edges. */
3404 || contains_abnormal_ssa_name_p (def)
3405 /* Do not emit expensive expressions. The rationale is that
3406 when someone writes a code like
3407
3408 while (n > 45) n -= 45;
3409
3410 he probably knows that n is not large, and does not want it
3411 to be turned into n %= 45. */
3412 || expression_expensive_p (def))
3413 {
3414 gsi_next (&psi);
3415 continue;
3416 }
3417
3418 /* Eliminate the PHI node and replace it by a computation outside
3419 the loop. */
3420 def = unshare_expr (def);
3421 remove_phi_node (&psi, false);
3422
3423 def = force_gimple_operand_gsi (&bsi, def, false, NULL_TREE,
3424 true, GSI_SAME_STMT);
3425 ass = gimple_build_assign (rslt, def);
3426 gsi_insert_before (&bsi, ass, GSI_SAME_STMT);
3427 }
3428 }
3429 return 0;
3430 }
3431
3432 #include "gt-tree-scalar-evolution.h"