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