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