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