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