re PR tree-optimization/17742 (C Optimization error with -O1 on i686)
[gcc.git] / gcc / tree-scalar-evolution.c
1 /* Scalar evolution detector.
2 Copyright (C) 2003, 2004 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
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 MODIFY_EXPR: 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 ({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 2: 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 3: Higher degree polynomials.
159
160 | loop_1
161 | a = phi (2, b)
162 | c = phi (5, d)
163 | b = a + 1
164 | d = c + a
165 | endloop
166
167 a -> {2, +, 1}_1
168 b -> {3, +, 1}_1
169 c -> {5, +, a}_1
170 d -> {5 + a, +, a}_1
171
172 instantiate_parameters ({5, +, a}_1) -> {5, +, 2, +, 1}_1
173 instantiate_parameters ({5 + a, +, a}_1) -> {7, +, 3, +, 1}_1
174
175 Example 4: Lucas, Fibonacci, or mixers in general.
176
177 | loop_1
178 | a = phi (1, b)
179 | c = phi (3, d)
180 | b = c
181 | d = c + a
182 | endloop
183
184 a -> (1, c)_1
185 c -> {3, +, a}_1
186
187 The syntax "(1, c)_1" stands for a PEELED_CHREC that has the
188 following semantics: during the first iteration of the loop_1, the
189 variable contains the value 1, and then it contains the value "c".
190 Note that this syntax is close to the syntax of the loop-phi-node:
191 "a -> (1, c)_1" vs. "a = phi (1, c)".
192
193 The symbolic chrec representation contains all the semantics of the
194 original code. What is more difficult is to use this information.
195
196 Example 5: Flip-flops, or exchangers.
197
198 | loop_1
199 | a = phi (1, b)
200 | c = phi (3, d)
201 | b = c
202 | d = a
203 | endloop
204
205 a -> (1, c)_1
206 c -> (3, a)_1
207
208 Based on these symbolic chrecs, it is possible to refine this
209 information into the more precise PERIODIC_CHRECs:
210
211 a -> |1, 3|_1
212 c -> |3, 1|_1
213
214 This transformation is not yet implemented.
215
216 Further readings:
217
218 You can find a more detailed description of the algorithm in:
219 http://icps.u-strasbg.fr/~pop/DEA_03_Pop.pdf
220 http://icps.u-strasbg.fr/~pop/DEA_03_Pop.ps.gz. But note that
221 this is a preliminary report and some of the details of the
222 algorithm have changed. I'm working on a research report that
223 updates the description of the algorithms to reflect the design
224 choices used in this implementation.
225
226 A set of slides show a high level overview of the algorithm and run
227 an example through the scalar evolution analyzer:
228 http://cri.ensmp.fr/~pop/gcc/mar04/slides.pdf
229
230 The slides that I have presented at the GCC Summit'04 are available
231 at: http://cri.ensmp.fr/~pop/gcc/20040604/gccsummit-lno-spop.pdf
232 */
233
234 #include "config.h"
235 #include "system.h"
236 #include "coretypes.h"
237 #include "tm.h"
238 #include "errors.h"
239 #include "ggc.h"
240 #include "tree.h"
241
242 /* These RTL headers are needed for basic-block.h. */
243 #include "rtl.h"
244 #include "basic-block.h"
245 #include "diagnostic.h"
246 #include "tree-flow.h"
247 #include "tree-dump.h"
248 #include "timevar.h"
249 #include "cfgloop.h"
250 #include "tree-chrec.h"
251 #include "tree-scalar-evolution.h"
252 #include "tree-pass.h"
253 #include "flags.h"
254
255 static tree analyze_scalar_evolution_1 (struct loop *, tree, tree);
256 static tree resolve_mixers (struct loop *, tree);
257
258 /* The cached information about a ssa name VAR, claiming that inside LOOP,
259 the value of VAR can be expressed as CHREC. */
260
261 struct scev_info_str
262 {
263 tree var;
264 tree chrec;
265 };
266
267 /* Counters for the scev database. */
268 static unsigned nb_set_scev = 0;
269 static unsigned nb_get_scev = 0;
270
271 /* The following trees are unique elements. Thus the comparison of
272 another element to these elements should be done on the pointer to
273 these trees, and not on their value. */
274
275 /* The SSA_NAMEs that are not yet analyzed are qualified with NULL_TREE. */
276 tree chrec_not_analyzed_yet;
277
278 /* Reserved to the cases where the analyzer has detected an
279 undecidable property at compile time. */
280 tree chrec_dont_know;
281
282 /* When the analyzer has detected that a property will never
283 happen, then it qualifies it with chrec_known. */
284 tree chrec_known;
285
286 static bitmap already_instantiated;
287
288 static htab_t scalar_evolution_info;
289
290 \f
291 /* Constructs a new SCEV_INFO_STR structure. */
292
293 static inline struct scev_info_str *
294 new_scev_info_str (tree var)
295 {
296 struct scev_info_str *res;
297
298 res = xmalloc (sizeof (struct scev_info_str));
299 res->var = var;
300 res->chrec = chrec_not_analyzed_yet;
301
302 return res;
303 }
304
305 /* Computes a hash function for database element ELT. */
306
307 static hashval_t
308 hash_scev_info (const void *elt)
309 {
310 return SSA_NAME_VERSION (((struct scev_info_str *) elt)->var);
311 }
312
313 /* Compares database elements E1 and E2. */
314
315 static int
316 eq_scev_info (const void *e1, const void *e2)
317 {
318 const struct scev_info_str *elt1 = e1;
319 const struct scev_info_str *elt2 = e2;
320
321 return elt1->var == elt2->var;
322 }
323
324 /* Deletes database element E. */
325
326 static void
327 del_scev_info (void *e)
328 {
329 free (e);
330 }
331
332 /* Get the index corresponding to VAR in the current LOOP. If
333 it's the first time we ask for this VAR, then we return
334 chrec_not_analyzed_yet for this VAR and return its index. */
335
336 static tree *
337 find_var_scev_info (tree var)
338 {
339 struct scev_info_str *res;
340 struct scev_info_str tmp;
341 PTR *slot;
342
343 tmp.var = var;
344 slot = htab_find_slot (scalar_evolution_info, &tmp, INSERT);
345
346 if (!*slot)
347 *slot = new_scev_info_str (var);
348 res = *slot;
349
350 return &res->chrec;
351 }
352
353 /* Tries to express CHREC in wider type TYPE. */
354
355 tree
356 count_ev_in_wider_type (tree type, tree chrec)
357 {
358 tree base, step;
359 struct loop *loop;
360
361 if (!evolution_function_is_affine_p (chrec))
362 return fold_convert (type, chrec);
363
364 base = CHREC_LEFT (chrec);
365 step = CHREC_RIGHT (chrec);
366 loop = current_loops->parray[CHREC_VARIABLE (chrec)];
367
368 /* TODO -- if we knew the statement at that the conversion occurs,
369 we could pass it to can_count_iv_in_wider_type and get a better
370 result. */
371 step = can_count_iv_in_wider_type (loop, type, base, step, NULL_TREE);
372 if (!step)
373 return fold_convert (type, chrec);
374 base = chrec_convert (type, base);
375
376 return build_polynomial_chrec (CHREC_VARIABLE (chrec),
377 base, step);
378 }
379
380 /* Return true when CHREC contains symbolic names defined in
381 LOOP_NB. */
382
383 bool
384 chrec_contains_symbols_defined_in_loop (tree chrec, unsigned loop_nb)
385 {
386 if (chrec == NULL_TREE)
387 return false;
388
389 if (TREE_INVARIANT (chrec))
390 return false;
391
392 if (TREE_CODE (chrec) == VAR_DECL
393 || TREE_CODE (chrec) == PARM_DECL
394 || TREE_CODE (chrec) == FUNCTION_DECL
395 || TREE_CODE (chrec) == LABEL_DECL
396 || TREE_CODE (chrec) == RESULT_DECL
397 || TREE_CODE (chrec) == FIELD_DECL)
398 return true;
399
400 if (TREE_CODE (chrec) == SSA_NAME)
401 {
402 tree def = SSA_NAME_DEF_STMT (chrec);
403 struct loop *def_loop = loop_containing_stmt (def);
404 struct loop *loop = current_loops->parray[loop_nb];
405
406 if (def_loop == NULL)
407 return false;
408
409 if (loop == def_loop || flow_loop_nested_p (loop, def_loop))
410 return true;
411
412 return false;
413 }
414
415 switch (TREE_CODE_LENGTH (TREE_CODE (chrec)))
416 {
417 case 3:
418 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec, 2),
419 loop_nb))
420 return true;
421
422 case 2:
423 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec, 1),
424 loop_nb))
425 return true;
426
427 case 1:
428 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec, 0),
429 loop_nb))
430 return true;
431
432 default:
433 return false;
434 }
435 }
436
437 /* Return true when PHI is a loop-phi-node. */
438
439 static bool
440 loop_phi_node_p (tree phi)
441 {
442 /* The implementation of this function is based on the following
443 property: "all the loop-phi-nodes of a loop are contained in the
444 loop's header basic block". */
445
446 return loop_containing_stmt (phi)->header == bb_for_stmt (phi);
447 }
448
449 /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
450 In general, in the case of multivariate evolutions we want to get
451 the evolution in different loops. LOOP specifies the level for
452 which to get the evolution.
453
454 Example:
455
456 | for (j = 0; j < 100; j++)
457 | {
458 | for (k = 0; k < 100; k++)
459 | {
460 | i = k + j; - Here the value of i is a function of j, k.
461 | }
462 | ... = i - Here the value of i is a function of j.
463 | }
464 | ... = i - Here the value of i is a scalar.
465
466 Example:
467
468 | i_0 = ...
469 | loop_1 10 times
470 | i_1 = phi (i_0, i_2)
471 | i_2 = i_1 + 2
472 | endloop
473
474 This loop has the same effect as:
475 LOOP_1 has the same effect as:
476
477 | i_1 = i_0 + 20
478
479 The overall effect of the loop, "i_0 + 20" in the previous example,
480 is obtained by passing in the parameters: LOOP = 1,
481 EVOLUTION_FN = {i_0, +, 2}_1.
482 */
483
484 static tree
485 compute_overall_effect_of_inner_loop (struct loop *loop, tree evolution_fn)
486 {
487 bool val = false;
488
489 if (evolution_fn == chrec_dont_know)
490 return chrec_dont_know;
491
492 else if (TREE_CODE (evolution_fn) == POLYNOMIAL_CHREC)
493 {
494 if (CHREC_VARIABLE (evolution_fn) >= (unsigned) loop->num)
495 {
496 struct loop *inner_loop =
497 current_loops->parray[CHREC_VARIABLE (evolution_fn)];
498 tree nb_iter = number_of_iterations_in_loop (inner_loop);
499
500 if (nb_iter == chrec_dont_know)
501 return chrec_dont_know;
502 else
503 {
504 tree res;
505
506 /* Number of iterations is off by one (the ssa name we
507 analyze must be defined before the exit). */
508 nb_iter = chrec_fold_minus (chrec_type (nb_iter),
509 nb_iter,
510 build_int_cst_type (chrec_type (nb_iter), 1));
511
512 /* evolution_fn is the evolution function in LOOP. Get
513 its value in the nb_iter-th iteration. */
514 res = chrec_apply (inner_loop->num, evolution_fn, nb_iter);
515
516 /* Continue the computation until ending on a parent of LOOP. */
517 return compute_overall_effect_of_inner_loop (loop, res);
518 }
519 }
520 else
521 return evolution_fn;
522 }
523
524 /* If the evolution function is an invariant, there is nothing to do. */
525 else if (no_evolution_in_loop_p (evolution_fn, loop->num, &val) && val)
526 return evolution_fn;
527
528 else
529 return chrec_dont_know;
530 }
531
532 /* Determine whether the CHREC is always positive/negative. If the expression
533 cannot be statically analyzed, return false, otherwise set the answer into
534 VALUE. */
535
536 bool
537 chrec_is_positive (tree chrec, bool *value)
538 {
539 bool value0, value1;
540 bool value2;
541 tree end_value;
542 tree nb_iter;
543
544 switch (TREE_CODE (chrec))
545 {
546 case POLYNOMIAL_CHREC:
547 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
548 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
549 return false;
550
551 /* FIXME -- overflows. */
552 if (value0 == value1)
553 {
554 *value = value0;
555 return true;
556 }
557
558 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
559 and the proof consists in showing that the sign never
560 changes during the execution of the loop, from 0 to
561 loop->nb_iterations. */
562 if (!evolution_function_is_affine_p (chrec))
563 return false;
564
565 nb_iter = number_of_iterations_in_loop
566 (current_loops->parray[CHREC_VARIABLE (chrec)]);
567
568 if (chrec_contains_undetermined (nb_iter))
569 return false;
570
571 nb_iter = chrec_fold_minus
572 (chrec_type (nb_iter), nb_iter,
573 build_int_cst (chrec_type (nb_iter), 1));
574
575 #if 0
576 /* TODO -- If the test is after the exit, we may decrease the number of
577 iterations by one. */
578 if (after_exit)
579 nb_iter = chrec_fold_minus
580 (chrec_type (nb_iter), nb_iter,
581 build_int_cst (chrec_type (nb_iter), 1));
582 #endif
583
584 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
585
586 if (!chrec_is_positive (end_value, &value2))
587 return false;
588
589 *value = value0;
590 return value0 == value1;
591
592 case INTEGER_CST:
593 *value = (tree_int_cst_sgn (chrec) == 1);
594 return true;
595
596 default:
597 return false;
598 }
599 }
600
601 /* Associate CHREC to SCALAR. */
602
603 static void
604 set_scalar_evolution (tree scalar, tree chrec)
605 {
606 tree *scalar_info;
607
608 if (TREE_CODE (scalar) != SSA_NAME)
609 return;
610
611 scalar_info = find_var_scev_info (scalar);
612
613 if (dump_file)
614 {
615 if (dump_flags & TDF_DETAILS)
616 {
617 fprintf (dump_file, "(set_scalar_evolution \n");
618 fprintf (dump_file, " (scalar = ");
619 print_generic_expr (dump_file, scalar, 0);
620 fprintf (dump_file, ")\n (scalar_evolution = ");
621 print_generic_expr (dump_file, chrec, 0);
622 fprintf (dump_file, "))\n");
623 }
624 if (dump_flags & TDF_STATS)
625 nb_set_scev++;
626 }
627
628 *scalar_info = chrec;
629 }
630
631 /* Retrieve the chrec associated to SCALAR in the LOOP. */
632
633 static tree
634 get_scalar_evolution (tree scalar)
635 {
636 tree res;
637
638 if (dump_file)
639 {
640 if (dump_flags & TDF_DETAILS)
641 {
642 fprintf (dump_file, "(get_scalar_evolution \n");
643 fprintf (dump_file, " (scalar = ");
644 print_generic_expr (dump_file, scalar, 0);
645 fprintf (dump_file, ")\n");
646 }
647 if (dump_flags & TDF_STATS)
648 nb_get_scev++;
649 }
650
651 switch (TREE_CODE (scalar))
652 {
653 case SSA_NAME:
654 res = *find_var_scev_info (scalar);
655 break;
656
657 case REAL_CST:
658 case INTEGER_CST:
659 res = scalar;
660 break;
661
662 default:
663 res = chrec_not_analyzed_yet;
664 break;
665 }
666
667 if (dump_file && (dump_flags & TDF_DETAILS))
668 {
669 fprintf (dump_file, " (scalar_evolution = ");
670 print_generic_expr (dump_file, res, 0);
671 fprintf (dump_file, "))\n");
672 }
673
674 return res;
675 }
676
677 /* Helper function for add_to_evolution. Returns the evolution
678 function for an assignment of the form "a = b + c", where "a" and
679 "b" are on the strongly connected component. CHREC_BEFORE is the
680 information that we already have collected up to this point.
681 TO_ADD is the evolution of "c".
682
683 When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
684 evolution the expression TO_ADD, otherwise construct an evolution
685 part for this loop. */
686
687 static tree
688 add_to_evolution_1 (unsigned loop_nb,
689 tree chrec_before,
690 tree to_add)
691 {
692 switch (TREE_CODE (chrec_before))
693 {
694 case POLYNOMIAL_CHREC:
695 if (CHREC_VARIABLE (chrec_before) <= loop_nb)
696 {
697 unsigned var;
698 tree left, right;
699 tree type = chrec_type (chrec_before);
700
701 /* When there is no evolution part in this loop, build it. */
702 if (CHREC_VARIABLE (chrec_before) < loop_nb)
703 {
704 var = loop_nb;
705 left = chrec_before;
706 right = build_int_cst (type, 0);
707 }
708 else
709 {
710 var = CHREC_VARIABLE (chrec_before);
711 left = CHREC_LEFT (chrec_before);
712 right = CHREC_RIGHT (chrec_before);
713 }
714
715 return build_polynomial_chrec
716 (var, left, chrec_fold_plus (type, right, to_add));
717 }
718 else
719 /* Search the evolution in LOOP_NB. */
720 return build_polynomial_chrec
721 (CHREC_VARIABLE (chrec_before),
722 add_to_evolution_1 (loop_nb, CHREC_LEFT (chrec_before), to_add),
723 CHREC_RIGHT (chrec_before));
724
725 default:
726 /* These nodes do not depend on a loop. */
727 if (chrec_before == chrec_dont_know)
728 return chrec_dont_know;
729 return build_polynomial_chrec (loop_nb, chrec_before, to_add);
730 }
731 }
732
733 /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
734 of LOOP_NB.
735
736 Description (provided for completeness, for those who read code in
737 a plane, and for my poor 62 bytes brain that would have forgotten
738 all this in the next two or three months):
739
740 The algorithm of translation of programs from the SSA representation
741 into the chrecs syntax is based on a pattern matching. After having
742 reconstructed the overall tree expression for a loop, there are only
743 two cases that can arise:
744
745 1. a = loop-phi (init, a + expr)
746 2. a = loop-phi (init, expr)
747
748 where EXPR is either a scalar constant with respect to the analyzed
749 loop (this is a degree 0 polynomial), or an expression containing
750 other loop-phi definitions (these are higher degree polynomials).
751
752 Examples:
753
754 1.
755 | init = ...
756 | loop_1
757 | a = phi (init, a + 5)
758 | endloop
759
760 2.
761 | inita = ...
762 | initb = ...
763 | loop_1
764 | a = phi (inita, 2 * b + 3)
765 | b = phi (initb, b + 1)
766 | endloop
767
768 For the first case, the semantics of the SSA representation is:
769
770 | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
771
772 that is, there is a loop index "x" that determines the scalar value
773 of the variable during the loop execution. During the first
774 iteration, the value is that of the initial condition INIT, while
775 during the subsequent iterations, it is the sum of the initial
776 condition with the sum of all the values of EXPR from the initial
777 iteration to the before last considered iteration.
778
779 For the second case, the semantics of the SSA program is:
780
781 | a (x) = init, if x = 0;
782 | expr (x - 1), otherwise.
783
784 The second case corresponds to the PEELED_CHREC, whose syntax is
785 close to the syntax of a loop-phi-node:
786
787 | phi (init, expr) vs. (init, expr)_x
788
789 The proof of the translation algorithm for the first case is a
790 proof by structural induction based on the degree of EXPR.
791
792 Degree 0:
793 When EXPR is a constant with respect to the analyzed loop, or in
794 other words when EXPR is a polynomial of degree 0, the evolution of
795 the variable A in the loop is an affine function with an initial
796 condition INIT, and a step EXPR. In order to show this, we start
797 from the semantics of the SSA representation:
798
799 f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
800
801 and since "expr (j)" is a constant with respect to "j",
802
803 f (x) = init + x * expr
804
805 Finally, based on the semantics of the pure sum chrecs, by
806 identification we get the corresponding chrecs syntax:
807
808 f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
809 f (x) -> {init, +, expr}_x
810
811 Higher degree:
812 Suppose that EXPR is a polynomial of degree N with respect to the
813 analyzed loop_x for which we have already determined that it is
814 written under the chrecs syntax:
815
816 | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
817
818 We start from the semantics of the SSA program:
819
820 | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
821 |
822 | f (x) = init + \sum_{j = 0}^{x - 1}
823 | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
824 |
825 | f (x) = init + \sum_{j = 0}^{x - 1}
826 | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
827 |
828 | f (x) = init + \sum_{k = 0}^{n - 1}
829 | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
830 |
831 | f (x) = init + \sum_{k = 0}^{n - 1}
832 | (b_k * \binom{x}{k + 1})
833 |
834 | f (x) = init + b_0 * \binom{x}{1} + ...
835 | + b_{n-1} * \binom{x}{n}
836 |
837 | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
838 | + b_{n-1} * \binom{x}{n}
839 |
840
841 And finally from the definition of the chrecs syntax, we identify:
842 | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x
843
844 This shows the mechanism that stands behind the add_to_evolution
845 function. An important point is that the use of symbolic
846 parameters avoids the need of an analysis schedule.
847
848 Example:
849
850 | inita = ...
851 | initb = ...
852 | loop_1
853 | a = phi (inita, a + 2 + b)
854 | b = phi (initb, b + 1)
855 | endloop
856
857 When analyzing "a", the algorithm keeps "b" symbolically:
858
859 | a -> {inita, +, 2 + b}_1
860
861 Then, after instantiation, the analyzer ends on the evolution:
862
863 | a -> {inita, +, 2 + initb, +, 1}_1
864
865 */
866
867 static tree
868 add_to_evolution (unsigned loop_nb,
869 tree chrec_before,
870 enum tree_code code,
871 tree to_add)
872 {
873 tree type = chrec_type (to_add);
874 tree res = NULL_TREE;
875
876 if (to_add == NULL_TREE)
877 return chrec_before;
878
879 /* TO_ADD is either a scalar, or a parameter. TO_ADD is not
880 instantiated at this point. */
881 if (TREE_CODE (to_add) == POLYNOMIAL_CHREC)
882 /* This should not happen. */
883 return chrec_dont_know;
884
885 if (dump_file && (dump_flags & TDF_DETAILS))
886 {
887 fprintf (dump_file, "(add_to_evolution \n");
888 fprintf (dump_file, " (loop_nb = %d)\n", loop_nb);
889 fprintf (dump_file, " (chrec_before = ");
890 print_generic_expr (dump_file, chrec_before, 0);
891 fprintf (dump_file, ")\n (to_add = ");
892 print_generic_expr (dump_file, to_add, 0);
893 fprintf (dump_file, ")\n");
894 }
895
896 if (code == MINUS_EXPR)
897 to_add = chrec_fold_multiply (type, to_add,
898 build_int_cst_type (type, -1));
899
900 res = add_to_evolution_1 (loop_nb, chrec_before, to_add);
901
902 if (dump_file && (dump_flags & TDF_DETAILS))
903 {
904 fprintf (dump_file, " (res = ");
905 print_generic_expr (dump_file, res, 0);
906 fprintf (dump_file, "))\n");
907 }
908
909 return res;
910 }
911
912 /* Helper function. */
913
914 static inline tree
915 set_nb_iterations_in_loop (struct loop *loop,
916 tree res)
917 {
918 res = chrec_fold_plus (chrec_type (res), res,
919 build_int_cst_type (chrec_type (res), 1));
920
921 /* FIXME HWI: However we want to store one iteration less than the
922 count of the loop in order to be compatible with the other
923 nb_iter computations in loop-iv. This also allows the
924 representation of nb_iters that are equal to MAX_INT. */
925 if ((TREE_CODE (res) == INTEGER_CST && TREE_INT_CST_LOW (res) == 0)
926 || TREE_OVERFLOW (res))
927 res = chrec_dont_know;
928
929 if (dump_file && (dump_flags & TDF_DETAILS))
930 {
931 fprintf (dump_file, " (set_nb_iterations_in_loop = ");
932 print_generic_expr (dump_file, res, 0);
933 fprintf (dump_file, "))\n");
934 }
935
936 loop->nb_iterations = res;
937 return res;
938 }
939
940 \f
941
942 /* This section selects the loops that will be good candidates for the
943 scalar evolution analysis. For the moment, greedily select all the
944 loop nests we could analyze. */
945
946 /* Return true when it is possible to analyze the condition expression
947 EXPR. */
948
949 static bool
950 analyzable_condition (tree expr)
951 {
952 tree condition;
953
954 if (TREE_CODE (expr) != COND_EXPR)
955 return false;
956
957 condition = TREE_OPERAND (expr, 0);
958
959 switch (TREE_CODE (condition))
960 {
961 case SSA_NAME:
962 /* Volatile expressions are not analyzable. */
963 if (TREE_THIS_VOLATILE (SSA_NAME_VAR (condition)))
964 return false;
965 return true;
966
967 case LT_EXPR:
968 case LE_EXPR:
969 case GT_EXPR:
970 case GE_EXPR:
971 case EQ_EXPR:
972 case NE_EXPR:
973 {
974 tree opnd0, opnd1;
975
976 opnd0 = TREE_OPERAND (condition, 0);
977 opnd1 = TREE_OPERAND (condition, 1);
978
979 if (TREE_CODE (opnd0) == SSA_NAME
980 && TREE_THIS_VOLATILE (SSA_NAME_VAR (opnd0)))
981 return false;
982
983 if (TREE_CODE (opnd1) == SSA_NAME
984 && TREE_THIS_VOLATILE (SSA_NAME_VAR (opnd1)))
985 return false;
986
987 return true;
988 }
989
990 default:
991 return false;
992 }
993
994 return false;
995 }
996
997 /* For a loop with a single exit edge, return the COND_EXPR that
998 guards the exit edge. If the expression is too difficult to
999 analyze, then give up. */
1000
1001 tree
1002 get_loop_exit_condition (struct loop *loop)
1003 {
1004 tree res = NULL_TREE;
1005 edge exit_edge = loop->single_exit;
1006
1007
1008 if (dump_file && (dump_flags & TDF_DETAILS))
1009 fprintf (dump_file, "(get_loop_exit_condition \n ");
1010
1011 if (exit_edge)
1012 {
1013 tree expr;
1014
1015 expr = last_stmt (exit_edge->src);
1016 if (analyzable_condition (expr))
1017 res = expr;
1018 }
1019
1020 if (dump_file && (dump_flags & TDF_DETAILS))
1021 {
1022 print_generic_expr (dump_file, res, 0);
1023 fprintf (dump_file, ")\n");
1024 }
1025
1026 return res;
1027 }
1028
1029 /* Recursively determine and enqueue the exit conditions for a loop. */
1030
1031 static void
1032 get_exit_conditions_rec (struct loop *loop,
1033 varray_type *exit_conditions)
1034 {
1035 if (!loop)
1036 return;
1037
1038 /* Recurse on the inner loops, then on the next (sibling) loops. */
1039 get_exit_conditions_rec (loop->inner, exit_conditions);
1040 get_exit_conditions_rec (loop->next, exit_conditions);
1041
1042 if (loop->single_exit)
1043 {
1044 tree loop_condition = get_loop_exit_condition (loop);
1045
1046 if (loop_condition)
1047 VARRAY_PUSH_TREE (*exit_conditions, loop_condition);
1048 }
1049 }
1050
1051 /* Select the candidate loop nests for the analysis. This function
1052 initializes the EXIT_CONDITIONS array. */
1053
1054 static void
1055 select_loops_exit_conditions (struct loops *loops,
1056 varray_type *exit_conditions)
1057 {
1058 struct loop *function_body = loops->parray[0];
1059
1060 get_exit_conditions_rec (function_body->inner, exit_conditions);
1061 }
1062
1063 \f
1064 /* Depth first search algorithm. */
1065
1066 static bool follow_ssa_edge (struct loop *loop, tree, tree, tree *);
1067
1068 /* Follow the ssa edge into the right hand side RHS of an assignment.
1069 Return true if the strongly connected component has been found. */
1070
1071 static bool
1072 follow_ssa_edge_in_rhs (struct loop *loop,
1073 tree rhs,
1074 tree halting_phi,
1075 tree *evolution_of_loop)
1076 {
1077 bool res = false;
1078 tree rhs0, rhs1;
1079 tree type_rhs = TREE_TYPE (rhs);
1080
1081 /* The RHS is one of the following cases:
1082 - an SSA_NAME,
1083 - an INTEGER_CST,
1084 - a PLUS_EXPR,
1085 - a MINUS_EXPR,
1086 - other cases are not yet handled.
1087 */
1088 switch (TREE_CODE (rhs))
1089 {
1090 case NOP_EXPR:
1091 /* This assignment is under the form "a_1 = (cast) rhs. */
1092 res = follow_ssa_edge_in_rhs (loop, TREE_OPERAND (rhs, 0), halting_phi,
1093 evolution_of_loop);
1094 *evolution_of_loop = chrec_convert (TREE_TYPE (rhs), *evolution_of_loop);
1095 break;
1096
1097 case INTEGER_CST:
1098 /* This assignment is under the form "a_1 = 7". */
1099 res = false;
1100 break;
1101
1102 case SSA_NAME:
1103 /* This assignment is under the form: "a_1 = b_2". */
1104 res = follow_ssa_edge
1105 (loop, SSA_NAME_DEF_STMT (rhs), halting_phi, evolution_of_loop);
1106 break;
1107
1108 case PLUS_EXPR:
1109 /* This case is under the form "rhs0 + rhs1". */
1110 rhs0 = TREE_OPERAND (rhs, 0);
1111 rhs1 = TREE_OPERAND (rhs, 1);
1112 STRIP_TYPE_NOPS (rhs0);
1113 STRIP_TYPE_NOPS (rhs1);
1114
1115 if (TREE_CODE (rhs0) == SSA_NAME)
1116 {
1117 if (TREE_CODE (rhs1) == SSA_NAME)
1118 {
1119 /* Match an assignment under the form:
1120 "a = b + c". */
1121 res = follow_ssa_edge
1122 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
1123 evolution_of_loop);
1124
1125 if (res)
1126 *evolution_of_loop = add_to_evolution
1127 (loop->num,
1128 chrec_convert (type_rhs, *evolution_of_loop),
1129 PLUS_EXPR, rhs1);
1130
1131 else
1132 {
1133 res = follow_ssa_edge
1134 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
1135 evolution_of_loop);
1136
1137 if (res)
1138 *evolution_of_loop = add_to_evolution
1139 (loop->num,
1140 chrec_convert (type_rhs, *evolution_of_loop),
1141 PLUS_EXPR, rhs0);
1142 }
1143 }
1144
1145 else
1146 {
1147 /* Match an assignment under the form:
1148 "a = b + ...". */
1149 res = follow_ssa_edge
1150 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
1151 evolution_of_loop);
1152 if (res)
1153 *evolution_of_loop = add_to_evolution
1154 (loop->num, chrec_convert (type_rhs, *evolution_of_loop),
1155 PLUS_EXPR, rhs1);
1156 }
1157 }
1158
1159 else if (TREE_CODE (rhs1) == SSA_NAME)
1160 {
1161 /* Match an assignment under the form:
1162 "a = ... + c". */
1163 res = follow_ssa_edge
1164 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
1165 evolution_of_loop);
1166 if (res)
1167 *evolution_of_loop = add_to_evolution
1168 (loop->num, chrec_convert (type_rhs, *evolution_of_loop),
1169 PLUS_EXPR, rhs0);
1170 }
1171
1172 else
1173 /* Otherwise, match an assignment under the form:
1174 "a = ... + ...". */
1175 /* And there is nothing to do. */
1176 res = false;
1177
1178 break;
1179
1180 case MINUS_EXPR:
1181 /* This case is under the form "opnd0 = rhs0 - rhs1". */
1182 rhs0 = TREE_OPERAND (rhs, 0);
1183 rhs1 = TREE_OPERAND (rhs, 1);
1184 STRIP_TYPE_NOPS (rhs0);
1185 STRIP_TYPE_NOPS (rhs1);
1186
1187 if (TREE_CODE (rhs0) == SSA_NAME)
1188 {
1189 /* Match an assignment under the form:
1190 "a = b - ...". */
1191 res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
1192 evolution_of_loop);
1193 if (res)
1194 *evolution_of_loop = add_to_evolution
1195 (loop->num, chrec_convert (type_rhs, *evolution_of_loop),
1196 MINUS_EXPR, rhs1);
1197 }
1198 else
1199 /* Otherwise, match an assignment under the form:
1200 "a = ... - ...". */
1201 /* And there is nothing to do. */
1202 res = false;
1203
1204 break;
1205
1206 case MULT_EXPR:
1207 /* This case is under the form "opnd0 = rhs0 * rhs1". */
1208 rhs0 = TREE_OPERAND (rhs, 0);
1209 rhs1 = TREE_OPERAND (rhs, 1);
1210 STRIP_TYPE_NOPS (rhs0);
1211 STRIP_TYPE_NOPS (rhs1);
1212
1213 if (TREE_CODE (rhs0) == SSA_NAME)
1214 {
1215 if (TREE_CODE (rhs1) == SSA_NAME)
1216 {
1217 /* Match an assignment under the form:
1218 "a = b * c". */
1219 res = follow_ssa_edge
1220 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
1221 evolution_of_loop);
1222
1223 if (res)
1224 *evolution_of_loop = chrec_dont_know;
1225
1226 else
1227 {
1228 res = follow_ssa_edge
1229 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
1230 evolution_of_loop);
1231
1232 if (res)
1233 *evolution_of_loop = chrec_dont_know;
1234 }
1235 }
1236
1237 else
1238 {
1239 /* Match an assignment under the form:
1240 "a = b * ...". */
1241 res = follow_ssa_edge
1242 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
1243 evolution_of_loop);
1244 if (res)
1245 *evolution_of_loop = chrec_dont_know;
1246 }
1247 }
1248
1249 else if (TREE_CODE (rhs1) == SSA_NAME)
1250 {
1251 /* Match an assignment under the form:
1252 "a = ... * c". */
1253 res = follow_ssa_edge
1254 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
1255 evolution_of_loop);
1256 if (res)
1257 *evolution_of_loop = chrec_dont_know;
1258 }
1259
1260 else
1261 /* Otherwise, match an assignment under the form:
1262 "a = ... * ...". */
1263 /* And there is nothing to do. */
1264 res = false;
1265
1266 break;
1267
1268 default:
1269 res = false;
1270 break;
1271 }
1272
1273 return res;
1274 }
1275
1276 /* Checks whether the I-th argument of a PHI comes from a backedge. */
1277
1278 static bool
1279 backedge_phi_arg_p (tree phi, int i)
1280 {
1281 edge e = PHI_ARG_EDGE (phi, i);
1282
1283 /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
1284 about updating it anywhere, and this should work as well most of the
1285 time. */
1286 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1287 return true;
1288
1289 return false;
1290 }
1291
1292 /* Helper function for one branch of the condition-phi-node. Return
1293 true if the strongly connected component has been found following
1294 this path. */
1295
1296 static inline bool
1297 follow_ssa_edge_in_condition_phi_branch (int i,
1298 struct loop *loop,
1299 tree condition_phi,
1300 tree halting_phi,
1301 tree *evolution_of_branch,
1302 tree init_cond)
1303 {
1304 tree branch = PHI_ARG_DEF (condition_phi, i);
1305 *evolution_of_branch = chrec_dont_know;
1306
1307 /* Do not follow back edges (they must belong to an irreducible loop, which
1308 we really do not want to worry about). */
1309 if (backedge_phi_arg_p (condition_phi, i))
1310 return false;
1311
1312 if (TREE_CODE (branch) == SSA_NAME)
1313 {
1314 *evolution_of_branch = init_cond;
1315 return follow_ssa_edge (loop, SSA_NAME_DEF_STMT (branch), halting_phi,
1316 evolution_of_branch);
1317 }
1318
1319 /* This case occurs when one of the condition branches sets
1320 the variable to a constant: i.e. a phi-node like
1321 "a_2 = PHI <a_7(5), 2(6)>;".
1322
1323 FIXME: This case have to be refined correctly:
1324 in some cases it is possible to say something better than
1325 chrec_dont_know, for example using a wrap-around notation. */
1326 return false;
1327 }
1328
1329 /* This function merges the branches of a condition-phi-node in a
1330 loop. */
1331
1332 static bool
1333 follow_ssa_edge_in_condition_phi (struct loop *loop,
1334 tree condition_phi,
1335 tree halting_phi,
1336 tree *evolution_of_loop)
1337 {
1338 int i;
1339 tree init = *evolution_of_loop;
1340 tree evolution_of_branch;
1341
1342 if (!follow_ssa_edge_in_condition_phi_branch (0, loop, condition_phi,
1343 halting_phi,
1344 &evolution_of_branch,
1345 init))
1346 return false;
1347 *evolution_of_loop = evolution_of_branch;
1348
1349 for (i = 1; i < PHI_NUM_ARGS (condition_phi); i++)
1350 {
1351 /* Quickly give up when the evolution of one of the branches is
1352 not known. */
1353 if (*evolution_of_loop == chrec_dont_know)
1354 return true;
1355
1356 if (!follow_ssa_edge_in_condition_phi_branch (i, loop, condition_phi,
1357 halting_phi,
1358 &evolution_of_branch,
1359 init))
1360 return false;
1361
1362 *evolution_of_loop = chrec_merge (*evolution_of_loop,
1363 evolution_of_branch);
1364 }
1365
1366 return true;
1367 }
1368
1369 /* Follow an SSA edge in an inner loop. It computes the overall
1370 effect of the loop, and following the symbolic initial conditions,
1371 it follows the edges in the parent loop. The inner loop is
1372 considered as a single statement. */
1373
1374 static bool
1375 follow_ssa_edge_inner_loop_phi (struct loop *outer_loop,
1376 tree loop_phi_node,
1377 tree halting_phi,
1378 tree *evolution_of_loop)
1379 {
1380 struct loop *loop = loop_containing_stmt (loop_phi_node);
1381 tree ev = analyze_scalar_evolution (loop, PHI_RESULT (loop_phi_node));
1382
1383 /* Sometimes, the inner loop is too difficult to analyze, and the
1384 result of the analysis is a symbolic parameter. */
1385 if (ev == PHI_RESULT (loop_phi_node))
1386 {
1387 bool res = false;
1388 int i;
1389
1390 for (i = 0; i < PHI_NUM_ARGS (loop_phi_node); i++)
1391 {
1392 tree arg = PHI_ARG_DEF (loop_phi_node, i);
1393 basic_block bb;
1394
1395 /* Follow the edges that exit the inner loop. */
1396 bb = PHI_ARG_EDGE (loop_phi_node, i)->src;
1397 if (!flow_bb_inside_loop_p (loop, bb))
1398 res = res || follow_ssa_edge_in_rhs (outer_loop, arg, halting_phi,
1399 evolution_of_loop);
1400 }
1401
1402 /* If the path crosses this loop-phi, give up. */
1403 if (res == true)
1404 *evolution_of_loop = chrec_dont_know;
1405
1406 return res;
1407 }
1408
1409 /* Otherwise, compute the overall effect of the inner loop. */
1410 ev = compute_overall_effect_of_inner_loop (loop, ev);
1411 return follow_ssa_edge_in_rhs (outer_loop, ev, halting_phi,
1412 evolution_of_loop);
1413 }
1414
1415 /* Follow an SSA edge from a loop-phi-node to itself, constructing a
1416 path that is analyzed on the return walk. */
1417
1418 static bool
1419 follow_ssa_edge (struct loop *loop,
1420 tree def,
1421 tree halting_phi,
1422 tree *evolution_of_loop)
1423 {
1424 struct loop *def_loop;
1425
1426 if (TREE_CODE (def) == NOP_EXPR)
1427 return false;
1428
1429 def_loop = loop_containing_stmt (def);
1430
1431 switch (TREE_CODE (def))
1432 {
1433 case PHI_NODE:
1434 if (!loop_phi_node_p (def))
1435 /* DEF is a condition-phi-node. Follow the branches, and
1436 record their evolutions. Finally, merge the collected
1437 information and set the approximation to the main
1438 variable. */
1439 return follow_ssa_edge_in_condition_phi
1440 (loop, def, halting_phi, evolution_of_loop);
1441
1442 /* When the analyzed phi is the halting_phi, the
1443 depth-first search is over: we have found a path from
1444 the halting_phi to itself in the loop. */
1445 if (def == halting_phi)
1446 return true;
1447
1448 /* Otherwise, the evolution of the HALTING_PHI depends
1449 on the evolution of another loop-phi-node, i.e. the
1450 evolution function is a higher degree polynomial. */
1451 if (def_loop == loop)
1452 return false;
1453
1454 /* Inner loop. */
1455 if (flow_loop_nested_p (loop, def_loop))
1456 return follow_ssa_edge_inner_loop_phi
1457 (loop, def, halting_phi, evolution_of_loop);
1458
1459 /* Outer loop. */
1460 return false;
1461
1462 case MODIFY_EXPR:
1463 return follow_ssa_edge_in_rhs (loop,
1464 TREE_OPERAND (def, 1),
1465 halting_phi,
1466 evolution_of_loop);
1467
1468 default:
1469 /* At this level of abstraction, the program is just a set
1470 of MODIFY_EXPRs and PHI_NODEs. In principle there is no
1471 other node to be handled. */
1472 return false;
1473 }
1474 }
1475
1476 \f
1477
1478 /* Given a LOOP_PHI_NODE, this function determines the evolution
1479 function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */
1480
1481 static tree
1482 analyze_evolution_in_loop (tree loop_phi_node,
1483 tree init_cond)
1484 {
1485 int i;
1486 tree evolution_function = chrec_not_analyzed_yet;
1487 struct loop *loop = loop_containing_stmt (loop_phi_node);
1488 basic_block bb;
1489
1490 if (dump_file && (dump_flags & TDF_DETAILS))
1491 {
1492 fprintf (dump_file, "(analyze_evolution_in_loop \n");
1493 fprintf (dump_file, " (loop_phi_node = ");
1494 print_generic_expr (dump_file, loop_phi_node, 0);
1495 fprintf (dump_file, ")\n");
1496 }
1497
1498 for (i = 0; i < PHI_NUM_ARGS (loop_phi_node); i++)
1499 {
1500 tree arg = PHI_ARG_DEF (loop_phi_node, i);
1501 tree ssa_chain, ev_fn;
1502 bool res;
1503
1504 /* Select the edges that enter the loop body. */
1505 bb = PHI_ARG_EDGE (loop_phi_node, i)->src;
1506 if (!flow_bb_inside_loop_p (loop, bb))
1507 continue;
1508
1509 if (TREE_CODE (arg) == SSA_NAME)
1510 {
1511 ssa_chain = SSA_NAME_DEF_STMT (arg);
1512
1513 /* Pass in the initial condition to the follow edge function. */
1514 ev_fn = init_cond;
1515 res = follow_ssa_edge (loop, ssa_chain, loop_phi_node, &ev_fn);
1516 }
1517 else
1518 res = false;
1519
1520 /* When it is impossible to go back on the same
1521 loop_phi_node by following the ssa edges, the
1522 evolution is represented by a peeled chrec, i.e. the
1523 first iteration, EV_FN has the value INIT_COND, then
1524 all the other iterations it has the value of ARG.
1525 For the moment, PEELED_CHREC nodes are not built. */
1526 if (!res)
1527 ev_fn = chrec_dont_know;
1528
1529 /* When there are multiple back edges of the loop (which in fact never
1530 happens currently, but nevertheless), merge their evolutions. */
1531 evolution_function = chrec_merge (evolution_function, ev_fn);
1532 }
1533
1534 if (dump_file && (dump_flags & TDF_DETAILS))
1535 {
1536 fprintf (dump_file, " (evolution_function = ");
1537 print_generic_expr (dump_file, evolution_function, 0);
1538 fprintf (dump_file, "))\n");
1539 }
1540
1541 return evolution_function;
1542 }
1543
1544 /* Given a loop-phi-node, return the initial conditions of the
1545 variable on entry of the loop. When the CCP has propagated
1546 constants into the loop-phi-node, the initial condition is
1547 instantiated, otherwise the initial condition is kept symbolic.
1548 This analyzer does not analyze the evolution outside the current
1549 loop, and leaves this task to the on-demand tree reconstructor. */
1550
1551 static tree
1552 analyze_initial_condition (tree loop_phi_node)
1553 {
1554 int i;
1555 tree init_cond = chrec_not_analyzed_yet;
1556 struct loop *loop = bb_for_stmt (loop_phi_node)->loop_father;
1557
1558 if (dump_file && (dump_flags & TDF_DETAILS))
1559 {
1560 fprintf (dump_file, "(analyze_initial_condition \n");
1561 fprintf (dump_file, " (loop_phi_node = \n");
1562 print_generic_expr (dump_file, loop_phi_node, 0);
1563 fprintf (dump_file, ")\n");
1564 }
1565
1566 for (i = 0; i < PHI_NUM_ARGS (loop_phi_node); i++)
1567 {
1568 tree branch = PHI_ARG_DEF (loop_phi_node, i);
1569 basic_block bb = PHI_ARG_EDGE (loop_phi_node, i)->src;
1570
1571 /* When the branch is oriented to the loop's body, it does
1572 not contribute to the initial condition. */
1573 if (flow_bb_inside_loop_p (loop, bb))
1574 continue;
1575
1576 if (init_cond == chrec_not_analyzed_yet)
1577 {
1578 init_cond = branch;
1579 continue;
1580 }
1581
1582 if (TREE_CODE (branch) == SSA_NAME)
1583 {
1584 init_cond = chrec_dont_know;
1585 break;
1586 }
1587
1588 init_cond = chrec_merge (init_cond, branch);
1589 }
1590
1591 /* Ooops -- a loop without an entry??? */
1592 if (init_cond == chrec_not_analyzed_yet)
1593 init_cond = chrec_dont_know;
1594
1595 if (dump_file && (dump_flags & TDF_DETAILS))
1596 {
1597 fprintf (dump_file, " (init_cond = ");
1598 print_generic_expr (dump_file, init_cond, 0);
1599 fprintf (dump_file, "))\n");
1600 }
1601
1602 return init_cond;
1603 }
1604
1605 /* Analyze the scalar evolution for LOOP_PHI_NODE. */
1606
1607 static tree
1608 interpret_loop_phi (struct loop *loop, tree loop_phi_node)
1609 {
1610 tree res;
1611 struct loop *phi_loop = loop_containing_stmt (loop_phi_node);
1612 tree init_cond;
1613
1614 if (phi_loop != loop)
1615 {
1616 struct loop *subloop;
1617 tree evolution_fn = analyze_scalar_evolution
1618 (phi_loop, PHI_RESULT (loop_phi_node));
1619
1620 /* Dive one level deeper. */
1621 subloop = superloop_at_depth (phi_loop, loop->depth + 1);
1622
1623 /* Interpret the subloop. */
1624 res = compute_overall_effect_of_inner_loop (subloop, evolution_fn);
1625 return res;
1626 }
1627
1628 /* Otherwise really interpret the loop phi. */
1629 init_cond = analyze_initial_condition (loop_phi_node);
1630 res = analyze_evolution_in_loop (loop_phi_node, init_cond);
1631
1632 return res;
1633 }
1634
1635 /* This function merges the branches of a condition-phi-node,
1636 contained in the outermost loop, and whose arguments are already
1637 analyzed. */
1638
1639 static tree
1640 interpret_condition_phi (struct loop *loop, tree condition_phi)
1641 {
1642 int i;
1643 tree res = chrec_not_analyzed_yet;
1644
1645 for (i = 0; i < PHI_NUM_ARGS (condition_phi); i++)
1646 {
1647 tree branch_chrec;
1648
1649 if (backedge_phi_arg_p (condition_phi, i))
1650 {
1651 res = chrec_dont_know;
1652 break;
1653 }
1654
1655 branch_chrec = analyze_scalar_evolution
1656 (loop, PHI_ARG_DEF (condition_phi, i));
1657
1658 res = chrec_merge (res, branch_chrec);
1659 }
1660
1661 return res;
1662 }
1663
1664 /* Interpret the right hand side of a modify_expr OPND1. If we didn't
1665 analyzed this node before, follow the definitions until ending
1666 either on an analyzed modify_expr, or on a loop-phi-node. On the
1667 return path, this function propagates evolutions (ala constant copy
1668 propagation). OPND1 is not a GIMPLE expression because we could
1669 analyze the effect of an inner loop: see interpret_loop_phi. */
1670
1671 static tree
1672 interpret_rhs_modify_expr (struct loop *loop,
1673 tree opnd1, tree type)
1674 {
1675 tree res, opnd10, opnd11, chrec10, chrec11;
1676
1677 if (is_gimple_min_invariant (opnd1))
1678 return chrec_convert (type, opnd1);
1679
1680 switch (TREE_CODE (opnd1))
1681 {
1682 case PLUS_EXPR:
1683 opnd10 = TREE_OPERAND (opnd1, 0);
1684 opnd11 = TREE_OPERAND (opnd1, 1);
1685 chrec10 = analyze_scalar_evolution (loop, opnd10);
1686 chrec11 = analyze_scalar_evolution (loop, opnd11);
1687 chrec10 = chrec_convert (type, chrec10);
1688 chrec11 = chrec_convert (type, chrec11);
1689 res = chrec_fold_plus (type, chrec10, chrec11);
1690 break;
1691
1692 case MINUS_EXPR:
1693 opnd10 = TREE_OPERAND (opnd1, 0);
1694 opnd11 = TREE_OPERAND (opnd1, 1);
1695 chrec10 = analyze_scalar_evolution (loop, opnd10);
1696 chrec11 = analyze_scalar_evolution (loop, opnd11);
1697 chrec10 = chrec_convert (type, chrec10);
1698 chrec11 = chrec_convert (type, chrec11);
1699 res = chrec_fold_minus (type, chrec10, chrec11);
1700 break;
1701
1702 case NEGATE_EXPR:
1703 opnd10 = TREE_OPERAND (opnd1, 0);
1704 chrec10 = analyze_scalar_evolution (loop, opnd10);
1705 chrec10 = chrec_convert (type, chrec10);
1706 res = chrec_fold_minus (type, build_int_cst (type, 0), chrec10);
1707 break;
1708
1709 case MULT_EXPR:
1710 opnd10 = TREE_OPERAND (opnd1, 0);
1711 opnd11 = TREE_OPERAND (opnd1, 1);
1712 chrec10 = analyze_scalar_evolution (loop, opnd10);
1713 chrec11 = analyze_scalar_evolution (loop, opnd11);
1714 chrec10 = chrec_convert (type, chrec10);
1715 chrec11 = chrec_convert (type, chrec11);
1716 res = chrec_fold_multiply (type, chrec10, chrec11);
1717 break;
1718
1719 case SSA_NAME:
1720 res = chrec_convert (type, analyze_scalar_evolution (loop, opnd1));
1721 break;
1722
1723 case NOP_EXPR:
1724 case CONVERT_EXPR:
1725 opnd10 = TREE_OPERAND (opnd1, 0);
1726 chrec10 = analyze_scalar_evolution (loop, opnd10);
1727 res = chrec_convert (type, chrec10);
1728 break;
1729
1730 default:
1731 res = chrec_dont_know;
1732 break;
1733 }
1734
1735 return res;
1736 }
1737
1738 \f
1739
1740 /* This section contains all the entry points:
1741 - number_of_iterations_in_loop,
1742 - analyze_scalar_evolution,
1743 - instantiate_parameters.
1744 */
1745
1746 /* Compute and return the evolution function in WRTO_LOOP, the nearest
1747 common ancestor of DEF_LOOP and USE_LOOP. */
1748
1749 static tree
1750 compute_scalar_evolution_in_loop (struct loop *wrto_loop,
1751 struct loop *def_loop,
1752 tree ev)
1753 {
1754 tree res;
1755 if (def_loop == wrto_loop)
1756 return ev;
1757
1758 def_loop = superloop_at_depth (def_loop, wrto_loop->depth + 1);
1759 res = compute_overall_effect_of_inner_loop (def_loop, ev);
1760
1761 return analyze_scalar_evolution_1 (wrto_loop, res, chrec_not_analyzed_yet);
1762 }
1763
1764 /* Helper recursive function. */
1765
1766 static tree
1767 analyze_scalar_evolution_1 (struct loop *loop, tree var, tree res)
1768 {
1769 tree def, type = TREE_TYPE (var);
1770 basic_block bb;
1771 struct loop *def_loop;
1772
1773 if (loop == NULL)
1774 return chrec_dont_know;
1775
1776 if (TREE_CODE (var) != SSA_NAME)
1777 return interpret_rhs_modify_expr (loop, var, type);
1778
1779 def = SSA_NAME_DEF_STMT (var);
1780 bb = bb_for_stmt (def);
1781 def_loop = bb ? bb->loop_father : NULL;
1782
1783 if (bb == NULL
1784 || !flow_bb_inside_loop_p (loop, bb))
1785 {
1786 /* Keep the symbolic form. */
1787 res = var;
1788 goto set_and_end;
1789 }
1790
1791 if (res != chrec_not_analyzed_yet)
1792 {
1793 if (loop != bb->loop_father)
1794 res = compute_scalar_evolution_in_loop
1795 (find_common_loop (loop, bb->loop_father), bb->loop_father, res);
1796
1797 goto set_and_end;
1798 }
1799
1800 if (loop != def_loop)
1801 {
1802 res = analyze_scalar_evolution_1 (def_loop, var, chrec_not_analyzed_yet);
1803 res = compute_scalar_evolution_in_loop (loop, def_loop, res);
1804
1805 goto set_and_end;
1806 }
1807
1808 switch (TREE_CODE (def))
1809 {
1810 case MODIFY_EXPR:
1811 res = interpret_rhs_modify_expr (loop, TREE_OPERAND (def, 1), type);
1812 break;
1813
1814 case PHI_NODE:
1815 if (loop_phi_node_p (def))
1816 res = interpret_loop_phi (loop, def);
1817 else
1818 res = interpret_condition_phi (loop, def);
1819 break;
1820
1821 default:
1822 res = chrec_dont_know;
1823 break;
1824 }
1825
1826 set_and_end:
1827
1828 /* Keep the symbolic form. */
1829 if (res == chrec_dont_know)
1830 res = var;
1831
1832 if (loop == def_loop)
1833 set_scalar_evolution (var, res);
1834
1835 return res;
1836 }
1837
1838 /* Entry point for the scalar evolution analyzer.
1839 Analyzes and returns the scalar evolution of the ssa_name VAR.
1840 LOOP_NB is the identifier number of the loop in which the variable
1841 is used.
1842
1843 Example of use: having a pointer VAR to a SSA_NAME node, STMT a
1844 pointer to the statement that uses this variable, in order to
1845 determine the evolution function of the variable, use the following
1846 calls:
1847
1848 unsigned loop_nb = loop_containing_stmt (stmt)->num;
1849 tree chrec_with_symbols = analyze_scalar_evolution (loop_nb, var);
1850 tree chrec_instantiated = instantiate_parameters
1851 (loop_nb, chrec_with_symbols);
1852 */
1853
1854 tree
1855 analyze_scalar_evolution (struct loop *loop, tree var)
1856 {
1857 tree res;
1858
1859 if (dump_file && (dump_flags & TDF_DETAILS))
1860 {
1861 fprintf (dump_file, "(analyze_scalar_evolution \n");
1862 fprintf (dump_file, " (loop_nb = %d)\n", loop->num);
1863 fprintf (dump_file, " (scalar = ");
1864 print_generic_expr (dump_file, var, 0);
1865 fprintf (dump_file, ")\n");
1866 }
1867
1868 res = analyze_scalar_evolution_1 (loop, var, get_scalar_evolution (var));
1869
1870 if (TREE_CODE (var) == SSA_NAME && res == chrec_dont_know)
1871 res = var;
1872
1873 if (dump_file && (dump_flags & TDF_DETAILS))
1874 fprintf (dump_file, ")\n");
1875
1876 return res;
1877 }
1878
1879 /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
1880 WRTO_LOOP (which should be a superloop of both USE_LOOP and definition
1881 of VERSION). */
1882
1883 static tree
1884 analyze_scalar_evolution_in_loop (struct loop *wrto_loop, struct loop *use_loop,
1885 tree version)
1886 {
1887 bool val = false;
1888 tree ev = version;
1889
1890 while (1)
1891 {
1892 ev = analyze_scalar_evolution (use_loop, ev);
1893 ev = resolve_mixers (use_loop, ev);
1894
1895 if (use_loop == wrto_loop)
1896 return ev;
1897
1898 /* If the value of the use changes in the inner loop, we cannot express
1899 its value in the outer loop (we might try to return interval chrec,
1900 but we do not have a user for it anyway) */
1901 if (!no_evolution_in_loop_p (ev, use_loop->num, &val)
1902 || !val)
1903 return chrec_dont_know;
1904
1905 use_loop = use_loop->outer;
1906 }
1907 }
1908
1909 /* Analyze all the parameters of the chrec that were left under a symbolic form,
1910 with respect to LOOP. CHREC is the chrec to instantiate. If
1911 ALLOW_SUPERLOOP_CHRECS is true, replacing loop invariants with
1912 outer loop chrecs is done. */
1913
1914 static tree
1915 instantiate_parameters_1 (struct loop *loop, tree chrec,
1916 bool allow_superloop_chrecs)
1917 {
1918 tree res, op0, op1, op2;
1919 basic_block def_bb;
1920 struct loop *def_loop;
1921
1922 if (chrec == NULL_TREE
1923 || automatically_generated_chrec_p (chrec))
1924 return chrec;
1925
1926 if (is_gimple_min_invariant (chrec))
1927 return chrec;
1928
1929 switch (TREE_CODE (chrec))
1930 {
1931 case SSA_NAME:
1932 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (chrec));
1933
1934 /* A parameter (or loop invariant and we do not want to include
1935 evolutions in outer loops), nothing to do. */
1936 if (!def_bb
1937 || (!allow_superloop_chrecs
1938 && !flow_bb_inside_loop_p (loop, def_bb)))
1939 return chrec;
1940
1941 /* Don't instantiate the SSA_NAME if it is in a mixer
1942 structure. This is used for avoiding the instantiation of
1943 recursively defined functions, such as:
1944
1945 | a_2 -> {0, +, 1, +, a_2}_1 */
1946
1947 if (bitmap_bit_p (already_instantiated, SSA_NAME_VERSION (chrec)))
1948 {
1949 if (!flow_bb_inside_loop_p (loop, def_bb))
1950 {
1951 /* We may keep the loop invariant in symbolic form. */
1952 return chrec;
1953 }
1954 else
1955 {
1956 /* Something with unknown behavior in LOOP. */
1957 return chrec_dont_know;
1958 }
1959 }
1960
1961 def_loop = find_common_loop (loop, def_bb->loop_father);
1962
1963 /* If the analysis yields a parametric chrec, instantiate the
1964 result again. Avoid the cyclic instantiation in mixers. */
1965 bitmap_set_bit (already_instantiated, SSA_NAME_VERSION (chrec));
1966 res = analyze_scalar_evolution (def_loop, chrec);
1967 res = instantiate_parameters_1 (loop, res, allow_superloop_chrecs);
1968 bitmap_clear_bit (already_instantiated, SSA_NAME_VERSION (chrec));
1969 return res;
1970
1971 case POLYNOMIAL_CHREC:
1972 op0 = instantiate_parameters_1 (loop, CHREC_LEFT (chrec),
1973 allow_superloop_chrecs);
1974 op1 = instantiate_parameters_1 (loop, CHREC_RIGHT (chrec),
1975 allow_superloop_chrecs);
1976 return build_polynomial_chrec (CHREC_VARIABLE (chrec), op0, op1);
1977
1978 case PLUS_EXPR:
1979 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
1980 allow_superloop_chrecs);
1981 op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
1982 allow_superloop_chrecs);
1983 return chrec_fold_plus (TREE_TYPE (chrec), op0, op1);
1984
1985 case MINUS_EXPR:
1986 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
1987 allow_superloop_chrecs);
1988 op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
1989 allow_superloop_chrecs);
1990 return chrec_fold_minus (TREE_TYPE (chrec), op0, op1);
1991
1992 case MULT_EXPR:
1993 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
1994 allow_superloop_chrecs);
1995 op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
1996 allow_superloop_chrecs);
1997 return chrec_fold_multiply (TREE_TYPE (chrec), op0, op1);
1998
1999 case NOP_EXPR:
2000 case CONVERT_EXPR:
2001 case NON_LVALUE_EXPR:
2002 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
2003 allow_superloop_chrecs);
2004 if (op0 == chrec_dont_know)
2005 return chrec_dont_know;
2006
2007 return chrec_convert (TREE_TYPE (chrec), op0);
2008
2009 case SCEV_NOT_KNOWN:
2010 return chrec_dont_know;
2011
2012 case SCEV_KNOWN:
2013 return chrec_known;
2014
2015 default:
2016 break;
2017 }
2018
2019 switch (TREE_CODE_LENGTH (TREE_CODE (chrec)))
2020 {
2021 case 3:
2022 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
2023 allow_superloop_chrecs);
2024 op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
2025 allow_superloop_chrecs);
2026 op2 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 2),
2027 allow_superloop_chrecs);
2028 if (op0 == chrec_dont_know
2029 || op1 == chrec_dont_know
2030 || op2 == chrec_dont_know)
2031 return chrec_dont_know;
2032 return fold (build (TREE_CODE (chrec),
2033 TREE_TYPE (chrec), op0, op1, op2));
2034
2035 case 2:
2036 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
2037 allow_superloop_chrecs);
2038 op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
2039 allow_superloop_chrecs);
2040 if (op0 == chrec_dont_know
2041 || op1 == chrec_dont_know)
2042 return chrec_dont_know;
2043 return fold (build (TREE_CODE (chrec), TREE_TYPE (chrec), op0, op1));
2044
2045 case 1:
2046 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
2047 allow_superloop_chrecs);
2048 if (op0 == chrec_dont_know)
2049 return chrec_dont_know;
2050 return fold (build1 (TREE_CODE (chrec), TREE_TYPE (chrec), op0));
2051
2052 case 0:
2053 return chrec;
2054
2055 default:
2056 break;
2057 }
2058
2059 /* Too complicated to handle. */
2060 return chrec_dont_know;
2061 }
2062
2063 /* Analyze all the parameters of the chrec that were left under a
2064 symbolic form. LOOP is the loop in which symbolic names have to
2065 be analyzed and instantiated. */
2066
2067 tree
2068 instantiate_parameters (struct loop *loop,
2069 tree chrec)
2070 {
2071 tree res;
2072
2073 if (dump_file && (dump_flags & TDF_DETAILS))
2074 {
2075 fprintf (dump_file, "(instantiate_parameters \n");
2076 fprintf (dump_file, " (loop_nb = %d)\n", loop->num);
2077 fprintf (dump_file, " (chrec = ");
2078 print_generic_expr (dump_file, chrec, 0);
2079 fprintf (dump_file, ")\n");
2080 }
2081
2082 res = instantiate_parameters_1 (loop, chrec, true);
2083
2084 if (dump_file && (dump_flags & TDF_DETAILS))
2085 {
2086 fprintf (dump_file, " (res = ");
2087 print_generic_expr (dump_file, res, 0);
2088 fprintf (dump_file, "))\n");
2089 }
2090
2091 return res;
2092 }
2093
2094 /* Similar to instantiate_parameters, but does not introduce the
2095 evolutions in outer loops for LOOP invariants in CHREC. */
2096
2097 static tree
2098 resolve_mixers (struct loop *loop, tree chrec)
2099 {
2100 return instantiate_parameters_1 (loop, chrec, false);
2101 }
2102
2103 /* Entry point for the analysis of the number of iterations pass.
2104 This function tries to safely approximate the number of iterations
2105 the loop will run. When this property is not decidable at compile
2106 time, the result is chrec_dont_know. Otherwise the result is
2107 a scalar or a symbolic parameter.
2108
2109 Example of analysis: suppose that the loop has an exit condition:
2110
2111 "if (b > 49) goto end_loop;"
2112
2113 and that in a previous analysis we have determined that the
2114 variable 'b' has an evolution function:
2115
2116 "EF = {23, +, 5}_2".
2117
2118 When we evaluate the function at the point 5, i.e. the value of the
2119 variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
2120 and EF (6) = 53. In this case the value of 'b' on exit is '53' and
2121 the loop body has been executed 6 times. */
2122
2123 tree
2124 number_of_iterations_in_loop (struct loop *loop)
2125 {
2126 tree res, type;
2127 edge exit;
2128 struct tree_niter_desc niter_desc;
2129
2130 /* Determine whether the number_of_iterations_in_loop has already
2131 been computed. */
2132 res = loop->nb_iterations;
2133 if (res)
2134 return res;
2135 res = chrec_dont_know;
2136
2137 if (dump_file && (dump_flags & TDF_DETAILS))
2138 fprintf (dump_file, "(number_of_iterations_in_loop\n");
2139
2140 exit = loop->single_exit;
2141 if (!exit)
2142 goto end;
2143
2144 if (!number_of_iterations_exit (loop, exit, &niter_desc))
2145 goto end;
2146
2147 type = TREE_TYPE (niter_desc.niter);
2148 if (integer_nonzerop (niter_desc.may_be_zero))
2149 res = build_int_cst (type, 0);
2150 else if (integer_zerop (niter_desc.may_be_zero))
2151 res = niter_desc.niter;
2152 else
2153 res = chrec_dont_know;
2154
2155 end:
2156 return set_nb_iterations_in_loop (loop, res);
2157 }
2158
2159 /* One of the drivers for testing the scalar evolutions analysis.
2160 This function computes the number of iterations for all the loops
2161 from the EXIT_CONDITIONS array. */
2162
2163 static void
2164 number_of_iterations_for_all_loops (varray_type exit_conditions)
2165 {
2166 unsigned int i;
2167 unsigned nb_chrec_dont_know_loops = 0;
2168 unsigned nb_static_loops = 0;
2169
2170 for (i = 0; i < VARRAY_ACTIVE_SIZE (exit_conditions); i++)
2171 {
2172 tree res = number_of_iterations_in_loop
2173 (loop_containing_stmt (VARRAY_TREE (exit_conditions, i)));
2174 if (chrec_contains_undetermined (res))
2175 nb_chrec_dont_know_loops++;
2176 else
2177 nb_static_loops++;
2178 }
2179
2180 if (dump_file)
2181 {
2182 fprintf (dump_file, "\n(\n");
2183 fprintf (dump_file, "-----------------------------------------\n");
2184 fprintf (dump_file, "%d\tnb_chrec_dont_know_loops\n", nb_chrec_dont_know_loops);
2185 fprintf (dump_file, "%d\tnb_static_loops\n", nb_static_loops);
2186 fprintf (dump_file, "%d\tnb_total_loops\n", current_loops->num);
2187 fprintf (dump_file, "-----------------------------------------\n");
2188 fprintf (dump_file, ")\n\n");
2189
2190 print_loop_ir (dump_file);
2191 }
2192 }
2193
2194 \f
2195
2196 /* Counters for the stats. */
2197
2198 struct chrec_stats
2199 {
2200 unsigned nb_chrecs;
2201 unsigned nb_affine;
2202 unsigned nb_affine_multivar;
2203 unsigned nb_higher_poly;
2204 unsigned nb_chrec_dont_know;
2205 unsigned nb_undetermined;
2206 };
2207
2208 /* Reset the counters. */
2209
2210 static inline void
2211 reset_chrecs_counters (struct chrec_stats *stats)
2212 {
2213 stats->nb_chrecs = 0;
2214 stats->nb_affine = 0;
2215 stats->nb_affine_multivar = 0;
2216 stats->nb_higher_poly = 0;
2217 stats->nb_chrec_dont_know = 0;
2218 stats->nb_undetermined = 0;
2219 }
2220
2221 /* Dump the contents of a CHREC_STATS structure. */
2222
2223 static void
2224 dump_chrecs_stats (FILE *file, struct chrec_stats *stats)
2225 {
2226 fprintf (file, "\n(\n");
2227 fprintf (file, "-----------------------------------------\n");
2228 fprintf (file, "%d\taffine univariate chrecs\n", stats->nb_affine);
2229 fprintf (file, "%d\taffine multivariate chrecs\n", stats->nb_affine_multivar);
2230 fprintf (file, "%d\tdegree greater than 2 polynomials\n",
2231 stats->nb_higher_poly);
2232 fprintf (file, "%d\tchrec_dont_know chrecs\n", stats->nb_chrec_dont_know);
2233 fprintf (file, "-----------------------------------------\n");
2234 fprintf (file, "%d\ttotal chrecs\n", stats->nb_chrecs);
2235 fprintf (file, "%d\twith undetermined coefficients\n",
2236 stats->nb_undetermined);
2237 fprintf (file, "-----------------------------------------\n");
2238 fprintf (file, "%d\tchrecs in the scev database\n",
2239 (int) htab_elements (scalar_evolution_info));
2240 fprintf (file, "%d\tsets in the scev database\n", nb_set_scev);
2241 fprintf (file, "%d\tgets in the scev database\n", nb_get_scev);
2242 fprintf (file, "-----------------------------------------\n");
2243 fprintf (file, ")\n\n");
2244 }
2245
2246 /* Gather statistics about CHREC. */
2247
2248 static void
2249 gather_chrec_stats (tree chrec, struct chrec_stats *stats)
2250 {
2251 if (dump_file && (dump_flags & TDF_STATS))
2252 {
2253 fprintf (dump_file, "(classify_chrec ");
2254 print_generic_expr (dump_file, chrec, 0);
2255 fprintf (dump_file, "\n");
2256 }
2257
2258 stats->nb_chrecs++;
2259
2260 if (chrec == NULL_TREE)
2261 {
2262 stats->nb_undetermined++;
2263 return;
2264 }
2265
2266 switch (TREE_CODE (chrec))
2267 {
2268 case POLYNOMIAL_CHREC:
2269 if (evolution_function_is_affine_p (chrec))
2270 {
2271 if (dump_file && (dump_flags & TDF_STATS))
2272 fprintf (dump_file, " affine_univariate\n");
2273 stats->nb_affine++;
2274 }
2275 else if (evolution_function_is_affine_multivariate_p (chrec))
2276 {
2277 if (dump_file && (dump_flags & TDF_STATS))
2278 fprintf (dump_file, " affine_multivariate\n");
2279 stats->nb_affine_multivar++;
2280 }
2281 else
2282 {
2283 if (dump_file && (dump_flags & TDF_STATS))
2284 fprintf (dump_file, " higher_degree_polynomial\n");
2285 stats->nb_higher_poly++;
2286 }
2287
2288 break;
2289
2290 default:
2291 break;
2292 }
2293
2294 if (chrec_contains_undetermined (chrec))
2295 {
2296 if (dump_file && (dump_flags & TDF_STATS))
2297 fprintf (dump_file, " undetermined\n");
2298 stats->nb_undetermined++;
2299 }
2300
2301 if (dump_file && (dump_flags & TDF_STATS))
2302 fprintf (dump_file, ")\n");
2303 }
2304
2305 /* One of the drivers for testing the scalar evolutions analysis.
2306 This function analyzes the scalar evolution of all the scalars
2307 defined as loop phi nodes in one of the loops from the
2308 EXIT_CONDITIONS array.
2309
2310 TODO Optimization: A loop is in canonical form if it contains only
2311 a single scalar loop phi node. All the other scalars that have an
2312 evolution in the loop are rewritten in function of this single
2313 index. This allows the parallelization of the loop. */
2314
2315 static void
2316 analyze_scalar_evolution_for_all_loop_phi_nodes (varray_type exit_conditions)
2317 {
2318 unsigned int i;
2319 struct chrec_stats stats;
2320
2321 reset_chrecs_counters (&stats);
2322
2323 for (i = 0; i < VARRAY_ACTIVE_SIZE (exit_conditions); i++)
2324 {
2325 struct loop *loop;
2326 basic_block bb;
2327 tree phi, chrec;
2328
2329 loop = loop_containing_stmt (VARRAY_TREE (exit_conditions, i));
2330 bb = loop->header;
2331
2332 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2333 if (is_gimple_reg (PHI_RESULT (phi)))
2334 {
2335 chrec = instantiate_parameters
2336 (loop,
2337 analyze_scalar_evolution (loop, PHI_RESULT (phi)));
2338
2339 if (dump_file && (dump_flags & TDF_STATS))
2340 gather_chrec_stats (chrec, &stats);
2341 }
2342 }
2343
2344 if (dump_file && (dump_flags & TDF_STATS))
2345 dump_chrecs_stats (dump_file, &stats);
2346 }
2347
2348 /* Callback for htab_traverse, gathers information on chrecs in the
2349 hashtable. */
2350
2351 static int
2352 gather_stats_on_scev_database_1 (void **slot, void *stats)
2353 {
2354 struct scev_info_str *entry = *slot;
2355
2356 gather_chrec_stats (entry->chrec, stats);
2357
2358 return 1;
2359 }
2360
2361 /* Classify the chrecs of the whole database. */
2362
2363 void
2364 gather_stats_on_scev_database (void)
2365 {
2366 struct chrec_stats stats;
2367
2368 if (!dump_file)
2369 return;
2370
2371 reset_chrecs_counters (&stats);
2372
2373 htab_traverse (scalar_evolution_info, gather_stats_on_scev_database_1,
2374 &stats);
2375
2376 dump_chrecs_stats (dump_file, &stats);
2377 }
2378
2379 \f
2380
2381 /* Initializer. */
2382
2383 static void
2384 initialize_scalar_evolutions_analyzer (void)
2385 {
2386 /* The elements below are unique. */
2387 if (chrec_dont_know == NULL_TREE)
2388 {
2389 chrec_not_analyzed_yet = NULL_TREE;
2390 chrec_dont_know = make_node (SCEV_NOT_KNOWN);
2391 chrec_known = make_node (SCEV_KNOWN);
2392 TREE_TYPE (chrec_dont_know) = NULL_TREE;
2393 TREE_TYPE (chrec_known) = NULL_TREE;
2394 }
2395 }
2396
2397 /* Initialize the analysis of scalar evolutions for LOOPS. */
2398
2399 void
2400 scev_initialize (struct loops *loops)
2401 {
2402 unsigned i;
2403 current_loops = loops;
2404
2405 scalar_evolution_info = htab_create (100, hash_scev_info,
2406 eq_scev_info, del_scev_info);
2407 already_instantiated = BITMAP_XMALLOC ();
2408
2409 initialize_scalar_evolutions_analyzer ();
2410
2411 for (i = 1; i < loops->num; i++)
2412 if (loops->parray[i])
2413 loops->parray[i]->nb_iterations = NULL_TREE;
2414 }
2415
2416 /* Cleans up the information cached by the scalar evolutions analysis. */
2417
2418 void
2419 scev_reset (void)
2420 {
2421 unsigned i;
2422 struct loop *loop;
2423
2424 if (!scalar_evolution_info || !current_loops)
2425 return;
2426
2427 htab_empty (scalar_evolution_info);
2428 for (i = 1; i < current_loops->num; i++)
2429 {
2430 loop = current_loops->parray[i];
2431 if (loop)
2432 loop->nb_iterations = NULL_TREE;
2433 }
2434 }
2435
2436 /* Checks whether OP behaves as a simple affine iv of LOOP in STMT and returns
2437 its BASE and STEP if possible. */
2438
2439 bool
2440 simple_iv (struct loop *loop, tree stmt, tree op, tree *base, tree *step)
2441 {
2442 basic_block bb = bb_for_stmt (stmt);
2443 tree type, ev;
2444
2445 *base = NULL_TREE;
2446 *step = NULL_TREE;
2447
2448 type = TREE_TYPE (op);
2449 if (TREE_CODE (type) != INTEGER_TYPE
2450 && TREE_CODE (type) != POINTER_TYPE)
2451 return false;
2452
2453 ev = analyze_scalar_evolution_in_loop (loop, bb->loop_father, op);
2454 if (chrec_contains_undetermined (ev))
2455 return false;
2456
2457 if (tree_does_not_contain_chrecs (ev)
2458 && !chrec_contains_symbols_defined_in_loop (ev, loop->num))
2459 {
2460 *base = ev;
2461 return true;
2462 }
2463
2464 if (TREE_CODE (ev) != POLYNOMIAL_CHREC
2465 || CHREC_VARIABLE (ev) != (unsigned) loop->num)
2466 return false;
2467
2468 *step = CHREC_RIGHT (ev);
2469 if (TREE_CODE (*step) != INTEGER_CST)
2470 return false;
2471 *base = CHREC_LEFT (ev);
2472 if (tree_contains_chrecs (*base)
2473 || chrec_contains_symbols_defined_in_loop (*base, loop->num))
2474 return false;
2475
2476 return true;
2477 }
2478
2479 /* Runs the analysis of scalar evolutions. */
2480
2481 void
2482 scev_analysis (void)
2483 {
2484 varray_type exit_conditions;
2485
2486 VARRAY_GENERIC_PTR_INIT (exit_conditions, 37, "exit_conditions");
2487 select_loops_exit_conditions (current_loops, &exit_conditions);
2488
2489 if (dump_file && (dump_flags & TDF_STATS))
2490 analyze_scalar_evolution_for_all_loop_phi_nodes (exit_conditions);
2491
2492 number_of_iterations_for_all_loops (exit_conditions);
2493 VARRAY_CLEAR (exit_conditions);
2494 }
2495
2496 /* Finalize the scalar evolution analysis. */
2497
2498 void
2499 scev_finalize (void)
2500 {
2501 htab_delete (scalar_evolution_info);
2502 BITMAP_XFREE (already_instantiated);
2503 }
2504