Merged r157653 through r157895 into branch.
[official-gcc.git] / gcc / tree-chrec.c
blob929bbc08e02feb683f3c6c80d7659a1f04a87093
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
11 version.
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
16 for more details.
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
24 variables.
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "ggc.h"
32 #include "tree.h"
33 #include "real.h"
34 #include "diagnostic.h"
35 #include "cfgloop.h"
36 #include "tree-flow.h"
37 #include "tree-chrec.h"
38 #include "tree-pass.h"
39 #include "params.h"
40 #include "flags.h"
41 #include "tree-scalar-evolution.h"
45 /* Extended folder for chrecs. */
47 /* Determines whether CST is not a constant evolution. */
49 static inline bool
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. */
57 static inline tree
58 chrec_fold_poly_cst (enum tree_code code,
59 tree type,
60 tree poly,
61 tree cst)
63 gcc_assert (poly);
64 gcc_assert (cst);
65 gcc_assert (TREE_CODE (poly) == POLYNOMIAL_CHREC);
66 gcc_assert (!is_not_constant_evolution (cst));
67 gcc_assert (type == chrec_type (poly));
69 switch (code)
71 case PLUS_EXPR:
72 return build_polynomial_chrec
73 (CHREC_VARIABLE (poly),
74 chrec_fold_plus (type, CHREC_LEFT (poly), cst),
75 CHREC_RIGHT (poly));
77 case MINUS_EXPR:
78 return build_polynomial_chrec
79 (CHREC_VARIABLE (poly),
80 chrec_fold_minus (type, CHREC_LEFT (poly), cst),
81 CHREC_RIGHT (poly));
83 case MULT_EXPR:
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));
89 default:
90 return chrec_dont_know;
94 /* Fold the addition of two polynomial functions. */
96 static inline tree
97 chrec_fold_plus_poly_poly (enum tree_code code,
98 tree type,
99 tree poly0,
100 tree poly1)
102 tree left, right;
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;
107 gcc_assert (poly0);
108 gcc_assert (poly1);
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);
113 else
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));
128 else
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));
145 else
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));
163 else
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))
172 return left;
173 else
174 return build_polynomial_chrec
175 (CHREC_VARIABLE (poly0), left, right);
180 /* Fold the multiplication of two polynomial functions. */
182 static inline tree
183 chrec_fold_multiply_poly_poly (tree type,
184 tree poly0,
185 tree poly1)
187 tree t0, t1, t2;
188 int var;
189 struct loop *loop0 = get_chrec_loop (poly0);
190 struct loop *loop1 = get_chrec_loop (poly1);
192 gcc_assert (poly0);
193 gcc_assert (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. */
221 /* "a*c". */
222 t0 = chrec_fold_multiply (type, CHREC_LEFT (poly0), CHREC_LEFT (poly1));
224 /* "a*d + b*c". */
225 t1 = chrec_fold_multiply (type, CHREC_LEFT (poly0), CHREC_RIGHT (poly1));
226 t1 = chrec_fold_plus (type, t1, chrec_fold_multiply (type,
227 CHREC_RIGHT (poly0),
228 CHREC_LEFT (poly1)));
229 /* "b*d". */
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);
233 /* "2*b*d". */
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. */
246 static inline tree
247 chrec_fold_automatically_generated_operands (tree op0,
248 tree op1)
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)
256 return 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. */
268 static tree
269 chrec_fold_plus_1 (enum tree_code code, tree type,
270 tree op0, tree op1)
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);
286 CASE_CONVERT:
287 if (tree_contains_chrecs (op1, NULL))
288 return chrec_dont_know;
290 default:
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),
295 CHREC_RIGHT (op0));
296 else
297 return build_polynomial_chrec
298 (CHREC_VARIABLE (op0),
299 chrec_fold_minus (type, CHREC_LEFT (op0), op1),
300 CHREC_RIGHT (op0));
303 CASE_CONVERT:
304 if (tree_contains_chrecs (op0, NULL))
305 return chrec_dont_know;
307 default:
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)),
315 CHREC_RIGHT (op1));
316 else
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)));
325 CASE_CONVERT:
326 if (tree_contains_chrecs (op1, NULL))
327 return chrec_dont_know;
329 default:
331 int size = 0;
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));
340 else
341 return chrec_dont_know;
347 /* Fold the addition of two chrecs. */
349 tree
350 chrec_fold_plus (tree type,
351 tree op0,
352 tree op1)
354 enum tree_code code;
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;
366 else
367 code = PLUS_EXPR;
369 return chrec_fold_plus_1 (code, type, op0, op1);
372 /* Fold the subtraction of two chrecs. */
374 tree
375 chrec_fold_minus (tree type,
376 tree op0,
377 tree op1)
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))
384 return op0;
386 return chrec_fold_plus_1 (MINUS_EXPR, type, op0, op1);
389 /* Fold the multiplication of two chrecs. */
391 tree
392 chrec_fold_multiply (tree type,
393 tree op0,
394 tree op1)
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);
408 CASE_CONVERT:
409 if (tree_contains_chrecs (op1, NULL))
410 return chrec_dont_know;
412 default:
413 if (integer_onep (op1))
414 return op0;
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));
424 CASE_CONVERT:
425 if (tree_contains_chrecs (op0, NULL))
426 return chrec_dont_know;
428 default:
429 if (integer_onep (op0))
430 return op1;
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));
443 CASE_CONVERT:
444 if (tree_contains_chrecs (op1, NULL))
445 return chrec_dont_know;
447 default:
448 if (integer_onep (op1))
449 return op0;
450 if (integer_zerop (op1))
451 return build_int_cst (type, 0);
452 return fold_build2 (MULT_EXPR, type, op0, op1);
459 /* Operations. */
461 /* Evaluate the binomial coefficient. Return NULL_TREE if the intermediate
462 calculation overflows, otherwise return C(n,k) with type TYPE. */
464 static tree
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;
469 unsigned int i;
470 tree res;
472 /* Handle the most frequent cases. */
473 if (k == 0)
474 return build_int_cst (type, 1);
475 if (k == 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)
481 return NULL_TREE;
483 /* Numerator = n. */
484 lnum = TREE_INT_CST_LOW (n);
485 hnum = TREE_INT_CST_HIGH (n);
487 /* Denominator = 2. */
488 ldenom = 2;
489 hdenom = 0;
491 /* Index = Numerator-1. */
492 if (lnum == 0)
494 hidx = hnum - 1;
495 lidx = ~ (unsigned HOST_WIDE_INT) 0;
497 else
499 hidx = hnum;
500 lidx = lnum - 1;
503 /* Numerator = Numerator*Index = n*(n-1). */
504 if (mul_double (lnum, hnum, lidx, hidx, &lnum, &hnum))
505 return NULL_TREE;
507 for (i = 3; i <= k; i++)
509 /* Index--. */
510 if (lidx == 0)
512 hidx--;
513 lidx = ~ (unsigned HOST_WIDE_INT) 0;
515 else
516 lidx--;
518 /* Numerator *= Index. */
519 if (mul_double (lnum, hnum, lidx, hidx, &lnum, &hnum))
520 return NULL_TREE;
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. */
537 static tree
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);
555 if (!binomial_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);
563 if (!binomial_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,
572 var = 1
573 chrec = {3, +, 4}_1
574 x = 10
576 The result is given by the Newton's interpolating formula:
577 3 * \binom{10}{0} + 4 * \binom{10}{1}.
580 tree
581 chrec_apply (unsigned var,
582 tree chrec,
583 tree x)
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)
612 res = 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);
618 else
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");
633 return res;
636 /* Replaces the initial condition in CHREC with INIT_COND. */
638 tree
639 chrec_replace_initial_condition (tree chrec,
640 tree init_cond)
642 if (automatically_generated_chrec_p (chrec))
643 return 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));
655 default:
656 return init_cond;
660 /* Returns the initial condition of a given CHREC. */
662 tree
663 initial_condition (tree chrec)
665 if (automatically_generated_chrec_p (chrec))
666 return chrec;
668 if (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
669 return initial_condition (CHREC_LEFT (chrec));
670 else
671 return chrec;
674 /* Returns a univariate function that represents the evolution in
675 LOOP_NUM. Mask the evolution of any other loop. */
677 tree
678 hide_evolution_in_other_loops_than_loop (tree chrec,
679 unsigned loop_num)
681 struct loop *loop = get_loop (loop_num), *chloop;
682 if (automatically_generated_chrec_p (chrec))
683 return chrec;
685 switch (TREE_CODE (chrec))
687 case POLYNOMIAL_CHREC:
688 chloop = get_chrec_loop (chrec);
690 if (chloop == loop)
691 return build_polynomial_chrec
692 (loop_num,
693 hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec),
694 loop_num),
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);
701 else
703 gcc_assert (flow_loop_nested_p (loop, chloop));
704 return hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec),
705 loop_num);
708 default:
709 return chrec;
713 /* Returns the evolution part of CHREC in LOOP_NUM when RIGHT is
714 true, otherwise returns the initial condition in LOOP_NUM. */
716 static tree
717 chrec_component_in_loop_num (tree chrec,
718 unsigned loop_num,
719 bool right)
721 tree component;
722 struct loop *loop = get_loop (loop_num), *chloop;
724 if (automatically_generated_chrec_p (chrec))
725 return chrec;
727 switch (TREE_CODE (chrec))
729 case POLYNOMIAL_CHREC:
730 chloop = get_chrec_loop (chrec);
732 if (chloop == loop)
734 if (right)
735 component = CHREC_RIGHT (chrec);
736 else
737 component = CHREC_LEFT (chrec);
739 if (TREE_CODE (CHREC_LEFT (chrec)) != POLYNOMIAL_CHREC
740 || CHREC_VARIABLE (CHREC_LEFT (chrec)) != CHREC_VARIABLE (chrec))
741 return component;
743 else
744 return build_polynomial_chrec
745 (loop_num,
746 chrec_component_in_loop_num (CHREC_LEFT (chrec),
747 loop_num,
748 right),
749 component);
752 else if (flow_loop_nested_p (chloop, loop))
753 /* There is no evolution part in this loop. */
754 return NULL_TREE;
756 else
758 gcc_assert (flow_loop_nested_p (loop, chloop));
759 return chrec_component_in_loop_num (CHREC_LEFT (chrec),
760 loop_num,
761 right);
764 default:
765 if (right)
766 return NULL_TREE;
767 else
768 return 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
774 {1, +, 2}_1 */
776 tree
777 evolution_part_in_loop_num (tree chrec,
778 unsigned loop_num)
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
785 {0, +, 1}_1 */
787 tree
788 initial_condition_in_loop_num (tree chrec,
789 unsigned loop_num)
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. */
799 tree
800 reset_evolution_in_loop (unsigned loop_num,
801 tree chrec,
802 tree new_evol)
804 struct loop *loop = get_loop (loop_num);
806 if (POINTER_TYPE_P (chrec_type (chrec)))
807 gcc_assert (sizetype == chrec_type (new_evol));
808 else
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),
815 new_evol);
816 tree right = reset_evolution_in_loop (loop_num, CHREC_RIGHT (chrec),
817 new_evol);
818 return build3 (POLYNOMIAL_CHREC, TREE_TYPE (left),
819 build_int_cst (NULL_TREE, CHREC_VARIABLE (chrec)),
820 left, right);
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. */
833 tree
834 chrec_merge (tree chrec1,
835 tree chrec2)
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)
843 return chrec_known;
845 if (chrec1 == chrec_not_analyzed_yet)
846 return chrec2;
847 if (chrec2 == chrec_not_analyzed_yet)
848 return chrec1;
850 if (eq_evolutions_p (chrec1, chrec2))
851 return chrec1;
853 return chrec_dont_know;
858 /* Observers. */
860 /* Helper function for is_multivariate_chrec. */
862 static bool
863 is_multivariate_chrec_rec (const_tree chrec, unsigned int rec_var)
865 if (chrec == NULL_TREE)
866 return false;
868 if (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
870 if (CHREC_VARIABLE (chrec) != rec_var)
871 return true;
872 else
873 return (is_multivariate_chrec_rec (CHREC_LEFT (chrec), rec_var)
874 || is_multivariate_chrec_rec (CHREC_RIGHT (chrec), rec_var));
876 else
877 return false;
880 /* Determine whether the given chrec is multivariate or not. */
882 bool
883 is_multivariate_chrec (const_tree chrec)
885 if (chrec == NULL_TREE)
886 return false;
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)));
893 else
894 return false;
897 /* Determines whether the chrec contains symbolic names or not. */
899 bool
900 chrec_contains_symbols (const_tree chrec)
902 int i, n;
904 if (chrec == NULL_TREE)
905 return false;
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)
914 return true;
916 n = TREE_OPERAND_LENGTH (chrec);
917 for (i = 0; i < n; i++)
918 if (chrec_contains_symbols (TREE_OPERAND (chrec, i)))
919 return true;
920 return false;
923 /* Determines whether the chrec contains undetermined coefficients. */
925 bool
926 chrec_contains_undetermined (const_tree chrec)
928 int i, n;
930 if (chrec == chrec_dont_know)
931 return true;
933 if (chrec == NULL_TREE)
934 return false;
936 n = TREE_OPERAND_LENGTH (chrec);
937 for (i = 0; i < n; i++)
938 if (chrec_contains_undetermined (TREE_OPERAND (chrec, i)))
939 return true;
940 return false;
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
945 the tree. */
947 bool
948 tree_contains_chrecs (const_tree expr, int *size)
950 int i, n;
952 if (expr == NULL_TREE)
953 return false;
955 if (size)
956 (*size)++;
958 if (tree_is_chrec (expr))
959 return true;
961 n = TREE_OPERAND_LENGTH (expr);
962 for (i = 0; i < n; i++)
963 if (tree_contains_chrecs (TREE_OPERAND (expr, i), size))
964 return true;
965 return false;
968 /* Recursive helper function. */
970 static bool
971 evolution_function_is_invariant_rec_p (tree chrec, int loopnum)
973 if (evolution_function_is_constant_p (chrec))
974 return true;
976 if (TREE_CODE (chrec) == SSA_NAME
977 && (loopnum == 0
978 || expr_invariant_in_loop_p (get_loop (loopnum), chrec)))
979 return true;
981 if (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
983 if (CHREC_VARIABLE (chrec) == (unsigned) loopnum
984 || !evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec),
985 loopnum)
986 || !evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec),
987 loopnum))
988 return false;
989 return true;
992 switch (TREE_OPERAND_LENGTH (chrec))
994 case 2:
995 if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec, 1),
996 loopnum))
997 return false;
999 case 1:
1000 if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec, 0),
1001 loopnum))
1002 return false;
1003 return true;
1005 default:
1006 return false;
1009 return false;
1012 /* Return true if CHREC is invariant in loop LOOPNUM, false otherwise. */
1014 bool
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
1021 evolution. */
1023 bool
1024 evolution_function_is_affine_multivariate_p (const_tree chrec, int loopnum)
1026 if (chrec == NULL_TREE)
1027 return false;
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))
1035 return true;
1036 else
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))
1043 return true;
1044 else
1045 return false;
1048 else
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))
1055 return true;
1056 else
1057 return false;
1060 default:
1061 return false;
1065 /* Determine whether the given tree is a function in zero or one
1066 variables. */
1068 bool
1069 evolution_function_is_univariate_p (const_tree chrec)
1071 if (chrec == NULL_TREE)
1072 return true;
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)))
1081 return false;
1082 if (!evolution_function_is_univariate_p (CHREC_LEFT (chrec)))
1083 return false;
1084 break;
1086 default:
1087 break;
1090 switch (TREE_CODE (CHREC_RIGHT (chrec)))
1092 case POLYNOMIAL_CHREC:
1093 if (CHREC_VARIABLE (chrec) != CHREC_VARIABLE (CHREC_RIGHT (chrec)))
1094 return false;
1095 if (!evolution_function_is_univariate_p (CHREC_RIGHT (chrec)))
1096 return false;
1097 break;
1099 default:
1100 break;
1103 default:
1104 return true;
1108 /* Returns the number of variables of CHREC. Example: the call
1109 nb_vars_in_chrec ({{0, +, 1}_5, +, 2}_6) returns 2. */
1111 unsigned
1112 nb_vars_in_chrec (tree chrec)
1114 if (chrec == NULL_TREE)
1115 return 0;
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)));
1123 default:
1124 return 0;
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. */
1138 bool
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;
1149 /* In general,
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
1163 happen. */
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
1171 cases:
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
1175 TYPE unsigned.
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;
1184 else
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;
1193 else
1194 must_check_rslt_overflow = true;
1196 else
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))
1202 return false;
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. */
1212 new_step = *step;
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))
1220 return false;
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))
1226 return false;
1228 *base = new_base;
1229 *step = new_step;
1230 return true;
1234 /* Convert CHREC for the right hand side of a CREC.
1235 The increment for a pointer type is always sizetype. */
1236 tree
1237 chrec_convert_rhs (tree type, tree chrec, gimple at_stmt)
1239 if (POINTER_TYPE_P (type))
1240 type = sizetype;
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}
1263 instead of
1265 {(uint) 0, +, (uint) 260}
1268 tree
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. */
1286 static tree
1287 chrec_convert_1 (tree type, tree chrec, gimple at_stmt,
1288 bool use_overflow_semantics)
1290 tree ct, res;
1291 tree base, step;
1292 struct loop *loop;
1294 if (automatically_generated_chrec_p (chrec))
1295 return chrec;
1297 ct = chrec_type (chrec);
1298 if (ct == type)
1299 return chrec;
1301 if (!evolution_function_is_affine_p (chrec))
1302 goto keep_cast;
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. */
1313 keep_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)));
1327 else
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;
1345 return res;
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
1350 otherwise. */
1352 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)
1359 return NULL_TREE;
1361 inner_type = TREE_TYPE (chrec);
1362 if (TYPE_PRECISION (type) > TYPE_PRECISION (inner_type))
1363 return NULL_TREE;
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);
1370 if (!lc)
1371 lc = chrec_convert (type, left, NULL);
1372 rc = chrec_convert_aggressive (rtype, right);
1373 if (!rc)
1374 rc = chrec_convert (rtype, right, NULL);
1376 return build_polynomial_chrec (CHREC_VARIABLE (chrec), lc, rc);
1379 /* Returns true when CHREC0 == CHREC1. */
1381 bool
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))
1387 return false;
1389 if (chrec0 == chrec1)
1390 return true;
1392 switch (TREE_CODE (chrec0))
1394 case INTEGER_CST:
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)));
1401 default:
1402 return false;
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. */
1410 enum ev_direction
1411 scev_direction (const_tree chrec)
1413 const_tree step;
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;
1424 else
1425 return EV_DIR_GROWS;
1428 /* Iterates over all the components of SCEV, and calls CBCK. */
1430 void
1431 for_each_scev_op (tree *scev, bool (*cbck) (tree *, void *), void *data)
1433 switch (TREE_CODE_LENGTH (TREE_CODE (*scev)))
1435 case 3:
1436 for_each_scev_op (&TREE_OPERAND (*scev, 2), cbck, data);
1438 case 2:
1439 for_each_scev_op (&TREE_OPERAND (*scev, 1), cbck, data);
1441 case 1:
1442 for_each_scev_op (&TREE_OPERAND (*scev, 0), cbck, data);
1444 default:
1445 cbck (scev, data);
1446 break;
1450 /* Returns true when the operation can be part of a linear
1451 expression. */
1453 static inline bool
1454 operator_is_linear (tree scev)
1456 switch (TREE_CODE (scev))
1458 case INTEGER_CST:
1459 case POLYNOMIAL_CHREC:
1460 case PLUS_EXPR:
1461 case POINTER_PLUS_EXPR:
1462 case MULT_EXPR:
1463 case MINUS_EXPR:
1464 case NEGATE_EXPR:
1465 case SSA_NAME:
1466 case NON_LVALUE_EXPR:
1467 case BIT_NOT_EXPR:
1468 CASE_CONVERT:
1469 return true;
1471 default:
1472 return false;
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". */
1480 bool
1481 scev_is_linear_expression (tree scev)
1483 if (scev == NULL
1484 || !operator_is_linear (scev))
1485 return false;
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)))
1493 return false;
1495 switch (TREE_CODE_LENGTH (TREE_CODE (scev)))
1497 case 3:
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));
1502 case 2:
1503 return scev_is_linear_expression (TREE_OPERAND (scev, 0))
1504 && scev_is_linear_expression (TREE_OPERAND (scev, 1));
1506 case 1:
1507 return scev_is_linear_expression (TREE_OPERAND (scev, 0));
1509 case 0:
1510 return true;
1512 default:
1513 return false;
1517 /* Determines whether the expression CHREC contains only interger consts
1518 in the right parts. */
1520 bool
1521 evolution_function_right_is_integer_cst (const_tree chrec)
1523 if (chrec == NULL_TREE)
1524 return false;
1526 switch (TREE_CODE (chrec))
1528 case INTEGER_CST:
1529 return true;
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)));
1536 CASE_CONVERT:
1537 return evolution_function_right_is_integer_cst (TREE_OPERAND (chrec, 0));
1539 default:
1540 return false;