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