1 /* Chains of recurrences.
2 Copyright (C) 2003-2021 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
);
253 /* We can strip sign-conversions to signed by performing the
254 operation in unsigned. */
255 tree optype
= TREE_TYPE (TREE_OPERAND (op1
, 0));
256 if (INTEGRAL_TYPE_P (type
)
257 && INTEGRAL_TYPE_P (optype
)
258 && tree_nop_conversion_p (type
, optype
)
259 && TYPE_UNSIGNED (optype
))
260 return chrec_convert (type
,
261 chrec_fold_plus_1 (code
, optype
,
262 chrec_convert (optype
,
264 TREE_OPERAND (op1
, 0)),
266 if (tree_contains_chrecs (op1
, NULL
))
267 return chrec_dont_know
;
272 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
273 return build_polynomial_chrec
274 (CHREC_VARIABLE (op0
),
275 chrec_fold_plus (type
, CHREC_LEFT (op0
), op1
),
278 return build_polynomial_chrec
279 (CHREC_VARIABLE (op0
),
280 chrec_fold_minus (type
, CHREC_LEFT (op0
), op1
),
286 /* We can strip sign-conversions to signed by performing the
287 operation in unsigned. */
288 tree optype
= TREE_TYPE (TREE_OPERAND (op0
, 0));
289 if (INTEGRAL_TYPE_P (type
)
290 && INTEGRAL_TYPE_P (optype
)
291 && tree_nop_conversion_p (type
, optype
)
292 && TYPE_UNSIGNED (optype
))
293 return chrec_convert (type
,
294 chrec_fold_plus_1 (code
, optype
,
295 TREE_OPERAND (op0
, 0),
296 chrec_convert (optype
,
299 if (tree_contains_chrecs (op0
, NULL
))
300 return chrec_dont_know
;
305 switch (TREE_CODE (op1
))
307 case POLYNOMIAL_CHREC
:
309 (!chrec_contains_symbols_defined_in_loop (op1
,
310 CHREC_VARIABLE (op1
)));
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
;
333 if ((tree_contains_chrecs (op0
, &size
)
334 || tree_contains_chrecs (op1
, &size
))
335 && size
< param_scev_max_expr_size
)
336 return build2 (code
, type
, op0
, op1
);
337 else if (size
< param_scev_max_expr_size
)
339 if (code
== POINTER_PLUS_EXPR
)
340 return fold_build_pointer_plus (fold_convert (type
, op0
),
343 return fold_build2 (code
, type
,
344 fold_convert (type
, op0
),
345 fold_convert (type
, op1
));
348 return chrec_dont_know
;
354 /* Fold the addition of two chrecs. */
357 chrec_fold_plus (tree type
,
362 if (automatically_generated_chrec_p (op0
)
363 || automatically_generated_chrec_p (op1
))
364 return chrec_fold_automatically_generated_operands (op0
, op1
);
366 if (integer_zerop (op0
))
367 return chrec_convert (type
, op1
, NULL
);
368 if (integer_zerop (op1
))
369 return chrec_convert (type
, op0
, NULL
);
371 if (POINTER_TYPE_P (type
))
372 code
= POINTER_PLUS_EXPR
;
376 return chrec_fold_plus_1 (code
, type
, op0
, op1
);
379 /* Fold the subtraction of two chrecs. */
382 chrec_fold_minus (tree type
,
386 if (automatically_generated_chrec_p (op0
)
387 || automatically_generated_chrec_p (op1
))
388 return chrec_fold_automatically_generated_operands (op0
, op1
);
390 if (integer_zerop (op1
))
393 return chrec_fold_plus_1 (MINUS_EXPR
, type
, op0
, op1
);
396 /* Fold the multiplication of two chrecs. */
399 chrec_fold_multiply (tree type
,
403 if (automatically_generated_chrec_p (op0
)
404 || automatically_generated_chrec_p (op1
))
405 return chrec_fold_automatically_generated_operands (op0
, op1
);
407 switch (TREE_CODE (op0
))
409 case POLYNOMIAL_CHREC
:
411 (!chrec_contains_symbols_defined_in_loop (op0
, CHREC_VARIABLE (op0
)));
412 switch (TREE_CODE (op1
))
414 case POLYNOMIAL_CHREC
:
416 (!chrec_contains_symbols_defined_in_loop (op1
,
417 CHREC_VARIABLE (op1
)));
418 return chrec_fold_multiply_poly_poly (type
, op0
, op1
);
421 if (tree_contains_chrecs (op1
, NULL
))
422 return chrec_dont_know
;
426 if (integer_onep (op1
))
428 if (integer_zerop (op1
))
429 return build_int_cst (type
, 0);
431 return build_polynomial_chrec
432 (CHREC_VARIABLE (op0
),
433 chrec_fold_multiply (type
, CHREC_LEFT (op0
), op1
),
434 chrec_fold_multiply (type
, CHREC_RIGHT (op0
), op1
));
438 if (tree_contains_chrecs (op0
, NULL
))
439 return chrec_dont_know
;
443 if (integer_onep (op0
))
446 if (integer_zerop (op0
))
447 return build_int_cst (type
, 0);
449 switch (TREE_CODE (op1
))
451 case POLYNOMIAL_CHREC
:
453 (!chrec_contains_symbols_defined_in_loop (op1
,
454 CHREC_VARIABLE (op1
)));
455 return build_polynomial_chrec
456 (CHREC_VARIABLE (op1
),
457 chrec_fold_multiply (type
, CHREC_LEFT (op1
), op0
),
458 chrec_fold_multiply (type
, CHREC_RIGHT (op1
), op0
));
461 if (tree_contains_chrecs (op1
, NULL
))
462 return chrec_dont_know
;
466 if (integer_onep (op1
))
468 if (integer_zerop (op1
))
469 return build_int_cst (type
, 0);
470 return fold_build2 (MULT_EXPR
, type
, op0
, op1
);
479 /* Evaluate the binomial coefficient. Return NULL_TREE if the intermediate
480 calculation overflows, otherwise return C(n,k) with type TYPE. */
483 tree_fold_binomial (tree type
, tree n
, unsigned int k
)
485 wi::overflow_type overflow
;
488 /* Handle the most frequent cases. */
490 return build_int_cst (type
, 1);
492 return fold_convert (type
, n
);
494 widest_int num
= wi::to_widest (n
);
496 /* Check that k <= n. */
497 if (wi::ltu_p (num
, k
))
500 /* Denominator = 2. */
501 widest_int denom
= 2;
503 /* Index = Numerator-1. */
504 widest_int idx
= num
- 1;
506 /* Numerator = Numerator*Index = n*(n-1). */
507 num
= wi::smul (num
, idx
, &overflow
);
511 for (i
= 3; i
<= k
; i
++)
516 /* Numerator *= Index. */
517 num
= wi::smul (num
, idx
, &overflow
);
521 /* Denominator *= i. */
525 /* Result = Numerator / Denominator. */
526 num
= wi::udiv_trunc (num
, denom
);
527 if (! wi::fits_to_tree_p (num
, type
))
529 return wide_int_to_tree (type
, num
);
532 /* Helper function. Use the Newton's interpolating formula for
533 evaluating the value of the evolution function.
534 The result may be in an unsigned type of CHREC. */
537 chrec_evaluate (unsigned var
, tree chrec
, tree n
, unsigned int k
)
539 tree arg0
, arg1
, binomial_n_k
;
540 tree type
= TREE_TYPE (chrec
);
541 class loop
*var_loop
= get_loop (cfun
, var
);
543 while (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
544 && flow_loop_nested_p (var_loop
, get_chrec_loop (chrec
)))
545 chrec
= CHREC_LEFT (chrec
);
547 /* The formula associates the expression and thus we have to make
548 sure to not introduce undefined overflow. */
550 if (INTEGRAL_TYPE_P (type
)
551 && ! TYPE_OVERFLOW_WRAPS (type
))
552 ctype
= unsigned_type_for (type
);
554 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
555 && CHREC_VARIABLE (chrec
) == var
)
557 arg1
= chrec_evaluate (var
, CHREC_RIGHT (chrec
), n
, k
+ 1);
558 if (arg1
== chrec_dont_know
)
559 return chrec_dont_know
;
560 binomial_n_k
= tree_fold_binomial (ctype
, n
, k
);
562 return chrec_dont_know
;
563 tree l
= chrec_convert (ctype
, CHREC_LEFT (chrec
), NULL
);
564 arg0
= fold_build2 (MULT_EXPR
, ctype
, l
, binomial_n_k
);
565 return chrec_fold_plus (ctype
, arg0
, arg1
);
568 binomial_n_k
= tree_fold_binomial (ctype
, n
, k
);
570 return chrec_dont_know
;
572 return fold_build2 (MULT_EXPR
, ctype
,
573 chrec_convert (ctype
, chrec
, NULL
), binomial_n_k
);
576 /* Evaluates "CHREC (X)" when the varying variable is VAR.
577 Example: Given the following parameters,
583 The result is given by the Newton's interpolating formula:
584 3 * \binom{10}{0} + 4 * \binom{10}{1}.
588 chrec_apply (unsigned var
,
592 tree type
= chrec_type (chrec
);
593 tree res
= chrec_dont_know
;
595 if (automatically_generated_chrec_p (chrec
)
596 || automatically_generated_chrec_p (x
)
598 /* When the symbols are defined in an outer loop, it is possible
599 to symbolically compute the apply, since the symbols are
600 constants with respect to the varying loop. */
601 || chrec_contains_symbols_defined_in_loop (chrec
, var
))
602 return chrec_dont_know
;
604 if (dump_file
&& (dump_flags
& TDF_SCEV
))
605 fprintf (dump_file
, "(chrec_apply \n");
607 if (TREE_CODE (x
) == INTEGER_CST
&& SCALAR_FLOAT_TYPE_P (type
))
608 x
= build_real_from_int_cst (type
, x
);
610 switch (TREE_CODE (chrec
))
612 case POLYNOMIAL_CHREC
:
613 if (evolution_function_is_affine_p (chrec
))
615 if (CHREC_VARIABLE (chrec
) != var
)
616 return build_polynomial_chrec
617 (CHREC_VARIABLE (chrec
),
618 chrec_apply (var
, CHREC_LEFT (chrec
), x
),
619 chrec_apply (var
, CHREC_RIGHT (chrec
), x
));
621 /* "{a, +, b} (x)" -> "a + b*x". */
622 x
= chrec_convert_rhs (type
, x
, NULL
);
623 res
= chrec_fold_multiply (TREE_TYPE (x
), CHREC_RIGHT (chrec
), x
);
624 res
= chrec_fold_plus (type
, CHREC_LEFT (chrec
), res
);
626 else if (TREE_CODE (x
) == INTEGER_CST
627 && tree_int_cst_sgn (x
) == 1)
628 /* testsuite/.../ssa-chrec-38.c. */
629 res
= chrec_convert (type
, chrec_evaluate (var
, chrec
, x
, 0), NULL
);
631 res
= chrec_dont_know
;
635 res
= chrec_convert (TREE_TYPE (chrec
),
636 chrec_apply (var
, TREE_OPERAND (chrec
, 0), x
),
645 if (dump_file
&& (dump_flags
& TDF_SCEV
))
647 fprintf (dump_file
, " (varying_loop = %d\n", var
);
648 fprintf (dump_file
, ")\n (chrec = ");
649 print_generic_expr (dump_file
, chrec
);
650 fprintf (dump_file
, ")\n (x = ");
651 print_generic_expr (dump_file
, x
);
652 fprintf (dump_file
, ")\n (res = ");
653 print_generic_expr (dump_file
, res
);
654 fprintf (dump_file
, "))\n");
660 /* For a given CHREC and an induction variable map IV_MAP that maps
661 (loop->num, expr) for every loop number of the current_loops an
662 expression, calls chrec_apply when the expression is not NULL. */
665 chrec_apply_map (tree chrec
, vec
<tree
> iv_map
)
670 FOR_EACH_VEC_ELT (iv_map
, i
, expr
)
672 chrec
= chrec_apply (i
, chrec
, expr
);
677 /* Replaces the initial condition in CHREC with INIT_COND. */
680 chrec_replace_initial_condition (tree chrec
,
683 if (automatically_generated_chrec_p (chrec
))
686 gcc_assert (chrec_type (chrec
) == chrec_type (init_cond
));
688 switch (TREE_CODE (chrec
))
690 case POLYNOMIAL_CHREC
:
691 return build_polynomial_chrec
692 (CHREC_VARIABLE (chrec
),
693 chrec_replace_initial_condition (CHREC_LEFT (chrec
), init_cond
),
694 CHREC_RIGHT (chrec
));
701 /* Returns the initial condition of a given CHREC. */
704 initial_condition (tree chrec
)
706 if (automatically_generated_chrec_p (chrec
))
709 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
710 return initial_condition (CHREC_LEFT (chrec
));
715 /* Returns a univariate function that represents the evolution in
716 LOOP_NUM. Mask the evolution of any other loop. */
719 hide_evolution_in_other_loops_than_loop (tree chrec
,
722 class loop
*loop
= get_loop (cfun
, loop_num
), *chloop
;
723 if (automatically_generated_chrec_p (chrec
))
726 switch (TREE_CODE (chrec
))
728 case POLYNOMIAL_CHREC
:
729 chloop
= get_chrec_loop (chrec
);
732 return build_polynomial_chrec
734 hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec
),
736 CHREC_RIGHT (chrec
));
738 else if (flow_loop_nested_p (chloop
, loop
))
739 /* There is no evolution in this loop. */
740 return initial_condition (chrec
);
742 else if (flow_loop_nested_p (loop
, chloop
))
743 return hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec
),
747 return chrec_dont_know
;
754 /* Returns the evolution part of CHREC in LOOP_NUM when RIGHT is
755 true, otherwise returns the initial condition in LOOP_NUM. */
758 chrec_component_in_loop_num (tree chrec
,
763 class loop
*loop
= get_loop (cfun
, loop_num
), *chloop
;
765 if (automatically_generated_chrec_p (chrec
))
768 switch (TREE_CODE (chrec
))
770 case POLYNOMIAL_CHREC
:
771 chloop
= get_chrec_loop (chrec
);
776 component
= CHREC_RIGHT (chrec
);
778 component
= CHREC_LEFT (chrec
);
780 if (TREE_CODE (CHREC_LEFT (chrec
)) != POLYNOMIAL_CHREC
781 || CHREC_VARIABLE (CHREC_LEFT (chrec
)) != CHREC_VARIABLE (chrec
))
785 return build_polynomial_chrec
787 chrec_component_in_loop_num (CHREC_LEFT (chrec
),
793 else if (flow_loop_nested_p (chloop
, loop
))
794 /* There is no evolution part in this loop. */
799 gcc_assert (flow_loop_nested_p (loop
, chloop
));
800 return chrec_component_in_loop_num (CHREC_LEFT (chrec
),
813 /* Returns the evolution part in LOOP_NUM. Example: the call
814 evolution_part_in_loop_num ({{0, +, 1}_1, +, 2}_1, 1) returns
818 evolution_part_in_loop_num (tree chrec
,
821 return chrec_component_in_loop_num (chrec
, loop_num
, true);
824 /* Returns the initial condition in LOOP_NUM. Example: the call
825 initial_condition_in_loop_num ({{0, +, 1}_1, +, 2}_2, 2) returns
829 initial_condition_in_loop_num (tree chrec
,
832 return chrec_component_in_loop_num (chrec
, loop_num
, false);
835 /* Set or reset the evolution of CHREC to NEW_EVOL in loop LOOP_NUM.
836 This function is essentially used for setting the evolution to
837 chrec_dont_know, for example after having determined that it is
838 impossible to say how many times a loop will execute. */
841 reset_evolution_in_loop (unsigned loop_num
,
845 class loop
*loop
= get_loop (cfun
, loop_num
);
847 if (POINTER_TYPE_P (chrec_type (chrec
)))
848 gcc_assert (ptrofftype_p (chrec_type (new_evol
)));
850 gcc_assert (chrec_type (chrec
) == chrec_type (new_evol
));
852 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
853 && flow_loop_nested_p (loop
, get_chrec_loop (chrec
)))
855 tree left
= reset_evolution_in_loop (loop_num
, CHREC_LEFT (chrec
),
857 tree right
= reset_evolution_in_loop (loop_num
, CHREC_RIGHT (chrec
),
859 return build_polynomial_chrec (CHREC_VARIABLE (chrec
), left
, right
);
862 while (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
863 && CHREC_VARIABLE (chrec
) == loop_num
)
864 chrec
= CHREC_LEFT (chrec
);
866 return build_polynomial_chrec (loop_num
, chrec
, new_evol
);
869 /* Merges two evolution functions that were found by following two
870 alternate paths of a conditional expression. */
873 chrec_merge (tree chrec1
,
876 if (chrec1
== chrec_dont_know
877 || chrec2
== chrec_dont_know
)
878 return chrec_dont_know
;
880 if (chrec1
== chrec_known
881 || chrec2
== chrec_known
)
884 if (chrec1
== chrec_not_analyzed_yet
)
886 if (chrec2
== chrec_not_analyzed_yet
)
889 if (eq_evolutions_p (chrec1
, chrec2
))
892 return chrec_dont_know
;
899 /* Helper function for is_multivariate_chrec. */
902 is_multivariate_chrec_rec (const_tree chrec
, unsigned int rec_var
)
904 if (chrec
== NULL_TREE
)
907 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
909 if (CHREC_VARIABLE (chrec
) != rec_var
)
912 return (is_multivariate_chrec_rec (CHREC_LEFT (chrec
), rec_var
)
913 || is_multivariate_chrec_rec (CHREC_RIGHT (chrec
), rec_var
));
919 /* Determine whether the given chrec is multivariate or not. */
922 is_multivariate_chrec (const_tree chrec
)
924 if (chrec
== NULL_TREE
)
927 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
928 return (is_multivariate_chrec_rec (CHREC_LEFT (chrec
),
929 CHREC_VARIABLE (chrec
))
930 || is_multivariate_chrec_rec (CHREC_RIGHT (chrec
),
931 CHREC_VARIABLE (chrec
)));
936 /* Determines whether the chrec contains symbolic names or not. If LOOP isn't
937 NULL, we also consider chrec wrto outer loops of LOOP as symbol. */
940 chrec_contains_symbols (const_tree chrec
, hash_set
<const_tree
> &visited
,
945 if (chrec
== NULL_TREE
)
948 if (TREE_CODE (chrec
) == SSA_NAME
950 || TREE_CODE (chrec
) == POLY_INT_CST
951 || TREE_CODE (chrec
) == PARM_DECL
952 || TREE_CODE (chrec
) == FUNCTION_DECL
953 || TREE_CODE (chrec
) == LABEL_DECL
954 || TREE_CODE (chrec
) == RESULT_DECL
955 || TREE_CODE (chrec
) == FIELD_DECL
)
959 && TREE_CODE (chrec
) == POLYNOMIAL_CHREC
960 && flow_loop_nested_p (get_chrec_loop (chrec
), loop
))
963 if (visited
.add (chrec
))
966 n
= TREE_OPERAND_LENGTH (chrec
);
967 for (i
= 0; i
< n
; i
++)
968 if (chrec_contains_symbols (TREE_OPERAND (chrec
, i
), visited
, loop
))
973 /* Return true if CHREC contains any symbols. If LOOP is not NULL, check if
974 CHREC contains any chrec which is invariant wrto the loop (nest), in other
975 words, chrec defined by outer loops of loop, so from LOOP's point of view,
976 the chrec is considered as a SYMBOL. */
979 chrec_contains_symbols (const_tree chrec
, class loop
* loop
)
981 hash_set
<const_tree
> visited
;
982 return chrec_contains_symbols (chrec
, visited
, loop
);
985 /* Return true when CHREC contains symbolic names defined in
989 chrec_contains_symbols_defined_in_loop (const_tree chrec
, unsigned loop_nb
,
990 hash_set
<const_tree
> &visited
)
994 if (chrec
== NULL_TREE
)
997 if (is_gimple_min_invariant (chrec
))
1000 if (TREE_CODE (chrec
) == SSA_NAME
)
1003 loop_p def_loop
, loop
;
1005 if (SSA_NAME_IS_DEFAULT_DEF (chrec
))
1008 def
= SSA_NAME_DEF_STMT (chrec
);
1009 def_loop
= loop_containing_stmt (def
);
1010 loop
= get_loop (cfun
, loop_nb
);
1012 if (def_loop
== NULL
)
1015 if (loop
== def_loop
|| flow_loop_nested_p (loop
, def_loop
))
1021 if (visited
.add (chrec
))
1024 n
= TREE_OPERAND_LENGTH (chrec
);
1025 for (i
= 0; i
< n
; i
++)
1026 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec
, i
),
1032 /* Return true when CHREC contains symbolic names defined in
1036 chrec_contains_symbols_defined_in_loop (const_tree chrec
, unsigned loop_nb
)
1038 hash_set
<const_tree
> visited
;
1039 return chrec_contains_symbols_defined_in_loop (chrec
, loop_nb
, visited
);
1042 /* Determines whether the chrec contains undetermined coefficients. */
1045 chrec_contains_undetermined (const_tree chrec
, hash_set
<const_tree
> &visited
)
1049 if (chrec
== chrec_dont_know
)
1052 if (chrec
== NULL_TREE
)
1055 if (visited
.add (chrec
))
1058 n
= TREE_OPERAND_LENGTH (chrec
);
1059 for (i
= 0; i
< n
; i
++)
1060 if (chrec_contains_undetermined (TREE_OPERAND (chrec
, i
), visited
))
1066 chrec_contains_undetermined (const_tree chrec
)
1068 hash_set
<const_tree
> visited
;
1069 return chrec_contains_undetermined (chrec
, visited
);
1072 /* Determines whether the tree EXPR contains chrecs, and increment
1073 SIZE if it is not a NULL pointer by an estimation of the depth of
1077 tree_contains_chrecs (const_tree expr
, int *size
, hash_set
<const_tree
> &visited
)
1081 if (expr
== NULL_TREE
)
1087 if (tree_is_chrec (expr
))
1090 if (visited
.add (expr
))
1093 n
= TREE_OPERAND_LENGTH (expr
);
1094 for (i
= 0; i
< n
; i
++)
1095 if (tree_contains_chrecs (TREE_OPERAND (expr
, i
), size
, visited
))
1101 tree_contains_chrecs (const_tree expr
, int *size
)
1103 hash_set
<const_tree
> visited
;
1104 return tree_contains_chrecs (expr
, size
, visited
);
1108 /* Recursive helper function. */
1111 evolution_function_is_invariant_rec_p (tree chrec
, int loopnum
)
1113 if (evolution_function_is_constant_p (chrec
))
1116 if (TREE_CODE (chrec
) == SSA_NAME
1118 || expr_invariant_in_loop_p (get_loop (cfun
, loopnum
), chrec
)))
1121 if (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
1123 if (CHREC_VARIABLE (chrec
) == (unsigned) loopnum
1124 || flow_loop_nested_p (get_loop (cfun
, loopnum
),
1125 get_chrec_loop (chrec
))
1126 || !evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec
),
1128 || !evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec
),
1134 switch (TREE_OPERAND_LENGTH (chrec
))
1137 if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec
, 1),
1143 if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec
, 0),
1155 /* Return true if CHREC is invariant in loop LOOPNUM, false otherwise. */
1158 evolution_function_is_invariant_p (tree chrec
, int loopnum
)
1160 return evolution_function_is_invariant_rec_p (chrec
, loopnum
);
1163 /* Determine whether the given tree is an affine multivariate
1167 evolution_function_is_affine_multivariate_p (const_tree chrec
, int loopnum
)
1169 if (chrec
== NULL_TREE
)
1172 switch (TREE_CODE (chrec
))
1174 case POLYNOMIAL_CHREC
:
1175 if (evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec
), loopnum
))
1177 if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec
), loopnum
))
1181 if (TREE_CODE (CHREC_RIGHT (chrec
)) == POLYNOMIAL_CHREC
1182 && CHREC_VARIABLE (CHREC_RIGHT (chrec
))
1183 != CHREC_VARIABLE (chrec
)
1184 && evolution_function_is_affine_multivariate_p
1185 (CHREC_RIGHT (chrec
), loopnum
))
1193 if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec
), loopnum
)
1194 && TREE_CODE (CHREC_LEFT (chrec
)) == POLYNOMIAL_CHREC
1195 && CHREC_VARIABLE (CHREC_LEFT (chrec
)) != CHREC_VARIABLE (chrec
)
1196 && evolution_function_is_affine_multivariate_p
1197 (CHREC_LEFT (chrec
), loopnum
))
1208 /* Determine whether the given tree is a function in zero or one
1209 variables with respect to loop specified by LOOPNUM. Note only positive
1210 LOOPNUM stands for a real loop. */
1213 evolution_function_is_univariate_p (const_tree chrec
, int loopnum
)
1215 if (chrec
== NULL_TREE
)
1219 switch (TREE_CODE (chrec
))
1221 case POLYNOMIAL_CHREC
:
1222 switch (TREE_CODE (CHREC_LEFT (chrec
)))
1224 case POLYNOMIAL_CHREC
:
1225 sub_chrec
= CHREC_LEFT (chrec
);
1226 if (CHREC_VARIABLE (chrec
) != CHREC_VARIABLE (sub_chrec
)
1228 || CHREC_VARIABLE (sub_chrec
) == (unsigned) loopnum
1229 || flow_loop_nested_p (get_loop (cfun
, loopnum
),
1230 get_chrec_loop (sub_chrec
))))
1232 if (!evolution_function_is_univariate_p (sub_chrec
, loopnum
))
1237 if (tree_contains_chrecs (CHREC_LEFT (chrec
), NULL
))
1242 switch (TREE_CODE (CHREC_RIGHT (chrec
)))
1244 case POLYNOMIAL_CHREC
:
1245 sub_chrec
= CHREC_RIGHT (chrec
);
1246 if (CHREC_VARIABLE (chrec
) != CHREC_VARIABLE (sub_chrec
)
1248 || CHREC_VARIABLE (sub_chrec
) == (unsigned) loopnum
1249 || flow_loop_nested_p (get_loop (cfun
, loopnum
),
1250 get_chrec_loop (sub_chrec
))))
1252 if (!evolution_function_is_univariate_p (sub_chrec
, loopnum
))
1257 if (tree_contains_chrecs (CHREC_RIGHT (chrec
), NULL
))
1268 /* Returns the number of variables of CHREC. Example: the call
1269 nb_vars_in_chrec ({{0, +, 1}_5, +, 2}_6) returns 2. */
1272 nb_vars_in_chrec (tree chrec
)
1274 if (chrec
== NULL_TREE
)
1277 switch (TREE_CODE (chrec
))
1279 case POLYNOMIAL_CHREC
:
1280 return 1 + nb_vars_in_chrec
1281 (initial_condition_in_loop_num (chrec
, CHREC_VARIABLE (chrec
)));
1288 /* Converts BASE and STEP of affine scev to TYPE. LOOP is the loop whose iv
1289 the scev corresponds to. AT_STMT is the statement at that the scev is
1290 evaluated. USE_OVERFLOW_SEMANTICS is true if this function should assume
1291 that the rules for overflow of the given language apply (e.g., that signed
1292 arithmetics in C does not overflow) -- i.e., to use them to avoid
1293 unnecessary tests, but also to enforce that the result follows them.
1294 FROM is the source variable converted if it's not NULL. Returns true if
1295 the conversion succeeded, false otherwise. */
1298 convert_affine_scev (class loop
*loop
, tree type
,
1299 tree
*base
, tree
*step
, gimple
*at_stmt
,
1300 bool use_overflow_semantics
, tree from
)
1302 tree ct
= TREE_TYPE (*step
);
1303 bool enforce_overflow_semantics
;
1304 bool must_check_src_overflow
, must_check_rslt_overflow
;
1305 tree new_base
, new_step
;
1306 tree step_type
= POINTER_TYPE_P (type
) ? sizetype
: type
;
1309 (TYPE) (BASE + STEP * i) = (TYPE) BASE + (TYPE -- sign extend) STEP * i,
1310 but we must check some assumptions.
1312 1) If [BASE, +, STEP] wraps, the equation is not valid when precision
1313 of CT is smaller than the precision of TYPE. For example, when we
1314 cast unsigned char [254, +, 1] to unsigned, the values on left side
1315 are 254, 255, 0, 1, ..., but those on the right side are
1316 254, 255, 256, 257, ...
1317 2) In case that we must also preserve the fact that signed ivs do not
1318 overflow, we must additionally check that the new iv does not wrap.
1319 For example, unsigned char [125, +, 1] casted to signed char could
1320 become a wrapping variable with values 125, 126, 127, -128, -127, ...,
1321 which would confuse optimizers that assume that this does not
1323 must_check_src_overflow
= TYPE_PRECISION (ct
) < TYPE_PRECISION (type
);
1325 enforce_overflow_semantics
= (use_overflow_semantics
1326 && nowrap_type_p (type
));
1327 if (enforce_overflow_semantics
)
1329 /* We can avoid checking whether the result overflows in the following
1332 -- must_check_src_overflow is true, and the range of TYPE is superset
1333 of the range of CT -- i.e., in all cases except if CT signed and
1335 -- both CT and TYPE have the same precision and signedness, and we
1336 verify instead that the source does not overflow (this may be
1337 easier than verifying it for the result, as we may use the
1338 information about the semantics of overflow in CT). */
1339 if (must_check_src_overflow
)
1341 if (TYPE_UNSIGNED (type
) && !TYPE_UNSIGNED (ct
))
1342 must_check_rslt_overflow
= true;
1344 must_check_rslt_overflow
= false;
1346 else if (TYPE_UNSIGNED (ct
) == TYPE_UNSIGNED (type
)
1347 && TYPE_PRECISION (ct
) == TYPE_PRECISION (type
))
1349 must_check_rslt_overflow
= false;
1350 must_check_src_overflow
= true;
1353 must_check_rslt_overflow
= true;
1356 must_check_rslt_overflow
= false;
1358 if (must_check_src_overflow
1359 && scev_probably_wraps_p (from
, *base
, *step
, at_stmt
, loop
,
1360 use_overflow_semantics
))
1363 new_base
= chrec_convert (type
, *base
, at_stmt
, use_overflow_semantics
);
1364 /* The step must be sign extended, regardless of the signedness
1365 of CT and TYPE. This only needs to be handled specially when
1366 CT is unsigned -- to avoid e.g. unsigned char [100, +, 255]
1367 (with values 100, 99, 98, ...) from becoming signed or unsigned
1368 [100, +, 255] with values 100, 355, ...; the sign-extension is
1369 performed by default when CT is signed. */
1371 if (TYPE_PRECISION (step_type
) > TYPE_PRECISION (ct
) && TYPE_UNSIGNED (ct
))
1373 tree signed_ct
= build_nonstandard_integer_type (TYPE_PRECISION (ct
), 0);
1374 new_step
= chrec_convert (signed_ct
, new_step
, at_stmt
,
1375 use_overflow_semantics
);
1377 new_step
= chrec_convert (step_type
, new_step
, at_stmt
,
1378 use_overflow_semantics
);
1380 if (automatically_generated_chrec_p (new_base
)
1381 || automatically_generated_chrec_p (new_step
))
1384 if (must_check_rslt_overflow
1385 /* Note that in this case we cannot use the fact that signed variables
1386 do not overflow, as this is what we are verifying for the new iv. */
1387 && scev_probably_wraps_p (NULL_TREE
, new_base
, new_step
,
1388 at_stmt
, loop
, false))
1397 /* Convert CHREC for the right hand side of a CHREC.
1398 The increment for a pointer type is always sizetype. */
1401 chrec_convert_rhs (tree type
, tree chrec
, gimple
*at_stmt
)
1403 if (POINTER_TYPE_P (type
))
1406 return chrec_convert (type
, chrec
, at_stmt
);
1409 /* Convert CHREC to TYPE. When the analyzer knows the context in
1410 which the CHREC is built, it sets AT_STMT to the statement that
1411 contains the definition of the analyzed variable, otherwise the
1412 conversion is less accurate: the information is used for
1413 determining a more accurate estimation of the number of iterations.
1414 By default AT_STMT could be safely set to NULL_TREE.
1416 USE_OVERFLOW_SEMANTICS is true if this function should assume that
1417 the rules for overflow of the given language apply (e.g., that signed
1418 arithmetics in C does not overflow) -- i.e., to use them to avoid
1419 unnecessary tests, but also to enforce that the result follows them.
1421 FROM is the source variable converted if it's not NULL. */
1424 chrec_convert_1 (tree type
, tree chrec
, gimple
*at_stmt
,
1425 bool use_overflow_semantics
, tree from
)
1431 if (automatically_generated_chrec_p (chrec
))
1434 ct
= chrec_type (chrec
);
1435 if (useless_type_conversion_p (type
, ct
))
1438 if (!evolution_function_is_affine_p (chrec
))
1441 loop
= get_chrec_loop (chrec
);
1442 base
= CHREC_LEFT (chrec
);
1443 step
= CHREC_RIGHT (chrec
);
1445 if (convert_affine_scev (loop
, type
, &base
, &step
, at_stmt
,
1446 use_overflow_semantics
, from
))
1447 return build_polynomial_chrec (loop
->num
, base
, step
);
1449 /* If we cannot propagate the cast inside the chrec, just keep the cast. */
1451 /* Fold will not canonicalize (long)(i - 1) to (long)i - 1 because that
1452 may be more expensive. We do want to perform this optimization here
1453 though for canonicalization reasons. */
1454 if (use_overflow_semantics
1455 && (TREE_CODE (chrec
) == PLUS_EXPR
1456 || TREE_CODE (chrec
) == MINUS_EXPR
)
1457 && TREE_CODE (type
) == INTEGER_TYPE
1458 && TREE_CODE (ct
) == INTEGER_TYPE
1459 && TYPE_PRECISION (type
) > TYPE_PRECISION (ct
)
1460 && TYPE_OVERFLOW_UNDEFINED (ct
))
1461 res
= fold_build2 (TREE_CODE (chrec
), type
,
1462 fold_convert (type
, TREE_OPERAND (chrec
, 0)),
1463 fold_convert (type
, TREE_OPERAND (chrec
, 1)));
1464 /* Similar perform the trick that (signed char)((int)x + 2) can be
1465 narrowed to (signed char)((unsigned char)x + 2). */
1466 else if (use_overflow_semantics
1467 && TREE_CODE (chrec
) == POLYNOMIAL_CHREC
1468 && TREE_CODE (ct
) == INTEGER_TYPE
1469 && TREE_CODE (type
) == INTEGER_TYPE
1470 && TYPE_OVERFLOW_UNDEFINED (type
)
1471 && TYPE_PRECISION (type
) < TYPE_PRECISION (ct
))
1473 tree utype
= unsigned_type_for (type
);
1474 res
= build_polynomial_chrec (CHREC_VARIABLE (chrec
),
1475 fold_convert (utype
,
1476 CHREC_LEFT (chrec
)),
1477 fold_convert (utype
,
1478 CHREC_RIGHT (chrec
)));
1479 res
= chrec_convert_1 (type
, res
, at_stmt
, use_overflow_semantics
, from
);
1482 res
= fold_convert (type
, chrec
);
1484 /* Don't propagate overflows. */
1485 if (CONSTANT_CLASS_P (res
))
1486 TREE_OVERFLOW (res
) = 0;
1488 /* But reject constants that don't fit in their type after conversion.
1489 This can happen if TYPE_MIN_VALUE or TYPE_MAX_VALUE are not the
1490 natural values associated with TYPE_PRECISION and TYPE_UNSIGNED,
1491 and can cause problems later when computing niters of loops. Note
1492 that we don't do the check before converting because we don't want
1493 to reject conversions of negative chrecs to unsigned types. */
1494 if (TREE_CODE (res
) == INTEGER_CST
1495 && TREE_CODE (type
) == INTEGER_TYPE
1496 && !int_fits_type_p (res
, type
))
1497 res
= chrec_dont_know
;
1502 /* Convert CHREC to TYPE. When the analyzer knows the context in
1503 which the CHREC is built, it sets AT_STMT to the statement that
1504 contains the definition of the analyzed variable, otherwise the
1505 conversion is less accurate: the information is used for
1506 determining a more accurate estimation of the number of iterations.
1507 By default AT_STMT could be safely set to NULL_TREE.
1509 The following rule is always true: TREE_TYPE (chrec) ==
1510 TREE_TYPE (CHREC_LEFT (chrec)) == TREE_TYPE (CHREC_RIGHT (chrec)).
1511 An example of what could happen when adding two chrecs and the type
1512 of the CHREC_RIGHT is different than CHREC_LEFT is:
1514 {(uint) 0, +, (uchar) 10} +
1515 {(uint) 0, +, (uchar) 250}
1517 that would produce a wrong result if CHREC_RIGHT is not (uint):
1519 {(uint) 0, +, (uchar) 4}
1523 {(uint) 0, +, (uint) 260}
1525 USE_OVERFLOW_SEMANTICS is true if this function should assume that
1526 the rules for overflow of the given language apply (e.g., that signed
1527 arithmetics in C does not overflow) -- i.e., to use them to avoid
1528 unnecessary tests, but also to enforce that the result follows them.
1530 FROM is the source variable converted if it's not NULL. */
1533 chrec_convert (tree type
, tree chrec
, gimple
*at_stmt
,
1534 bool use_overflow_semantics
, tree from
)
1536 return chrec_convert_1 (type
, chrec
, at_stmt
, use_overflow_semantics
, from
);
1539 /* Convert CHREC to TYPE, without regard to signed overflows. Returns the new
1540 chrec if something else than what chrec_convert would do happens, NULL_TREE
1541 otherwise. This function set TRUE to variable pointed by FOLD_CONVERSIONS
1542 if the result chrec may overflow. */
1545 chrec_convert_aggressive (tree type
, tree chrec
, bool *fold_conversions
)
1547 tree inner_type
, left
, right
, lc
, rc
, rtype
;
1549 gcc_assert (fold_conversions
!= NULL
);
1551 if (automatically_generated_chrec_p (chrec
)
1552 || TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
1555 inner_type
= TREE_TYPE (chrec
);
1556 if (TYPE_PRECISION (type
) > TYPE_PRECISION (inner_type
))
1559 if (useless_type_conversion_p (type
, inner_type
))
1562 if (!*fold_conversions
&& evolution_function_is_affine_p (chrec
))
1567 loop
= get_chrec_loop (chrec
);
1568 base
= CHREC_LEFT (chrec
);
1569 step
= CHREC_RIGHT (chrec
);
1570 if (convert_affine_scev (loop
, type
, &base
, &step
, NULL
, true))
1571 return build_polynomial_chrec (loop
->num
, base
, step
);
1573 rtype
= POINTER_TYPE_P (type
) ? sizetype
: type
;
1575 left
= CHREC_LEFT (chrec
);
1576 right
= CHREC_RIGHT (chrec
);
1577 lc
= chrec_convert_aggressive (type
, left
, fold_conversions
);
1579 lc
= chrec_convert (type
, left
, NULL
);
1580 rc
= chrec_convert_aggressive (rtype
, right
, fold_conversions
);
1582 rc
= chrec_convert (rtype
, right
, NULL
);
1584 *fold_conversions
= true;
1586 return build_polynomial_chrec (CHREC_VARIABLE (chrec
), lc
, rc
);
1589 /* Returns true when CHREC0 == CHREC1. */
1592 eq_evolutions_p (const_tree chrec0
, const_tree chrec1
)
1594 if (chrec0
== NULL_TREE
1595 || chrec1
== NULL_TREE
1596 || TREE_CODE (chrec0
) != TREE_CODE (chrec1
))
1599 if (chrec0
== chrec1
)
1602 if (! types_compatible_p (TREE_TYPE (chrec0
), TREE_TYPE (chrec1
)))
1605 switch (TREE_CODE (chrec0
))
1607 case POLYNOMIAL_CHREC
:
1608 return (CHREC_VARIABLE (chrec0
) == CHREC_VARIABLE (chrec1
)
1609 && eq_evolutions_p (CHREC_LEFT (chrec0
), CHREC_LEFT (chrec1
))
1610 && eq_evolutions_p (CHREC_RIGHT (chrec0
), CHREC_RIGHT (chrec1
)));
1615 case POINTER_PLUS_EXPR
:
1616 return eq_evolutions_p (TREE_OPERAND (chrec0
, 0),
1617 TREE_OPERAND (chrec1
, 0))
1618 && eq_evolutions_p (TREE_OPERAND (chrec0
, 1),
1619 TREE_OPERAND (chrec1
, 1));
1622 return eq_evolutions_p (TREE_OPERAND (chrec0
, 0),
1623 TREE_OPERAND (chrec1
, 0));
1626 return operand_equal_p (chrec0
, chrec1
, 0);
1630 /* Returns EV_GROWS if CHREC grows (assuming that it does not overflow),
1631 EV_DECREASES if it decreases, and EV_UNKNOWN if we cannot determine
1632 which of these cases happens. */
1635 scev_direction (const_tree chrec
)
1639 if (!evolution_function_is_affine_p (chrec
))
1640 return EV_DIR_UNKNOWN
;
1642 step
= CHREC_RIGHT (chrec
);
1643 if (TREE_CODE (step
) != INTEGER_CST
)
1644 return EV_DIR_UNKNOWN
;
1646 if (tree_int_cst_sign_bit (step
))
1647 return EV_DIR_DECREASES
;
1649 return EV_DIR_GROWS
;
1652 /* Iterates over all the components of SCEV, and calls CBCK. */
1655 for_each_scev_op (tree
*scev
, bool (*cbck
) (tree
*, void *), void *data
)
1657 switch (TREE_CODE_LENGTH (TREE_CODE (*scev
)))
1660 for_each_scev_op (&TREE_OPERAND (*scev
, 2), cbck
, data
);
1664 for_each_scev_op (&TREE_OPERAND (*scev
, 1), cbck
, data
);
1668 for_each_scev_op (&TREE_OPERAND (*scev
, 0), cbck
, data
);
1677 /* Returns true when the operation can be part of a linear
1681 operator_is_linear (tree scev
)
1683 switch (TREE_CODE (scev
))
1686 case POLYNOMIAL_CHREC
:
1688 case POINTER_PLUS_EXPR
:
1693 case NON_LVALUE_EXPR
:
1703 /* Return true when SCEV is a linear expression. Linear expressions
1704 can contain additions, substractions and multiplications.
1705 Multiplications are restricted to constant scaling: "cst * x". */
1708 scev_is_linear_expression (tree scev
)
1710 if (evolution_function_is_constant_p (scev
))
1714 || !operator_is_linear (scev
))
1717 if (TREE_CODE (scev
) == MULT_EXPR
)
1718 return !(tree_contains_chrecs (TREE_OPERAND (scev
, 0), NULL
)
1719 && tree_contains_chrecs (TREE_OPERAND (scev
, 1), NULL
));
1721 if (TREE_CODE (scev
) == POLYNOMIAL_CHREC
1722 && !evolution_function_is_affine_multivariate_p (scev
, CHREC_VARIABLE (scev
)))
1725 switch (TREE_CODE_LENGTH (TREE_CODE (scev
)))
1728 return scev_is_linear_expression (TREE_OPERAND (scev
, 0))
1729 && scev_is_linear_expression (TREE_OPERAND (scev
, 1))
1730 && scev_is_linear_expression (TREE_OPERAND (scev
, 2));
1733 return scev_is_linear_expression (TREE_OPERAND (scev
, 0))
1734 && scev_is_linear_expression (TREE_OPERAND (scev
, 1));
1737 return scev_is_linear_expression (TREE_OPERAND (scev
, 0));
1747 /* Determines whether the expression CHREC contains only interger consts
1748 in the right parts. */
1751 evolution_function_right_is_integer_cst (const_tree chrec
)
1753 if (chrec
== NULL_TREE
)
1756 switch (TREE_CODE (chrec
))
1761 case POLYNOMIAL_CHREC
:
1762 return TREE_CODE (CHREC_RIGHT (chrec
)) == INTEGER_CST
1763 && (TREE_CODE (CHREC_LEFT (chrec
)) != POLYNOMIAL_CHREC
1764 || evolution_function_right_is_integer_cst (CHREC_LEFT (chrec
)));
1767 return evolution_function_right_is_integer_cst (TREE_OPERAND (chrec
, 0));