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