1 /* Chains of recurrences.
2 Copyright (C) 2003-2024 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
5 This file is part of GCC.
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
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
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/>. */
21 /* This file implements operations on chains of recurrences. Chains
22 of recurrences are used for modeling evolution functions of scalar
28 #include "coretypes.h"
31 #include "gimple-expr.h"
32 #include "tree-pretty-print.h"
33 #include "fold-const.h"
35 #include "tree-ssa-loop-ivopts.h"
36 #include "tree-ssa-loop-niter.h"
37 #include "tree-chrec.h"
39 #include "tree-ssa-loop.h"
41 #include "tree-scalar-evolution.h"
43 /* Extended folder for chrecs. */
45 /* Fold the addition of two polynomial functions. */
48 chrec_fold_plus_poly_poly (enum tree_code code
,
54 class loop
*loop0
= get_chrec_loop (poly0
);
55 class loop
*loop1
= get_chrec_loop (poly1
);
56 tree rtype
= code
== POINTER_PLUS_EXPR
? chrec_type (poly1
) : type
;
60 gcc_assert (TREE_CODE (poly0
) == POLYNOMIAL_CHREC
);
61 gcc_assert (TREE_CODE (poly1
) == POLYNOMIAL_CHREC
);
62 if (POINTER_TYPE_P (chrec_type (poly0
)))
63 gcc_checking_assert (ptrofftype_p (chrec_type (poly1
))
64 && useless_type_conversion_p (type
, chrec_type (poly0
)));
66 gcc_checking_assert (useless_type_conversion_p (type
, chrec_type (poly0
))
67 && useless_type_conversion_p (type
, chrec_type (poly1
)));
70 {a, +, b}_1 + {c, +, d}_2 -> {{a, +, b}_1 + c, +, d}_2,
71 {a, +, b}_2 + {c, +, d}_1 -> {{c, +, d}_1 + a, +, b}_2,
72 {a, +, b}_x + {c, +, d}_x -> {a+c, +, b+d}_x. */
73 if (flow_loop_nested_p (loop0
, loop1
))
75 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
76 return build_polynomial_chrec
77 (CHREC_VARIABLE (poly1
),
78 chrec_fold_plus (type
, poly0
, CHREC_LEFT (poly1
)),
81 return build_polynomial_chrec
82 (CHREC_VARIABLE (poly1
),
83 chrec_fold_minus (type
, poly0
, CHREC_LEFT (poly1
)),
84 chrec_fold_multiply (type
, CHREC_RIGHT (poly1
),
85 SCALAR_FLOAT_TYPE_P (type
)
86 ? build_real (type
, dconstm1
)
87 : build_int_cst_type (type
, -1)));
90 if (flow_loop_nested_p (loop1
, loop0
))
92 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
93 return build_polynomial_chrec
94 (CHREC_VARIABLE (poly0
),
95 chrec_fold_plus (type
, CHREC_LEFT (poly0
), poly1
),
98 return build_polynomial_chrec
99 (CHREC_VARIABLE (poly0
),
100 chrec_fold_minus (type
, CHREC_LEFT (poly0
), poly1
),
101 CHREC_RIGHT (poly0
));
104 /* This function should never be called for chrecs of loops that
105 do not belong to the same loop nest. */
108 /* It still can happen if we are not in loop-closed SSA form. */
109 gcc_assert (! loops_state_satisfies_p (LOOP_CLOSED_SSA
));
110 return chrec_dont_know
;
113 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
115 left
= chrec_fold_plus
116 (type
, CHREC_LEFT (poly0
), CHREC_LEFT (poly1
));
117 right
= chrec_fold_plus
118 (rtype
, CHREC_RIGHT (poly0
), CHREC_RIGHT (poly1
));
122 left
= chrec_fold_minus
123 (type
, CHREC_LEFT (poly0
), CHREC_LEFT (poly1
));
124 right
= chrec_fold_minus
125 (type
, CHREC_RIGHT (poly0
), CHREC_RIGHT (poly1
));
128 if (chrec_zerop (right
))
131 return build_polynomial_chrec
132 (CHREC_VARIABLE (poly0
), left
, right
);
137 /* Fold the multiplication of two polynomial functions. */
140 chrec_fold_multiply_poly_poly (tree type
,
146 class loop
*loop0
= get_chrec_loop (poly0
);
147 class loop
*loop1
= get_chrec_loop (poly1
);
151 gcc_assert (TREE_CODE (poly0
) == POLYNOMIAL_CHREC
);
152 gcc_assert (TREE_CODE (poly1
) == POLYNOMIAL_CHREC
);
153 gcc_checking_assert (useless_type_conversion_p (type
, chrec_type (poly0
))
154 && useless_type_conversion_p (type
, chrec_type (poly1
)));
156 /* {a, +, b}_1 * {c, +, d}_2 -> {c*{a, +, b}_1, +, d}_2,
157 {a, +, b}_2 * {c, +, d}_1 -> {a*{c, +, d}_1, +, b}_2,
158 {a, +, b}_x * {c, +, d}_x -> {a*c, +, a*d + b*c + b*d, +, 2*b*d}_x. */
159 if (flow_loop_nested_p (loop0
, loop1
))
160 /* poly0 is a constant wrt. poly1. */
161 return build_polynomial_chrec
162 (CHREC_VARIABLE (poly1
),
163 chrec_fold_multiply (type
, CHREC_LEFT (poly1
), poly0
),
164 CHREC_RIGHT (poly1
));
166 if (flow_loop_nested_p (loop1
, loop0
))
167 /* poly1 is a constant wrt. poly0. */
168 return build_polynomial_chrec
169 (CHREC_VARIABLE (poly0
),
170 chrec_fold_multiply (type
, CHREC_LEFT (poly0
), poly1
),
171 CHREC_RIGHT (poly0
));
175 /* It still can happen if we are not in loop-closed SSA form. */
176 gcc_assert (! loops_state_satisfies_p (LOOP_CLOSED_SSA
));
177 return chrec_dont_know
;
180 /* poly0 and poly1 are two polynomials in the same variable,
181 {a, +, b}_x * {c, +, d}_x -> {a*c, +, a*d + b*c + b*d, +, 2*b*d}_x. */
184 t0
= chrec_fold_multiply (type
, CHREC_LEFT (poly0
), CHREC_LEFT (poly1
));
187 t1
= chrec_fold_multiply (type
, CHREC_LEFT (poly0
), CHREC_RIGHT (poly1
));
188 t1
= chrec_fold_plus (type
, t1
, chrec_fold_multiply (type
,
190 CHREC_LEFT (poly1
)));
192 t2
= chrec_fold_multiply (type
, CHREC_RIGHT (poly0
), CHREC_RIGHT (poly1
));
193 /* "a*d + b*c + b*d". */
194 t1
= chrec_fold_plus (type
, t1
, t2
);
196 t2
= chrec_fold_multiply (type
, SCALAR_FLOAT_TYPE_P (type
)
197 ? build_real (type
, dconst2
)
198 : build_int_cst (type
, 2), t2
);
200 var
= CHREC_VARIABLE (poly0
);
201 return build_polynomial_chrec (var
, t0
,
202 build_polynomial_chrec (var
, t1
, t2
));
205 /* When the operands are automatically_generated_chrec_p, the fold has
206 to respect the semantics of the operands. */
209 chrec_fold_automatically_generated_operands (tree op0
,
212 if (op0
== chrec_dont_know
213 || op1
== chrec_dont_know
)
214 return chrec_dont_know
;
216 if (op0
== chrec_known
217 || op1
== chrec_known
)
220 if (op0
== chrec_not_analyzed_yet
221 || op1
== chrec_not_analyzed_yet
)
222 return chrec_not_analyzed_yet
;
224 /* The default case produces a safe result. */
225 return chrec_dont_know
;
228 /* Fold the addition of two chrecs. */
231 chrec_fold_plus_1 (enum tree_code code
, tree type
,
234 if (automatically_generated_chrec_p (op0
)
235 || automatically_generated_chrec_p (op1
))
236 return chrec_fold_automatically_generated_operands (op0
, op1
);
238 switch (TREE_CODE (op0
))
240 case POLYNOMIAL_CHREC
:
242 (!chrec_contains_symbols_defined_in_loop (op0
, CHREC_VARIABLE (op0
)));
243 switch (TREE_CODE (op1
))
245 case POLYNOMIAL_CHREC
:
247 (!chrec_contains_symbols_defined_in_loop (op1
,
248 CHREC_VARIABLE (op1
)));
249 return chrec_fold_plus_poly_poly (code
, type
, op0
, op1
);
252 if (tree_contains_chrecs (op1
, NULL
))
254 /* We can strip sign-conversions to signed by performing the
255 operation in unsigned. */
256 tree optype
= TREE_TYPE (TREE_OPERAND (op1
, 0));
257 if (INTEGRAL_TYPE_P (type
)
258 && INTEGRAL_TYPE_P (optype
)
259 && tree_nop_conversion_p (type
, optype
)
260 && TYPE_UNSIGNED (optype
))
262 tree tem
= chrec_convert (optype
, op0
, NULL
);
263 if (TREE_CODE (tem
) == POLYNOMIAL_CHREC
)
264 return chrec_convert (type
,
265 chrec_fold_plus_1 (code
, optype
,
271 return chrec_dont_know
;
276 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
277 return build_polynomial_chrec
278 (CHREC_VARIABLE (op0
),
279 chrec_fold_plus (type
, CHREC_LEFT (op0
), op1
),
282 return build_polynomial_chrec
283 (CHREC_VARIABLE (op0
),
284 chrec_fold_minus (type
, CHREC_LEFT (op0
), op1
),
289 if (tree_contains_chrecs (op0
, NULL
))
291 /* We can strip sign-conversions to signed by performing the
292 operation in unsigned. */
293 tree optype
= TREE_TYPE (TREE_OPERAND (op0
, 0));
294 if (INTEGRAL_TYPE_P (type
)
295 && INTEGRAL_TYPE_P (optype
)
296 && tree_nop_conversion_p (type
, optype
)
297 && TYPE_UNSIGNED (optype
))
298 return chrec_convert (type
,
299 chrec_fold_plus_1 (code
, optype
,
300 TREE_OPERAND (op0
, 0),
301 chrec_convert (optype
,
304 return chrec_dont_know
;
309 gcc_checking_assert (!tree_contains_chrecs (op0
, NULL
));
310 switch (TREE_CODE (op1
))
312 case POLYNOMIAL_CHREC
:
314 (!chrec_contains_symbols_defined_in_loop (op1
,
315 CHREC_VARIABLE (op1
)));
316 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
317 return build_polynomial_chrec
318 (CHREC_VARIABLE (op1
),
319 chrec_fold_plus (type
, op0
, CHREC_LEFT (op1
)),
322 return build_polynomial_chrec
323 (CHREC_VARIABLE (op1
),
324 chrec_fold_minus (type
, op0
, CHREC_LEFT (op1
)),
325 chrec_fold_multiply (type
, CHREC_RIGHT (op1
),
326 SCALAR_FLOAT_TYPE_P (type
)
327 ? build_real (type
, dconstm1
)
328 : build_int_cst_type (type
, -1)));
331 if (tree_contains_chrecs (op1
, NULL
))
333 /* We can strip sign-conversions to signed by performing the
334 operation in unsigned. */
335 tree optype
= TREE_TYPE (TREE_OPERAND (op1
, 0));
336 if (INTEGRAL_TYPE_P (type
)
337 && INTEGRAL_TYPE_P (optype
)
338 && tree_nop_conversion_p (type
, optype
)
339 && TYPE_UNSIGNED (optype
))
340 return chrec_convert (type
,
341 chrec_fold_plus_1 (code
, optype
,
342 chrec_convert (optype
,
345 TREE_OPERAND (op1
, 0)),
347 return chrec_dont_know
;
354 if ((tree_contains_chrecs (op0
, &size
)
355 || tree_contains_chrecs (op1
, &size
))
356 && size
< param_scev_max_expr_size
)
357 return build2 (code
, type
, op0
, op1
);
358 else if (size
< param_scev_max_expr_size
)
360 if (code
== POINTER_PLUS_EXPR
)
361 return fold_build_pointer_plus (fold_convert (type
, op0
),
364 return fold_build2 (code
, type
,
365 fold_convert (type
, op0
),
366 fold_convert (type
, op1
));
369 return chrec_dont_know
;
375 /* Fold the addition of two chrecs. */
378 chrec_fold_plus (tree type
,
383 if (automatically_generated_chrec_p (op0
)
384 || automatically_generated_chrec_p (op1
))
385 return chrec_fold_automatically_generated_operands (op0
, op1
);
387 if (integer_zerop (op0
))
388 return chrec_convert (type
, op1
, NULL
);
389 if (integer_zerop (op1
))
390 return chrec_convert (type
, op0
, NULL
);
392 if (POINTER_TYPE_P (type
))
393 code
= POINTER_PLUS_EXPR
;
397 return chrec_fold_plus_1 (code
, type
, op0
, op1
);
400 /* Fold the subtraction of two chrecs. */
403 chrec_fold_minus (tree type
,
407 if (automatically_generated_chrec_p (op0
)
408 || automatically_generated_chrec_p (op1
))
409 return chrec_fold_automatically_generated_operands (op0
, op1
);
411 if (integer_zerop (op1
))
414 return chrec_fold_plus_1 (MINUS_EXPR
, type
, op0
, op1
);
417 /* Fold the multiplication of two chrecs. */
420 chrec_fold_multiply (tree type
,
424 if (automatically_generated_chrec_p (op0
)
425 || automatically_generated_chrec_p (op1
))
426 return chrec_fold_automatically_generated_operands (op0
, op1
);
428 if (TREE_CODE (op0
) != POLYNOMIAL_CHREC
429 && TREE_CODE (op1
) == POLYNOMIAL_CHREC
)
430 std::swap (op0
, op1
);
432 switch (TREE_CODE (op0
))
434 case POLYNOMIAL_CHREC
:
436 (!chrec_contains_symbols_defined_in_loop (op0
, CHREC_VARIABLE (op0
)));
437 switch (TREE_CODE (op1
))
439 case POLYNOMIAL_CHREC
:
441 (!chrec_contains_symbols_defined_in_loop (op1
,
442 CHREC_VARIABLE (op1
)));
443 return chrec_fold_multiply_poly_poly (type
, op0
, op1
);
446 if (tree_contains_chrecs (op1
, NULL
))
448 /* We can strip sign-conversions to signed by performing the
449 operation in unsigned. */
450 tree optype
= TREE_TYPE (TREE_OPERAND (op1
, 0));
451 if (INTEGRAL_TYPE_P (type
)
452 && INTEGRAL_TYPE_P (optype
)
453 && tree_nop_conversion_p (type
, optype
)
454 && TYPE_UNSIGNED (optype
))
456 tree tem
= chrec_convert (optype
, op0
, NULL
);
457 if (TREE_CODE (tem
) == POLYNOMIAL_CHREC
)
458 return chrec_convert (type
,
459 chrec_fold_multiply (optype
, tem
,
464 return chrec_dont_know
;
469 if (integer_onep (op1
))
471 if (integer_zerop (op1
))
472 return build_int_cst (type
, 0);
474 /* When overflow is undefined and CHREC_LEFT/RIGHT do not have the
475 same sign or CHREC_LEFT is zero then folding the multiply into
476 the addition does not have the same behavior on overflow.
477 Using unsigned arithmetic in that case causes too many performance
478 regressions, but catch the constant case where the multiplication
479 of the step overflows. */
480 if (INTEGRAL_TYPE_P (type
)
481 && TYPE_OVERFLOW_UNDEFINED (type
)
482 && !integer_zerop (CHREC_LEFT (op0
))
483 && TREE_CODE (op1
) == INTEGER_CST
484 && TREE_CODE (CHREC_RIGHT (op0
)) == INTEGER_CST
)
486 wi::overflow_type ovf
= wi::OVF_NONE
;
488 = wi::mul (wi::to_wide (CHREC_RIGHT (op0
)),
489 wi::to_wide (op1
), TYPE_SIGN (type
), &ovf
);
490 if (ovf
!= wi::OVF_NONE
)
492 tree utype
= unsigned_type_for (type
);
493 tree uop1
= chrec_convert_rhs (utype
, op1
);
494 tree uleft0
= chrec_convert_rhs (utype
, CHREC_LEFT (op0
));
495 tree uright0
= chrec_convert_rhs (utype
, CHREC_RIGHT (op0
));
496 tree left
= chrec_fold_multiply (utype
, uleft0
, uop1
);
497 tree right
= chrec_fold_multiply (utype
, uright0
, uop1
);
498 tree tem
= build_polynomial_chrec (CHREC_VARIABLE (op0
),
500 return chrec_convert_rhs (type
, tem
);
503 tree left
= chrec_fold_multiply (type
, CHREC_LEFT (op0
), op1
);
504 tree right
= chrec_fold_multiply (type
, CHREC_RIGHT (op0
), op1
);
505 return build_polynomial_chrec (CHREC_VARIABLE (op0
), left
, right
);
509 if (tree_contains_chrecs (op0
, NULL
))
511 /* We can strip sign-conversions to signed by performing the
512 operation in unsigned. */
513 tree optype
= TREE_TYPE (TREE_OPERAND (op0
, 0));
514 if (INTEGRAL_TYPE_P (type
)
515 && INTEGRAL_TYPE_P (optype
)
516 && tree_nop_conversion_p (type
, optype
)
517 && TYPE_UNSIGNED (optype
))
518 return chrec_convert (type
,
519 chrec_fold_multiply (optype
,
520 TREE_OPERAND (op0
, 0),
521 chrec_convert (optype
,
525 return chrec_dont_know
;
530 gcc_checking_assert (!tree_contains_chrecs (op0
, NULL
));
531 if (integer_onep (op0
))
534 if (integer_zerop (op0
))
535 return build_int_cst (type
, 0);
537 switch (TREE_CODE (op1
))
539 case POLYNOMIAL_CHREC
:
543 if (tree_contains_chrecs (op1
, NULL
))
544 return chrec_fold_multiply (type
, op1
, op0
);
548 if (integer_onep (op1
))
550 if (integer_zerop (op1
))
551 return build_int_cst (type
, 0);
552 return fold_build2 (MULT_EXPR
, type
, op0
, op1
);
561 /* Evaluate the binomial coefficient. Return NULL_TREE if the intermediate
562 calculation overflows, otherwise return C(n,k) with type TYPE. */
565 tree_fold_binomial (tree type
, tree n
, unsigned int k
)
567 wi::overflow_type overflow
;
570 /* Handle the most frequent cases. */
572 return build_int_cst (type
, 1);
574 return fold_convert (type
, n
);
576 widest_int num
= wi::to_widest (n
);
578 /* Check that k <= n. */
579 if (wi::ltu_p (num
, k
))
582 /* Denominator = 2. */
583 widest_int denom
= 2;
585 /* Index = Numerator-1. */
586 widest_int idx
= num
- 1;
588 /* Numerator = Numerator*Index = n*(n-1). */
589 num
= wi::smul (num
, idx
, &overflow
);
593 for (i
= 3; i
<= k
; i
++)
598 /* Numerator *= Index. */
599 num
= wi::smul (num
, idx
, &overflow
);
603 /* Denominator *= i. */
607 /* Result = Numerator / Denominator. */
608 num
= wi::udiv_trunc (num
, denom
);
609 if (! wi::fits_to_tree_p (num
, type
))
611 return wide_int_to_tree (type
, num
);
614 /* Helper function. Use the Newton's interpolating formula for
615 evaluating the value of the evolution function.
616 The result may be in an unsigned type of CHREC. */
619 chrec_evaluate (unsigned var
, tree chrec
, tree n
, unsigned int k
)
621 tree arg0
, arg1
, binomial_n_k
;
622 tree type
= TREE_TYPE (chrec
);
623 class loop
*var_loop
= get_loop (cfun
, var
);
625 while (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
626 && flow_loop_nested_p (var_loop
, get_chrec_loop (chrec
)))
627 chrec
= CHREC_LEFT (chrec
);
629 /* The formula associates the expression and thus we have to make
630 sure to not introduce undefined overflow. */
632 if (INTEGRAL_TYPE_P (type
)
633 && ! TYPE_OVERFLOW_WRAPS (type
))
634 ctype
= unsigned_type_for (type
);
636 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
637 && CHREC_VARIABLE (chrec
) == var
)
639 arg1
= chrec_evaluate (var
, CHREC_RIGHT (chrec
), n
, k
+ 1);
640 if (arg1
== chrec_dont_know
)
641 return chrec_dont_know
;
642 binomial_n_k
= tree_fold_binomial (ctype
, n
, k
);
644 return chrec_dont_know
;
645 tree l
= chrec_convert (ctype
, CHREC_LEFT (chrec
), NULL
);
646 arg0
= fold_build2 (MULT_EXPR
, ctype
, l
, binomial_n_k
);
647 return chrec_fold_plus (ctype
, arg0
, arg1
);
650 binomial_n_k
= tree_fold_binomial (ctype
, n
, k
);
652 return chrec_dont_know
;
654 return fold_build2 (MULT_EXPR
, ctype
,
655 chrec_convert (ctype
, chrec
, NULL
), binomial_n_k
);
658 /* Evaluates "CHREC (X)" when the varying variable is VAR.
659 Example: Given the following parameters,
665 The result is given by the Newton's interpolating formula:
666 3 * \binom{10}{0} + 4 * \binom{10}{1}.
670 chrec_apply (unsigned var
,
674 tree type
= chrec_type (chrec
);
675 tree res
= chrec_dont_know
;
677 if (automatically_generated_chrec_p (chrec
)
678 || automatically_generated_chrec_p (x
)
680 /* When the symbols are defined in an outer loop, it is possible
681 to symbolically compute the apply, since the symbols are
682 constants with respect to the varying loop. */
683 || chrec_contains_symbols_defined_in_loop (chrec
, var
))
684 return chrec_dont_know
;
686 if (dump_file
&& (dump_flags
& TDF_SCEV
))
687 fprintf (dump_file
, "(chrec_apply \n");
689 if (TREE_CODE (x
) == INTEGER_CST
&& SCALAR_FLOAT_TYPE_P (type
))
690 x
= build_real_from_int_cst (type
, x
);
692 switch (TREE_CODE (chrec
))
694 case POLYNOMIAL_CHREC
:
695 if (evolution_function_is_affine_p (chrec
))
697 tree chrecr
= CHREC_RIGHT (chrec
);
698 tree chrecl
= CHREC_LEFT (chrec
);
699 if (CHREC_VARIABLE (chrec
) != var
)
700 res
= build_polynomial_chrec (CHREC_VARIABLE (chrec
),
701 chrec_apply (var
, chrecl
, x
),
702 chrec_apply (var
, chrecr
, x
));
704 /* "{a, +, a}" (x-1) -> "a*x". */
705 else if (operand_equal_p (chrecl
, chrecr
)
706 && TREE_CODE (x
) == PLUS_EXPR
707 && integer_all_onesp (TREE_OPERAND (x
, 1))
708 && !POINTER_TYPE_P (type
)
709 && TYPE_PRECISION (TREE_TYPE (x
))
710 >= TYPE_PRECISION (type
))
712 /* We know the number of iterations can't be negative. */
713 res
= build_int_cst (TREE_TYPE (x
), 1);
714 res
= chrec_fold_plus (TREE_TYPE (x
), x
, res
);
715 res
= chrec_convert_rhs (type
, res
, NULL
);
716 res
= chrec_fold_multiply (type
, chrecr
, res
);
718 /* "{a, +, b} (x)" -> "a + b*x". */
721 /* The overall increment might not fit in a signed type so
722 use an unsigned computation to get at the final value
723 and avoid undefined signed overflow. */
724 tree utype
= TREE_TYPE (chrecr
);
725 if (INTEGRAL_TYPE_P (utype
) && !TYPE_OVERFLOW_WRAPS (utype
))
726 utype
= unsigned_type_for (TREE_TYPE (chrecr
));
727 res
= chrec_convert_rhs (utype
, x
, NULL
);
728 res
= chrec_fold_multiply (utype
,
729 chrec_convert (utype
, chrecr
, NULL
),
731 /* When the resulting increment fits the original type
732 do the increment in it. */
733 if (TREE_CODE (res
) == INTEGER_CST
734 && int_fits_type_p (res
, TREE_TYPE (chrecr
)))
736 res
= chrec_convert (TREE_TYPE (chrecr
), res
, NULL
);
737 res
= chrec_fold_plus (type
, chrecl
, res
);
741 res
= chrec_fold_plus (utype
,
742 chrec_convert (utype
, chrecl
, NULL
),
744 res
= chrec_convert (type
, res
, NULL
);
748 else if (TREE_CODE (x
) == INTEGER_CST
749 && tree_int_cst_sgn (x
) == 1)
750 /* testsuite/.../ssa-chrec-38.c. */
751 res
= chrec_convert (type
, chrec_evaluate (var
, chrec
, x
, 0), NULL
);
753 res
= chrec_dont_know
;
757 res
= chrec_convert (TREE_TYPE (chrec
),
758 chrec_apply (var
, TREE_OPERAND (chrec
, 0), x
),
767 if (dump_file
&& (dump_flags
& TDF_SCEV
))
769 fprintf (dump_file
, " (varying_loop = %d", var
);
770 fprintf (dump_file
, ")\n (chrec = ");
771 print_generic_expr (dump_file
, chrec
);
772 fprintf (dump_file
, ")\n (x = ");
773 print_generic_expr (dump_file
, x
);
774 fprintf (dump_file
, ")\n (res = ");
775 print_generic_expr (dump_file
, res
);
776 fprintf (dump_file
, "))\n");
782 /* For a given CHREC and an induction variable map IV_MAP that maps
783 (loop->num, expr) for every loop number of the current_loops an
784 expression, calls chrec_apply when the expression is not NULL. */
787 chrec_apply_map (tree chrec
, vec
<tree
> iv_map
)
792 FOR_EACH_VEC_ELT (iv_map
, i
, expr
)
794 chrec
= chrec_apply (i
, chrec
, expr
);
799 /* Replaces the initial condition in CHREC with INIT_COND. */
802 chrec_replace_initial_condition (tree chrec
,
805 if (automatically_generated_chrec_p (chrec
))
808 gcc_assert (chrec_type (chrec
) == chrec_type (init_cond
));
810 switch (TREE_CODE (chrec
))
812 case POLYNOMIAL_CHREC
:
813 return build_polynomial_chrec
814 (CHREC_VARIABLE (chrec
),
815 chrec_replace_initial_condition (CHREC_LEFT (chrec
), init_cond
),
816 CHREC_RIGHT (chrec
));
823 /* Returns the initial condition of a given CHREC. */
826 initial_condition (tree chrec
)
828 if (automatically_generated_chrec_p (chrec
))
831 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
832 return initial_condition (CHREC_LEFT (chrec
));
837 /* Returns a univariate function that represents the evolution in
838 LOOP_NUM. Mask the evolution of any other loop. */
841 hide_evolution_in_other_loops_than_loop (tree chrec
,
844 class loop
*loop
= get_loop (cfun
, loop_num
), *chloop
;
845 if (automatically_generated_chrec_p (chrec
))
848 switch (TREE_CODE (chrec
))
850 case POLYNOMIAL_CHREC
:
851 chloop
= get_chrec_loop (chrec
);
854 return build_polynomial_chrec
856 hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec
),
858 CHREC_RIGHT (chrec
));
860 else if (flow_loop_nested_p (chloop
, loop
))
861 /* There is no evolution in this loop. */
862 return initial_condition (chrec
);
864 else if (flow_loop_nested_p (loop
, chloop
))
865 return hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec
),
869 return chrec_dont_know
;
876 /* Returns the evolution part of CHREC in LOOP_NUM when RIGHT is
877 true, otherwise returns the initial condition in LOOP_NUM. */
880 chrec_component_in_loop_num (tree chrec
,
885 class loop
*loop
= get_loop (cfun
, loop_num
), *chloop
;
887 if (automatically_generated_chrec_p (chrec
))
890 switch (TREE_CODE (chrec
))
892 case POLYNOMIAL_CHREC
:
893 chloop
= get_chrec_loop (chrec
);
898 component
= CHREC_RIGHT (chrec
);
900 component
= CHREC_LEFT (chrec
);
902 if (TREE_CODE (CHREC_LEFT (chrec
)) != POLYNOMIAL_CHREC
903 || CHREC_VARIABLE (CHREC_LEFT (chrec
)) != CHREC_VARIABLE (chrec
))
907 return build_polynomial_chrec
909 chrec_component_in_loop_num (CHREC_LEFT (chrec
),
915 else if (flow_loop_nested_p (chloop
, loop
))
916 /* There is no evolution part in this loop. */
921 gcc_assert (flow_loop_nested_p (loop
, chloop
));
922 return chrec_component_in_loop_num (CHREC_LEFT (chrec
),
935 /* Returns the evolution part in LOOP_NUM. Example: the call
936 evolution_part_in_loop_num ({{0, +, 1}_1, +, 2}_1, 1) returns
940 evolution_part_in_loop_num (tree chrec
,
943 return chrec_component_in_loop_num (chrec
, loop_num
, true);
946 /* Returns the initial condition in LOOP_NUM. Example: the call
947 initial_condition_in_loop_num ({{0, +, 1}_1, +, 2}_2, 2) returns
951 initial_condition_in_loop_num (tree chrec
,
954 return chrec_component_in_loop_num (chrec
, loop_num
, false);
957 /* Set or reset the evolution of CHREC to NEW_EVOL in loop LOOP_NUM.
958 This function is essentially used for setting the evolution to
959 chrec_dont_know, for example after having determined that it is
960 impossible to say how many times a loop will execute. */
963 reset_evolution_in_loop (unsigned loop_num
,
967 class loop
*loop
= get_loop (cfun
, loop_num
);
969 if (POINTER_TYPE_P (chrec_type (chrec
)))
970 gcc_assert (ptrofftype_p (chrec_type (new_evol
)));
972 gcc_assert (chrec_type (chrec
) == chrec_type (new_evol
));
974 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
975 && flow_loop_nested_p (loop
, get_chrec_loop (chrec
)))
977 tree left
= reset_evolution_in_loop (loop_num
, CHREC_LEFT (chrec
),
979 tree right
= reset_evolution_in_loop (loop_num
, CHREC_RIGHT (chrec
),
981 return build_polynomial_chrec (CHREC_VARIABLE (chrec
), left
, right
);
984 while (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
985 && CHREC_VARIABLE (chrec
) == loop_num
)
986 chrec
= CHREC_LEFT (chrec
);
988 return build_polynomial_chrec (loop_num
, chrec
, new_evol
);
991 /* Merges two evolution functions that were found by following two
992 alternate paths of a conditional expression. */
995 chrec_merge (tree chrec1
,
998 if (chrec1
== chrec_dont_know
999 || chrec2
== chrec_dont_know
)
1000 return chrec_dont_know
;
1002 if (chrec1
== chrec_known
1003 || chrec2
== chrec_known
)
1006 if (chrec1
== chrec_not_analyzed_yet
)
1008 if (chrec2
== chrec_not_analyzed_yet
)
1011 if (eq_evolutions_p (chrec1
, chrec2
))
1014 return chrec_dont_know
;
1021 /* Helper function for is_multivariate_chrec. */
1024 is_multivariate_chrec_rec (const_tree chrec
, unsigned int rec_var
)
1026 if (chrec
== NULL_TREE
)
1029 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
1031 if (CHREC_VARIABLE (chrec
) != rec_var
)
1034 return (is_multivariate_chrec_rec (CHREC_LEFT (chrec
), rec_var
)
1035 || is_multivariate_chrec_rec (CHREC_RIGHT (chrec
), rec_var
));
1041 /* Determine whether the given chrec is multivariate or not. */
1044 is_multivariate_chrec (const_tree chrec
)
1046 if (chrec
== NULL_TREE
)
1049 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
1050 return (is_multivariate_chrec_rec (CHREC_LEFT (chrec
),
1051 CHREC_VARIABLE (chrec
))
1052 || is_multivariate_chrec_rec (CHREC_RIGHT (chrec
),
1053 CHREC_VARIABLE (chrec
)));
1058 /* Determines whether the chrec contains symbolic names or not. If LOOP isn't
1059 NULL, we also consider chrec wrto outer loops of LOOP as symbol. */
1062 chrec_contains_symbols (const_tree chrec
, hash_set
<const_tree
> &visited
,
1067 if (chrec
== NULL_TREE
)
1070 if (TREE_CODE (chrec
) == SSA_NAME
1072 || TREE_CODE (chrec
) == POLY_INT_CST
1073 || TREE_CODE (chrec
) == PARM_DECL
1074 || TREE_CODE (chrec
) == FUNCTION_DECL
1075 || TREE_CODE (chrec
) == LABEL_DECL
1076 || TREE_CODE (chrec
) == RESULT_DECL
1077 || TREE_CODE (chrec
) == FIELD_DECL
)
1081 && TREE_CODE (chrec
) == POLYNOMIAL_CHREC
1082 && flow_loop_nested_p (get_chrec_loop (chrec
), loop
))
1085 if (visited
.add (chrec
))
1088 n
= TREE_OPERAND_LENGTH (chrec
);
1089 for (i
= 0; i
< n
; i
++)
1090 if (chrec_contains_symbols (TREE_OPERAND (chrec
, i
), visited
, loop
))
1095 /* Return true if CHREC contains any symbols. If LOOP is not NULL, check if
1096 CHREC contains any chrec which is invariant wrto the loop (nest), in other
1097 words, chrec defined by outer loops of loop, so from LOOP's point of view,
1098 the chrec is considered as a SYMBOL. */
1101 chrec_contains_symbols (const_tree chrec
, class loop
* loop
)
1103 hash_set
<const_tree
> visited
;
1104 return chrec_contains_symbols (chrec
, visited
, loop
);
1107 /* Return true when CHREC contains symbolic names defined in
1111 chrec_contains_symbols_defined_in_loop (const_tree chrec
, unsigned loop_nb
,
1112 hash_set
<const_tree
> &visited
)
1116 if (chrec
== NULL_TREE
)
1119 if (is_gimple_min_invariant (chrec
))
1122 if (TREE_CODE (chrec
) == SSA_NAME
)
1125 loop_p def_loop
, loop
;
1127 if (SSA_NAME_IS_DEFAULT_DEF (chrec
))
1130 def
= SSA_NAME_DEF_STMT (chrec
);
1131 def_loop
= loop_containing_stmt (def
);
1132 loop
= get_loop (cfun
, loop_nb
);
1134 if (def_loop
== NULL
)
1137 if (loop
== def_loop
|| flow_loop_nested_p (loop
, def_loop
))
1143 if (visited
.add (chrec
))
1146 n
= TREE_OPERAND_LENGTH (chrec
);
1147 for (i
= 0; i
< n
; i
++)
1148 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec
, i
),
1154 /* Return true when CHREC contains symbolic names defined in
1158 chrec_contains_symbols_defined_in_loop (const_tree chrec
, unsigned loop_nb
)
1160 hash_set
<const_tree
> visited
;
1161 return chrec_contains_symbols_defined_in_loop (chrec
, loop_nb
, visited
);
1164 /* Determines whether the chrec contains undetermined coefficients. */
1167 chrec_contains_undetermined (const_tree chrec
, hash_set
<const_tree
> &visited
)
1171 if (chrec
== chrec_dont_know
)
1174 if (chrec
== NULL_TREE
)
1177 if (visited
.add (chrec
))
1180 n
= TREE_OPERAND_LENGTH (chrec
);
1181 for (i
= 0; i
< n
; i
++)
1182 if (chrec_contains_undetermined (TREE_OPERAND (chrec
, i
), visited
))
1188 chrec_contains_undetermined (const_tree chrec
)
1190 hash_set
<const_tree
> visited
;
1191 return chrec_contains_undetermined (chrec
, visited
);
1194 /* Determines whether the tree EXPR contains chrecs, and increment
1195 SIZE if it is not a NULL pointer by an estimation of the depth of
1199 tree_contains_chrecs (const_tree expr
, int *size
, hash_set
<const_tree
> &visited
)
1203 if (expr
== NULL_TREE
)
1209 if (tree_is_chrec (expr
))
1212 if (visited
.add (expr
))
1215 n
= TREE_OPERAND_LENGTH (expr
);
1216 for (i
= 0; i
< n
; i
++)
1217 if (tree_contains_chrecs (TREE_OPERAND (expr
, i
), size
, visited
))
1223 tree_contains_chrecs (const_tree expr
, int *size
)
1225 hash_set
<const_tree
> visited
;
1226 return tree_contains_chrecs (expr
, size
, visited
);
1230 /* Recursive helper function. */
1233 evolution_function_is_invariant_rec_p (tree chrec
, int loopnum
)
1235 if (evolution_function_is_constant_p (chrec
))
1238 if (TREE_CODE (chrec
) == SSA_NAME
1240 || expr_invariant_in_loop_p (get_loop (cfun
, loopnum
), chrec
)))
1243 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
1245 if (CHREC_VARIABLE (chrec
) == (unsigned) loopnum
1246 || flow_loop_nested_p (get_loop (cfun
, loopnum
),
1247 get_chrec_loop (chrec
))
1248 || !evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec
),
1250 || !evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec
),
1256 switch (TREE_OPERAND_LENGTH (chrec
))
1259 if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec
, 1),
1265 if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec
, 0),
1275 /* Return true if CHREC is invariant in loop LOOPNUM, false otherwise. */
1278 evolution_function_is_invariant_p (tree chrec
, int loopnum
)
1280 return evolution_function_is_invariant_rec_p (chrec
, loopnum
);
1283 /* Determine whether the given tree is an affine multivariate
1287 evolution_function_is_affine_multivariate_p (const_tree chrec
, int loopnum
)
1289 if (chrec
== NULL_TREE
)
1292 switch (TREE_CODE (chrec
))
1294 case POLYNOMIAL_CHREC
:
1295 if (evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec
), loopnum
))
1297 if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec
), loopnum
))
1301 if (TREE_CODE (CHREC_RIGHT (chrec
)) == POLYNOMIAL_CHREC
1302 && CHREC_VARIABLE (CHREC_RIGHT (chrec
))
1303 != CHREC_VARIABLE (chrec
)
1304 && evolution_function_is_affine_multivariate_p
1305 (CHREC_RIGHT (chrec
), loopnum
))
1313 if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec
), loopnum
)
1314 && TREE_CODE (CHREC_LEFT (chrec
)) == POLYNOMIAL_CHREC
1315 && CHREC_VARIABLE (CHREC_LEFT (chrec
)) != CHREC_VARIABLE (chrec
)
1316 && evolution_function_is_affine_multivariate_p
1317 (CHREC_LEFT (chrec
), loopnum
))
1328 /* Determine whether the given tree is a function in zero or one
1329 variables with respect to loop specified by LOOPNUM. Note only positive
1330 LOOPNUM stands for a real loop. */
1333 evolution_function_is_univariate_p (const_tree chrec
, int loopnum
)
1335 if (chrec
== NULL_TREE
)
1339 switch (TREE_CODE (chrec
))
1341 case POLYNOMIAL_CHREC
:
1342 switch (TREE_CODE (CHREC_LEFT (chrec
)))
1344 case POLYNOMIAL_CHREC
:
1345 sub_chrec
= CHREC_LEFT (chrec
);
1346 if (CHREC_VARIABLE (chrec
) != CHREC_VARIABLE (sub_chrec
)
1348 || CHREC_VARIABLE (sub_chrec
) == (unsigned) loopnum
1349 || flow_loop_nested_p (get_loop (cfun
, loopnum
),
1350 get_chrec_loop (sub_chrec
))))
1352 if (!evolution_function_is_univariate_p (sub_chrec
, loopnum
))
1357 if (tree_contains_chrecs (CHREC_LEFT (chrec
), NULL
))
1362 switch (TREE_CODE (CHREC_RIGHT (chrec
)))
1364 case POLYNOMIAL_CHREC
:
1365 sub_chrec
= CHREC_RIGHT (chrec
);
1366 if (CHREC_VARIABLE (chrec
) != CHREC_VARIABLE (sub_chrec
)
1368 || CHREC_VARIABLE (sub_chrec
) == (unsigned) loopnum
1369 || flow_loop_nested_p (get_loop (cfun
, loopnum
),
1370 get_chrec_loop (sub_chrec
))))
1372 if (!evolution_function_is_univariate_p (sub_chrec
, loopnum
))
1377 if (tree_contains_chrecs (CHREC_RIGHT (chrec
), NULL
))
1388 /* Returns the number of variables of CHREC. Example: the call
1389 nb_vars_in_chrec ({{0, +, 1}_5, +, 2}_6) returns 2. */
1392 nb_vars_in_chrec (tree chrec
)
1394 if (chrec
== NULL_TREE
)
1397 switch (TREE_CODE (chrec
))
1399 case POLYNOMIAL_CHREC
:
1400 return 1 + nb_vars_in_chrec
1401 (initial_condition_in_loop_num (chrec
, CHREC_VARIABLE (chrec
)));
1408 /* Converts BASE and STEP of affine scev to TYPE. LOOP is the loop whose iv
1409 the scev corresponds to. AT_STMT is the statement at that the scev is
1410 evaluated. USE_OVERFLOW_SEMANTICS is true if this function should assume
1411 that the rules for overflow of the given language apply (e.g., that signed
1412 arithmetics in C does not overflow) -- i.e., to use them to avoid
1413 unnecessary tests, but also to enforce that the result follows them.
1414 FROM is the source variable converted if it's not NULL. Returns true if
1415 the conversion succeeded, false otherwise. */
1418 convert_affine_scev (class loop
*loop
, tree type
,
1419 tree
*base
, tree
*step
, gimple
*at_stmt
,
1420 bool use_overflow_semantics
, tree from
)
1422 tree ct
= TREE_TYPE (*step
);
1423 bool enforce_overflow_semantics
;
1424 bool must_check_src_overflow
, must_check_rslt_overflow
;
1425 tree new_base
, new_step
;
1426 tree step_type
= POINTER_TYPE_P (type
) ? sizetype
: type
;
1429 (TYPE) (BASE + STEP * i) = (TYPE) BASE + (TYPE -- sign extend) STEP * i,
1430 but we must check some assumptions.
1432 1) If [BASE, +, STEP] wraps, the equation is not valid when precision
1433 of CT is smaller than the precision of TYPE. For example, when we
1434 cast unsigned char [254, +, 1] to unsigned, the values on left side
1435 are 254, 255, 0, 1, ..., but those on the right side are
1436 254, 255, 256, 257, ...
1437 2) In case that we must also preserve the fact that signed ivs do not
1438 overflow, we must additionally check that the new iv does not wrap.
1439 For example, unsigned char [125, +, 1] casted to signed char could
1440 become a wrapping variable with values 125, 126, 127, -128, -127, ...,
1441 which would confuse optimizers that assume that this does not
1443 must_check_src_overflow
= TYPE_PRECISION (ct
) < TYPE_PRECISION (type
);
1445 enforce_overflow_semantics
= (use_overflow_semantics
1446 && nowrap_type_p (type
));
1447 if (enforce_overflow_semantics
)
1449 /* We can avoid checking whether the result overflows in the following
1452 -- must_check_src_overflow is true, and the range of TYPE is superset
1453 of the range of CT -- i.e., in all cases except if CT signed and
1455 -- both CT and TYPE have the same precision and signedness, and we
1456 verify instead that the source does not overflow (this may be
1457 easier than verifying it for the result, as we may use the
1458 information about the semantics of overflow in CT). */
1459 if (must_check_src_overflow
)
1461 if (TYPE_UNSIGNED (type
) && !TYPE_UNSIGNED (ct
))
1462 must_check_rslt_overflow
= true;
1464 must_check_rslt_overflow
= false;
1466 else if (TYPE_UNSIGNED (ct
) == TYPE_UNSIGNED (type
)
1467 && TYPE_PRECISION (ct
) == TYPE_PRECISION (type
))
1469 must_check_rslt_overflow
= false;
1470 must_check_src_overflow
= true;
1473 must_check_rslt_overflow
= true;
1476 must_check_rslt_overflow
= false;
1478 if (must_check_src_overflow
1479 && scev_probably_wraps_p (from
, *base
, *step
, at_stmt
, loop
,
1480 use_overflow_semantics
))
1483 new_base
= chrec_convert (type
, *base
, at_stmt
, use_overflow_semantics
);
1484 /* The step must be sign extended, regardless of the signedness
1485 of CT and TYPE. This only needs to be handled specially when
1486 CT is unsigned -- to avoid e.g. unsigned char [100, +, 255]
1487 (with values 100, 99, 98, ...) from becoming signed or unsigned
1488 [100, +, 255] with values 100, 355, ...; the sign-extension is
1489 performed by default when CT is signed. */
1491 if (TYPE_PRECISION (step_type
) > TYPE_PRECISION (ct
) && TYPE_UNSIGNED (ct
))
1493 tree signed_ct
= build_nonstandard_integer_type (TYPE_PRECISION (ct
), 0);
1494 new_step
= chrec_convert (signed_ct
, new_step
, at_stmt
,
1495 use_overflow_semantics
);
1497 new_step
= chrec_convert (step_type
, new_step
, at_stmt
,
1498 use_overflow_semantics
);
1500 if (automatically_generated_chrec_p (new_base
)
1501 || automatically_generated_chrec_p (new_step
))
1504 if (must_check_rslt_overflow
1505 /* Note that in this case we cannot use the fact that signed variables
1506 do not overflow, as this is what we are verifying for the new iv. */
1507 && scev_probably_wraps_p (NULL_TREE
, new_base
, new_step
,
1508 at_stmt
, loop
, false))
1517 /* Convert CHREC for the right hand side of a CHREC.
1518 The increment for a pointer type is always sizetype. */
1521 chrec_convert_rhs (tree type
, tree chrec
, gimple
*at_stmt
)
1523 if (POINTER_TYPE_P (type
))
1526 return chrec_convert (type
, chrec
, at_stmt
);
1529 /* Convert CHREC to TYPE. When the analyzer knows the context in
1530 which the CHREC is built, it sets AT_STMT to the statement that
1531 contains the definition of the analyzed variable, otherwise the
1532 conversion is less accurate: the information is used for
1533 determining a more accurate estimation of the number of iterations.
1534 By default AT_STMT could be safely set to NULL_TREE.
1536 USE_OVERFLOW_SEMANTICS is true if this function should assume that
1537 the rules for overflow of the given language apply (e.g., that signed
1538 arithmetics in C does not overflow) -- i.e., to use them to avoid
1539 unnecessary tests, but also to enforce that the result follows them.
1541 FROM is the source variable converted if it's not NULL. */
1544 chrec_convert_1 (tree type
, tree chrec
, gimple
*at_stmt
,
1545 bool use_overflow_semantics
, tree from
)
1551 if (automatically_generated_chrec_p (chrec
))
1554 ct
= chrec_type (chrec
);
1555 if (useless_type_conversion_p (type
, ct
))
1558 if (!evolution_function_is_affine_p (chrec
))
1561 loop
= get_chrec_loop (chrec
);
1562 base
= CHREC_LEFT (chrec
);
1563 step
= CHREC_RIGHT (chrec
);
1565 if (convert_affine_scev (loop
, type
, &base
, &step
, at_stmt
,
1566 use_overflow_semantics
, from
))
1567 return build_polynomial_chrec (loop
->num
, base
, step
);
1569 /* If we cannot propagate the cast inside the chrec, just keep the cast. */
1571 /* Fold will not canonicalize (long)(i - 1) to (long)i - 1 because that
1572 may be more expensive. We do want to perform this optimization here
1573 though for canonicalization reasons. */
1574 if (use_overflow_semantics
1575 && (TREE_CODE (chrec
) == PLUS_EXPR
1576 || TREE_CODE (chrec
) == MINUS_EXPR
)
1577 && TREE_CODE (type
) == INTEGER_TYPE
1578 && TREE_CODE (ct
) == INTEGER_TYPE
1579 && TYPE_PRECISION (type
) > TYPE_PRECISION (ct
)
1580 && TYPE_OVERFLOW_UNDEFINED (ct
))
1581 res
= fold_build2 (TREE_CODE (chrec
), type
,
1582 fold_convert (type
, TREE_OPERAND (chrec
, 0)),
1583 fold_convert (type
, TREE_OPERAND (chrec
, 1)));
1584 /* Similar perform the trick that (signed char)((int)x + 2) can be
1585 narrowed to (signed char)((unsigned char)x + 2). */
1586 else if (use_overflow_semantics
1587 && TREE_CODE (chrec
) == POLYNOMIAL_CHREC
1588 && TREE_CODE (ct
) == INTEGER_TYPE
1589 && TREE_CODE (type
) == INTEGER_TYPE
1590 && TYPE_OVERFLOW_UNDEFINED (type
)
1591 && TYPE_PRECISION (type
) < TYPE_PRECISION (ct
))
1593 tree utype
= unsigned_type_for (type
);
1594 res
= build_polynomial_chrec (CHREC_VARIABLE (chrec
),
1595 fold_convert (utype
,
1596 CHREC_LEFT (chrec
)),
1597 fold_convert (utype
,
1598 CHREC_RIGHT (chrec
)));
1599 res
= chrec_convert_1 (type
, res
, at_stmt
, use_overflow_semantics
, from
);
1602 res
= fold_convert (type
, chrec
);
1604 /* Don't propagate overflows. */
1605 if (CONSTANT_CLASS_P (res
))
1606 TREE_OVERFLOW (res
) = 0;
1608 /* But reject constants that don't fit in their type after conversion.
1609 This can happen if TYPE_MIN_VALUE or TYPE_MAX_VALUE are not the
1610 natural values associated with TYPE_PRECISION and TYPE_UNSIGNED,
1611 and can cause problems later when computing niters of loops. Note
1612 that we don't do the check before converting because we don't want
1613 to reject conversions of negative chrecs to unsigned types. */
1614 if (TREE_CODE (res
) == INTEGER_CST
1615 && TREE_CODE (type
) == INTEGER_TYPE
1616 && !int_fits_type_p (res
, type
))
1617 res
= chrec_dont_know
;
1622 /* Convert CHREC to TYPE. When the analyzer knows the context in
1623 which the CHREC is built, it sets AT_STMT to the statement that
1624 contains the definition of the analyzed variable, otherwise the
1625 conversion is less accurate: the information is used for
1626 determining a more accurate estimation of the number of iterations.
1627 By default AT_STMT could be safely set to NULL_TREE.
1629 The following rule is always true: TREE_TYPE (chrec) ==
1630 TREE_TYPE (CHREC_LEFT (chrec)) == TREE_TYPE (CHREC_RIGHT (chrec)).
1631 An example of what could happen when adding two chrecs and the type
1632 of the CHREC_RIGHT is different than CHREC_LEFT is:
1634 {(uint) 0, +, (uchar) 10} +
1635 {(uint) 0, +, (uchar) 250}
1637 that would produce a wrong result if CHREC_RIGHT is not (uint):
1639 {(uint) 0, +, (uchar) 4}
1643 {(uint) 0, +, (uint) 260}
1645 USE_OVERFLOW_SEMANTICS is true if this function should assume that
1646 the rules for overflow of the given language apply (e.g., that signed
1647 arithmetics in C does not overflow) -- i.e., to use them to avoid
1648 unnecessary tests, but also to enforce that the result follows them.
1650 FROM is the source variable converted if it's not NULL. */
1653 chrec_convert (tree type
, tree chrec
, gimple
*at_stmt
,
1654 bool use_overflow_semantics
, tree from
)
1656 return chrec_convert_1 (type
, chrec
, at_stmt
, use_overflow_semantics
, from
);
1659 /* Convert CHREC to TYPE, without regard to signed overflows. Returns the new
1660 chrec if something else than what chrec_convert would do happens, NULL_TREE
1661 otherwise. This function set TRUE to variable pointed by FOLD_CONVERSIONS
1662 if the result chrec may overflow. */
1665 chrec_convert_aggressive (tree type
, tree chrec
, bool *fold_conversions
)
1667 tree inner_type
, left
, right
, lc
, rc
, rtype
;
1669 gcc_assert (fold_conversions
!= NULL
);
1671 if (automatically_generated_chrec_p (chrec
)
1672 || TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
1675 inner_type
= TREE_TYPE (chrec
);
1676 if (TYPE_PRECISION (type
) > TYPE_PRECISION (inner_type
))
1679 if (useless_type_conversion_p (type
, inner_type
))
1682 if (!*fold_conversions
&& evolution_function_is_affine_p (chrec
))
1687 loop
= get_chrec_loop (chrec
);
1688 base
= CHREC_LEFT (chrec
);
1689 step
= CHREC_RIGHT (chrec
);
1690 if (convert_affine_scev (loop
, type
, &base
, &step
, NULL
, true))
1691 return build_polynomial_chrec (loop
->num
, base
, step
);
1693 rtype
= POINTER_TYPE_P (type
) ? sizetype
: type
;
1695 left
= CHREC_LEFT (chrec
);
1696 right
= CHREC_RIGHT (chrec
);
1697 lc
= chrec_convert_aggressive (type
, left
, fold_conversions
);
1699 lc
= chrec_convert (type
, left
, NULL
);
1700 rc
= chrec_convert_aggressive (rtype
, right
, fold_conversions
);
1702 rc
= chrec_convert (rtype
, right
, NULL
);
1704 *fold_conversions
= true;
1706 return build_polynomial_chrec (CHREC_VARIABLE (chrec
), lc
, rc
);
1709 /* Returns true when CHREC0 == CHREC1. */
1712 eq_evolutions_p (const_tree chrec0
, const_tree chrec1
)
1714 if (chrec0
== NULL_TREE
1715 || chrec1
== NULL_TREE
1716 || TREE_CODE (chrec0
) != TREE_CODE (chrec1
))
1719 if (operand_equal_p (chrec0
, chrec1
, 0))
1722 if (! types_compatible_p (TREE_TYPE (chrec0
), TREE_TYPE (chrec1
)))
1725 switch (TREE_CODE (chrec0
))
1727 case POLYNOMIAL_CHREC
:
1728 return (CHREC_VARIABLE (chrec0
) == CHREC_VARIABLE (chrec1
)
1729 && eq_evolutions_p (CHREC_LEFT (chrec0
), CHREC_LEFT (chrec1
))
1730 && eq_evolutions_p (CHREC_RIGHT (chrec0
), CHREC_RIGHT (chrec1
)));
1735 case POINTER_PLUS_EXPR
:
1736 return eq_evolutions_p (TREE_OPERAND (chrec0
, 0),
1737 TREE_OPERAND (chrec1
, 0))
1738 && eq_evolutions_p (TREE_OPERAND (chrec0
, 1),
1739 TREE_OPERAND (chrec1
, 1));
1742 return eq_evolutions_p (TREE_OPERAND (chrec0
, 0),
1743 TREE_OPERAND (chrec1
, 0));
1750 /* Returns EV_GROWS if CHREC grows (assuming that it does not overflow),
1751 EV_DECREASES if it decreases, and EV_UNKNOWN if we cannot determine
1752 which of these cases happens. */
1755 scev_direction (const_tree chrec
)
1759 if (!evolution_function_is_affine_p (chrec
))
1760 return EV_DIR_UNKNOWN
;
1762 step
= CHREC_RIGHT (chrec
);
1763 if (TREE_CODE (step
) != INTEGER_CST
)
1764 return EV_DIR_UNKNOWN
;
1766 if (tree_int_cst_sign_bit (step
))
1767 return EV_DIR_DECREASES
;
1769 return EV_DIR_GROWS
;
1772 /* Iterates over all the components of SCEV, and calls CBCK. */
1775 for_each_scev_op (tree
*scev
, bool (*cbck
) (tree
*, void *), void *data
)
1777 switch (TREE_CODE_LENGTH (TREE_CODE (*scev
)))
1780 for_each_scev_op (&TREE_OPERAND (*scev
, 2), cbck
, data
);
1784 for_each_scev_op (&TREE_OPERAND (*scev
, 1), cbck
, data
);
1788 for_each_scev_op (&TREE_OPERAND (*scev
, 0), cbck
, data
);
1797 /* Returns true when the operation can be part of a linear
1801 operator_is_linear (tree scev
)
1803 switch (TREE_CODE (scev
))
1806 case POLYNOMIAL_CHREC
:
1808 case POINTER_PLUS_EXPR
:
1813 case NON_LVALUE_EXPR
:
1823 /* Return true when SCEV is a linear expression. Linear expressions
1824 can contain additions, substractions and multiplications.
1825 Multiplications are restricted to constant scaling: "cst * x". */
1828 scev_is_linear_expression (tree scev
)
1830 if (evolution_function_is_constant_p (scev
))
1834 || !operator_is_linear (scev
))
1837 if (TREE_CODE (scev
) == MULT_EXPR
)
1838 return !(tree_contains_chrecs (TREE_OPERAND (scev
, 0), NULL
)
1839 && tree_contains_chrecs (TREE_OPERAND (scev
, 1), NULL
));
1841 if (TREE_CODE (scev
) == POLYNOMIAL_CHREC
1842 && !evolution_function_is_affine_multivariate_p (scev
, CHREC_VARIABLE (scev
)))
1845 switch (TREE_CODE_LENGTH (TREE_CODE (scev
)))
1848 return scev_is_linear_expression (TREE_OPERAND (scev
, 0))
1849 && scev_is_linear_expression (TREE_OPERAND (scev
, 1))
1850 && scev_is_linear_expression (TREE_OPERAND (scev
, 2));
1853 return scev_is_linear_expression (TREE_OPERAND (scev
, 0))
1854 && scev_is_linear_expression (TREE_OPERAND (scev
, 1));
1857 return scev_is_linear_expression (TREE_OPERAND (scev
, 0));
1867 /* Determines whether the expression CHREC contains only interger consts
1868 in the right parts. */
1871 evolution_function_right_is_integer_cst (const_tree chrec
)
1873 if (chrec
== NULL_TREE
)
1876 switch (TREE_CODE (chrec
))
1881 case POLYNOMIAL_CHREC
:
1882 return TREE_CODE (CHREC_RIGHT (chrec
)) == INTEGER_CST
1883 && (TREE_CODE (CHREC_LEFT (chrec
)) != POLYNOMIAL_CHREC
1884 || evolution_function_right_is_integer_cst (CHREC_LEFT (chrec
)));
1887 return evolution_function_right_is_integer_cst (TREE_OPERAND (chrec
, 0));