1 /* Chains of recurrences.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file implements operations on chains of recurrences. Chains
23 of recurrences are used for modeling evolution functions of scalar
29 #include "coretypes.h"
30 #include "tree-pretty-print.h"
32 #include "tree-flow.h"
33 #include "tree-chrec.h"
34 #include "tree-pass.h"
36 #include "tree-scalar-evolution.h"
38 /* Extended folder for chrecs. */
40 /* Determines whether CST is not a constant evolution. */
43 is_not_constant_evolution (const_tree cst
)
45 return (TREE_CODE (cst
) == POLYNOMIAL_CHREC
);
48 /* Fold CODE for a polynomial function and a constant. */
51 chrec_fold_poly_cst (enum tree_code code
,
58 gcc_assert (TREE_CODE (poly
) == POLYNOMIAL_CHREC
);
59 gcc_assert (!is_not_constant_evolution (cst
));
60 gcc_assert (type
== chrec_type (poly
));
65 return build_polynomial_chrec
66 (CHREC_VARIABLE (poly
),
67 chrec_fold_plus (type
, CHREC_LEFT (poly
), cst
),
71 return build_polynomial_chrec
72 (CHREC_VARIABLE (poly
),
73 chrec_fold_minus (type
, CHREC_LEFT (poly
), cst
),
77 return build_polynomial_chrec
78 (CHREC_VARIABLE (poly
),
79 chrec_fold_multiply (type
, CHREC_LEFT (poly
), cst
),
80 chrec_fold_multiply (type
, CHREC_RIGHT (poly
), cst
));
83 return chrec_dont_know
;
87 /* Fold the addition of two polynomial functions. */
90 chrec_fold_plus_poly_poly (enum tree_code code
,
96 struct loop
*loop0
= get_chrec_loop (poly0
);
97 struct loop
*loop1
= get_chrec_loop (poly1
);
98 tree rtype
= code
== POINTER_PLUS_EXPR
? sizetype
: type
;
102 gcc_assert (TREE_CODE (poly0
) == POLYNOMIAL_CHREC
);
103 gcc_assert (TREE_CODE (poly1
) == POLYNOMIAL_CHREC
);
104 if (POINTER_TYPE_P (chrec_type (poly0
)))
105 gcc_assert (chrec_type (poly1
) == sizetype
);
107 gcc_assert (chrec_type (poly0
) == chrec_type (poly1
));
108 gcc_assert (type
== chrec_type (poly0
));
111 {a, +, b}_1 + {c, +, d}_2 -> {{a, +, b}_1 + c, +, d}_2,
112 {a, +, b}_2 + {c, +, d}_1 -> {{c, +, d}_1 + a, +, b}_2,
113 {a, +, b}_x + {c, +, d}_x -> {a+c, +, b+d}_x. */
114 if (flow_loop_nested_p (loop0
, loop1
))
116 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
117 return build_polynomial_chrec
118 (CHREC_VARIABLE (poly1
),
119 chrec_fold_plus (type
, poly0
, CHREC_LEFT (poly1
)),
120 CHREC_RIGHT (poly1
));
122 return build_polynomial_chrec
123 (CHREC_VARIABLE (poly1
),
124 chrec_fold_minus (type
, poly0
, CHREC_LEFT (poly1
)),
125 chrec_fold_multiply (type
, CHREC_RIGHT (poly1
),
126 SCALAR_FLOAT_TYPE_P (type
)
127 ? build_real (type
, dconstm1
)
128 : build_int_cst_type (type
, -1)));
131 if (flow_loop_nested_p (loop1
, loop0
))
133 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
134 return build_polynomial_chrec
135 (CHREC_VARIABLE (poly0
),
136 chrec_fold_plus (type
, CHREC_LEFT (poly0
), poly1
),
137 CHREC_RIGHT (poly0
));
139 return build_polynomial_chrec
140 (CHREC_VARIABLE (poly0
),
141 chrec_fold_minus (type
, CHREC_LEFT (poly0
), poly1
),
142 CHREC_RIGHT (poly0
));
145 /* This function should never be called for chrecs of loops that
146 do not belong to the same loop nest. */
147 gcc_assert (loop0
== loop1
);
149 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
151 left
= chrec_fold_plus
152 (type
, CHREC_LEFT (poly0
), CHREC_LEFT (poly1
));
153 right
= chrec_fold_plus
154 (rtype
, CHREC_RIGHT (poly0
), CHREC_RIGHT (poly1
));
158 left
= chrec_fold_minus
159 (type
, CHREC_LEFT (poly0
), CHREC_LEFT (poly1
));
160 right
= chrec_fold_minus
161 (type
, CHREC_RIGHT (poly0
), CHREC_RIGHT (poly1
));
164 if (chrec_zerop (right
))
167 return build_polynomial_chrec
168 (CHREC_VARIABLE (poly0
), left
, right
);
173 /* Fold the multiplication of two polynomial functions. */
176 chrec_fold_multiply_poly_poly (tree type
,
182 struct loop
*loop0
= get_chrec_loop (poly0
);
183 struct loop
*loop1
= get_chrec_loop (poly1
);
187 gcc_assert (TREE_CODE (poly0
) == POLYNOMIAL_CHREC
);
188 gcc_assert (TREE_CODE (poly1
) == POLYNOMIAL_CHREC
);
189 gcc_assert (chrec_type (poly0
) == chrec_type (poly1
));
190 gcc_assert (type
== chrec_type (poly0
));
192 /* {a, +, b}_1 * {c, +, d}_2 -> {c*{a, +, b}_1, +, d}_2,
193 {a, +, b}_2 * {c, +, d}_1 -> {a*{c, +, d}_1, +, b}_2,
194 {a, +, b}_x * {c, +, d}_x -> {a*c, +, a*d + b*c + b*d, +, 2*b*d}_x. */
195 if (flow_loop_nested_p (loop0
, loop1
))
196 /* poly0 is a constant wrt. poly1. */
197 return build_polynomial_chrec
198 (CHREC_VARIABLE (poly1
),
199 chrec_fold_multiply (type
, CHREC_LEFT (poly1
), poly0
),
200 CHREC_RIGHT (poly1
));
202 if (flow_loop_nested_p (loop1
, loop0
))
203 /* poly1 is a constant wrt. poly0. */
204 return build_polynomial_chrec
205 (CHREC_VARIABLE (poly0
),
206 chrec_fold_multiply (type
, CHREC_LEFT (poly0
), poly1
),
207 CHREC_RIGHT (poly0
));
209 gcc_assert (loop0
== loop1
);
211 /* poly0 and poly1 are two polynomials in the same variable,
212 {a, +, b}_x * {c, +, d}_x -> {a*c, +, a*d + b*c + b*d, +, 2*b*d}_x. */
215 t0
= chrec_fold_multiply (type
, CHREC_LEFT (poly0
), CHREC_LEFT (poly1
));
218 t1
= chrec_fold_multiply (type
, CHREC_LEFT (poly0
), CHREC_RIGHT (poly1
));
219 t1
= chrec_fold_plus (type
, t1
, chrec_fold_multiply (type
,
221 CHREC_LEFT (poly1
)));
223 t2
= chrec_fold_multiply (type
, CHREC_RIGHT (poly0
), CHREC_RIGHT (poly1
));
224 /* "a*d + b*c + b*d". */
225 t1
= chrec_fold_plus (type
, t1
, t2
);
227 t2
= chrec_fold_multiply (type
, SCALAR_FLOAT_TYPE_P (type
)
228 ? build_real (type
, dconst2
)
229 : build_int_cst (type
, 2), t2
);
231 var
= CHREC_VARIABLE (poly0
);
232 return build_polynomial_chrec (var
, t0
,
233 build_polynomial_chrec (var
, t1
, t2
));
236 /* When the operands are automatically_generated_chrec_p, the fold has
237 to respect the semantics of the operands. */
240 chrec_fold_automatically_generated_operands (tree op0
,
243 if (op0
== chrec_dont_know
244 || op1
== chrec_dont_know
)
245 return chrec_dont_know
;
247 if (op0
== chrec_known
248 || op1
== chrec_known
)
251 if (op0
== chrec_not_analyzed_yet
252 || op1
== chrec_not_analyzed_yet
)
253 return chrec_not_analyzed_yet
;
255 /* The default case produces a safe result. */
256 return chrec_dont_know
;
259 /* Fold the addition of two chrecs. */
262 chrec_fold_plus_1 (enum tree_code code
, tree type
,
265 tree op1_type
= code
== POINTER_PLUS_EXPR
? sizetype
: type
;
267 if (automatically_generated_chrec_p (op0
)
268 || automatically_generated_chrec_p (op1
))
269 return chrec_fold_automatically_generated_operands (op0
, op1
);
271 switch (TREE_CODE (op0
))
273 case POLYNOMIAL_CHREC
:
274 switch (TREE_CODE (op1
))
276 case POLYNOMIAL_CHREC
:
277 return chrec_fold_plus_poly_poly (code
, type
, op0
, op1
);
280 if (tree_contains_chrecs (op1
, NULL
))
281 return chrec_dont_know
;
284 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
285 return build_polynomial_chrec
286 (CHREC_VARIABLE (op0
),
287 chrec_fold_plus (type
, CHREC_LEFT (op0
), op1
),
290 return build_polynomial_chrec
291 (CHREC_VARIABLE (op0
),
292 chrec_fold_minus (type
, CHREC_LEFT (op0
), op1
),
297 if (tree_contains_chrecs (op0
, NULL
))
298 return chrec_dont_know
;
301 switch (TREE_CODE (op1
))
303 case POLYNOMIAL_CHREC
:
304 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
305 return build_polynomial_chrec
306 (CHREC_VARIABLE (op1
),
307 chrec_fold_plus (type
, op0
, CHREC_LEFT (op1
)),
310 return build_polynomial_chrec
311 (CHREC_VARIABLE (op1
),
312 chrec_fold_minus (type
, op0
, CHREC_LEFT (op1
)),
313 chrec_fold_multiply (type
, CHREC_RIGHT (op1
),
314 SCALAR_FLOAT_TYPE_P (type
)
315 ? build_real (type
, dconstm1
)
316 : build_int_cst_type (type
, -1)));
319 if (tree_contains_chrecs (op1
, NULL
))
320 return chrec_dont_know
;
325 if ((tree_contains_chrecs (op0
, &size
)
326 || tree_contains_chrecs (op1
, &size
))
327 && size
< PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE
))
328 return build2 (code
, type
, op0
, op1
);
329 else if (size
< PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE
))
330 return fold_build2 (code
, type
,
331 fold_convert (type
, op0
),
332 fold_convert (op1_type
, op1
));
334 return chrec_dont_know
;
340 /* Fold the addition of two chrecs. */
343 chrec_fold_plus (tree type
,
348 if (automatically_generated_chrec_p (op0
)
349 || automatically_generated_chrec_p (op1
))
350 return chrec_fold_automatically_generated_operands (op0
, op1
);
352 if (integer_zerop (op0
))
353 return chrec_convert (type
, op1
, NULL
);
354 if (integer_zerop (op1
))
355 return chrec_convert (type
, op0
, NULL
);
357 if (POINTER_TYPE_P (type
))
358 code
= POINTER_PLUS_EXPR
;
362 return chrec_fold_plus_1 (code
, type
, op0
, op1
);
365 /* Fold the subtraction of two chrecs. */
368 chrec_fold_minus (tree type
,
372 if (automatically_generated_chrec_p (op0
)
373 || automatically_generated_chrec_p (op1
))
374 return chrec_fold_automatically_generated_operands (op0
, op1
);
376 if (integer_zerop (op1
))
379 return chrec_fold_plus_1 (MINUS_EXPR
, type
, op0
, op1
);
382 /* Fold the multiplication of two chrecs. */
385 chrec_fold_multiply (tree type
,
389 if (automatically_generated_chrec_p (op0
)
390 || automatically_generated_chrec_p (op1
))
391 return chrec_fold_automatically_generated_operands (op0
, op1
);
393 switch (TREE_CODE (op0
))
395 case POLYNOMIAL_CHREC
:
396 switch (TREE_CODE (op1
))
398 case POLYNOMIAL_CHREC
:
399 return chrec_fold_multiply_poly_poly (type
, op0
, op1
);
402 if (tree_contains_chrecs (op1
, NULL
))
403 return chrec_dont_know
;
406 if (integer_onep (op1
))
408 if (integer_zerop (op1
))
409 return build_int_cst (type
, 0);
411 return build_polynomial_chrec
412 (CHREC_VARIABLE (op0
),
413 chrec_fold_multiply (type
, CHREC_LEFT (op0
), op1
),
414 chrec_fold_multiply (type
, CHREC_RIGHT (op0
), op1
));
418 if (tree_contains_chrecs (op0
, NULL
))
419 return chrec_dont_know
;
422 if (integer_onep (op0
))
425 if (integer_zerop (op0
))
426 return build_int_cst (type
, 0);
428 switch (TREE_CODE (op1
))
430 case POLYNOMIAL_CHREC
:
431 return build_polynomial_chrec
432 (CHREC_VARIABLE (op1
),
433 chrec_fold_multiply (type
, CHREC_LEFT (op1
), op0
),
434 chrec_fold_multiply (type
, CHREC_RIGHT (op1
), op0
));
437 if (tree_contains_chrecs (op1
, NULL
))
438 return chrec_dont_know
;
441 if (integer_onep (op1
))
443 if (integer_zerop (op1
))
444 return build_int_cst (type
, 0);
445 return fold_build2 (MULT_EXPR
, type
, op0
, op1
);
454 /* Evaluate the binomial coefficient. Return NULL_TREE if the intermediate
455 calculation overflows, otherwise return C(n,k) with type TYPE. */
458 tree_fold_binomial (tree type
, tree n
, unsigned int k
)
460 unsigned HOST_WIDE_INT lidx
, lnum
, ldenom
, lres
, ldum
;
461 HOST_WIDE_INT hidx
, hnum
, hdenom
, hres
, hdum
;
465 /* Handle the most frequent cases. */
467 return build_int_cst (type
, 1);
469 return fold_convert (type
, n
);
471 /* Check that k <= n. */
472 if (TREE_INT_CST_HIGH (n
) == 0
473 && TREE_INT_CST_LOW (n
) < k
)
477 lnum
= TREE_INT_CST_LOW (n
);
478 hnum
= TREE_INT_CST_HIGH (n
);
480 /* Denominator = 2. */
484 /* Index = Numerator-1. */
488 lidx
= ~ (unsigned HOST_WIDE_INT
) 0;
496 /* Numerator = Numerator*Index = n*(n-1). */
497 if (mul_double (lnum
, hnum
, lidx
, hidx
, &lnum
, &hnum
))
500 for (i
= 3; i
<= k
; i
++)
506 lidx
= ~ (unsigned HOST_WIDE_INT
) 0;
511 /* Numerator *= Index. */
512 if (mul_double (lnum
, hnum
, lidx
, hidx
, &lnum
, &hnum
))
515 /* Denominator *= i. */
516 mul_double (ldenom
, hdenom
, i
, 0, &ldenom
, &hdenom
);
519 /* Result = Numerator / Denominator. */
520 div_and_round_double (EXACT_DIV_EXPR
, 1, lnum
, hnum
, ldenom
, hdenom
,
521 &lres
, &hres
, &ldum
, &hdum
);
523 res
= build_int_cst_wide (type
, lres
, hres
);
524 return int_fits_type_p (res
, type
) ? res
: NULL_TREE
;
527 /* Helper function. Use the Newton's interpolating formula for
528 evaluating the value of the evolution function. */
531 chrec_evaluate (unsigned var
, tree chrec
, tree n
, unsigned int k
)
533 tree arg0
, arg1
, binomial_n_k
;
534 tree type
= TREE_TYPE (chrec
);
535 struct loop
*var_loop
= get_loop (var
);
537 while (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
538 && flow_loop_nested_p (var_loop
, get_chrec_loop (chrec
)))
539 chrec
= CHREC_LEFT (chrec
);
541 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
542 && CHREC_VARIABLE (chrec
) == var
)
544 arg1
= chrec_evaluate (var
, CHREC_RIGHT (chrec
), n
, k
+ 1);
545 if (arg1
== chrec_dont_know
)
546 return chrec_dont_know
;
547 binomial_n_k
= tree_fold_binomial (type
, n
, k
);
549 return chrec_dont_know
;
550 arg0
= fold_build2 (MULT_EXPR
, type
,
551 CHREC_LEFT (chrec
), binomial_n_k
);
552 return chrec_fold_plus (type
, arg0
, arg1
);
555 binomial_n_k
= tree_fold_binomial (type
, n
, k
);
557 return chrec_dont_know
;
559 return fold_build2 (MULT_EXPR
, type
, chrec
, binomial_n_k
);
562 /* Evaluates "CHREC (X)" when the varying variable is VAR.
563 Example: Given the following parameters,
569 The result is given by the Newton's interpolating formula:
570 3 * \binom{10}{0} + 4 * \binom{10}{1}.
574 chrec_apply (unsigned var
,
578 tree type
= chrec_type (chrec
);
579 tree res
= chrec_dont_know
;
581 if (automatically_generated_chrec_p (chrec
)
582 || automatically_generated_chrec_p (x
)
584 /* When the symbols are defined in an outer loop, it is possible
585 to symbolically compute the apply, since the symbols are
586 constants with respect to the varying loop. */
587 || chrec_contains_symbols_defined_in_loop (chrec
, var
))
588 return chrec_dont_know
;
590 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
591 fprintf (dump_file
, "(chrec_apply \n");
593 if (TREE_CODE (x
) == INTEGER_CST
&& SCALAR_FLOAT_TYPE_P (type
))
594 x
= build_real_from_int_cst (type
, x
);
596 switch (TREE_CODE (chrec
))
598 case POLYNOMIAL_CHREC
:
599 if (evolution_function_is_affine_p (chrec
))
601 if (CHREC_VARIABLE (chrec
) != var
)
602 return build_polynomial_chrec
603 (CHREC_VARIABLE (chrec
),
604 chrec_apply (var
, CHREC_LEFT (chrec
), x
),
605 chrec_apply (var
, CHREC_RIGHT (chrec
), x
));
607 /* "{a, +, b} (x)" -> "a + b*x". */
608 x
= chrec_convert_rhs (type
, x
, NULL
);
609 res
= chrec_fold_multiply (TREE_TYPE (x
), CHREC_RIGHT (chrec
), x
);
610 res
= chrec_fold_plus (type
, CHREC_LEFT (chrec
), res
);
612 else if (TREE_CODE (x
) == INTEGER_CST
613 && tree_int_cst_sgn (x
) == 1)
614 /* testsuite/.../ssa-chrec-38.c. */
615 res
= chrec_evaluate (var
, chrec
, x
, 0);
617 res
= chrec_dont_know
;
621 res
= chrec_convert (TREE_TYPE (chrec
),
622 chrec_apply (var
, TREE_OPERAND (chrec
, 0), x
),
631 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
633 fprintf (dump_file
, " (varying_loop = %d\n", var
);
634 fprintf (dump_file
, ")\n (chrec = ");
635 print_generic_expr (dump_file
, chrec
, 0);
636 fprintf (dump_file
, ")\n (x = ");
637 print_generic_expr (dump_file
, x
, 0);
638 fprintf (dump_file
, ")\n (res = ");
639 print_generic_expr (dump_file
, res
, 0);
640 fprintf (dump_file
, "))\n");
646 /* For a given CHREC and an induction variable map IV_MAP that maps
647 (loop->num, expr) for every loop number of the current_loops an
648 expression, calls chrec_apply when the expression is not NULL. */
651 chrec_apply_map (tree chrec
, VEC (tree
, heap
) *iv_map
)
656 FOR_EACH_VEC_ELT (tree
, iv_map
, i
, expr
)
658 chrec
= chrec_apply (i
, chrec
, expr
);
663 /* Replaces the initial condition in CHREC with INIT_COND. */
666 chrec_replace_initial_condition (tree chrec
,
669 if (automatically_generated_chrec_p (chrec
))
672 gcc_assert (chrec_type (chrec
) == chrec_type (init_cond
));
674 switch (TREE_CODE (chrec
))
676 case POLYNOMIAL_CHREC
:
677 return build_polynomial_chrec
678 (CHREC_VARIABLE (chrec
),
679 chrec_replace_initial_condition (CHREC_LEFT (chrec
), init_cond
),
680 CHREC_RIGHT (chrec
));
687 /* Returns the initial condition of a given CHREC. */
690 initial_condition (tree chrec
)
692 if (automatically_generated_chrec_p (chrec
))
695 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
696 return initial_condition (CHREC_LEFT (chrec
));
701 /* Returns a univariate function that represents the evolution in
702 LOOP_NUM. Mask the evolution of any other loop. */
705 hide_evolution_in_other_loops_than_loop (tree chrec
,
708 struct loop
*loop
= get_loop (loop_num
), *chloop
;
709 if (automatically_generated_chrec_p (chrec
))
712 switch (TREE_CODE (chrec
))
714 case POLYNOMIAL_CHREC
:
715 chloop
= get_chrec_loop (chrec
);
718 return build_polynomial_chrec
720 hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec
),
722 CHREC_RIGHT (chrec
));
724 else if (flow_loop_nested_p (chloop
, loop
))
725 /* There is no evolution in this loop. */
726 return initial_condition (chrec
);
730 gcc_assert (flow_loop_nested_p (loop
, chloop
));
731 return hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec
),
740 /* Returns the evolution part of CHREC in LOOP_NUM when RIGHT is
741 true, otherwise returns the initial condition in LOOP_NUM. */
744 chrec_component_in_loop_num (tree chrec
,
749 struct loop
*loop
= get_loop (loop_num
), *chloop
;
751 if (automatically_generated_chrec_p (chrec
))
754 switch (TREE_CODE (chrec
))
756 case POLYNOMIAL_CHREC
:
757 chloop
= get_chrec_loop (chrec
);
762 component
= CHREC_RIGHT (chrec
);
764 component
= CHREC_LEFT (chrec
);
766 if (TREE_CODE (CHREC_LEFT (chrec
)) != POLYNOMIAL_CHREC
767 || CHREC_VARIABLE (CHREC_LEFT (chrec
)) != CHREC_VARIABLE (chrec
))
771 return build_polynomial_chrec
773 chrec_component_in_loop_num (CHREC_LEFT (chrec
),
779 else if (flow_loop_nested_p (chloop
, loop
))
780 /* There is no evolution part in this loop. */
785 gcc_assert (flow_loop_nested_p (loop
, chloop
));
786 return chrec_component_in_loop_num (CHREC_LEFT (chrec
),
799 /* Returns the evolution part in LOOP_NUM. Example: the call
800 evolution_part_in_loop_num ({{0, +, 1}_1, +, 2}_1, 1) returns
804 evolution_part_in_loop_num (tree chrec
,
807 return chrec_component_in_loop_num (chrec
, loop_num
, true);
810 /* Returns the initial condition in LOOP_NUM. Example: the call
811 initial_condition_in_loop_num ({{0, +, 1}_1, +, 2}_2, 2) returns
815 initial_condition_in_loop_num (tree chrec
,
818 return chrec_component_in_loop_num (chrec
, loop_num
, false);
821 /* Set or reset the evolution of CHREC to NEW_EVOL in loop LOOP_NUM.
822 This function is essentially used for setting the evolution to
823 chrec_dont_know, for example after having determined that it is
824 impossible to say how many times a loop will execute. */
827 reset_evolution_in_loop (unsigned loop_num
,
831 struct loop
*loop
= get_loop (loop_num
);
833 if (POINTER_TYPE_P (chrec_type (chrec
)))
834 gcc_assert (sizetype
== chrec_type (new_evol
));
836 gcc_assert (chrec_type (chrec
) == chrec_type (new_evol
));
838 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
839 && flow_loop_nested_p (loop
, get_chrec_loop (chrec
)))
841 tree left
= reset_evolution_in_loop (loop_num
, CHREC_LEFT (chrec
),
843 tree right
= reset_evolution_in_loop (loop_num
, CHREC_RIGHT (chrec
),
845 return build3 (POLYNOMIAL_CHREC
, TREE_TYPE (left
),
846 CHREC_VAR (chrec
), left
, right
);
849 while (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
850 && CHREC_VARIABLE (chrec
) == loop_num
)
851 chrec
= CHREC_LEFT (chrec
);
853 return build_polynomial_chrec (loop_num
, chrec
, new_evol
);
856 /* Merges two evolution functions that were found by following two
857 alternate paths of a conditional expression. */
860 chrec_merge (tree chrec1
,
863 if (chrec1
== chrec_dont_know
864 || chrec2
== chrec_dont_know
)
865 return chrec_dont_know
;
867 if (chrec1
== chrec_known
868 || chrec2
== chrec_known
)
871 if (chrec1
== chrec_not_analyzed_yet
)
873 if (chrec2
== chrec_not_analyzed_yet
)
876 if (eq_evolutions_p (chrec1
, chrec2
))
879 return chrec_dont_know
;
886 /* Helper function for is_multivariate_chrec. */
889 is_multivariate_chrec_rec (const_tree chrec
, unsigned int rec_var
)
891 if (chrec
== NULL_TREE
)
894 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
896 if (CHREC_VARIABLE (chrec
) != rec_var
)
899 return (is_multivariate_chrec_rec (CHREC_LEFT (chrec
), rec_var
)
900 || is_multivariate_chrec_rec (CHREC_RIGHT (chrec
), rec_var
));
906 /* Determine whether the given chrec is multivariate or not. */
909 is_multivariate_chrec (const_tree chrec
)
911 if (chrec
== NULL_TREE
)
914 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
915 return (is_multivariate_chrec_rec (CHREC_LEFT (chrec
),
916 CHREC_VARIABLE (chrec
))
917 || is_multivariate_chrec_rec (CHREC_RIGHT (chrec
),
918 CHREC_VARIABLE (chrec
)));
923 /* Determines whether the chrec contains symbolic names or not. */
926 chrec_contains_symbols (const_tree chrec
)
930 if (chrec
== NULL_TREE
)
933 if (TREE_CODE (chrec
) == SSA_NAME
934 || TREE_CODE (chrec
) == VAR_DECL
935 || TREE_CODE (chrec
) == PARM_DECL
936 || TREE_CODE (chrec
) == FUNCTION_DECL
937 || TREE_CODE (chrec
) == LABEL_DECL
938 || TREE_CODE (chrec
) == RESULT_DECL
939 || TREE_CODE (chrec
) == FIELD_DECL
)
942 n
= TREE_OPERAND_LENGTH (chrec
);
943 for (i
= 0; i
< n
; i
++)
944 if (chrec_contains_symbols (TREE_OPERAND (chrec
, i
)))
949 /* Determines whether the chrec contains undetermined coefficients. */
952 chrec_contains_undetermined (const_tree chrec
)
956 if (chrec
== chrec_dont_know
)
959 if (chrec
== NULL_TREE
)
962 n
= TREE_OPERAND_LENGTH (chrec
);
963 for (i
= 0; i
< n
; i
++)
964 if (chrec_contains_undetermined (TREE_OPERAND (chrec
, i
)))
969 /* Determines whether the tree EXPR contains chrecs, and increment
970 SIZE if it is not a NULL pointer by an estimation of the depth of
974 tree_contains_chrecs (const_tree expr
, int *size
)
978 if (expr
== NULL_TREE
)
984 if (tree_is_chrec (expr
))
987 n
= TREE_OPERAND_LENGTH (expr
);
988 for (i
= 0; i
< n
; i
++)
989 if (tree_contains_chrecs (TREE_OPERAND (expr
, i
), size
))
994 /* Recursive helper function. */
997 evolution_function_is_invariant_rec_p (tree chrec
, int loopnum
)
999 if (evolution_function_is_constant_p (chrec
))
1002 if (TREE_CODE (chrec
) == SSA_NAME
1004 || expr_invariant_in_loop_p (get_loop (loopnum
), chrec
)))
1007 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
1009 if (CHREC_VARIABLE (chrec
) == (unsigned) loopnum
1010 || !evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec
),
1012 || !evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec
),
1018 switch (TREE_OPERAND_LENGTH (chrec
))
1021 if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec
, 1),
1026 if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec
, 0),
1038 /* Return true if CHREC is invariant in loop LOOPNUM, false otherwise. */
1041 evolution_function_is_invariant_p (tree chrec
, int loopnum
)
1043 return evolution_function_is_invariant_rec_p (chrec
, loopnum
);
1046 /* Determine whether the given tree is an affine multivariate
1050 evolution_function_is_affine_multivariate_p (const_tree chrec
, int loopnum
)
1052 if (chrec
== NULL_TREE
)
1055 switch (TREE_CODE (chrec
))
1057 case POLYNOMIAL_CHREC
:
1058 if (evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec
), loopnum
))
1060 if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec
), loopnum
))
1064 if (TREE_CODE (CHREC_RIGHT (chrec
)) == POLYNOMIAL_CHREC
1065 && CHREC_VARIABLE (CHREC_RIGHT (chrec
))
1066 != CHREC_VARIABLE (chrec
)
1067 && evolution_function_is_affine_multivariate_p
1068 (CHREC_RIGHT (chrec
), loopnum
))
1076 if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec
), loopnum
)
1077 && TREE_CODE (CHREC_LEFT (chrec
)) == POLYNOMIAL_CHREC
1078 && CHREC_VARIABLE (CHREC_LEFT (chrec
)) != CHREC_VARIABLE (chrec
)
1079 && evolution_function_is_affine_multivariate_p
1080 (CHREC_LEFT (chrec
), loopnum
))
1091 /* Determine whether the given tree is a function in zero or one
1095 evolution_function_is_univariate_p (const_tree chrec
)
1097 if (chrec
== NULL_TREE
)
1100 switch (TREE_CODE (chrec
))
1102 case POLYNOMIAL_CHREC
:
1103 switch (TREE_CODE (CHREC_LEFT (chrec
)))
1105 case POLYNOMIAL_CHREC
:
1106 if (CHREC_VARIABLE (chrec
) != CHREC_VARIABLE (CHREC_LEFT (chrec
)))
1108 if (!evolution_function_is_univariate_p (CHREC_LEFT (chrec
)))
1116 switch (TREE_CODE (CHREC_RIGHT (chrec
)))
1118 case POLYNOMIAL_CHREC
:
1119 if (CHREC_VARIABLE (chrec
) != CHREC_VARIABLE (CHREC_RIGHT (chrec
)))
1121 if (!evolution_function_is_univariate_p (CHREC_RIGHT (chrec
)))
1134 /* Returns the number of variables of CHREC. Example: the call
1135 nb_vars_in_chrec ({{0, +, 1}_5, +, 2}_6) returns 2. */
1138 nb_vars_in_chrec (tree chrec
)
1140 if (chrec
== NULL_TREE
)
1143 switch (TREE_CODE (chrec
))
1145 case POLYNOMIAL_CHREC
:
1146 return 1 + nb_vars_in_chrec
1147 (initial_condition_in_loop_num (chrec
, CHREC_VARIABLE (chrec
)));
1154 static tree
chrec_convert_1 (tree
, tree
, gimple
, bool);
1156 /* Converts BASE and STEP of affine scev to TYPE. LOOP is the loop whose iv
1157 the scev corresponds to. AT_STMT is the statement at that the scev is
1158 evaluated. USE_OVERFLOW_SEMANTICS is true if this function should assume that
1159 the rules for overflow of the given language apply (e.g., that signed
1160 arithmetics in C does not overflow) -- i.e., to use them to avoid unnecessary
1161 tests, but also to enforce that the result follows them. Returns true if the
1162 conversion succeeded, false otherwise. */
1165 convert_affine_scev (struct loop
*loop
, tree type
,
1166 tree
*base
, tree
*step
, gimple at_stmt
,
1167 bool use_overflow_semantics
)
1169 tree ct
= TREE_TYPE (*step
);
1170 bool enforce_overflow_semantics
;
1171 bool must_check_src_overflow
, must_check_rslt_overflow
;
1172 tree new_base
, new_step
;
1173 tree step_type
= POINTER_TYPE_P (type
) ? sizetype
: type
;
1176 (TYPE) (BASE + STEP * i) = (TYPE) BASE + (TYPE -- sign extend) STEP * i,
1177 but we must check some assumptions.
1179 1) If [BASE, +, STEP] wraps, the equation is not valid when precision
1180 of CT is smaller than the precision of TYPE. For example, when we
1181 cast unsigned char [254, +, 1] to unsigned, the values on left side
1182 are 254, 255, 0, 1, ..., but those on the right side are
1183 254, 255, 256, 257, ...
1184 2) In case that we must also preserve the fact that signed ivs do not
1185 overflow, we must additionally check that the new iv does not wrap.
1186 For example, unsigned char [125, +, 1] casted to signed char could
1187 become a wrapping variable with values 125, 126, 127, -128, -127, ...,
1188 which would confuse optimizers that assume that this does not
1190 must_check_src_overflow
= TYPE_PRECISION (ct
) < TYPE_PRECISION (type
);
1192 enforce_overflow_semantics
= (use_overflow_semantics
1193 && nowrap_type_p (type
));
1194 if (enforce_overflow_semantics
)
1196 /* We can avoid checking whether the result overflows in the following
1199 -- must_check_src_overflow is true, and the range of TYPE is superset
1200 of the range of CT -- i.e., in all cases except if CT signed and
1202 -- both CT and TYPE have the same precision and signedness, and we
1203 verify instead that the source does not overflow (this may be
1204 easier than verifying it for the result, as we may use the
1205 information about the semantics of overflow in CT). */
1206 if (must_check_src_overflow
)
1208 if (TYPE_UNSIGNED (type
) && !TYPE_UNSIGNED (ct
))
1209 must_check_rslt_overflow
= true;
1211 must_check_rslt_overflow
= false;
1213 else if (TYPE_UNSIGNED (ct
) == TYPE_UNSIGNED (type
)
1214 && TYPE_PRECISION (ct
) == TYPE_PRECISION (type
))
1216 must_check_rslt_overflow
= false;
1217 must_check_src_overflow
= true;
1220 must_check_rslt_overflow
= true;
1223 must_check_rslt_overflow
= false;
1225 if (must_check_src_overflow
1226 && scev_probably_wraps_p (*base
, *step
, at_stmt
, loop
,
1227 use_overflow_semantics
))
1230 new_base
= chrec_convert_1 (type
, *base
, at_stmt
,
1231 use_overflow_semantics
);
1232 /* The step must be sign extended, regardless of the signedness
1233 of CT and TYPE. This only needs to be handled specially when
1234 CT is unsigned -- to avoid e.g. unsigned char [100, +, 255]
1235 (with values 100, 99, 98, ...) from becoming signed or unsigned
1236 [100, +, 255] with values 100, 355, ...; the sign-extension is
1237 performed by default when CT is signed. */
1239 if (TYPE_PRECISION (step_type
) > TYPE_PRECISION (ct
) && TYPE_UNSIGNED (ct
))
1241 tree signed_ct
= build_nonstandard_integer_type (TYPE_PRECISION (ct
), 0);
1242 new_step
= chrec_convert_1 (signed_ct
, new_step
, at_stmt
,
1243 use_overflow_semantics
);
1245 new_step
= chrec_convert_1 (step_type
, new_step
, at_stmt
, use_overflow_semantics
);
1247 if (automatically_generated_chrec_p (new_base
)
1248 || automatically_generated_chrec_p (new_step
))
1251 if (must_check_rslt_overflow
1252 /* Note that in this case we cannot use the fact that signed variables
1253 do not overflow, as this is what we are verifying for the new iv. */
1254 && scev_probably_wraps_p (new_base
, new_step
, at_stmt
, loop
, false))
1263 /* Convert CHREC for the right hand side of a CHREC.
1264 The increment for a pointer type is always sizetype. */
1267 chrec_convert_rhs (tree type
, tree chrec
, gimple at_stmt
)
1269 if (POINTER_TYPE_P (type
))
1272 return chrec_convert (type
, chrec
, at_stmt
);
1275 /* Convert CHREC to TYPE. When the analyzer knows the context in
1276 which the CHREC is built, it sets AT_STMT to the statement that
1277 contains the definition of the analyzed variable, otherwise the
1278 conversion is less accurate: the information is used for
1279 determining a more accurate estimation of the number of iterations.
1280 By default AT_STMT could be safely set to NULL_TREE.
1282 The following rule is always true: TREE_TYPE (chrec) ==
1283 TREE_TYPE (CHREC_LEFT (chrec)) == TREE_TYPE (CHREC_RIGHT (chrec)).
1284 An example of what could happen when adding two chrecs and the type
1285 of the CHREC_RIGHT is different than CHREC_LEFT is:
1287 {(uint) 0, +, (uchar) 10} +
1288 {(uint) 0, +, (uchar) 250}
1290 that would produce a wrong result if CHREC_RIGHT is not (uint):
1292 {(uint) 0, +, (uchar) 4}
1296 {(uint) 0, +, (uint) 260}
1300 chrec_convert (tree type
, tree chrec
, gimple at_stmt
)
1302 return chrec_convert_1 (type
, chrec
, at_stmt
, true);
1305 /* Convert CHREC to TYPE. When the analyzer knows the context in
1306 which the CHREC is built, it sets AT_STMT to the statement that
1307 contains the definition of the analyzed variable, otherwise the
1308 conversion is less accurate: the information is used for
1309 determining a more accurate estimation of the number of iterations.
1310 By default AT_STMT could be safely set to NULL_TREE.
1312 USE_OVERFLOW_SEMANTICS is true if this function should assume that
1313 the rules for overflow of the given language apply (e.g., that signed
1314 arithmetics in C does not overflow) -- i.e., to use them to avoid unnecessary
1315 tests, but also to enforce that the result follows them. */
1318 chrec_convert_1 (tree type
, tree chrec
, gimple at_stmt
,
1319 bool use_overflow_semantics
)
1325 if (automatically_generated_chrec_p (chrec
))
1328 ct
= chrec_type (chrec
);
1332 if (!evolution_function_is_affine_p (chrec
))
1335 loop
= get_chrec_loop (chrec
);
1336 base
= CHREC_LEFT (chrec
);
1337 step
= CHREC_RIGHT (chrec
);
1339 if (convert_affine_scev (loop
, type
, &base
, &step
, at_stmt
,
1340 use_overflow_semantics
))
1341 return build_polynomial_chrec (loop
->num
, base
, step
);
1343 /* If we cannot propagate the cast inside the chrec, just keep the cast. */
1345 /* Fold will not canonicalize (long)(i - 1) to (long)i - 1 because that
1346 may be more expensive. We do want to perform this optimization here
1347 though for canonicalization reasons. */
1348 if (use_overflow_semantics
1349 && (TREE_CODE (chrec
) == PLUS_EXPR
1350 || TREE_CODE (chrec
) == MINUS_EXPR
)
1351 && TREE_CODE (type
) == INTEGER_TYPE
1352 && TREE_CODE (ct
) == INTEGER_TYPE
1353 && TYPE_PRECISION (type
) > TYPE_PRECISION (ct
)
1354 && TYPE_OVERFLOW_UNDEFINED (ct
))
1355 res
= fold_build2 (TREE_CODE (chrec
), type
,
1356 fold_convert (type
, TREE_OPERAND (chrec
, 0)),
1357 fold_convert (type
, TREE_OPERAND (chrec
, 1)));
1359 res
= fold_convert (type
, chrec
);
1361 /* Don't propagate overflows. */
1362 if (CONSTANT_CLASS_P (res
))
1363 TREE_OVERFLOW (res
) = 0;
1365 /* But reject constants that don't fit in their type after conversion.
1366 This can happen if TYPE_MIN_VALUE or TYPE_MAX_VALUE are not the
1367 natural values associated with TYPE_PRECISION and TYPE_UNSIGNED,
1368 and can cause problems later when computing niters of loops. Note
1369 that we don't do the check before converting because we don't want
1370 to reject conversions of negative chrecs to unsigned types. */
1371 if (TREE_CODE (res
) == INTEGER_CST
1372 && TREE_CODE (type
) == INTEGER_TYPE
1373 && !int_fits_type_p (res
, type
))
1374 res
= chrec_dont_know
;
1379 /* Convert CHREC to TYPE, without regard to signed overflows. Returns the new
1380 chrec if something else than what chrec_convert would do happens, NULL_TREE
1384 chrec_convert_aggressive (tree type
, tree chrec
)
1386 tree inner_type
, left
, right
, lc
, rc
, rtype
;
1388 if (automatically_generated_chrec_p (chrec
)
1389 || TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
1392 inner_type
= TREE_TYPE (chrec
);
1393 if (TYPE_PRECISION (type
) > TYPE_PRECISION (inner_type
))
1396 rtype
= POINTER_TYPE_P (type
) ? sizetype
: type
;
1398 left
= CHREC_LEFT (chrec
);
1399 right
= CHREC_RIGHT (chrec
);
1400 lc
= chrec_convert_aggressive (type
, left
);
1402 lc
= chrec_convert (type
, left
, NULL
);
1403 rc
= chrec_convert_aggressive (rtype
, right
);
1405 rc
= chrec_convert (rtype
, right
, NULL
);
1407 return build_polynomial_chrec (CHREC_VARIABLE (chrec
), lc
, rc
);
1410 /* Returns true when CHREC0 == CHREC1. */
1413 eq_evolutions_p (const_tree chrec0
, const_tree chrec1
)
1415 if (chrec0
== NULL_TREE
1416 || chrec1
== NULL_TREE
1417 || TREE_CODE (chrec0
) != TREE_CODE (chrec1
))
1420 if (chrec0
== chrec1
)
1423 switch (TREE_CODE (chrec0
))
1426 return operand_equal_p (chrec0
, chrec1
, 0);
1428 case POLYNOMIAL_CHREC
:
1429 return (CHREC_VARIABLE (chrec0
) == CHREC_VARIABLE (chrec1
)
1430 && eq_evolutions_p (CHREC_LEFT (chrec0
), CHREC_LEFT (chrec1
))
1431 && eq_evolutions_p (CHREC_RIGHT (chrec0
), CHREC_RIGHT (chrec1
)));
1436 case POINTER_PLUS_EXPR
:
1437 return eq_evolutions_p (TREE_OPERAND (chrec0
, 0),
1438 TREE_OPERAND (chrec1
, 0))
1439 && eq_evolutions_p (TREE_OPERAND (chrec0
, 1),
1440 TREE_OPERAND (chrec1
, 1));
1447 /* Returns EV_GROWS if CHREC grows (assuming that it does not overflow),
1448 EV_DECREASES if it decreases, and EV_UNKNOWN if we cannot determine
1449 which of these cases happens. */
1452 scev_direction (const_tree chrec
)
1456 if (!evolution_function_is_affine_p (chrec
))
1457 return EV_DIR_UNKNOWN
;
1459 step
= CHREC_RIGHT (chrec
);
1460 if (TREE_CODE (step
) != INTEGER_CST
)
1461 return EV_DIR_UNKNOWN
;
1463 if (tree_int_cst_sign_bit (step
))
1464 return EV_DIR_DECREASES
;
1466 return EV_DIR_GROWS
;
1469 /* Iterates over all the components of SCEV, and calls CBCK. */
1472 for_each_scev_op (tree
*scev
, bool (*cbck
) (tree
*, void *), void *data
)
1474 switch (TREE_CODE_LENGTH (TREE_CODE (*scev
)))
1477 for_each_scev_op (&TREE_OPERAND (*scev
, 2), cbck
, data
);
1480 for_each_scev_op (&TREE_OPERAND (*scev
, 1), cbck
, data
);
1483 for_each_scev_op (&TREE_OPERAND (*scev
, 0), cbck
, data
);
1491 /* Returns true when the operation can be part of a linear
1495 operator_is_linear (tree scev
)
1497 switch (TREE_CODE (scev
))
1500 case POLYNOMIAL_CHREC
:
1502 case POINTER_PLUS_EXPR
:
1507 case NON_LVALUE_EXPR
:
1517 /* Return true when SCEV is a linear expression. Linear expressions
1518 can contain additions, substractions and multiplications.
1519 Multiplications are restricted to constant scaling: "cst * x". */
1522 scev_is_linear_expression (tree scev
)
1525 || !operator_is_linear (scev
))
1528 if (TREE_CODE (scev
) == MULT_EXPR
)
1529 return !(tree_contains_chrecs (TREE_OPERAND (scev
, 0), NULL
)
1530 && tree_contains_chrecs (TREE_OPERAND (scev
, 1), NULL
));
1532 if (TREE_CODE (scev
) == POLYNOMIAL_CHREC
1533 && !evolution_function_is_affine_multivariate_p (scev
, CHREC_VARIABLE (scev
)))
1536 switch (TREE_CODE_LENGTH (TREE_CODE (scev
)))
1539 return scev_is_linear_expression (TREE_OPERAND (scev
, 0))
1540 && scev_is_linear_expression (TREE_OPERAND (scev
, 1))
1541 && scev_is_linear_expression (TREE_OPERAND (scev
, 2));
1544 return scev_is_linear_expression (TREE_OPERAND (scev
, 0))
1545 && scev_is_linear_expression (TREE_OPERAND (scev
, 1));
1548 return scev_is_linear_expression (TREE_OPERAND (scev
, 0));
1558 /* Determines whether the expression CHREC contains only interger consts
1559 in the right parts. */
1562 evolution_function_right_is_integer_cst (const_tree chrec
)
1564 if (chrec
== NULL_TREE
)
1567 switch (TREE_CODE (chrec
))
1572 case POLYNOMIAL_CHREC
:
1573 return TREE_CODE (CHREC_RIGHT (chrec
)) == INTEGER_CST
1574 && (TREE_CODE (CHREC_LEFT (chrec
)) != POLYNOMIAL_CHREC
1575 || evolution_function_right_is_integer_cst (CHREC_LEFT (chrec
)));
1578 return evolution_function_right_is_integer_cst (TREE_OPERAND (chrec
, 0));