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