]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-scalar-evolution.c
re PR tree-optimization/84178 (ICE in release_bb_predicate)
[thirdparty/gcc.git] / gcc / tree-scalar-evolution.c
CommitLineData
e9eb809d 1/* Scalar evolution detector.
85ec4feb 2 Copyright (C) 2003-2018 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;
f37fac2b 1734 poly_int64 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
f37fac2b 1773 if (maybe_ne (bitpos, 0))
bef28ced 1774 {
f37fac2b 1775 unitpos = size_int (exact_div (bitpos, BITS_PER_UNIT));
bef28ced
JL
1776 chrec3 = analyze_scalar_evolution (loop, unitpos);
1777 chrec3 = chrec_convert (TREE_TYPE (unitpos), chrec3, at_stmt);
0547c9b6 1778 chrec3 = instantiate_parameters (loop, chrec3);
bef28ced
JL
1779 res = chrec_fold_plus (type, res, chrec3);
1780 }
1781 }
1782 else
1783 res = chrec_dont_know;
1784 break;
6a02a719 1785
5be014d5 1786 case POINTER_PLUS_EXPR:
726a989a
RB
1787 chrec1 = analyze_scalar_evolution (loop, rhs1);
1788 chrec2 = analyze_scalar_evolution (loop, rhs2);
1789 chrec1 = chrec_convert (type, chrec1, at_stmt);
0d82a1c8 1790 chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt);
0547c9b6
RB
1791 chrec1 = instantiate_parameters (loop, chrec1);
1792 chrec2 = instantiate_parameters (loop, chrec2);
726a989a 1793 res = chrec_fold_plus (type, chrec1, chrec2);
5be014d5
AP
1794 break;
1795
9baba81b 1796 case PLUS_EXPR:
726a989a
RB
1797 chrec1 = analyze_scalar_evolution (loop, rhs1);
1798 chrec2 = analyze_scalar_evolution (loop, rhs2);
f802a424
RB
1799 ctype = type;
1800 /* When the stmt is conditionally executed re-write the CHREC
1801 into a form that has well-defined behavior on overflow. */
1802 if (at_stmt
1803 && INTEGRAL_TYPE_P (type)
1804 && ! TYPE_OVERFLOW_WRAPS (type)
1805 && ! dominated_by_p (CDI_DOMINATORS, loop->latch,
1806 gimple_bb (at_stmt)))
1807 ctype = unsigned_type_for (type);
1808 chrec1 = chrec_convert (ctype, chrec1, at_stmt);
1809 chrec2 = chrec_convert (ctype, chrec2, at_stmt);
0547c9b6
RB
1810 chrec1 = instantiate_parameters (loop, chrec1);
1811 chrec2 = instantiate_parameters (loop, chrec2);
f802a424
RB
1812 res = chrec_fold_plus (ctype, chrec1, chrec2);
1813 if (type != ctype)
1814 res = chrec_convert (type, res, at_stmt);
9baba81b 1815 break;
b8698a0f 1816
9baba81b 1817 case MINUS_EXPR:
726a989a
RB
1818 chrec1 = analyze_scalar_evolution (loop, rhs1);
1819 chrec2 = analyze_scalar_evolution (loop, rhs2);
f802a424
RB
1820 ctype = type;
1821 /* When the stmt is conditionally executed re-write the CHREC
1822 into a form that has well-defined behavior on overflow. */
1823 if (at_stmt
1824 && INTEGRAL_TYPE_P (type)
1825 && ! TYPE_OVERFLOW_WRAPS (type)
1826 && ! dominated_by_p (CDI_DOMINATORS,
1827 loop->latch, gimple_bb (at_stmt)))
1828 ctype = unsigned_type_for (type);
1829 chrec1 = chrec_convert (ctype, chrec1, at_stmt);
1830 chrec2 = chrec_convert (ctype, chrec2, at_stmt);
0547c9b6
RB
1831 chrec1 = instantiate_parameters (loop, chrec1);
1832 chrec2 = instantiate_parameters (loop, chrec2);
f802a424
RB
1833 res = chrec_fold_minus (ctype, chrec1, chrec2);
1834 if (type != ctype)
1835 res = chrec_convert (type, res, at_stmt);
9baba81b
SP
1836 break;
1837
1838 case NEGATE_EXPR:
726a989a 1839 chrec1 = analyze_scalar_evolution (loop, rhs1);
f802a424
RB
1840 ctype = type;
1841 /* When the stmt is conditionally executed re-write the CHREC
1842 into a form that has well-defined behavior on overflow. */
1843 if (at_stmt
1844 && INTEGRAL_TYPE_P (type)
1845 && ! TYPE_OVERFLOW_WRAPS (type)
1846 && ! dominated_by_p (CDI_DOMINATORS,
1847 loop->latch, gimple_bb (at_stmt)))
1848 ctype = unsigned_type_for (type);
1849 chrec1 = chrec_convert (ctype, chrec1, at_stmt);
9a75ede0 1850 /* TYPE may be integer, real or complex, so use fold_convert. */
0547c9b6 1851 chrec1 = instantiate_parameters (loop, chrec1);
f802a424
RB
1852 res = chrec_fold_multiply (ctype, chrec1,
1853 fold_convert (ctype, integer_minus_one_node));
1854 if (type != ctype)
1855 res = chrec_convert (type, res, at_stmt);
9baba81b
SP
1856 break;
1857
418df9d7
JJ
1858 case BIT_NOT_EXPR:
1859 /* Handle ~X as -1 - X. */
1860 chrec1 = analyze_scalar_evolution (loop, rhs1);
1861 chrec1 = chrec_convert (type, chrec1, at_stmt);
0547c9b6 1862 chrec1 = instantiate_parameters (loop, chrec1);
418df9d7
JJ
1863 res = chrec_fold_minus (type,
1864 fold_convert (type, integer_minus_one_node),
1865 chrec1);
1866 break;
1867
9baba81b 1868 case MULT_EXPR:
726a989a
RB
1869 chrec1 = analyze_scalar_evolution (loop, rhs1);
1870 chrec2 = analyze_scalar_evolution (loop, rhs2);
f802a424
RB
1871 ctype = type;
1872 /* When the stmt is conditionally executed re-write the CHREC
1873 into a form that has well-defined behavior on overflow. */
1874 if (at_stmt
1875 && INTEGRAL_TYPE_P (type)
1876 && ! TYPE_OVERFLOW_WRAPS (type)
1877 && ! dominated_by_p (CDI_DOMINATORS,
1878 loop->latch, gimple_bb (at_stmt)))
1879 ctype = unsigned_type_for (type);
1880 chrec1 = chrec_convert (ctype, chrec1, at_stmt);
1881 chrec2 = chrec_convert (ctype, chrec2, at_stmt);
0547c9b6
RB
1882 chrec1 = instantiate_parameters (loop, chrec1);
1883 chrec2 = instantiate_parameters (loop, chrec2);
f802a424
RB
1884 res = chrec_fold_multiply (ctype, chrec1, chrec2);
1885 if (type != ctype)
1886 res = chrec_convert (type, res, at_stmt);
0bca51f0 1887 break;
b8698a0f 1888
60f2d2f3
AL
1889 case LSHIFT_EXPR:
1890 {
1891 /* Handle A<<B as A * (1<<B). */
1892 tree uns = unsigned_type_for (type);
1893 chrec1 = analyze_scalar_evolution (loop, rhs1);
1894 chrec2 = analyze_scalar_evolution (loop, rhs2);
1895 chrec1 = chrec_convert (uns, chrec1, at_stmt);
1896 chrec1 = instantiate_parameters (loop, chrec1);
1897 chrec2 = instantiate_parameters (loop, chrec2);
1898
1899 tree one = build_int_cst (uns, 1);
1900 chrec2 = fold_build2 (LSHIFT_EXPR, uns, one, chrec2);
1901 res = chrec_fold_multiply (uns, chrec1, chrec2);
1902 res = chrec_convert (type, res, at_stmt);
1903 }
1904 break;
1905
1043771b 1906 CASE_CONVERT:
195b4c50
RG
1907 /* In case we have a truncation of a widened operation that in
1908 the truncated type has undefined overflow behavior analyze
1909 the operation done in an unsigned type of the same precision
1910 as the final truncation. We cannot derive a scalar evolution
1911 for the widened operation but for the truncated result. */
1912 if (TREE_CODE (type) == INTEGER_TYPE
1913 && TREE_CODE (TREE_TYPE (rhs1)) == INTEGER_TYPE
1914 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (rhs1))
1915 && TYPE_OVERFLOW_UNDEFINED (type)
1916 && TREE_CODE (rhs1) == SSA_NAME
1917 && (def = SSA_NAME_DEF_STMT (rhs1))
1918 && is_gimple_assign (def)
1919 && TREE_CODE_CLASS (gimple_assign_rhs_code (def)) == tcc_binary
1920 && TREE_CODE (gimple_assign_rhs2 (def)) == INTEGER_CST)
1921 {
1922 tree utype = unsigned_type_for (type);
1923 chrec1 = interpret_rhs_expr (loop, at_stmt, utype,
1924 gimple_assign_rhs1 (def),
1925 gimple_assign_rhs_code (def),
1926 gimple_assign_rhs2 (def));
1927 }
1928 else
1929 chrec1 = analyze_scalar_evolution (loop, rhs1);
b24d9420 1930 res = chrec_convert (type, chrec1, at_stmt, true, rhs1);
9baba81b 1931 break;
e6d62b46
BC
1932
1933 case BIT_AND_EXPR:
1934 /* Given int variable A, handle A&0xffff as (int)(unsigned short)A.
1935 If A is SCEV and its value is in the range of representable set
1936 of type unsigned short, the result expression is a (no-overflow)
1937 SCEV. */
1938 res = chrec_dont_know;
1939 if (tree_fits_uhwi_p (rhs2))
1940 {
1941 int precision;
1942 unsigned HOST_WIDE_INT val = tree_to_uhwi (rhs2);
1943
1944 val ++;
1945 /* Skip if value of rhs2 wraps in unsigned HOST_WIDE_INT or
1946 it's not the maximum value of a smaller type than rhs1. */
1947 if (val != 0
1948 && (precision = exact_log2 (val)) > 0
1949 && (unsigned) precision < TYPE_PRECISION (TREE_TYPE (rhs1)))
1950 {
1951 tree utype = build_nonstandard_integer_type (precision, 1);
1952
1953 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (rhs1)))
1954 {
1955 chrec1 = analyze_scalar_evolution (loop, rhs1);
1956 chrec1 = chrec_convert (utype, chrec1, at_stmt);
1957 res = chrec_convert (TREE_TYPE (rhs1), chrec1, at_stmt);
1958 }
1959 }
1960 }
1961 break;
b8698a0f 1962
9baba81b
SP
1963 default:
1964 res = chrec_dont_know;
1965 break;
1966 }
b8698a0f 1967
9baba81b
SP
1968 return res;
1969}
1970
726a989a
RB
1971/* Interpret the expression EXPR. */
1972
1973static tree
355fe088 1974interpret_expr (struct loop *loop, gimple *at_stmt, tree expr)
726a989a
RB
1975{
1976 enum tree_code code;
1977 tree type = TREE_TYPE (expr), op0, op1;
1978
1979 if (automatically_generated_chrec_p (expr))
1980 return expr;
1981
4e71066d
RG
1982 if (TREE_CODE (expr) == POLYNOMIAL_CHREC
1983 || get_gimple_rhs_class (TREE_CODE (expr)) == GIMPLE_TERNARY_RHS)
726a989a
RB
1984 return chrec_dont_know;
1985
1986 extract_ops_from_tree (expr, &code, &op0, &op1);
1987
1988 return interpret_rhs_expr (loop, at_stmt, type,
1989 op0, code, op1);
1990}
1991
1992/* Interpret the rhs of the assignment STMT. */
1993
1994static tree
355fe088 1995interpret_gimple_assign (struct loop *loop, gimple *stmt)
726a989a
RB
1996{
1997 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
1998 enum tree_code code = gimple_assign_rhs_code (stmt);
1999
2000 return interpret_rhs_expr (loop, stmt, type,
2001 gimple_assign_rhs1 (stmt), code,
2002 gimple_assign_rhs2 (stmt));
2003}
2004
9baba81b
SP
2005\f
2006
b8698a0f 2007/* This section contains all the entry points:
9baba81b
SP
2008 - number_of_iterations_in_loop,
2009 - analyze_scalar_evolution,
2010 - instantiate_parameters.
2011*/
2012
9baba81b
SP
2013/* Helper recursive function. */
2014
2015static tree
fb1fe1f3 2016analyze_scalar_evolution_1 (struct loop *loop, tree var)
9baba81b 2017{
355fe088 2018 gimple *def;
9baba81b
SP
2019 basic_block bb;
2020 struct loop *def_loop;
fb1fe1f3 2021 tree res;
9baba81b 2022
9baba81b 2023 if (TREE_CODE (var) != SSA_NAME)
726a989a 2024 return interpret_expr (loop, NULL, var);
9baba81b
SP
2025
2026 def = SSA_NAME_DEF_STMT (var);
726a989a 2027 bb = gimple_bb (def);
5355943c 2028 def_loop = bb->loop_father;
9baba81b 2029
5355943c 2030 if (!flow_bb_inside_loop_p (loop, bb))
9baba81b 2031 {
806f2c1b
AL
2032 /* Keep symbolic form, but look through obvious copies for constants. */
2033 res = follow_copies_to_constant (var);
9baba81b
SP
2034 goto set_and_end;
2035 }
2036
9baba81b
SP
2037 if (loop != def_loop)
2038 {
fb1fe1f3 2039 res = analyze_scalar_evolution_1 (def_loop, var);
5355943c
RB
2040 struct loop *loop_to_skip = superloop_at_depth (def_loop,
2041 loop_depth (loop) + 1);
2042 res = compute_overall_effect_of_inner_loop (loop_to_skip, res);
2043 if (chrec_contains_symbols_defined_in_loop (res, loop->num))
2044 res = analyze_scalar_evolution_1 (loop, res);
9baba81b
SP
2045 goto set_and_end;
2046 }
2047
726a989a 2048 switch (gimple_code (def))
9baba81b 2049 {
726a989a
RB
2050 case GIMPLE_ASSIGN:
2051 res = interpret_gimple_assign (loop, def);
9baba81b
SP
2052 break;
2053
726a989a 2054 case GIMPLE_PHI:
9baba81b 2055 if (loop_phi_node_p (def))
538dd0b7 2056 res = interpret_loop_phi (loop, as_a <gphi *> (def));
9baba81b 2057 else
538dd0b7 2058 res = interpret_condition_phi (loop, as_a <gphi *> (def));
9baba81b
SP
2059 break;
2060
2061 default:
2062 res = chrec_dont_know;
2063 break;
2064 }
2065
2066 set_and_end:
2067
2068 /* Keep the symbolic form. */
2069 if (res == chrec_dont_know)
2070 res = var;
2071
2072 if (loop == def_loop)
a213b219 2073 set_scalar_evolution (block_before_loop (loop), var, res);
9baba81b
SP
2074
2075 return res;
2076}
2077
52bdd655
SP
2078/* Analyzes and returns the scalar evolution of the ssa_name VAR in
2079 LOOP. LOOP is the loop in which the variable is used.
b8698a0f 2080
9baba81b
SP
2081 Example of use: having a pointer VAR to a SSA_NAME node, STMT a
2082 pointer to the statement that uses this variable, in order to
2083 determine the evolution function of the variable, use the following
2084 calls:
b8698a0f 2085
52bdd655
SP
2086 loop_p loop = loop_containing_stmt (stmt);
2087 tree chrec_with_symbols = analyze_scalar_evolution (loop, var);
3f227a8c 2088 tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols);
9baba81b
SP
2089*/
2090
b8698a0f 2091tree
9baba81b
SP
2092analyze_scalar_evolution (struct loop *loop, tree var)
2093{
2094 tree res;
2095
a68f286c
RB
2096 /* ??? Fix callers. */
2097 if (! loop)
2098 return var;
2099
dfedbe40 2100 if (dump_file && (dump_flags & TDF_SCEV))
9baba81b
SP
2101 {
2102 fprintf (dump_file, "(analyze_scalar_evolution \n");
2103 fprintf (dump_file, " (loop_nb = %d)\n", loop->num);
2104 fprintf (dump_file, " (scalar = ");
ef6cb4c7 2105 print_generic_expr (dump_file, var);
9baba81b
SP
2106 fprintf (dump_file, ")\n");
2107 }
2108
a213b219 2109 res = get_scalar_evolution (block_before_loop (loop), var);
fb1fe1f3
RB
2110 if (res == chrec_not_analyzed_yet)
2111 res = analyze_scalar_evolution_1 (loop, var);
9baba81b 2112
dfedbe40 2113 if (dump_file && (dump_flags & TDF_SCEV))
9baba81b
SP
2114 fprintf (dump_file, ")\n");
2115
2116 return res;
2117}
2118
bef28ced
JL
2119/* Analyzes and returns the scalar evolution of VAR address in LOOP. */
2120
2121static tree
2122analyze_scalar_evolution_for_address_of (struct loop *loop, tree var)
2123{
2124 return analyze_scalar_evolution (loop, build_fold_addr_expr (var));
2125}
2126
9baba81b 2127/* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
f017bf5e 2128 WRTO_LOOP (which should be a superloop of USE_LOOP)
a6f778b2
ZD
2129
2130 FOLDED_CASTS is set to true if resolve_mixers used
2131 chrec_convert_aggressive (TODO -- not really, we are way too conservative
b8698a0f
L
2132 at the moment in order to keep things simple).
2133
f017bf5e
ZD
2134 To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following
2135 example:
2136
2137 for (i = 0; i < 100; i++) -- loop 1
2138 {
2139 for (j = 0; j < 100; j++) -- loop 2
2140 {
2141 k1 = i;
2142 k2 = j;
2143
2144 use2 (k1, k2);
2145
2146 for (t = 0; t < 100; t++) -- loop 3
2147 use3 (k1, k2);
2148
2149 }
2150 use1 (k1, k2);
2151 }
2152
2153 Both k1 and k2 are invariants in loop3, thus
2154 analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1
2155 analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2
2156
2157 As they are invariant, it does not matter whether we consider their
2158 usage in loop 3 or loop 2, hence
2159 analyze_scalar_evolution_in_loop (loop2, loop3, k1) =
2160 analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i
2161 analyze_scalar_evolution_in_loop (loop2, loop3, k2) =
2162 analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2
2163
2164 Similarly for their evolutions with respect to loop 1. The values of K2
2165 in the use in loop 2 vary independently on loop 1, thus we cannot express
2166 the evolution with respect to loop 1:
2167 analyze_scalar_evolution_in_loop (loop1, loop3, k1) =
2168 analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1
2169 analyze_scalar_evolution_in_loop (loop1, loop3, k2) =
2170 analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know
2171
2172 The value of k2 in the use in loop 1 is known, though:
2173 analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1
2174 analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100
2175 */
9baba81b
SP
2176
2177static tree
2178analyze_scalar_evolution_in_loop (struct loop *wrto_loop, struct loop *use_loop,
a6f778b2 2179 tree version, bool *folded_casts)
9baba81b
SP
2180{
2181 bool val = false;
a6f778b2 2182 tree ev = version, tmp;
9baba81b 2183
b8698a0f 2184 /* We cannot just do
f017bf5e
ZD
2185
2186 tmp = analyze_scalar_evolution (use_loop, version);
c70ed622 2187 ev = resolve_mixers (wrto_loop, tmp, folded_casts);
f017bf5e
ZD
2188
2189 as resolve_mixers would query the scalar evolution with respect to
2190 wrto_loop. For example, in the situation described in the function
2191 comment, suppose that wrto_loop = loop1, use_loop = loop3 and
2192 version = k2. Then
2193
2194 analyze_scalar_evolution (use_loop, version) = k2
2195
c70ed622
BC
2196 and resolve_mixers (loop1, k2, folded_casts) finds that the value of
2197 k2 in loop 1 is 100, which is a wrong result, since we are interested
2198 in the value in loop 3.
f017bf5e
ZD
2199
2200 Instead, we need to proceed from use_loop to wrto_loop loop by loop,
2201 each time checking that there is no evolution in the inner loop. */
2202
a6f778b2
ZD
2203 if (folded_casts)
2204 *folded_casts = false;
9baba81b
SP
2205 while (1)
2206 {
a6f778b2 2207 tmp = analyze_scalar_evolution (use_loop, ev);
c70ed622 2208 ev = resolve_mixers (use_loop, tmp, folded_casts);
9baba81b
SP
2209
2210 if (use_loop == wrto_loop)
2211 return ev;
2212
2213 /* If the value of the use changes in the inner loop, we cannot express
2214 its value in the outer loop (we might try to return interval chrec,
2215 but we do not have a user for it anyway) */
2216 if (!no_evolution_in_loop_p (ev, use_loop->num, &val)
2217 || !val)
2218 return chrec_dont_know;
2219
9ba025a2 2220 use_loop = loop_outer (use_loop);
9baba81b
SP
2221 }
2222}
2223
eb0bc7af 2224
fdd43ac4
RB
2225/* Hashtable helpers for a temporary hash-table used when
2226 instantiating a CHREC or resolving mixers. For this use
2227 instantiated_below is always the same. */
2228
fdd43ac4 2229struct instantiate_cache_type
eb0bc7af 2230{
a3cc13cc
RB
2231 htab_t map;
2232 vec<scev_info_str> entries;
b8698a0f 2233
c3284718 2234 instantiate_cache_type () : map (NULL), entries (vNULL) {}
fdd43ac4 2235 ~instantiate_cache_type ();
0547c9b6
RB
2236 tree get (unsigned slot) { return entries[slot].chrec; }
2237 void set (unsigned slot, tree chrec) { entries[slot].chrec = chrec; }
fdd43ac4 2238};
eb0bc7af 2239
fdd43ac4
RB
2240instantiate_cache_type::~instantiate_cache_type ()
2241{
0547c9b6 2242 if (map != NULL)
fdd43ac4 2243 {
a3cc13cc 2244 htab_delete (map);
fdd43ac4
RB
2245 entries.release ();
2246 }
eb0bc7af
ZD
2247}
2248
a3cc13cc
RB
2249/* Cache to avoid infinite recursion when instantiating an SSA name.
2250 Live during the outermost instantiate_scev or resolve_mixers call. */
2251static instantiate_cache_type *global_cache;
2252
2253/* Computes a hash function for database element ELT. */
2254
2255static inline hashval_t
2256hash_idx_scev_info (const void *elt_)
2257{
2258 unsigned idx = ((size_t) elt_) - 2;
907dadbd 2259 return scev_info_hasher::hash (&global_cache->entries[idx]);
a3cc13cc
RB
2260}
2261
2262/* Compares database elements E1 and E2. */
2263
2264static inline int
2265eq_idx_scev_info (const void *e1, const void *e2)
2266{
2267 unsigned idx1 = ((size_t) e1) - 2;
907dadbd
TS
2268 return scev_info_hasher::equal (&global_cache->entries[idx1],
2269 (const scev_info_str *) e2);
a3cc13cc
RB
2270}
2271
0547c9b6 2272/* Returns from CACHE the slot number of the cached chrec for NAME. */
fdd43ac4 2273
0547c9b6 2274static unsigned
a3cc13cc 2275get_instantiated_value_entry (instantiate_cache_type &cache,
a68f286c 2276 tree name, edge instantiate_below)
fdd43ac4 2277{
0547c9b6 2278 if (!cache.map)
fdd43ac4 2279 {
a3cc13cc 2280 cache.map = htab_create (10, hash_idx_scev_info, eq_idx_scev_info, NULL);
fdd43ac4
RB
2281 cache.entries.create (10);
2282 }
b8698a0f 2283
a3cc13cc
RB
2284 scev_info_str e;
2285 e.name_version = SSA_NAME_VERSION (name);
a68f286c 2286 e.instantiated_below = instantiate_below->dest->index;
a3cc13cc 2287 void **slot = htab_find_slot_with_hash (cache.map, &e,
907dadbd 2288 scev_info_hasher::hash (&e), INSERT);
a3cc13cc 2289 if (!*slot)
fdd43ac4
RB
2290 {
2291 e.chrec = chrec_not_analyzed_yet;
a3cc13cc 2292 *slot = (void *)(size_t)(cache.entries.length () + 2);
fdd43ac4 2293 cache.entries.safe_push (e);
fdd43ac4
RB
2294 }
2295
a3cc13cc 2296 return ((size_t)*slot) - 2;
eb0bc7af
ZD
2297}
2298
0547c9b6 2299
18aed06a
SP
2300/* Return the closed_loop_phi node for VAR. If there is none, return
2301 NULL_TREE. */
2302
2303static tree
2304loop_closed_phi_def (tree var)
2305{
2306 struct loop *loop;
2307 edge exit;
538dd0b7
DM
2308 gphi *phi;
2309 gphi_iterator psi;
18aed06a
SP
2310
2311 if (var == NULL_TREE
2312 || TREE_CODE (var) != SSA_NAME)
2313 return NULL_TREE;
2314
2315 loop = loop_containing_stmt (SSA_NAME_DEF_STMT (var));
ac8f6c69 2316 exit = single_exit (loop);
18aed06a
SP
2317 if (!exit)
2318 return NULL_TREE;
2319
726a989a
RB
2320 for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi))
2321 {
538dd0b7 2322 phi = psi.phi ();
726a989a
RB
2323 if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == var)
2324 return PHI_RESULT (phi);
2325 }
18aed06a
SP
2326
2327 return NULL_TREE;
2328}
2329
a68f286c 2330static tree instantiate_scev_r (edge, struct loop *, struct loop *,
c70ed622 2331 tree, bool *, int);
320f5a78
SP
2332
2333/* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2334 and EVOLUTION_LOOP, that were left under a symbolic form.
2335
2495a183 2336 CHREC is an SSA_NAME to be instantiated.
320f5a78
SP
2337
2338 CACHE is the cache of already instantiated values.
2339
c70ed622
BC
2340 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2341 conversions that may wrap in signed/pointer type are folded, as long
2342 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2343 then we don't do such fold.
320f5a78
SP
2344
2345 SIZE_EXPR is used for computing the size of the expression to be
2346 instantiated, and to stop if it exceeds some limit. */
2347
2348static tree
a68f286c 2349instantiate_scev_name (edge instantiate_below,
8b679c9b
RB
2350 struct loop *evolution_loop, struct loop *inner_loop,
2351 tree chrec,
c70ed622 2352 bool *fold_conversions,
4a8fb1a1 2353 int size_expr)
320f5a78 2354{
2495a183
SP
2355 tree res;
2356 struct loop *def_loop;
2357 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (chrec));
20179b0d 2358
92900aec 2359 /* A parameter, nothing to do. */
2495a183 2360 if (!def_bb
92900aec 2361 || !dominated_by_p (CDI_DOMINATORS, def_bb, instantiate_below->dest))
2495a183 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
a68f286c
RB
2383 if (! dominated_by_p (CDI_DOMINATORS,
2384 def_loop->header, instantiate_below->dest))
2385 {
2386 gimple *def = SSA_NAME_DEF_STMT (chrec);
2387 if (gassign *ass = dyn_cast <gassign *> (def))
2388 {
2389 switch (gimple_assign_rhs_class (ass))
2390 {
2391 case GIMPLE_UNARY_RHS:
2392 {
2393 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2394 inner_loop, gimple_assign_rhs1 (ass),
2395 fold_conversions, size_expr);
2396 if (op0 == chrec_dont_know)
2397 return chrec_dont_know;
2398 res = fold_build1 (gimple_assign_rhs_code (ass),
2399 TREE_TYPE (chrec), op0);
2400 break;
2401 }
2402 case GIMPLE_BINARY_RHS:
2403 {
2404 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2405 inner_loop, gimple_assign_rhs1 (ass),
2406 fold_conversions, size_expr);
2407 if (op0 == chrec_dont_know)
2408 return chrec_dont_know;
2409 tree op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2410 inner_loop, gimple_assign_rhs2 (ass),
2411 fold_conversions, size_expr);
2412 if (op1 == chrec_dont_know)
2413 return chrec_dont_know;
2414 res = fold_build2 (gimple_assign_rhs_code (ass),
2415 TREE_TYPE (chrec), op0, op1);
2416 break;
2417 }
2418 default:
2419 res = chrec_dont_know;
2420 }
2421 }
2422 else
2423 res = chrec_dont_know;
2424 global_cache->set (si, res);
2425 return res;
2426 }
2427
320f5a78
SP
2428 /* If the analysis yields a parametric chrec, instantiate the
2429 result again. */
2430 res = analyze_scalar_evolution (def_loop, chrec);
2431
2847388e 2432 /* Don't instantiate default definitions. */
320f5a78 2433 if (TREE_CODE (res) == SSA_NAME
2847388e
SP
2434 && SSA_NAME_IS_DEFAULT_DEF (res))
2435 ;
2436
2437 /* Don't instantiate loop-closed-ssa phi nodes. */
2438 else if (TREE_CODE (res) == SSA_NAME
2439 && loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res)))
2440 > loop_depth (def_loop))
320f5a78
SP
2441 {
2442 if (res == chrec)
2443 res = loop_closed_phi_def (chrec);
2444 else
2445 res = chrec;
2446
7472eb13
SP
2447 /* When there is no loop_closed_phi_def, it means that the
2448 variable is not used after the loop: try to still compute the
2449 value of the variable when exiting the loop. */
2450 if (res == NULL_TREE)
2451 {
2452 loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (chrec));
2453 res = analyze_scalar_evolution (loop, chrec);
2454 res = compute_overall_effect_of_inner_loop (loop, res);
8b679c9b
RB
2455 res = instantiate_scev_r (instantiate_below, evolution_loop,
2456 inner_loop, res,
0547c9b6 2457 fold_conversions, size_expr);
7472eb13 2458 }
a68f286c
RB
2459 else if (dominated_by_p (CDI_DOMINATORS,
2460 gimple_bb (SSA_NAME_DEF_STMT (res)),
2461 instantiate_below->dest))
320f5a78
SP
2462 res = chrec_dont_know;
2463 }
2464
2465 else if (res != chrec_dont_know)
8b679c9b
RB
2466 {
2467 if (inner_loop
63fdb7be 2468 && def_bb->loop_father != inner_loop
8b679c9b
RB
2469 && !flow_loop_nested_p (def_bb->loop_father, inner_loop))
2470 /* ??? We could try to compute the overall effect of the loop here. */
2471 res = chrec_dont_know;
2472 else
2473 res = instantiate_scev_r (instantiate_below, evolution_loop,
2474 inner_loop, res,
0547c9b6 2475 fold_conversions, size_expr);
8b679c9b 2476 }
320f5a78
SP
2477
2478 /* Store the correct value to the cache. */
0547c9b6 2479 global_cache->set (si, res);
320f5a78 2480 return res;
320f5a78
SP
2481}
2482
ec6636eb
SP
2483/* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2484 and EVOLUTION_LOOP, that were left under a symbolic form.
2485
2486 CHREC is a polynomial chain of recurrence to be instantiated.
2487
2488 CACHE is the cache of already instantiated values.
2489
c70ed622
BC
2490 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2491 conversions that may wrap in signed/pointer type are folded, as long
2492 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2493 then we don't do such fold.
ec6636eb
SP
2494
2495 SIZE_EXPR is used for computing the size of the expression to be
2496 instantiated, and to stop if it exceeds some limit. */
2497
2498static tree
a68f286c 2499instantiate_scev_poly (edge instantiate_below,
8b679c9b 2500 struct loop *evolution_loop, struct loop *,
c70ed622 2501 tree chrec, bool *fold_conversions, int size_expr)
ec6636eb
SP
2502{
2503 tree op1;
9e5dc77f 2504 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
8b679c9b 2505 get_chrec_loop (chrec),
0547c9b6 2506 CHREC_LEFT (chrec), fold_conversions,
ec6636eb
SP
2507 size_expr);
2508 if (op0 == chrec_dont_know)
2509 return chrec_dont_know;
2510
9e5dc77f 2511 op1 = instantiate_scev_r (instantiate_below, evolution_loop,
8b679c9b 2512 get_chrec_loop (chrec),
0547c9b6 2513 CHREC_RIGHT (chrec), fold_conversions,
ec6636eb
SP
2514 size_expr);
2515 if (op1 == chrec_dont_know)
2516 return chrec_dont_know;
2517
2518 if (CHREC_LEFT (chrec) != op0
2519 || CHREC_RIGHT (chrec) != op1)
2520 {
2521 op1 = chrec_convert_rhs (chrec_type (op0), op1, NULL);
8b679c9b 2522 chrec = build_polynomial_chrec (CHREC_VARIABLE (chrec), op0, op1);
ec6636eb 2523 }
4bf4e169 2524
ec6636eb
SP
2525 return chrec;
2526}
2527
15fda317
SP
2528/* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2529 and EVOLUTION_LOOP, that were left under a symbolic form.
2530
ffa34f4b 2531 "C0 CODE C1" is a binary expression of type TYPE to be instantiated.
15fda317
SP
2532
2533 CACHE is the cache of already instantiated values.
2534
c70ed622
BC
2535 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2536 conversions that may wrap in signed/pointer type are folded, as long
2537 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2538 then we don't do such fold.
15fda317
SP
2539
2540 SIZE_EXPR is used for computing the size of the expression to be
2541 instantiated, and to stop if it exceeds some limit. */
2542
2543static tree
a68f286c 2544instantiate_scev_binary (edge instantiate_below,
8b679c9b
RB
2545 struct loop *evolution_loop, struct loop *inner_loop,
2546 tree chrec, enum tree_code code,
ffa34f4b 2547 tree type, tree c0, tree c1,
c70ed622 2548 bool *fold_conversions, int size_expr)
15fda317
SP
2549{
2550 tree op1;
8b679c9b 2551 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
0547c9b6 2552 c0, fold_conversions, size_expr);
15fda317
SP
2553 if (op0 == chrec_dont_know)
2554 return chrec_dont_know;
2555
8b679c9b 2556 op1 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
0547c9b6 2557 c1, fold_conversions, size_expr);
15fda317
SP
2558 if (op1 == chrec_dont_know)
2559 return chrec_dont_know;
2560
ffa34f4b
SP
2561 if (c0 != op0
2562 || c1 != op1)
15fda317 2563 {
15fda317
SP
2564 op0 = chrec_convert (type, op0, NULL);
2565 op1 = chrec_convert_rhs (type, op1, NULL);
2566
ffa34f4b 2567 switch (code)
15fda317
SP
2568 {
2569 case POINTER_PLUS_EXPR:
2570 case PLUS_EXPR:
2571 return chrec_fold_plus (type, op0, op1);
2572
2573 case MINUS_EXPR:
2574 return chrec_fold_minus (type, op0, op1);
2575
2576 case MULT_EXPR:
2577 return chrec_fold_multiply (type, op0, op1);
2578
2579 default:
2580 gcc_unreachable ();
2581 }
2582 }
2583
ffa34f4b 2584 return chrec ? chrec : fold_build2 (code, type, c0, c1);
15fda317
SP
2585}
2586
a213b219 2587/* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
9c382ce9
SP
2588 and EVOLUTION_LOOP, that were left under a symbolic form.
2589
2590 "CHREC" that stands for a convert expression "(TYPE) OP" is to be
2591 instantiated.
2592
2593 CACHE is the cache of already instantiated values.
2594
c70ed622
BC
2595 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2596 conversions that may wrap in signed/pointer type are folded, as long
2597 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2598 then we don't do such fold.
9c382ce9
SP
2599
2600 SIZE_EXPR is used for computing the size of the expression to be
2601 instantiated, and to stop if it exceeds some limit. */
2602
2603static tree
a68f286c 2604instantiate_scev_convert (edge instantiate_below,
8b679c9b 2605 struct loop *evolution_loop, struct loop *inner_loop,
0547c9b6 2606 tree chrec, tree type, tree op,
c70ed622 2607 bool *fold_conversions, int size_expr)
9c382ce9 2608{
8b679c9b
RB
2609 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2610 inner_loop, op,
0547c9b6 2611 fold_conversions, size_expr);
9c382ce9
SP
2612
2613 if (op0 == chrec_dont_know)
2614 return chrec_dont_know;
2615
2616 if (fold_conversions)
2617 {
c70ed622 2618 tree tmp = chrec_convert_aggressive (type, op0, fold_conversions);
9c382ce9
SP
2619 if (tmp)
2620 return tmp;
9c382ce9 2621
c70ed622
BC
2622 /* If we used chrec_convert_aggressive, we can no longer assume that
2623 signed chrecs do not overflow, as chrec_convert does, so avoid
2624 calling it in that case. */
2625 if (*fold_conversions)
2626 {
2627 if (chrec && op0 == op)
2628 return chrec;
9c382ce9 2629
c70ed622
BC
2630 return fold_convert (type, op0);
2631 }
2632 }
9c382ce9
SP
2633
2634 return chrec_convert (type, op0, NULL);
2635}
2636
7ec0665d
SP
2637/* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2638 and EVOLUTION_LOOP, that were left under a symbolic form.
2639
4b9d48a1 2640 CHREC is a BIT_NOT_EXPR or a NEGATE_EXPR expression to be instantiated.
7ec0665d 2641 Handle ~X as -1 - X.
4b9d48a1 2642 Handle -X as -1 * X.
7ec0665d
SP
2643
2644 CACHE is the cache of already instantiated values.
2645
c70ed622
BC
2646 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2647 conversions that may wrap in signed/pointer type are folded, as long
2648 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2649 then we don't do such fold.
7ec0665d
SP
2650
2651 SIZE_EXPR is used for computing the size of the expression to be
2652 instantiated, and to stop if it exceeds some limit. */
2653
2654static tree
a68f286c 2655instantiate_scev_not (edge instantiate_below,
8b679c9b
RB
2656 struct loop *evolution_loop, struct loop *inner_loop,
2657 tree chrec,
20179b0d 2658 enum tree_code code, tree type, tree op,
c70ed622 2659 bool *fold_conversions, int size_expr)
7ec0665d 2660{
8b679c9b
RB
2661 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2662 inner_loop, op,
0547c9b6 2663 fold_conversions, size_expr);
20179b0d 2664
7ec0665d
SP
2665 if (op0 == chrec_dont_know)
2666 return chrec_dont_know;
2667
20179b0d 2668 if (op != op0)
7ec0665d
SP
2669 {
2670 op0 = chrec_convert (type, op0, NULL);
4b9d48a1 2671
20179b0d 2672 switch (code)
4b9d48a1
SP
2673 {
2674 case BIT_NOT_EXPR:
2675 return chrec_fold_minus
2676 (type, fold_convert (type, integer_minus_one_node), op0);
2677
2678 case NEGATE_EXPR:
2679 return chrec_fold_multiply
2680 (type, fold_convert (type, integer_minus_one_node), op0);
2681
2682 default:
2683 gcc_unreachable ();
2684 }
7ec0665d 2685 }
4b9d48a1 2686
20179b0d 2687 return chrec ? chrec : fold_build1 (code, type, op0);
7ec0665d
SP
2688}
2689
9e5dc77f
SP
2690/* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2691 and EVOLUTION_LOOP, that were left under a symbolic form.
2692
2693 CHREC is the scalar evolution to instantiate.
2694
2695 CACHE is the cache of already instantiated values.
2282a0e6 2696
c70ed622
BC
2697 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2698 conversions that may wrap in signed/pointer type are folded, as long
2699 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2700 then we don't do such fold.
9e5dc77f
SP
2701
2702 SIZE_EXPR is used for computing the size of the expression to be
2703 instantiated, and to stop if it exceeds some limit. */
2704
2705static tree
a68f286c 2706instantiate_scev_r (edge instantiate_below,
8b679c9b
RB
2707 struct loop *evolution_loop, struct loop *inner_loop,
2708 tree chrec,
c70ed622 2709 bool *fold_conversions, int size_expr)
9e5dc77f 2710{
47ae9e4c
SP
2711 /* Give up if the expression is larger than the MAX that we allow. */
2712 if (size_expr++ > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE))
2713 return chrec_dont_know;
2714
81fada9a
JJ
2715 if (chrec == NULL_TREE
2716 || automatically_generated_chrec_p (chrec)
d7770457 2717 || is_gimple_min_invariant (chrec))
9baba81b
SP
2718 return chrec;
2719
2720 switch (TREE_CODE (chrec))
2721 {
2722 case SSA_NAME:
8b679c9b
RB
2723 return instantiate_scev_name (instantiate_below, evolution_loop,
2724 inner_loop, chrec,
0547c9b6 2725 fold_conversions, size_expr);
9baba81b
SP
2726
2727 case POLYNOMIAL_CHREC:
8b679c9b
RB
2728 return instantiate_scev_poly (instantiate_below, evolution_loop,
2729 inner_loop, chrec,
0547c9b6 2730 fold_conversions, size_expr);
9baba81b 2731
5be014d5 2732 case POINTER_PLUS_EXPR:
9baba81b 2733 case PLUS_EXPR:
9baba81b 2734 case MINUS_EXPR:
9baba81b 2735 case MULT_EXPR:
8b679c9b
RB
2736 return instantiate_scev_binary (instantiate_below, evolution_loop,
2737 inner_loop, chrec,
ffa34f4b
SP
2738 TREE_CODE (chrec), chrec_type (chrec),
2739 TREE_OPERAND (chrec, 0),
2740 TREE_OPERAND (chrec, 1),
0547c9b6 2741 fold_conversions, size_expr);
9baba81b 2742
1043771b 2743 CASE_CONVERT:
8b679c9b
RB
2744 return instantiate_scev_convert (instantiate_below, evolution_loop,
2745 inner_loop, chrec,
9c382ce9 2746 TREE_TYPE (chrec), TREE_OPERAND (chrec, 0),
0547c9b6 2747 fold_conversions, size_expr);
9baba81b 2748
4b9d48a1 2749 case NEGATE_EXPR:
418df9d7 2750 case BIT_NOT_EXPR:
8b679c9b
RB
2751 return instantiate_scev_not (instantiate_below, evolution_loop,
2752 inner_loop, chrec,
20179b0d
SP
2753 TREE_CODE (chrec), TREE_TYPE (chrec),
2754 TREE_OPERAND (chrec, 0),
0547c9b6 2755 fold_conversions, size_expr);
418df9d7 2756
4c7d6755 2757 case ADDR_EXPR:
a68f286c
RB
2758 if (is_gimple_min_invariant (chrec))
2759 return chrec;
2760 /* Fallthru. */
9baba81b
SP
2761 case SCEV_NOT_KNOWN:
2762 return chrec_dont_know;
2763
2764 case SCEV_KNOWN:
2765 return chrec_known;
15fda317 2766
9baba81b 2767 default:
a68f286c
RB
2768 if (CONSTANT_CLASS_P (chrec))
2769 return chrec;
2770 return chrec_dont_know;
9baba81b 2771 }
9baba81b 2772}
e9eb809d
ZD
2773
2774/* Analyze all the parameters of the chrec that were left under a
a213b219
SP
2775 symbolic form. INSTANTIATE_BELOW is the basic block that stops the
2776 recursive instantiation of parameters: a parameter is a variable
2777 that is defined in a basic block that dominates INSTANTIATE_BELOW or
2778 a function parameter. */
e9eb809d
ZD
2779
2780tree
a68f286c 2781instantiate_scev (edge instantiate_below, struct loop *evolution_loop,
3f227a8c 2782 tree chrec)
e9eb809d 2783{
9baba81b
SP
2784 tree res;
2785
dfedbe40 2786 if (dump_file && (dump_flags & TDF_SCEV))
9baba81b 2787 {
3f227a8c 2788 fprintf (dump_file, "(instantiate_scev \n");
a68f286c
RB
2789 fprintf (dump_file, " (instantiate_below = %d -> %d)\n",
2790 instantiate_below->src->index, instantiate_below->dest->index);
2791 if (evolution_loop)
2792 fprintf (dump_file, " (evolution_loop = %d)\n", evolution_loop->num);
9baba81b 2793 fprintf (dump_file, " (chrec = ");
ef6cb4c7 2794 print_generic_expr (dump_file, chrec);
9baba81b
SP
2795 fprintf (dump_file, ")\n");
2796 }
b8698a0f 2797
0547c9b6
RB
2798 bool destr = false;
2799 if (!global_cache)
2800 {
2801 global_cache = new instantiate_cache_type;
2802 destr = true;
2803 }
2804
8b679c9b 2805 res = instantiate_scev_r (instantiate_below, evolution_loop,
c70ed622 2806 NULL, chrec, NULL, 0);
0547c9b6
RB
2807
2808 if (destr)
2809 {
2810 delete global_cache;
2811 global_cache = NULL;
2812 }
9baba81b 2813
dfedbe40 2814 if (dump_file && (dump_flags & TDF_SCEV))
9baba81b
SP
2815 {
2816 fprintf (dump_file, " (res = ");
ef6cb4c7 2817 print_generic_expr (dump_file, res);
9baba81b
SP
2818 fprintf (dump_file, "))\n");
2819 }
eb0bc7af 2820
9baba81b
SP
2821 return res;
2822}
2823
2824/* Similar to instantiate_parameters, but does not introduce the
2282a0e6
ZD
2825 evolutions in outer loops for LOOP invariants in CHREC, and does not
2826 care about causing overflows, as long as they do not affect value
2827 of an expression. */
9baba81b 2828
3cb960c7 2829tree
c70ed622 2830resolve_mixers (struct loop *loop, tree chrec, bool *folded_casts)
9baba81b 2831{
0547c9b6 2832 bool destr = false;
c70ed622 2833 bool fold_conversions = false;
0547c9b6
RB
2834 if (!global_cache)
2835 {
2836 global_cache = new instantiate_cache_type;
2837 destr = true;
2838 }
2839
a68f286c 2840 tree ret = instantiate_scev_r (loop_preheader_edge (loop), loop, NULL,
c70ed622
BC
2841 chrec, &fold_conversions, 0);
2842
2843 if (folded_casts && !*folded_casts)
2844 *folded_casts = fold_conversions;
0547c9b6
RB
2845
2846 if (destr)
2847 {
2848 delete global_cache;
2849 global_cache = NULL;
2850 }
2851
eb0bc7af 2852 return ret;
9baba81b
SP
2853}
2854
b8698a0f 2855/* Entry point for the analysis of the number of iterations pass.
9baba81b
SP
2856 This function tries to safely approximate the number of iterations
2857 the loop will run. When this property is not decidable at compile
0a74c758
SP
2858 time, the result is chrec_dont_know. Otherwise the result is a
2859 scalar or a symbolic parameter. When the number of iterations may
2860 be equal to zero and the property cannot be determined at compile
2861 time, the result is a COND_EXPR that represents in a symbolic form
2862 the conditions under which the number of iterations is not zero.
b8698a0f 2863
9baba81b 2864 Example of analysis: suppose that the loop has an exit condition:
b8698a0f 2865
9baba81b 2866 "if (b > 49) goto end_loop;"
b8698a0f 2867
9baba81b
SP
2868 and that in a previous analysis we have determined that the
2869 variable 'b' has an evolution function:
b8698a0f
L
2870
2871 "EF = {23, +, 5}_2".
2872
9baba81b
SP
2873 When we evaluate the function at the point 5, i.e. the value of the
2874 variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
2875 and EF (6) = 53. In this case the value of 'b' on exit is '53' and
2876 the loop body has been executed 6 times. */
2877
b8698a0f 2878tree
a14865db 2879number_of_latch_executions (struct loop *loop)
9baba81b 2880{
9baba81b
SP
2881 edge exit;
2882 struct tree_niter_desc niter_desc;
0a74c758
SP
2883 tree may_be_zero;
2884 tree res;
9baba81b 2885
0a74c758 2886 /* Determine whether the number of iterations in loop has already
9baba81b
SP
2887 been computed. */
2888 res = loop->nb_iterations;
2889 if (res)
2890 return res;
0a74c758
SP
2891
2892 may_be_zero = NULL_TREE;
9baba81b 2893
dfedbe40 2894 if (dump_file && (dump_flags & TDF_SCEV))
0a74c758 2895 fprintf (dump_file, "(number_of_iterations_in_loop = \n");
b8698a0f 2896
0a74c758 2897 res = chrec_dont_know;
ac8f6c69 2898 exit = single_exit (loop);
9baba81b 2899
0a74c758
SP
2900 if (exit && number_of_iterations_exit (loop, exit, &niter_desc, false))
2901 {
2902 may_be_zero = niter_desc.may_be_zero;
2903 res = niter_desc.niter;
2904 }
2905
2906 if (res == chrec_dont_know
2907 || !may_be_zero
2908 || integer_zerop (may_be_zero))
2909 ;
2910 else if (integer_nonzerop (may_be_zero))
2911 res = build_int_cst (TREE_TYPE (res), 0);
9baba81b 2912
0a74c758
SP
2913 else if (COMPARISON_CLASS_P (may_be_zero))
2914 res = fold_build3 (COND_EXPR, TREE_TYPE (res), may_be_zero,
2915 build_int_cst (TREE_TYPE (res), 0), res);
9baba81b
SP
2916 else
2917 res = chrec_dont_know;
2918
dfedbe40 2919 if (dump_file && (dump_flags & TDF_SCEV))
0a74c758
SP
2920 {
2921 fprintf (dump_file, " (set_nb_iterations_in_loop = ");
ef6cb4c7 2922 print_generic_expr (dump_file, res);
0a74c758
SP
2923 fprintf (dump_file, "))\n");
2924 }
2925
2926 loop->nb_iterations = res;
2927 return res;
9baba81b 2928}
9baba81b
SP
2929\f
2930
2931/* Counters for the stats. */
2932
b8698a0f 2933struct chrec_stats
9baba81b
SP
2934{
2935 unsigned nb_chrecs;
2936 unsigned nb_affine;
2937 unsigned nb_affine_multivar;
2938 unsigned nb_higher_poly;
2939 unsigned nb_chrec_dont_know;
2940 unsigned nb_undetermined;
2941};
2942
2943/* Reset the counters. */
2944
2945static inline void
2946reset_chrecs_counters (struct chrec_stats *stats)
2947{
2948 stats->nb_chrecs = 0;
2949 stats->nb_affine = 0;
2950 stats->nb_affine_multivar = 0;
2951 stats->nb_higher_poly = 0;
2952 stats->nb_chrec_dont_know = 0;
2953 stats->nb_undetermined = 0;
2954}
2955
2956/* Dump the contents of a CHREC_STATS structure. */
2957
2958static void
2959dump_chrecs_stats (FILE *file, struct chrec_stats *stats)
2960{
2961 fprintf (file, "\n(\n");
2962 fprintf (file, "-----------------------------------------\n");
2963 fprintf (file, "%d\taffine univariate chrecs\n", stats->nb_affine);
2964 fprintf (file, "%d\taffine multivariate chrecs\n", stats->nb_affine_multivar);
b8698a0f 2965 fprintf (file, "%d\tdegree greater than 2 polynomials\n",
9baba81b
SP
2966 stats->nb_higher_poly);
2967 fprintf (file, "%d\tchrec_dont_know chrecs\n", stats->nb_chrec_dont_know);
2968 fprintf (file, "-----------------------------------------\n");
2969 fprintf (file, "%d\ttotal chrecs\n", stats->nb_chrecs);
b8698a0f 2970 fprintf (file, "%d\twith undetermined coefficients\n",
9baba81b
SP
2971 stats->nb_undetermined);
2972 fprintf (file, "-----------------------------------------\n");
b8698a0f 2973 fprintf (file, "%d\tchrecs in the scev database\n",
907dadbd 2974 (int) scalar_evolution_info->elements ());
9baba81b
SP
2975 fprintf (file, "%d\tsets in the scev database\n", nb_set_scev);
2976 fprintf (file, "%d\tgets in the scev database\n", nb_get_scev);
2977 fprintf (file, "-----------------------------------------\n");
2978 fprintf (file, ")\n\n");
2979}
2980
2981/* Gather statistics about CHREC. */
2982
2983static void
2984gather_chrec_stats (tree chrec, struct chrec_stats *stats)
2985{
2986 if (dump_file && (dump_flags & TDF_STATS))
2987 {
2988 fprintf (dump_file, "(classify_chrec ");
ef6cb4c7 2989 print_generic_expr (dump_file, chrec);
9baba81b
SP
2990 fprintf (dump_file, "\n");
2991 }
b8698a0f 2992
9baba81b 2993 stats->nb_chrecs++;
b8698a0f 2994
9baba81b
SP
2995 if (chrec == NULL_TREE)
2996 {
2997 stats->nb_undetermined++;
2998 return;
2999 }
b8698a0f 3000
9baba81b
SP
3001 switch (TREE_CODE (chrec))
3002 {
3003 case POLYNOMIAL_CHREC:
3004 if (evolution_function_is_affine_p (chrec))
3005 {
3006 if (dump_file && (dump_flags & TDF_STATS))
3007 fprintf (dump_file, " affine_univariate\n");
3008 stats->nb_affine++;
3009 }
a50411de 3010 else if (evolution_function_is_affine_multivariate_p (chrec, 0))
9baba81b
SP
3011 {
3012 if (dump_file && (dump_flags & TDF_STATS))
3013 fprintf (dump_file, " affine_multivariate\n");
3014 stats->nb_affine_multivar++;
3015 }
3016 else
3017 {
3018 if (dump_file && (dump_flags & TDF_STATS))
3019 fprintf (dump_file, " higher_degree_polynomial\n");
3020 stats->nb_higher_poly++;
3021 }
b8698a0f 3022
9baba81b
SP
3023 break;
3024
3025 default:
3026 break;
3027 }
b8698a0f 3028
9baba81b
SP
3029 if (chrec_contains_undetermined (chrec))
3030 {
3031 if (dump_file && (dump_flags & TDF_STATS))
3032 fprintf (dump_file, " undetermined\n");
3033 stats->nb_undetermined++;
3034 }
b8698a0f 3035
9baba81b
SP
3036 if (dump_file && (dump_flags & TDF_STATS))
3037 fprintf (dump_file, ")\n");
3038}
3039
9baba81b
SP
3040/* Classify the chrecs of the whole database. */
3041
b8698a0f 3042void
9baba81b
SP
3043gather_stats_on_scev_database (void)
3044{
3045 struct chrec_stats stats;
b8698a0f 3046
9baba81b
SP
3047 if (!dump_file)
3048 return;
b8698a0f 3049
9baba81b 3050 reset_chrecs_counters (&stats);
b8698a0f 3051
907dadbd
TS
3052 hash_table<scev_info_hasher>::iterator iter;
3053 scev_info_str *elt;
3054 FOR_EACH_HASH_TABLE_ELEMENT (*scalar_evolution_info, elt, scev_info_str *,
3055 iter)
3056 gather_chrec_stats (elt->chrec, &stats);
9baba81b
SP
3057
3058 dump_chrecs_stats (dump_file, &stats);
3059}
3060
3061\f
3062
3063/* Initializer. */
3064
3065static void
3066initialize_scalar_evolutions_analyzer (void)
3067{
3068 /* The elements below are unique. */
3069 if (chrec_dont_know == NULL_TREE)
3070 {
3071 chrec_not_analyzed_yet = NULL_TREE;
3072 chrec_dont_know = make_node (SCEV_NOT_KNOWN);
3073 chrec_known = make_node (SCEV_KNOWN);
d5ab5675
ZD
3074 TREE_TYPE (chrec_dont_know) = void_type_node;
3075 TREE_TYPE (chrec_known) = void_type_node;
9baba81b
SP
3076 }
3077}
3078
3079/* Initialize the analysis of scalar evolutions for LOOPS. */
3080
3081void
d73be268 3082scev_initialize (void)
9baba81b 3083{
42fd6772 3084 struct loop *loop;
9baba81b 3085
fb1fe1f3
RB
3086 gcc_assert (! scev_initialized_p ());
3087
907dadbd 3088 scalar_evolution_info = hash_table<scev_info_hasher>::create_ggc (100);
b8698a0f 3089
9baba81b
SP
3090 initialize_scalar_evolutions_analyzer ();
3091
f0bd40b1 3092 FOR_EACH_LOOP (loop, 0)
42fd6772
ZD
3093 {
3094 loop->nb_iterations = NULL_TREE;
3095 }
9baba81b
SP
3096}
3097
e3a8f1fa
JH
3098/* Return true if SCEV is initialized. */
3099
3100bool
3101scev_initialized_p (void)
3102{
3103 return scalar_evolution_info != NULL;
3104}
3105
a7bf45de
SP
3106/* Cleans up the information cached by the scalar evolutions analysis
3107 in the hash table. */
3108
3109void
3110scev_reset_htab (void)
3111{
3112 if (!scalar_evolution_info)
3113 return;
3114
907dadbd 3115 scalar_evolution_info->empty ();
a7bf45de
SP
3116}
3117
3118/* Cleans up the information cached by the scalar evolutions analysis
3119 in the hash table and in the loop->nb_iterations. */
9baba81b
SP
3120
3121void
3122scev_reset (void)
3123{
9baba81b
SP
3124 struct loop *loop;
3125
a7bf45de
SP
3126 scev_reset_htab ();
3127
f0bd40b1 3128 FOR_EACH_LOOP (loop, 0)
9baba81b 3129 {
42fd6772 3130 loop->nb_iterations = NULL_TREE;
9baba81b 3131 }
e9eb809d
ZD
3132}
3133
1e3d54b4
JH
3134/* Return true if the IV calculation in TYPE can overflow based on the knowledge
3135 of the upper bound on the number of iterations of LOOP, the BASE and STEP
3136 of IV.
3137
3138 We do not use information whether TYPE can overflow so it is safe to
3139 use this test even for derived IVs not computed every iteration or
3140 hypotetical IVs to be inserted into code. */
3141
019d6598 3142bool
1e3d54b4
JH
3143iv_can_overflow_p (struct loop *loop, tree type, tree base, tree step)
3144{
3145 widest_int nit;
3146 wide_int base_min, base_max, step_min, step_max, type_min, type_max;
3147 signop sgn = TYPE_SIGN (type);
3148
3149 if (integer_zerop (step))
3150 return false;
3151
3152 if (TREE_CODE (base) == INTEGER_CST)
8e6cdc90 3153 base_min = base_max = wi::to_wide (base);
1e3d54b4
JH
3154 else if (TREE_CODE (base) == SSA_NAME
3155 && INTEGRAL_TYPE_P (TREE_TYPE (base))
3156 && get_range_info (base, &base_min, &base_max) == VR_RANGE)
3157 ;
3158 else
3159 return true;
3160
3161 if (TREE_CODE (step) == INTEGER_CST)
8e6cdc90 3162 step_min = step_max = wi::to_wide (step);
1e3d54b4
JH
3163 else if (TREE_CODE (step) == SSA_NAME
3164 && INTEGRAL_TYPE_P (TREE_TYPE (step))
3165 && get_range_info (step, &step_min, &step_max) == VR_RANGE)
3166 ;
3167 else
3168 return true;
3169
3170 if (!get_max_loop_iterations (loop, &nit))
3171 return true;
3172
3173 type_min = wi::min_value (type);
3174 type_max = wi::max_value (type);
3175
3176 /* Just sanity check that we don't see values out of the range of the type.
3177 In this case the arithmetics bellow would overflow. */
3178 gcc_checking_assert (wi::ge_p (base_min, type_min, sgn)
3179 && wi::le_p (base_max, type_max, sgn));
3180
3181 /* Account the possible increment in the last ieration. */
3182 bool overflow = false;
3183 nit = wi::add (nit, 1, SIGNED, &overflow);
3184 if (overflow)
3185 return true;
3186
3187 /* NIT is typeless and can exceed the precision of the type. In this case
3188 overflow is always possible, because we know STEP is non-zero. */
3189 if (wi::min_precision (nit, UNSIGNED) > TYPE_PRECISION (type))
3190 return true;
3191 wide_int nit2 = wide_int::from (nit, TYPE_PRECISION (type), UNSIGNED);
3192
3193 /* If step can be positive, check that nit*step <= type_max-base.
3194 This can be done by unsigned arithmetic and we only need to watch overflow
3195 in the multiplication. The right hand side can always be represented in
3196 the type. */
3197 if (sgn == UNSIGNED || !wi::neg_p (step_max))
3198 {
3199 bool overflow = false;
3200 if (wi::gtu_p (wi::mul (step_max, nit2, UNSIGNED, &overflow),
3201 type_max - base_max)
3202 || overflow)
3203 return true;
3204 }
3205 /* If step can be negative, check that nit*(-step) <= base_min-type_min. */
3206 if (sgn == SIGNED && wi::neg_p (step_min))
3207 {
3208 bool overflow = false, overflow2 = false;
3209 if (wi::gtu_p (wi::mul (wi::neg (step_min, &overflow2),
3210 nit2, UNSIGNED, &overflow),
3211 base_min - type_min)
3212 || overflow || overflow2)
3213 return true;
3214 }
3215
3216 return false;
3217}
3218
43aabfcf
BC
3219/* Given EV with form of "(type) {inner_base, inner_step}_loop", this
3220 function tries to derive condition under which it can be simplified
3221 into "{(type)inner_base, (type)inner_step}_loop". The condition is
3222 the maximum number that inner iv can iterate. */
3223
3224static tree
3225derive_simple_iv_with_niters (tree ev, tree *niters)
3226{
3227 if (!CONVERT_EXPR_P (ev))
3228 return ev;
3229
3230 tree inner_ev = TREE_OPERAND (ev, 0);
3231 if (TREE_CODE (inner_ev) != POLYNOMIAL_CHREC)
3232 return ev;
3233
3234 tree init = CHREC_LEFT (inner_ev);
3235 tree step = CHREC_RIGHT (inner_ev);
3236 if (TREE_CODE (init) != INTEGER_CST
3237 || TREE_CODE (step) != INTEGER_CST || integer_zerop (step))
3238 return ev;
3239
3240 tree type = TREE_TYPE (ev);
3241 tree inner_type = TREE_TYPE (inner_ev);
3242 if (TYPE_PRECISION (inner_type) >= TYPE_PRECISION (type))
3243 return ev;
3244
3245 /* Type conversion in "(type) {inner_base, inner_step}_loop" can be
3246 folded only if inner iv won't overflow. We compute the maximum
3247 number the inner iv can iterate before overflowing and return the
3248 simplified affine iv. */
3249 tree delta;
3250 init = fold_convert (type, init);
3251 step = fold_convert (type, step);
3252 ev = build_polynomial_chrec (CHREC_VARIABLE (inner_ev), init, step);
3253 if (tree_int_cst_sign_bit (step))
3254 {
3255 tree bound = lower_bound_in_type (inner_type, inner_type);
3256 delta = fold_build2 (MINUS_EXPR, type, init, fold_convert (type, bound));
3257 step = fold_build1 (NEGATE_EXPR, type, step);
3258 }
3259 else
3260 {
3261 tree bound = upper_bound_in_type (inner_type, inner_type);
3262 delta = fold_build2 (MINUS_EXPR, type, fold_convert (type, bound), init);
3263 }
3264 *niters = fold_build2 (FLOOR_DIV_EXPR, type, delta, step);
3265 return ev;
3266}
3267
f017bf5e
ZD
3268/* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with
3269 respect to WRTO_LOOP and returns its base and step in IV if possible
3270 (see analyze_scalar_evolution_in_loop for more details on USE_LOOP
3271 and WRTO_LOOP). If ALLOW_NONCONSTANT_STEP is true, we want step to be
3272 invariant in LOOP. Otherwise we require it to be an integer constant.
b8698a0f 3273
f017bf5e
ZD
3274 IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g.
3275 because it is computed in signed arithmetics). Consequently, adding an
3276 induction variable
b8698a0f 3277
f017bf5e
ZD
3278 for (i = IV->base; ; i += IV->step)
3279
3280 is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is
3281 false for the type of the induction variable, or you can prove that i does
3282 not wrap by some other argument. Otherwise, this might introduce undefined
3283 behavior, and
b8698a0f 3284
43aabfcf
BC
3285 i = iv->base;
3286 for (; ; i = (type) ((unsigned type) i + (unsigned type) iv->step))
3287
3288 must be used instead.
3289
3290 When IV_NITERS is not NULL, this function also checks case in which OP
3291 is a conversion of an inner simple iv of below form:
3292
3293 (outer_type){inner_base, inner_step}_loop.
f017bf5e 3294
43aabfcf
BC
3295 If type of inner iv has smaller precision than outer_type, it can't be
3296 folded into {(outer_type)inner_base, (outer_type)inner_step}_loop because
3297 the inner iv could overflow/wrap. In this case, we derive a condition
3298 under which the inner iv won't overflow/wrap and do the simplification.
3299 The derived condition normally is the maximum number the inner iv can
3300 iterate, and will be stored in IV_NITERS. This is useful in loop niter
3301 analysis, to derive break conditions when a loop must terminate, when is
3302 infinite. */
e9eb809d
ZD
3303
3304bool
43aabfcf
BC
3305simple_iv_with_niters (struct loop *wrto_loop, struct loop *use_loop,
3306 tree op, affine_iv *iv, tree *iv_niters,
3307 bool allow_nonconstant_step)
e9eb809d 3308{
f3c5f3a3 3309 enum tree_code code;
8aa46dd2 3310 tree type, ev, base, e;
f3c5f3a3
BC
3311 wide_int extreme;
3312 bool folded_casts, overflow;
9baba81b 3313
a6f778b2
ZD
3314 iv->base = NULL_TREE;
3315 iv->step = NULL_TREE;
3316 iv->no_overflow = false;
9baba81b
SP
3317
3318 type = TREE_TYPE (op);
1ee0d660
EB
3319 if (!POINTER_TYPE_P (type)
3320 && !INTEGRAL_TYPE_P (type))
9baba81b
SP
3321 return false;
3322
f017bf5e 3323 ev = analyze_scalar_evolution_in_loop (wrto_loop, use_loop, op,
a6f778b2 3324 &folded_casts);
f017bf5e
ZD
3325 if (chrec_contains_undetermined (ev)
3326 || chrec_contains_symbols_defined_in_loop (ev, wrto_loop->num))
9baba81b
SP
3327 return false;
3328
f017bf5e 3329 if (tree_does_not_contain_chrecs (ev))
9baba81b 3330 {
a6f778b2 3331 iv->base = ev;
6e42ce54 3332 iv->step = build_int_cst (TREE_TYPE (ev), 0);
a6f778b2 3333 iv->no_overflow = true;
9baba81b
SP
3334 return true;
3335 }
3336
43aabfcf
BC
3337 /* If we can derive valid scalar evolution with assumptions. */
3338 if (iv_niters && TREE_CODE (ev) != POLYNOMIAL_CHREC)
3339 ev = derive_simple_iv_with_niters (ev, iv_niters);
3340
3341 if (TREE_CODE (ev) != POLYNOMIAL_CHREC)
3342 return false;
3343
3344 if (CHREC_VARIABLE (ev) != (unsigned) wrto_loop->num)
9baba81b
SP
3345 return false;
3346
a6f778b2 3347 iv->step = CHREC_RIGHT (ev);
f017bf5e
ZD
3348 if ((!allow_nonconstant_step && TREE_CODE (iv->step) != INTEGER_CST)
3349 || tree_contains_chrecs (iv->step, NULL))
9baba81b 3350 return false;
9be872b7 3351
a6f778b2 3352 iv->base = CHREC_LEFT (ev);
f017bf5e 3353 if (tree_contains_chrecs (iv->base, NULL))
9baba81b
SP
3354 return false;
3355
1210573b 3356 iv->no_overflow = !folded_casts && nowrap_type_p (type);
eeef0e45 3357
1e3d54b4
JH
3358 if (!iv->no_overflow
3359 && !iv_can_overflow_p (wrto_loop, type, iv->base, iv->step))
3360 iv->no_overflow = true;
3361
f3c5f3a3
BC
3362 /* Try to simplify iv base:
3363
3364 (signed T) ((unsigned T)base + step) ;; TREE_TYPE (base) == signed T
3365 == (signed T)(unsigned T)base + step
3366 == base + step
3367
3368 If we can prove operation (base + step) doesn't overflow or underflow.
3369 Specifically, we try to prove below conditions are satisfied:
3370
3371 base <= UPPER_BOUND (type) - step ;;step > 0
3372 base >= LOWER_BOUND (type) - step ;;step < 0
3373
3374 This is done by proving the reverse conditions are false using loop's
3375 initial conditions.
3376
3377 The is necessary to make loop niter, or iv overflow analysis easier
3378 for below example:
3379
3380 int foo (int *a, signed char s, signed char l)
3381 {
3382 signed char i;
3383 for (i = s; i < l; i++)
3384 a[i] = 0;
3385 return 0;
3386 }
3387
3388 Note variable I is firstly converted to type unsigned char, incremented,
3389 then converted back to type signed char. */
3390
3391 if (wrto_loop->num != use_loop->num)
3392 return true;
3393
3394 if (!CONVERT_EXPR_P (iv->base) || TREE_CODE (iv->step) != INTEGER_CST)
3395 return true;
3396
3397 type = TREE_TYPE (iv->base);
3398 e = TREE_OPERAND (iv->base, 0);
3399 if (TREE_CODE (e) != PLUS_EXPR
3400 || TREE_CODE (TREE_OPERAND (e, 1)) != INTEGER_CST
3401 || !tree_int_cst_equal (iv->step,
3402 fold_convert (type, TREE_OPERAND (e, 1))))
3403 return true;
3404 e = TREE_OPERAND (e, 0);
3405 if (!CONVERT_EXPR_P (e))
3406 return true;
3407 base = TREE_OPERAND (e, 0);
3408 if (!useless_type_conversion_p (type, TREE_TYPE (base)))
3409 return true;
3410
3411 if (tree_int_cst_sign_bit (iv->step))
3412 {
3413 code = LT_EXPR;
3414 extreme = wi::min_value (type);
3415 }
3416 else
3417 {
3418 code = GT_EXPR;
3419 extreme = wi::max_value (type);
3420 }
3421 overflow = false;
8e6cdc90
RS
3422 extreme = wi::sub (extreme, wi::to_wide (iv->step),
3423 TYPE_SIGN (type), &overflow);
f3c5f3a3
BC
3424 if (overflow)
3425 return true;
3426 e = fold_build2 (code, boolean_type_node, base,
3427 wide_int_to_tree (type, extreme));
8aa46dd2 3428 e = simplify_using_initial_conditions (use_loop, e);
f3c5f3a3
BC
3429 if (!integer_zerop (e))
3430 return true;
3431
3432 if (POINTER_TYPE_P (TREE_TYPE (base)))
3433 code = POINTER_PLUS_EXPR;
3434 else
3435 code = PLUS_EXPR;
3436
3437 iv->base = fold_build2 (code, TREE_TYPE (base), base, iv->step);
9baba81b
SP
3438 return true;
3439}
3440
43aabfcf
BC
3441/* Like simple_iv_with_niters, but return TRUE when OP behaves as a simple
3442 affine iv unconditionally. */
3443
3444bool
3445simple_iv (struct loop *wrto_loop, struct loop *use_loop, tree op,
3446 affine_iv *iv, bool allow_nonconstant_step)
3447{
3448 return simple_iv_with_niters (wrto_loop, use_loop, op, iv,
3449 NULL, allow_nonconstant_step);
3450}
3451
9baba81b
SP
3452/* Finalize the scalar evolution analysis. */
3453
3454void
3455scev_finalize (void)
3456{
d51157de
ZD
3457 if (!scalar_evolution_info)
3458 return;
907dadbd 3459 scalar_evolution_info->empty ();
c7b852c8 3460 scalar_evolution_info = NULL;
01f1c24e 3461 free_numbers_of_iterations_estimates (cfun);
9baba81b
SP
3462}
3463
771f882e
ZD
3464/* Returns true if the expression EXPR is considered to be too expensive
3465 for scev_const_prop. */
3466
3467bool
3468expression_expensive_p (tree expr)
3469{
3470 enum tree_code code;
3471
3472 if (is_gimple_val (expr))
3473 return false;
3474
3475 code = TREE_CODE (expr);
3476 if (code == TRUNC_DIV_EXPR
3477 || code == CEIL_DIV_EXPR
3478 || code == FLOOR_DIV_EXPR
3479 || code == ROUND_DIV_EXPR
3480 || code == TRUNC_MOD_EXPR
3481 || code == CEIL_MOD_EXPR
3482 || code == FLOOR_MOD_EXPR
3483 || code == ROUND_MOD_EXPR
3484 || code == EXACT_DIV_EXPR)
3485 {
3486 /* Division by power of two is usually cheap, so we allow it.
3487 Forbid anything else. */
3488 if (!integer_pow2p (TREE_OPERAND (expr, 1)))
3489 return true;
3490 }
3491
3492 switch (TREE_CODE_CLASS (code))
3493 {
3494 case tcc_binary:
3495 case tcc_comparison:
3496 if (expression_expensive_p (TREE_OPERAND (expr, 1)))
3497 return true;
3498
3499 /* Fallthru. */
3500 case tcc_unary:
3501 return expression_expensive_p (TREE_OPERAND (expr, 0));
3502
3503 default:
3504 return true;
3505 }
3506}
3507
f993a853
TV
3508/* Do final value replacement for LOOP. */
3509
3510void
3511final_value_replacement_loop (struct loop *loop)
3512{
3513 /* If we do not know exact number of iterations of the loop, we cannot
3514 replace the final value. */
3515 edge exit = single_exit (loop);
3516 if (!exit)
3517 return;
3518
3519 tree niter = number_of_latch_executions (loop);
3520 if (niter == chrec_dont_know)
3521 return;
3522
3523 /* Ensure that it is possible to insert new statements somewhere. */
3524 if (!single_pred_p (exit->dest))
3525 split_loop_exit_edge (exit);
3526
3527 /* Set stmt insertion pointer. All stmts are inserted before this point. */
3528 gimple_stmt_iterator gsi = gsi_after_labels (exit->dest);
3529
3530 struct loop *ex_loop
3531 = superloop_at_depth (loop,
3532 loop_depth (exit->dest->loop_father) + 1);
3533
3534 gphi_iterator psi;
3535 for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); )
3536 {
3537 gphi *phi = psi.phi ();
3538 tree rslt = PHI_RESULT (phi);
3539 tree def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
3540 if (virtual_operand_p (def))
3541 {
3542 gsi_next (&psi);
3543 continue;
3544 }
3545
3546 if (!POINTER_TYPE_P (TREE_TYPE (def))
3547 && !INTEGRAL_TYPE_P (TREE_TYPE (def)))
3548 {
3549 gsi_next (&psi);
3550 continue;
3551 }
3552
3553 bool folded_casts;
3554 def = analyze_scalar_evolution_in_loop (ex_loop, loop, def,
3555 &folded_casts);
3556 def = compute_overall_effect_of_inner_loop (ex_loop, def);
3557 if (!tree_does_not_contain_chrecs (def)
3558 || chrec_contains_symbols_defined_in_loop (def, ex_loop->num)
3559 /* Moving the computation from the loop may prolong life range
3560 of some ssa names, which may cause problems if they appear
3561 on abnormal edges. */
3562 || contains_abnormal_ssa_name_p (def)
3563 /* Do not emit expensive expressions. The rationale is that
3564 when someone writes a code like
3565
3566 while (n > 45) n -= 45;
3567
3568 he probably knows that n is not large, and does not want it
3569 to be turned into n %= 45. */
3570 || expression_expensive_p (def))
3571 {
3572 if (dump_file && (dump_flags & TDF_DETAILS))
3573 {
3574 fprintf (dump_file, "not replacing:\n ");
ef6cb4c7 3575 print_gimple_stmt (dump_file, phi, 0);
f993a853
TV
3576 fprintf (dump_file, "\n");
3577 }
3578 gsi_next (&psi);
3579 continue;
3580 }
3581
3582 /* Eliminate the PHI node and replace it by a computation outside
3583 the loop. */
3584 if (dump_file)
3585 {
3586 fprintf (dump_file, "\nfinal value replacement:\n ");
ef6cb4c7 3587 print_gimple_stmt (dump_file, phi, 0);
f993a853
TV
3588 fprintf (dump_file, " with\n ");
3589 }
3590 def = unshare_expr (def);
3591 remove_phi_node (&psi, false);
3592
3593 /* If def's type has undefined overflow and there were folded
3594 casts, rewrite all stmts added for def into arithmetics
3595 with defined overflow behavior. */
3596 if (folded_casts && ANY_INTEGRAL_TYPE_P (TREE_TYPE (def))
3597 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (def)))
3598 {
3599 gimple_seq stmts;
3600 gimple_stmt_iterator gsi2;
3601 def = force_gimple_operand (def, &stmts, true, NULL_TREE);
3602 gsi2 = gsi_start (stmts);
3603 while (!gsi_end_p (gsi2))
3604 {
3605 gimple *stmt = gsi_stmt (gsi2);
3606 gimple_stmt_iterator gsi3 = gsi2;
3607 gsi_next (&gsi2);
3608 gsi_remove (&gsi3, false);
3609 if (is_gimple_assign (stmt)
3610 && arith_code_with_undefined_signed_overflow
3611 (gimple_assign_rhs_code (stmt)))
3612 gsi_insert_seq_before (&gsi,
3613 rewrite_to_defined_overflow (stmt),
3614 GSI_SAME_STMT);
3615 else
3616 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
3617 }
3618 }
3619 else
3620 def = force_gimple_operand_gsi (&gsi, def, false, NULL_TREE,
3621 true, GSI_SAME_STMT);
3622
3623 gassign *ass = gimple_build_assign (rslt, def);
3624 gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
3625 if (dump_file)
3626 {
ef6cb4c7 3627 print_gimple_stmt (dump_file, ass, 0);
f993a853
TV
3628 fprintf (dump_file, "\n");
3629 }
3630 }
3631}
3632
684aaf29 3633/* Replace ssa names for that scev can prove they are constant by the
3ac01fde
ZD
3634 appropriate constants. Also perform final value replacement in loops,
3635 in case the replacement expressions are cheap.
b8698a0f 3636
684aaf29
ZD
3637 We only consider SSA names defined by phi nodes; rest is left to the
3638 ordinary constant propagation pass. */
3639
c2924966 3640unsigned int
684aaf29
ZD
3641scev_const_prop (void)
3642{
3643 basic_block bb;
726a989a 3644 tree name, type, ev;
538dd0b7 3645 gphi *phi;
f993a853 3646 struct loop *loop;
684aaf29 3647 bitmap ssa_names_to_remove = NULL;
3ac01fde 3648 unsigned i;
538dd0b7 3649 gphi_iterator psi;
684aaf29 3650
0fc822d0 3651 if (number_of_loops (cfun) <= 1)
c2924966 3652 return 0;
684aaf29 3653
11cd3bed 3654 FOR_EACH_BB_FN (bb, cfun)
684aaf29
ZD
3655 {
3656 loop = bb->loop_father;
3657
726a989a 3658 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
684aaf29 3659 {
538dd0b7 3660 phi = psi.phi ();
684aaf29
ZD
3661 name = PHI_RESULT (phi);
3662
ea057359 3663 if (virtual_operand_p (name))
684aaf29
ZD
3664 continue;
3665
3666 type = TREE_TYPE (name);
3667
3668 if (!POINTER_TYPE_P (type)
3669 && !INTEGRAL_TYPE_P (type))
3670 continue;
3671
c70ed622
BC
3672 ev = resolve_mixers (loop, analyze_scalar_evolution (loop, name),
3673 NULL);
684aaf29
ZD
3674 if (!is_gimple_min_invariant (ev)
3675 || !may_propagate_copy (name, ev))
3676 continue;
3677
3678 /* Replace the uses of the name. */
18aed06a 3679 if (name != ev)
ed22b76f
TV
3680 {
3681 if (dump_file && (dump_flags & TDF_DETAILS))
3682 {
3683 fprintf (dump_file, "Replacing uses of: ");
ef6cb4c7 3684 print_generic_expr (dump_file, name);
ed22b76f 3685 fprintf (dump_file, " with: ");
ef6cb4c7 3686 print_generic_expr (dump_file, ev);
ed22b76f
TV
3687 fprintf (dump_file, "\n");
3688 }
3689 replace_uses_by (name, ev);
3690 }
684aaf29
ZD
3691
3692 if (!ssa_names_to_remove)
3693 ssa_names_to_remove = BITMAP_ALLOC (NULL);
3694 bitmap_set_bit (ssa_names_to_remove, SSA_NAME_VERSION (name));
3695 }
3696 }
3697
9b3b55a1
DN
3698 /* Remove the ssa names that were replaced by constants. We do not
3699 remove them directly in the previous cycle, since this
3700 invalidates scev cache. */
684aaf29
ZD
3701 if (ssa_names_to_remove)
3702 {
3703 bitmap_iterator bi;
684aaf29
ZD
3704
3705 EXECUTE_IF_SET_IN_BITMAP (ssa_names_to_remove, 0, i, bi)
3706 {
726a989a 3707 gimple_stmt_iterator psi;
684aaf29 3708 name = ssa_name (i);
538dd0b7 3709 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (name));
684aaf29 3710
726a989a
RB
3711 gcc_assert (gimple_code (phi) == GIMPLE_PHI);
3712 psi = gsi_for_stmt (phi);
3713 remove_phi_node (&psi, true);
684aaf29
ZD
3714 }
3715
3716 BITMAP_FREE (ssa_names_to_remove);
3717 scev_reset ();
3718 }
3ac01fde
ZD
3719
3720 /* Now the regular final value replacement. */
f0bd40b1 3721 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
f993a853 3722 final_value_replacement_loop (loop);
925196ed 3723
c2924966 3724 return 0;
684aaf29 3725}
9e2f83a5
ZD
3726
3727#include "gt-tree-scalar-evolution.h"