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