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