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