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