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