1 /* Chains of recurrences.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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"
34 #include "diagnostic.h"
36 #include "tree-flow.h"
37 #include "tree-chrec.h"
38 #include "tree-pass.h"
41 #include "tree-scalar-evolution.h"
45 /* Extended folder for chrecs. */
47 /* Determines whether CST is not a constant evolution. */
50 is_not_constant_evolution (const_tree cst
)
52 return (TREE_CODE (cst
) == POLYNOMIAL_CHREC
);
55 /* Fold CODE for a polynomial function and a constant. */
58 chrec_fold_poly_cst (enum tree_code code
,
65 gcc_assert (TREE_CODE (poly
) == POLYNOMIAL_CHREC
);
66 gcc_assert (!is_not_constant_evolution (cst
));
67 gcc_assert (type
== chrec_type (poly
));
72 return build_polynomial_chrec
73 (CHREC_VARIABLE (poly
),
74 chrec_fold_plus (type
, CHREC_LEFT (poly
), cst
),
78 return build_polynomial_chrec
79 (CHREC_VARIABLE (poly
),
80 chrec_fold_minus (type
, CHREC_LEFT (poly
), cst
),
84 return build_polynomial_chrec
85 (CHREC_VARIABLE (poly
),
86 chrec_fold_multiply (type
, CHREC_LEFT (poly
), cst
),
87 chrec_fold_multiply (type
, CHREC_RIGHT (poly
), cst
));
90 return chrec_dont_know
;
94 /* Fold the addition of two polynomial functions. */
97 chrec_fold_plus_poly_poly (enum tree_code code
,
103 struct loop
*loop0
= get_chrec_loop (poly0
);
104 struct loop
*loop1
= get_chrec_loop (poly1
);
105 tree rtype
= code
== POINTER_PLUS_EXPR
? sizetype
: type
;
109 gcc_assert (TREE_CODE (poly0
) == POLYNOMIAL_CHREC
);
110 gcc_assert (TREE_CODE (poly1
) == POLYNOMIAL_CHREC
);
111 if (POINTER_TYPE_P (chrec_type (poly0
)))
112 gcc_assert (chrec_type (poly1
) == sizetype
);
114 gcc_assert (chrec_type (poly0
) == chrec_type (poly1
));
115 gcc_assert (type
== chrec_type (poly0
));
118 {a, +, b}_1 + {c, +, d}_2 -> {{a, +, b}_1 + c, +, d}_2,
119 {a, +, b}_2 + {c, +, d}_1 -> {{c, +, d}_1 + a, +, b}_2,
120 {a, +, b}_x + {c, +, d}_x -> {a+c, +, b+d}_x. */
121 if (flow_loop_nested_p (loop0
, loop1
))
123 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
124 return build_polynomial_chrec
125 (CHREC_VARIABLE (poly1
),
126 chrec_fold_plus (type
, poly0
, CHREC_LEFT (poly1
)),
127 CHREC_RIGHT (poly1
));
129 return build_polynomial_chrec
130 (CHREC_VARIABLE (poly1
),
131 chrec_fold_minus (type
, poly0
, CHREC_LEFT (poly1
)),
132 chrec_fold_multiply (type
, CHREC_RIGHT (poly1
),
133 SCALAR_FLOAT_TYPE_P (type
)
134 ? build_real (type
, dconstm1
)
135 : build_int_cst_type (type
, -1)));
138 if (flow_loop_nested_p (loop1
, loop0
))
140 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
141 return build_polynomial_chrec
142 (CHREC_VARIABLE (poly0
),
143 chrec_fold_plus (type
, CHREC_LEFT (poly0
), poly1
),
144 CHREC_RIGHT (poly0
));
146 return build_polynomial_chrec
147 (CHREC_VARIABLE (poly0
),
148 chrec_fold_minus (type
, CHREC_LEFT (poly0
), poly1
),
149 CHREC_RIGHT (poly0
));
152 /* This function should never be called for chrecs of loops that
153 do not belong to the same loop nest. */
154 gcc_assert (loop0
== loop1
);
156 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
158 left
= chrec_fold_plus
159 (type
, CHREC_LEFT (poly0
), CHREC_LEFT (poly1
));
160 right
= chrec_fold_plus
161 (rtype
, CHREC_RIGHT (poly0
), CHREC_RIGHT (poly1
));
165 left
= chrec_fold_minus
166 (type
, CHREC_LEFT (poly0
), CHREC_LEFT (poly1
));
167 right
= chrec_fold_minus
168 (type
, CHREC_RIGHT (poly0
), CHREC_RIGHT (poly1
));
171 if (chrec_zerop (right
))
174 return build_polynomial_chrec
175 (CHREC_VARIABLE (poly0
), left
, right
);
180 /* Fold the multiplication of two polynomial functions. */
183 chrec_fold_multiply_poly_poly (tree type
,
189 struct loop
*loop0
= get_chrec_loop (poly0
);
190 struct loop
*loop1
= get_chrec_loop (poly1
);
194 gcc_assert (TREE_CODE (poly0
) == POLYNOMIAL_CHREC
);
195 gcc_assert (TREE_CODE (poly1
) == POLYNOMIAL_CHREC
);
196 gcc_assert (chrec_type (poly0
) == chrec_type (poly1
));
197 gcc_assert (type
== chrec_type (poly0
));
199 /* {a, +, b}_1 * {c, +, d}_2 -> {c*{a, +, b}_1, +, d}_2,
200 {a, +, b}_2 * {c, +, d}_1 -> {a*{c, +, d}_1, +, b}_2,
201 {a, +, b}_x * {c, +, d}_x -> {a*c, +, a*d + b*c + b*d, +, 2*b*d}_x. */
202 if (flow_loop_nested_p (loop0
, loop1
))
203 /* poly0 is a constant wrt. poly1. */
204 return build_polynomial_chrec
205 (CHREC_VARIABLE (poly1
),
206 chrec_fold_multiply (type
, CHREC_LEFT (poly1
), poly0
),
207 CHREC_RIGHT (poly1
));
209 if (flow_loop_nested_p (loop1
, loop0
))
210 /* poly1 is a constant wrt. poly0. */
211 return build_polynomial_chrec
212 (CHREC_VARIABLE (poly0
),
213 chrec_fold_multiply (type
, CHREC_LEFT (poly0
), poly1
),
214 CHREC_RIGHT (poly0
));
216 gcc_assert (loop0
== loop1
);
218 /* poly0 and poly1 are two polynomials in the same variable,
219 {a, +, b}_x * {c, +, d}_x -> {a*c, +, a*d + b*c + b*d, +, 2*b*d}_x. */
222 t0
= chrec_fold_multiply (type
, CHREC_LEFT (poly0
), CHREC_LEFT (poly1
));
225 t1
= chrec_fold_multiply (type
, CHREC_LEFT (poly0
), CHREC_RIGHT (poly1
));
226 t1
= chrec_fold_plus (type
, t1
, chrec_fold_multiply (type
,
228 CHREC_LEFT (poly1
)));
230 t2
= chrec_fold_multiply (type
, CHREC_RIGHT (poly0
), CHREC_RIGHT (poly1
));
231 /* "a*d + b*c + b*d". */
232 t1
= chrec_fold_plus (type
, t1
, t2
);
234 t2
= chrec_fold_multiply (type
, SCALAR_FLOAT_TYPE_P (type
)
235 ? build_real (type
, dconst2
)
236 : build_int_cst (type
, 2), t2
);
238 var
= CHREC_VARIABLE (poly0
);
239 return build_polynomial_chrec (var
, t0
,
240 build_polynomial_chrec (var
, t1
, t2
));
243 /* When the operands are automatically_generated_chrec_p, the fold has
244 to respect the semantics of the operands. */
247 chrec_fold_automatically_generated_operands (tree op0
,
250 if (op0
== chrec_dont_know
251 || op1
== chrec_dont_know
)
252 return chrec_dont_know
;
254 if (op0
== chrec_known
255 || op1
== chrec_known
)
258 if (op0
== chrec_not_analyzed_yet
259 || op1
== chrec_not_analyzed_yet
)
260 return chrec_not_analyzed_yet
;
262 /* The default case produces a safe result. */
263 return chrec_dont_know
;
266 /* Fold the addition of two chrecs. */
269 chrec_fold_plus_1 (enum tree_code code
, tree type
,
272 tree op1_type
= code
== POINTER_PLUS_EXPR
? sizetype
: type
;
274 if (automatically_generated_chrec_p (op0
)
275 || automatically_generated_chrec_p (op1
))
276 return chrec_fold_automatically_generated_operands (op0
, op1
);
278 switch (TREE_CODE (op0
))
280 case POLYNOMIAL_CHREC
:
281 switch (TREE_CODE (op1
))
283 case POLYNOMIAL_CHREC
:
284 return chrec_fold_plus_poly_poly (code
, type
, op0
, op1
);
287 if (tree_contains_chrecs (op1
, NULL
))
288 return chrec_dont_know
;
291 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
292 return build_polynomial_chrec
293 (CHREC_VARIABLE (op0
),
294 chrec_fold_plus (type
, CHREC_LEFT (op0
), op1
),
297 return build_polynomial_chrec
298 (CHREC_VARIABLE (op0
),
299 chrec_fold_minus (type
, CHREC_LEFT (op0
), op1
),
304 if (tree_contains_chrecs (op0
, NULL
))
305 return chrec_dont_know
;
308 switch (TREE_CODE (op1
))
310 case POLYNOMIAL_CHREC
:
311 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
312 return build_polynomial_chrec
313 (CHREC_VARIABLE (op1
),
314 chrec_fold_plus (type
, op0
, CHREC_LEFT (op1
)),
317 return build_polynomial_chrec
318 (CHREC_VARIABLE (op1
),
319 chrec_fold_minus (type
, op0
, CHREC_LEFT (op1
)),
320 chrec_fold_multiply (type
, CHREC_RIGHT (op1
),
321 SCALAR_FLOAT_TYPE_P (type
)
322 ? build_real (type
, dconstm1
)
323 : build_int_cst_type (type
, -1)));
326 if (tree_contains_chrecs (op1
, NULL
))
327 return chrec_dont_know
;
332 if ((tree_contains_chrecs (op0
, &size
)
333 || tree_contains_chrecs (op1
, &size
))
334 && size
< PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE
))
335 return build2 (code
, type
, op0
, op1
);
336 else if (size
< PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE
))
337 return fold_build2 (code
, type
,
338 fold_convert (type
, op0
),
339 fold_convert (op1_type
, op1
));
341 return chrec_dont_know
;
347 /* Fold the addition of two chrecs. */
350 chrec_fold_plus (tree type
,
355 if (automatically_generated_chrec_p (op0
)
356 || automatically_generated_chrec_p (op1
))
357 return chrec_fold_automatically_generated_operands (op0
, op1
);
359 if (integer_zerop (op0
))
360 return chrec_convert (type
, op1
, NULL
);
361 if (integer_zerop (op1
))
362 return chrec_convert (type
, op0
, NULL
);
364 if (POINTER_TYPE_P (type
))
365 code
= POINTER_PLUS_EXPR
;
369 return chrec_fold_plus_1 (code
, type
, op0
, op1
);
372 /* Fold the subtraction of two chrecs. */
375 chrec_fold_minus (tree type
,
379 if (automatically_generated_chrec_p (op0
)
380 || automatically_generated_chrec_p (op1
))
381 return chrec_fold_automatically_generated_operands (op0
, op1
);
383 if (integer_zerop (op1
))
386 return chrec_fold_plus_1 (MINUS_EXPR
, type
, op0
, op1
);
389 /* Fold the multiplication of two chrecs. */
392 chrec_fold_multiply (tree type
,
396 if (automatically_generated_chrec_p (op0
)
397 || automatically_generated_chrec_p (op1
))
398 return chrec_fold_automatically_generated_operands (op0
, op1
);
400 switch (TREE_CODE (op0
))
402 case POLYNOMIAL_CHREC
:
403 switch (TREE_CODE (op1
))
405 case POLYNOMIAL_CHREC
:
406 return chrec_fold_multiply_poly_poly (type
, op0
, op1
);
409 if (tree_contains_chrecs (op1
, NULL
))
410 return chrec_dont_know
;
413 if (integer_onep (op1
))
415 if (integer_zerop (op1
))
416 return build_int_cst (type
, 0);
418 return build_polynomial_chrec
419 (CHREC_VARIABLE (op0
),
420 chrec_fold_multiply (type
, CHREC_LEFT (op0
), op1
),
421 chrec_fold_multiply (type
, CHREC_RIGHT (op0
), op1
));
425 if (tree_contains_chrecs (op0
, NULL
))
426 return chrec_dont_know
;
429 if (integer_onep (op0
))
432 if (integer_zerop (op0
))
433 return build_int_cst (type
, 0);
435 switch (TREE_CODE (op1
))
437 case POLYNOMIAL_CHREC
:
438 return build_polynomial_chrec
439 (CHREC_VARIABLE (op1
),
440 chrec_fold_multiply (type
, CHREC_LEFT (op1
), op0
),
441 chrec_fold_multiply (type
, CHREC_RIGHT (op1
), op0
));
444 if (tree_contains_chrecs (op1
, NULL
))
445 return chrec_dont_know
;
448 if (integer_onep (op1
))
450 if (integer_zerop (op1
))
451 return build_int_cst (type
, 0);
452 return fold_build2 (MULT_EXPR
, type
, op0
, op1
);
461 /* Evaluate the binomial coefficient. Return NULL_TREE if the intermediate
462 calculation overflows, otherwise return C(n,k) with type TYPE. */
465 tree_fold_binomial (tree type
, tree n
, unsigned int k
)
467 unsigned HOST_WIDE_INT lidx
, lnum
, ldenom
, lres
, ldum
;
468 HOST_WIDE_INT hidx
, hnum
, hdenom
, hres
, hdum
;
472 /* Handle the most frequent cases. */
474 return build_int_cst (type
, 1);
476 return fold_convert (type
, n
);
478 /* Check that k <= n. */
479 if (TREE_INT_CST_HIGH (n
) == 0
480 && TREE_INT_CST_LOW (n
) < k
)
484 lnum
= TREE_INT_CST_LOW (n
);
485 hnum
= TREE_INT_CST_HIGH (n
);
487 /* Denominator = 2. */
491 /* Index = Numerator-1. */
495 lidx
= ~ (unsigned HOST_WIDE_INT
) 0;
503 /* Numerator = Numerator*Index = n*(n-1). */
504 if (mul_double (lnum
, hnum
, lidx
, hidx
, &lnum
, &hnum
))
507 for (i
= 3; i
<= k
; i
++)
513 lidx
= ~ (unsigned HOST_WIDE_INT
) 0;
518 /* Numerator *= Index. */
519 if (mul_double (lnum
, hnum
, lidx
, hidx
, &lnum
, &hnum
))
522 /* Denominator *= i. */
523 mul_double (ldenom
, hdenom
, i
, 0, &ldenom
, &hdenom
);
526 /* Result = Numerator / Denominator. */
527 div_and_round_double (EXACT_DIV_EXPR
, 1, lnum
, hnum
, ldenom
, hdenom
,
528 &lres
, &hres
, &ldum
, &hdum
);
530 res
= build_int_cst_wide (type
, lres
, hres
);
531 return int_fits_type_p (res
, type
) ? res
: NULL_TREE
;
534 /* Helper function. Use the Newton's interpolating formula for
535 evaluating the value of the evolution function. */
538 chrec_evaluate (unsigned var
, tree chrec
, tree n
, unsigned int k
)
540 tree arg0
, arg1
, binomial_n_k
;
541 tree type
= TREE_TYPE (chrec
);
542 struct loop
*var_loop
= get_loop (var
);
544 while (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
545 && flow_loop_nested_p (var_loop
, get_chrec_loop (chrec
)))
546 chrec
= CHREC_LEFT (chrec
);
548 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
549 && CHREC_VARIABLE (chrec
) == var
)
551 arg1
= chrec_evaluate (var
, CHREC_RIGHT (chrec
), n
, k
+ 1);
552 if (arg1
== chrec_dont_know
)
553 return chrec_dont_know
;
554 binomial_n_k
= tree_fold_binomial (type
, n
, k
);
556 return chrec_dont_know
;
557 arg0
= fold_build2 (MULT_EXPR
, type
,
558 CHREC_LEFT (chrec
), binomial_n_k
);
559 return chrec_fold_plus (type
, arg0
, arg1
);
562 binomial_n_k
= tree_fold_binomial (type
, n
, k
);
564 return chrec_dont_know
;
566 return fold_build2 (MULT_EXPR
, type
, chrec
, binomial_n_k
);
569 /* Evaluates "CHREC (X)" when the varying variable is VAR.
570 Example: Given the following parameters,
576 The result is given by the Newton's interpolating formula:
577 3 * \binom{10}{0} + 4 * \binom{10}{1}.
581 chrec_apply (unsigned var
,
585 tree type
= chrec_type (chrec
);
586 tree res
= chrec_dont_know
;
588 if (automatically_generated_chrec_p (chrec
)
589 || automatically_generated_chrec_p (x
)
591 /* When the symbols are defined in an outer loop, it is possible
592 to symbolically compute the apply, since the symbols are
593 constants with respect to the varying loop. */
594 || chrec_contains_symbols_defined_in_loop (chrec
, var
))
595 return chrec_dont_know
;
597 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
598 fprintf (dump_file
, "(chrec_apply \n");
600 if (TREE_CODE (x
) == INTEGER_CST
&& SCALAR_FLOAT_TYPE_P (type
))
601 x
= build_real_from_int_cst (type
, x
);
603 if (evolution_function_is_affine_p (chrec
))
605 /* "{a, +, b} (x)" -> "a + b*x". */
606 x
= chrec_convert_rhs (type
, x
, NULL
);
607 res
= chrec_fold_multiply (TREE_TYPE (x
), CHREC_RIGHT (chrec
), x
);
608 res
= chrec_fold_plus (type
, CHREC_LEFT (chrec
), res
);
611 else if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
614 else if (TREE_CODE (x
) == INTEGER_CST
615 && tree_int_cst_sgn (x
) == 1)
616 /* testsuite/.../ssa-chrec-38.c. */
617 res
= chrec_evaluate (var
, chrec
, x
, 0);
619 res
= chrec_dont_know
;
621 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
623 fprintf (dump_file
, " (varying_loop = %d\n", var
);
624 fprintf (dump_file
, ")\n (chrec = ");
625 print_generic_expr (dump_file
, chrec
, 0);
626 fprintf (dump_file
, ")\n (x = ");
627 print_generic_expr (dump_file
, x
, 0);
628 fprintf (dump_file
, ")\n (res = ");
629 print_generic_expr (dump_file
, res
, 0);
630 fprintf (dump_file
, "))\n");
636 /* Replaces the initial condition in CHREC with INIT_COND. */
639 chrec_replace_initial_condition (tree chrec
,
642 if (automatically_generated_chrec_p (chrec
))
645 gcc_assert (chrec_type (chrec
) == chrec_type (init_cond
));
647 switch (TREE_CODE (chrec
))
649 case POLYNOMIAL_CHREC
:
650 return build_polynomial_chrec
651 (CHREC_VARIABLE (chrec
),
652 chrec_replace_initial_condition (CHREC_LEFT (chrec
), init_cond
),
653 CHREC_RIGHT (chrec
));
660 /* Returns the initial condition of a given CHREC. */
663 initial_condition (tree chrec
)
665 if (automatically_generated_chrec_p (chrec
))
668 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
669 return initial_condition (CHREC_LEFT (chrec
));
674 /* Returns a univariate function that represents the evolution in
675 LOOP_NUM. Mask the evolution of any other loop. */
678 hide_evolution_in_other_loops_than_loop (tree chrec
,
681 struct loop
*loop
= get_loop (loop_num
), *chloop
;
682 if (automatically_generated_chrec_p (chrec
))
685 switch (TREE_CODE (chrec
))
687 case POLYNOMIAL_CHREC
:
688 chloop
= get_chrec_loop (chrec
);
691 return build_polynomial_chrec
693 hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec
),
695 CHREC_RIGHT (chrec
));
697 else if (flow_loop_nested_p (chloop
, loop
))
698 /* There is no evolution in this loop. */
699 return initial_condition (chrec
);
703 gcc_assert (flow_loop_nested_p (loop
, chloop
));
704 return hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec
),
713 /* Returns the evolution part of CHREC in LOOP_NUM when RIGHT is
714 true, otherwise returns the initial condition in LOOP_NUM. */
717 chrec_component_in_loop_num (tree chrec
,
722 struct loop
*loop
= get_loop (loop_num
), *chloop
;
724 if (automatically_generated_chrec_p (chrec
))
727 switch (TREE_CODE (chrec
))
729 case POLYNOMIAL_CHREC
:
730 chloop
= get_chrec_loop (chrec
);
735 component
= CHREC_RIGHT (chrec
);
737 component
= CHREC_LEFT (chrec
);
739 if (TREE_CODE (CHREC_LEFT (chrec
)) != POLYNOMIAL_CHREC
740 || CHREC_VARIABLE (CHREC_LEFT (chrec
)) != CHREC_VARIABLE (chrec
))
744 return build_polynomial_chrec
746 chrec_component_in_loop_num (CHREC_LEFT (chrec
),
752 else if (flow_loop_nested_p (chloop
, loop
))
753 /* There is no evolution part in this loop. */
758 gcc_assert (flow_loop_nested_p (loop
, chloop
));
759 return chrec_component_in_loop_num (CHREC_LEFT (chrec
),
772 /* Returns the evolution part in LOOP_NUM. Example: the call
773 evolution_part_in_loop_num ({{0, +, 1}_1, +, 2}_1, 1) returns
777 evolution_part_in_loop_num (tree chrec
,
780 return chrec_component_in_loop_num (chrec
, loop_num
, true);
783 /* Returns the initial condition in LOOP_NUM. Example: the call
784 initial_condition_in_loop_num ({{0, +, 1}_1, +, 2}_2, 2) returns
788 initial_condition_in_loop_num (tree chrec
,
791 return chrec_component_in_loop_num (chrec
, loop_num
, false);
794 /* Set or reset the evolution of CHREC to NEW_EVOL in loop LOOP_NUM.
795 This function is essentially used for setting the evolution to
796 chrec_dont_know, for example after having determined that it is
797 impossible to say how many times a loop will execute. */
800 reset_evolution_in_loop (unsigned loop_num
,
804 struct loop
*loop
= get_loop (loop_num
);
806 if (POINTER_TYPE_P (chrec_type (chrec
)))
807 gcc_assert (sizetype
== chrec_type (new_evol
));
809 gcc_assert (chrec_type (chrec
) == chrec_type (new_evol
));
811 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
812 && flow_loop_nested_p (loop
, get_chrec_loop (chrec
)))
814 tree left
= reset_evolution_in_loop (loop_num
, CHREC_LEFT (chrec
),
816 tree right
= reset_evolution_in_loop (loop_num
, CHREC_RIGHT (chrec
),
818 return build3 (POLYNOMIAL_CHREC
, TREE_TYPE (left
),
819 build_int_cst (NULL_TREE
, CHREC_VARIABLE (chrec
)),
823 while (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
824 && CHREC_VARIABLE (chrec
) == loop_num
)
825 chrec
= CHREC_LEFT (chrec
);
827 return build_polynomial_chrec (loop_num
, chrec
, new_evol
);
830 /* Merges two evolution functions that were found by following two
831 alternate paths of a conditional expression. */
834 chrec_merge (tree chrec1
,
837 if (chrec1
== chrec_dont_know
838 || chrec2
== chrec_dont_know
)
839 return chrec_dont_know
;
841 if (chrec1
== chrec_known
842 || chrec2
== chrec_known
)
845 if (chrec1
== chrec_not_analyzed_yet
)
847 if (chrec2
== chrec_not_analyzed_yet
)
850 if (eq_evolutions_p (chrec1
, chrec2
))
853 return chrec_dont_know
;
860 /* Helper function for is_multivariate_chrec. */
863 is_multivariate_chrec_rec (const_tree chrec
, unsigned int rec_var
)
865 if (chrec
== NULL_TREE
)
868 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
870 if (CHREC_VARIABLE (chrec
) != rec_var
)
873 return (is_multivariate_chrec_rec (CHREC_LEFT (chrec
), rec_var
)
874 || is_multivariate_chrec_rec (CHREC_RIGHT (chrec
), rec_var
));
880 /* Determine whether the given chrec is multivariate or not. */
883 is_multivariate_chrec (const_tree chrec
)
885 if (chrec
== NULL_TREE
)
888 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
889 return (is_multivariate_chrec_rec (CHREC_LEFT (chrec
),
890 CHREC_VARIABLE (chrec
))
891 || is_multivariate_chrec_rec (CHREC_RIGHT (chrec
),
892 CHREC_VARIABLE (chrec
)));
897 /* Determines whether the chrec contains symbolic names or not. */
900 chrec_contains_symbols (const_tree chrec
)
904 if (chrec
== NULL_TREE
)
907 if (TREE_CODE (chrec
) == SSA_NAME
908 || TREE_CODE (chrec
) == VAR_DECL
909 || TREE_CODE (chrec
) == PARM_DECL
910 || TREE_CODE (chrec
) == FUNCTION_DECL
911 || TREE_CODE (chrec
) == LABEL_DECL
912 || TREE_CODE (chrec
) == RESULT_DECL
913 || TREE_CODE (chrec
) == FIELD_DECL
)
916 n
= TREE_OPERAND_LENGTH (chrec
);
917 for (i
= 0; i
< n
; i
++)
918 if (chrec_contains_symbols (TREE_OPERAND (chrec
, i
)))
923 /* Determines whether the chrec contains undetermined coefficients. */
926 chrec_contains_undetermined (const_tree chrec
)
930 if (chrec
== chrec_dont_know
)
933 if (chrec
== NULL_TREE
)
936 n
= TREE_OPERAND_LENGTH (chrec
);
937 for (i
= 0; i
< n
; i
++)
938 if (chrec_contains_undetermined (TREE_OPERAND (chrec
, i
)))
943 /* Determines whether the tree EXPR contains chrecs, and increment
944 SIZE if it is not a NULL pointer by an estimation of the depth of
948 tree_contains_chrecs (const_tree expr
, int *size
)
952 if (expr
== NULL_TREE
)
958 if (tree_is_chrec (expr
))
961 n
= TREE_OPERAND_LENGTH (expr
);
962 for (i
= 0; i
< n
; i
++)
963 if (tree_contains_chrecs (TREE_OPERAND (expr
, i
), size
))
968 /* Recursive helper function. */
971 evolution_function_is_invariant_rec_p (tree chrec
, int loopnum
)
973 if (evolution_function_is_constant_p (chrec
))
976 if (TREE_CODE (chrec
) == SSA_NAME
978 || expr_invariant_in_loop_p (get_loop (loopnum
), chrec
)))
981 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
983 if (CHREC_VARIABLE (chrec
) == (unsigned) loopnum
984 || !evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec
),
986 || !evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec
),
992 switch (TREE_OPERAND_LENGTH (chrec
))
995 if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec
, 1),
1000 if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec
, 0),
1012 /* Return true if CHREC is invariant in loop LOOPNUM, false otherwise. */
1015 evolution_function_is_invariant_p (tree chrec
, int loopnum
)
1017 return evolution_function_is_invariant_rec_p (chrec
, loopnum
);
1020 /* Determine whether the given tree is an affine multivariate
1024 evolution_function_is_affine_multivariate_p (const_tree chrec
, int loopnum
)
1026 if (chrec
== NULL_TREE
)
1029 switch (TREE_CODE (chrec
))
1031 case POLYNOMIAL_CHREC
:
1032 if (evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec
), loopnum
))
1034 if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec
), loopnum
))
1038 if (TREE_CODE (CHREC_RIGHT (chrec
)) == POLYNOMIAL_CHREC
1039 && CHREC_VARIABLE (CHREC_RIGHT (chrec
))
1040 != CHREC_VARIABLE (chrec
)
1041 && evolution_function_is_affine_multivariate_p
1042 (CHREC_RIGHT (chrec
), loopnum
))
1050 if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec
), loopnum
)
1051 && TREE_CODE (CHREC_LEFT (chrec
)) == POLYNOMIAL_CHREC
1052 && CHREC_VARIABLE (CHREC_LEFT (chrec
)) != CHREC_VARIABLE (chrec
)
1053 && evolution_function_is_affine_multivariate_p
1054 (CHREC_LEFT (chrec
), loopnum
))
1065 /* Determine whether the given tree is a function in zero or one
1069 evolution_function_is_univariate_p (const_tree chrec
)
1071 if (chrec
== NULL_TREE
)
1074 switch (TREE_CODE (chrec
))
1076 case POLYNOMIAL_CHREC
:
1077 switch (TREE_CODE (CHREC_LEFT (chrec
)))
1079 case POLYNOMIAL_CHREC
:
1080 if (CHREC_VARIABLE (chrec
) != CHREC_VARIABLE (CHREC_LEFT (chrec
)))
1082 if (!evolution_function_is_univariate_p (CHREC_LEFT (chrec
)))
1090 switch (TREE_CODE (CHREC_RIGHT (chrec
)))
1092 case POLYNOMIAL_CHREC
:
1093 if (CHREC_VARIABLE (chrec
) != CHREC_VARIABLE (CHREC_RIGHT (chrec
)))
1095 if (!evolution_function_is_univariate_p (CHREC_RIGHT (chrec
)))
1108 /* Returns the number of variables of CHREC. Example: the call
1109 nb_vars_in_chrec ({{0, +, 1}_5, +, 2}_6) returns 2. */
1112 nb_vars_in_chrec (tree chrec
)
1114 if (chrec
== NULL_TREE
)
1117 switch (TREE_CODE (chrec
))
1119 case POLYNOMIAL_CHREC
:
1120 return 1 + nb_vars_in_chrec
1121 (initial_condition_in_loop_num (chrec
, CHREC_VARIABLE (chrec
)));
1128 static tree
chrec_convert_1 (tree
, tree
, gimple
, bool);
1130 /* Converts BASE and STEP of affine scev to TYPE. LOOP is the loop whose iv
1131 the scev corresponds to. AT_STMT is the statement at that the scev is
1132 evaluated. USE_OVERFLOW_SEMANTICS is true if this function should assume that
1133 the rules for overflow of the given language apply (e.g., that signed
1134 arithmetics in C does not overflow) -- i.e., to use them to avoid unnecessary
1135 tests, but also to enforce that the result follows them. Returns true if the
1136 conversion succeeded, false otherwise. */
1139 convert_affine_scev (struct loop
*loop
, tree type
,
1140 tree
*base
, tree
*step
, gimple at_stmt
,
1141 bool use_overflow_semantics
)
1143 tree ct
= TREE_TYPE (*step
);
1144 bool enforce_overflow_semantics
;
1145 bool must_check_src_overflow
, must_check_rslt_overflow
;
1146 tree new_base
, new_step
;
1147 tree step_type
= POINTER_TYPE_P (type
) ? sizetype
: type
;
1150 (TYPE) (BASE + STEP * i) = (TYPE) BASE + (TYPE -- sign extend) STEP * i,
1151 but we must check some assumptions.
1153 1) If [BASE, +, STEP] wraps, the equation is not valid when precision
1154 of CT is smaller than the precision of TYPE. For example, when we
1155 cast unsigned char [254, +, 1] to unsigned, the values on left side
1156 are 254, 255, 0, 1, ..., but those on the right side are
1157 254, 255, 256, 257, ...
1158 2) In case that we must also preserve the fact that signed ivs do not
1159 overflow, we must additionally check that the new iv does not wrap.
1160 For example, unsigned char [125, +, 1] casted to signed char could
1161 become a wrapping variable with values 125, 126, 127, -128, -127, ...,
1162 which would confuse optimizers that assume that this does not
1164 must_check_src_overflow
= TYPE_PRECISION (ct
) < TYPE_PRECISION (type
);
1166 enforce_overflow_semantics
= (use_overflow_semantics
1167 && nowrap_type_p (type
));
1168 if (enforce_overflow_semantics
)
1170 /* We can avoid checking whether the result overflows in the following
1173 -- must_check_src_overflow is true, and the range of TYPE is superset
1174 of the range of CT -- i.e., in all cases except if CT signed and
1176 -- both CT and TYPE have the same precision and signedness, and we
1177 verify instead that the source does not overflow (this may be
1178 easier than verifying it for the result, as we may use the
1179 information about the semantics of overflow in CT). */
1180 if (must_check_src_overflow
)
1182 if (TYPE_UNSIGNED (type
) && !TYPE_UNSIGNED (ct
))
1183 must_check_rslt_overflow
= true;
1185 must_check_rslt_overflow
= false;
1187 else if (TYPE_UNSIGNED (ct
) == TYPE_UNSIGNED (type
)
1188 && TYPE_PRECISION (ct
) == TYPE_PRECISION (type
))
1190 must_check_rslt_overflow
= false;
1191 must_check_src_overflow
= true;
1194 must_check_rslt_overflow
= true;
1197 must_check_rslt_overflow
= false;
1199 if (must_check_src_overflow
1200 && scev_probably_wraps_p (*base
, *step
, at_stmt
, loop
,
1201 use_overflow_semantics
))
1204 new_base
= chrec_convert_1 (type
, *base
, at_stmt
,
1205 use_overflow_semantics
);
1206 /* The step must be sign extended, regardless of the signedness
1207 of CT and TYPE. This only needs to be handled specially when
1208 CT is unsigned -- to avoid e.g. unsigned char [100, +, 255]
1209 (with values 100, 99, 98, ...) from becoming signed or unsigned
1210 [100, +, 255] with values 100, 355, ...; the sign-extension is
1211 performed by default when CT is signed. */
1213 if (TYPE_PRECISION (step_type
) > TYPE_PRECISION (ct
) && TYPE_UNSIGNED (ct
))
1214 new_step
= chrec_convert_1 (signed_type_for (ct
), new_step
, at_stmt
,
1215 use_overflow_semantics
);
1216 new_step
= chrec_convert_1 (step_type
, new_step
, at_stmt
, use_overflow_semantics
);
1218 if (automatically_generated_chrec_p (new_base
)
1219 || automatically_generated_chrec_p (new_step
))
1222 if (must_check_rslt_overflow
1223 /* Note that in this case we cannot use the fact that signed variables
1224 do not overflow, as this is what we are verifying for the new iv. */
1225 && scev_probably_wraps_p (new_base
, new_step
, at_stmt
, loop
, false))
1234 /* Convert CHREC for the right hand side of a CREC.
1235 The increment for a pointer type is always sizetype. */
1237 chrec_convert_rhs (tree type
, tree chrec
, gimple at_stmt
)
1239 if (POINTER_TYPE_P (type
))
1241 return chrec_convert (type
, chrec
, at_stmt
);
1244 /* Convert CHREC to TYPE. When the analyzer knows the context in
1245 which the CHREC is built, it sets AT_STMT to the statement that
1246 contains the definition of the analyzed variable, otherwise the
1247 conversion is less accurate: the information is used for
1248 determining a more accurate estimation of the number of iterations.
1249 By default AT_STMT could be safely set to NULL_TREE.
1251 The following rule is always true: TREE_TYPE (chrec) ==
1252 TREE_TYPE (CHREC_LEFT (chrec)) == TREE_TYPE (CHREC_RIGHT (chrec)).
1253 An example of what could happen when adding two chrecs and the type
1254 of the CHREC_RIGHT is different than CHREC_LEFT is:
1256 {(uint) 0, +, (uchar) 10} +
1257 {(uint) 0, +, (uchar) 250}
1259 that would produce a wrong result if CHREC_RIGHT is not (uint):
1261 {(uint) 0, +, (uchar) 4}
1265 {(uint) 0, +, (uint) 260}
1269 chrec_convert (tree type
, tree chrec
, gimple at_stmt
)
1271 return chrec_convert_1 (type
, chrec
, at_stmt
, true);
1274 /* Convert CHREC to TYPE. When the analyzer knows the context in
1275 which the CHREC is built, it sets AT_STMT to the statement that
1276 contains the definition of the analyzed variable, otherwise the
1277 conversion is less accurate: the information is used for
1278 determining a more accurate estimation of the number of iterations.
1279 By default AT_STMT could be safely set to NULL_TREE.
1281 USE_OVERFLOW_SEMANTICS is true if this function should assume that
1282 the rules for overflow of the given language apply (e.g., that signed
1283 arithmetics in C does not overflow) -- i.e., to use them to avoid unnecessary
1284 tests, but also to enforce that the result follows them. */
1287 chrec_convert_1 (tree type
, tree chrec
, gimple at_stmt
,
1288 bool use_overflow_semantics
)
1294 if (automatically_generated_chrec_p (chrec
))
1297 ct
= chrec_type (chrec
);
1301 if (!evolution_function_is_affine_p (chrec
))
1304 loop
= get_chrec_loop (chrec
);
1305 base
= CHREC_LEFT (chrec
);
1306 step
= CHREC_RIGHT (chrec
);
1308 if (convert_affine_scev (loop
, type
, &base
, &step
, at_stmt
,
1309 use_overflow_semantics
))
1310 return build_polynomial_chrec (loop
->num
, base
, step
);
1312 /* If we cannot propagate the cast inside the chrec, just keep the cast. */
1314 /* Fold will not canonicalize (long)(i - 1) to (long)i - 1 because that
1315 may be more expensive. We do want to perform this optimization here
1316 though for canonicalization reasons. */
1317 if (use_overflow_semantics
1318 && (TREE_CODE (chrec
) == PLUS_EXPR
1319 || TREE_CODE (chrec
) == MINUS_EXPR
)
1320 && TREE_CODE (type
) == INTEGER_TYPE
1321 && TREE_CODE (ct
) == INTEGER_TYPE
1322 && TYPE_PRECISION (type
) > TYPE_PRECISION (ct
)
1323 && TYPE_OVERFLOW_UNDEFINED (ct
))
1324 res
= fold_build2 (TREE_CODE (chrec
), type
,
1325 fold_convert (type
, TREE_OPERAND (chrec
, 0)),
1326 fold_convert (type
, TREE_OPERAND (chrec
, 1)));
1328 res
= fold_convert (type
, chrec
);
1330 /* Don't propagate overflows. */
1331 if (CONSTANT_CLASS_P (res
))
1332 TREE_OVERFLOW (res
) = 0;
1334 /* But reject constants that don't fit in their type after conversion.
1335 This can happen if TYPE_MIN_VALUE or TYPE_MAX_VALUE are not the
1336 natural values associated with TYPE_PRECISION and TYPE_UNSIGNED,
1337 and can cause problems later when computing niters of loops. Note
1338 that we don't do the check before converting because we don't want
1339 to reject conversions of negative chrecs to unsigned types. */
1340 if (TREE_CODE (res
) == INTEGER_CST
1341 && TREE_CODE (type
) == INTEGER_TYPE
1342 && !int_fits_type_p (res
, type
))
1343 res
= chrec_dont_know
;
1348 /* Convert CHREC to TYPE, without regard to signed overflows. Returns the new
1349 chrec if something else than what chrec_convert would do happens, NULL_TREE
1353 chrec_convert_aggressive (tree type
, tree chrec
)
1355 tree inner_type
, left
, right
, lc
, rc
, rtype
;
1357 if (automatically_generated_chrec_p (chrec
)
1358 || TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
1361 inner_type
= TREE_TYPE (chrec
);
1362 if (TYPE_PRECISION (type
) > TYPE_PRECISION (inner_type
))
1365 rtype
= POINTER_TYPE_P (type
) ? sizetype
: type
;
1367 left
= CHREC_LEFT (chrec
);
1368 right
= CHREC_RIGHT (chrec
);
1369 lc
= chrec_convert_aggressive (type
, left
);
1371 lc
= chrec_convert (type
, left
, NULL
);
1372 rc
= chrec_convert_aggressive (rtype
, right
);
1374 rc
= chrec_convert (rtype
, right
, NULL
);
1376 return build_polynomial_chrec (CHREC_VARIABLE (chrec
), lc
, rc
);
1379 /* Returns true when CHREC0 == CHREC1. */
1382 eq_evolutions_p (const_tree chrec0
, const_tree chrec1
)
1384 if (chrec0
== NULL_TREE
1385 || chrec1
== NULL_TREE
1386 || TREE_CODE (chrec0
) != TREE_CODE (chrec1
))
1389 if (chrec0
== chrec1
)
1392 switch (TREE_CODE (chrec0
))
1395 return operand_equal_p (chrec0
, chrec1
, 0);
1397 case POLYNOMIAL_CHREC
:
1398 return (CHREC_VARIABLE (chrec0
) == CHREC_VARIABLE (chrec1
)
1399 && eq_evolutions_p (CHREC_LEFT (chrec0
), CHREC_LEFT (chrec1
))
1400 && eq_evolutions_p (CHREC_RIGHT (chrec0
), CHREC_RIGHT (chrec1
)));
1406 /* Returns EV_GROWS if CHREC grows (assuming that it does not overflow),
1407 EV_DECREASES if it decreases, and EV_UNKNOWN if we cannot determine
1408 which of these cases happens. */
1411 scev_direction (const_tree chrec
)
1415 if (!evolution_function_is_affine_p (chrec
))
1416 return EV_DIR_UNKNOWN
;
1418 step
= CHREC_RIGHT (chrec
);
1419 if (TREE_CODE (step
) != INTEGER_CST
)
1420 return EV_DIR_UNKNOWN
;
1422 if (tree_int_cst_sign_bit (step
))
1423 return EV_DIR_DECREASES
;
1425 return EV_DIR_GROWS
;
1428 /* Iterates over all the components of SCEV, and calls CBCK. */
1431 for_each_scev_op (tree
*scev
, bool (*cbck
) (tree
*, void *), void *data
)
1433 switch (TREE_CODE_LENGTH (TREE_CODE (*scev
)))
1436 for_each_scev_op (&TREE_OPERAND (*scev
, 2), cbck
, data
);
1439 for_each_scev_op (&TREE_OPERAND (*scev
, 1), cbck
, data
);
1442 for_each_scev_op (&TREE_OPERAND (*scev
, 0), cbck
, data
);
1450 /* Returns true when the operation can be part of a linear
1454 operator_is_linear (tree scev
)
1456 switch (TREE_CODE (scev
))
1459 case POLYNOMIAL_CHREC
:
1461 case POINTER_PLUS_EXPR
:
1466 case NON_LVALUE_EXPR
:
1476 /* Return true when SCEV is a linear expression. Linear expressions
1477 can contain additions, substractions and multiplications.
1478 Multiplications are restricted to constant scaling: "cst * x". */
1481 scev_is_linear_expression (tree scev
)
1484 || !operator_is_linear (scev
))
1487 if (TREE_CODE (scev
) == MULT_EXPR
)
1488 return !(tree_contains_chrecs (TREE_OPERAND (scev
, 0), NULL
)
1489 && tree_contains_chrecs (TREE_OPERAND (scev
, 1), NULL
));
1491 if (TREE_CODE (scev
) == POLYNOMIAL_CHREC
1492 && !evolution_function_is_affine_multivariate_p (scev
, CHREC_VARIABLE (scev
)))
1495 switch (TREE_CODE_LENGTH (TREE_CODE (scev
)))
1498 return scev_is_linear_expression (TREE_OPERAND (scev
, 0))
1499 && scev_is_linear_expression (TREE_OPERAND (scev
, 1))
1500 && scev_is_linear_expression (TREE_OPERAND (scev
, 2));
1503 return scev_is_linear_expression (TREE_OPERAND (scev
, 0))
1504 && scev_is_linear_expression (TREE_OPERAND (scev
, 1));
1507 return scev_is_linear_expression (TREE_OPERAND (scev
, 0));
1517 /* Determines whether the expression CHREC contains only interger consts
1518 in the right parts. */
1521 evolution_function_right_is_integer_cst (const_tree chrec
)
1523 if (chrec
== NULL_TREE
)
1526 switch (TREE_CODE (chrec
))
1531 case POLYNOMIAL_CHREC
:
1532 return TREE_CODE (CHREC_RIGHT (chrec
)) == INTEGER_CST
1533 && (TREE_CODE (CHREC_LEFT (chrec
)) != POLYNOMIAL_CHREC
1534 || evolution_function_right_is_integer_cst (CHREC_LEFT (chrec
)));
1537 return evolution_function_right_is_integer_cst (TREE_OPERAND (chrec
, 0));