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