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