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