1 /* Operations with affine combinations of trees.
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "coretypes.h"
28 #include "hard-reg-set.h"
30 #include "diagnostic.h"
31 #include "tree-dump.h"
32 #include "pointer-set.h"
33 #include "tree-affine.h"
34 #include "tree-gimple.h"
36 /* Extends CST as appropriate for the affine combinations COMB. */
39 double_int_ext_for_comb (double_int cst, aff_tree *comb)
41 return double_int_sext (cst, TYPE_PRECISION (comb->type));
44 /* Initializes affine combination COMB so that its value is zero in TYPE. */
47 aff_combination_zero (aff_tree *comb, tree type)
50 comb->offset = double_int_zero;
52 comb->rest = NULL_TREE;
55 /* Sets COMB to CST. */
58 aff_combination_const (aff_tree *comb, tree type, double_int cst)
60 aff_combination_zero (comb, type);
61 comb->offset = double_int_ext_for_comb (cst, comb);
64 /* Sets COMB to single element ELT. */
67 aff_combination_elt (aff_tree *comb, tree type, tree elt)
69 aff_combination_zero (comb, type);
72 comb->elts[0].val = elt;
73 comb->elts[0].coef = double_int_one;
76 /* Scales COMB by SCALE. */
79 aff_combination_scale (aff_tree *comb, double_int scale)
83 scale = double_int_ext_for_comb (scale, comb);
84 if (double_int_one_p (scale))
87 if (double_int_zero_p (scale))
89 aff_combination_zero (comb, comb->type);
94 = double_int_ext_for_comb (double_int_mul (scale, comb->offset), comb);
95 for (i = 0, j = 0; i < comb->n; i++)
100 = double_int_ext_for_comb (double_int_mul (scale, comb->elts[i].coef),
102 /* A coefficient may become zero due to overflow. Remove the zero
104 if (double_int_zero_p (new_coef))
106 comb->elts[j].coef = new_coef;
107 comb->elts[j].val = comb->elts[i].val;
114 if (comb->n < MAX_AFF_ELTS)
116 comb->elts[comb->n].coef = scale;
117 comb->elts[comb->n].val = comb->rest;
118 comb->rest = NULL_TREE;
122 comb->rest = fold_build2 (MULT_EXPR, comb->type, comb->rest,
123 double_int_to_tree (comb->type, scale));
127 /* Adds ELT * SCALE to COMB. */
130 aff_combination_add_elt (aff_tree *comb, tree elt, double_int scale)
134 scale = double_int_ext_for_comb (scale, comb);
135 if (double_int_zero_p (scale))
138 for (i = 0; i < comb->n; i++)
139 if (operand_equal_p (comb->elts[i].val, elt, 0))
143 new_coef = double_int_add (comb->elts[i].coef, scale);
144 new_coef = double_int_ext_for_comb (new_coef, comb);
145 if (!double_int_zero_p (new_coef))
147 comb->elts[i].coef = new_coef;
152 comb->elts[i] = comb->elts[comb->n];
156 gcc_assert (comb->n == MAX_AFF_ELTS - 1);
157 comb->elts[comb->n].coef = double_int_one;
158 comb->elts[comb->n].val = comb->rest;
159 comb->rest = NULL_TREE;
164 if (comb->n < MAX_AFF_ELTS)
166 comb->elts[comb->n].coef = scale;
167 comb->elts[comb->n].val = elt;
172 if (double_int_one_p (scale))
173 elt = fold_convert (comb->type, elt);
175 elt = fold_build2 (MULT_EXPR, comb->type,
176 fold_convert (comb->type, elt),
177 double_int_to_tree (comb->type, scale));
180 comb->rest = fold_build2 (PLUS_EXPR, comb->type, comb->rest, elt);
188 aff_combination_add_cst (aff_tree *c, double_int cst)
190 c->offset = double_int_ext_for_comb (double_int_add (c->offset, cst), c);
193 /* Adds COMB2 to COMB1. */
196 aff_combination_add (aff_tree *comb1, aff_tree *comb2)
200 aff_combination_add_cst (comb1, comb2->offset);
201 for (i = 0; i < comb2->n; i++)
202 aff_combination_add_elt (comb1, comb2->elts[i].val, comb2->elts[i].coef);
204 aff_combination_add_elt (comb1, comb2->rest, double_int_one);
207 /* Converts affine combination COMB to TYPE. */
210 aff_combination_convert (aff_tree *comb, tree type)
213 tree comb_type = comb->type;
215 if (TYPE_PRECISION (type) > TYPE_PRECISION (comb_type))
217 tree val = fold_convert (type, aff_combination_to_tree (comb));
218 tree_to_aff_combination (val, type, comb);
224 comb->rest = fold_convert (type, comb->rest);
226 if (TYPE_PRECISION (type) == TYPE_PRECISION (comb_type))
229 comb->offset = double_int_ext_for_comb (comb->offset, comb);
230 for (i = j = 0; i < comb->n; i++)
232 double_int new_coef = double_int_ext_for_comb (comb->elts[i].coef, comb);
233 if (double_int_zero_p (new_coef))
235 comb->elts[j].coef = new_coef;
236 comb->elts[j].val = fold_convert (type, comb->elts[i].val);
241 if (comb->n < MAX_AFF_ELTS && comb->rest)
243 comb->elts[comb->n].coef = double_int_one;
244 comb->elts[comb->n].val = comb->rest;
245 comb->rest = NULL_TREE;
250 /* Splits EXPR into an affine combination of parts. */
253 tree_to_aff_combination (tree expr, tree type, aff_tree *comb)
257 tree cst, core, toffset;
258 HOST_WIDE_INT bitpos, bitsize;
259 enum machine_mode mode;
260 int unsignedp, volatilep;
264 code = TREE_CODE (expr);
268 aff_combination_const (comb, type, tree_to_double_int (expr));
271 case POINTER_PLUS_EXPR:
272 tree_to_aff_combination (TREE_OPERAND (expr, 0), type, comb);
273 tree_to_aff_combination (TREE_OPERAND (expr, 1), sizetype, &tmp);
274 aff_combination_convert (&tmp, type);
275 aff_combination_add (comb, &tmp);
280 tree_to_aff_combination (TREE_OPERAND (expr, 0), type, comb);
281 tree_to_aff_combination (TREE_OPERAND (expr, 1), type, &tmp);
282 if (code == MINUS_EXPR)
283 aff_combination_scale (&tmp, double_int_minus_one);
284 aff_combination_add (comb, &tmp);
288 cst = TREE_OPERAND (expr, 1);
289 if (TREE_CODE (cst) != INTEGER_CST)
291 tree_to_aff_combination (TREE_OPERAND (expr, 0), type, comb);
292 aff_combination_scale (comb, tree_to_double_int (cst));
296 tree_to_aff_combination (TREE_OPERAND (expr, 0), type, comb);
297 aff_combination_scale (comb, double_int_minus_one);
302 tree_to_aff_combination (TREE_OPERAND (expr, 0), type, comb);
303 aff_combination_scale (comb, double_int_minus_one);
304 aff_combination_add_cst (comb, double_int_minus_one);
308 core = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize, &bitpos,
309 &toffset, &mode, &unsignedp, &volatilep,
311 if (bitpos % BITS_PER_UNIT != 0)
313 aff_combination_const (comb, type,
314 uhwi_to_double_int (bitpos / BITS_PER_UNIT));
315 core = build_fold_addr_expr (core);
316 if (TREE_CODE (core) == ADDR_EXPR)
317 aff_combination_add_elt (comb, core, double_int_one);
320 tree_to_aff_combination (core, type, &tmp);
321 aff_combination_add (comb, &tmp);
325 tree_to_aff_combination (toffset, type, &tmp);
326 aff_combination_add (comb, &tmp);
334 aff_combination_elt (comb, type, expr);
337 /* Creates EXPR + ELT * SCALE in TYPE. EXPR is taken from affine
341 add_elt_to_tree (tree expr, tree type, tree elt, double_int scale,
346 scale = double_int_ext_for_comb (scale, comb);
347 elt = fold_convert (type, elt);
349 if (double_int_one_p (scale))
354 return fold_build2 (PLUS_EXPR, type, expr, elt);
357 if (double_int_minus_one_p (scale))
360 return fold_build1 (NEGATE_EXPR, type, elt);
362 return fold_build2 (MINUS_EXPR, type, expr, elt);
366 return fold_build2 (MULT_EXPR, type, elt,
367 double_int_to_tree (type, scale));
369 if (double_int_negative_p (scale))
372 scale = double_int_neg (scale);
377 elt = fold_build2 (MULT_EXPR, type, elt,
378 double_int_to_tree (type, scale));
379 return fold_build2 (code, type, expr, elt);
382 /* Makes tree from the affine combination COMB. */
385 aff_combination_to_tree (aff_tree *comb)
387 tree type = comb->type;
388 tree expr = comb->rest;
392 gcc_assert (comb->n == MAX_AFF_ELTS || comb->rest == NULL_TREE);
394 for (i = 0; i < comb->n; i++)
395 expr = add_elt_to_tree (expr, type, comb->elts[i].val, comb->elts[i].coef,
398 /* Ensure that we get x - 1, not x + (-1) or x + 0xff..f if x is
400 if (double_int_negative_p (comb->offset))
402 off = double_int_neg (comb->offset);
403 sgn = double_int_minus_one;
408 sgn = double_int_one;
410 return add_elt_to_tree (expr, type, double_int_to_tree (type, off), sgn,
414 /* Copies the tree elements of COMB to ensure that they are not shared. */
417 unshare_aff_combination (aff_tree *comb)
421 for (i = 0; i < comb->n; i++)
422 comb->elts[i].val = unshare_expr (comb->elts[i].val);
424 comb->rest = unshare_expr (comb->rest);
427 /* Remove M-th element from COMB. */
430 aff_combination_remove_elt (aff_tree *comb, unsigned m)
434 comb->elts[m] = comb->elts[comb->n];
437 comb->elts[comb->n].coef = double_int_one;
438 comb->elts[comb->n].val = comb->rest;
439 comb->rest = NULL_TREE;
444 /* Adds C * COEF * VAL to R. VAL may be NULL, in that case only
445 C * COEF is added to R. */
449 aff_combination_add_product (aff_tree *c, double_int coef, tree val,
455 for (i = 0; i < c->n; i++)
457 aval = c->elts[i].val;
460 type = TREE_TYPE (aval);
461 aval = fold_build2 (MULT_EXPR, type, aval,
462 fold_convert (type, val));
465 aff_combination_add_elt (r, aval,
466 double_int_mul (coef, c->elts[i].coef));
474 type = TREE_TYPE (aval);
475 aval = fold_build2 (MULT_EXPR, type, aval,
476 fold_convert (type, val));
479 aff_combination_add_elt (r, aval, coef);
483 aff_combination_add_elt (r, val,
484 double_int_mul (coef, c->offset));
486 aff_combination_add_cst (r, double_int_mul (coef, c->offset));
489 /* Multiplies C1 by C2, storing the result to R */
492 aff_combination_mult (aff_tree *c1, aff_tree *c2, aff_tree *r)
495 gcc_assert (TYPE_PRECISION (c1->type) == TYPE_PRECISION (c2->type));
497 aff_combination_zero (r, c1->type);
499 for (i = 0; i < c2->n; i++)
500 aff_combination_add_product (c1, c2->elts[i].coef, c2->elts[i].val, r);
502 aff_combination_add_product (c1, double_int_one, c2->rest, r);
503 aff_combination_add_product (c1, c2->offset, NULL, r);
506 /* Returns the element of COMB whose value is VAL, or NULL if no such
507 element exists. If IDX is not NULL, it is set to the index of VAL in
510 static struct aff_comb_elt *
511 aff_combination_find_elt (aff_tree *comb, tree val, unsigned *idx)
515 for (i = 0; i < comb->n; i++)
516 if (operand_equal_p (comb->elts[i].val, val, 0))
521 return &comb->elts[i];
527 /* Element of the cache that maps ssa name NAME to its expanded form
528 as an affine expression EXPANSION. */
530 struct name_expansion
534 /* True if the expansion for the name is just being generated. */
535 unsigned in_progress : 1;
538 /* Similar to tree_to_aff_combination, but follows SSA name definitions
539 and expands them recursively. CACHE is used to cache the expansions
540 of the ssa names, to avoid exponential time complexity for cases
549 tree_to_aff_combination_expand (tree expr, tree type, aff_tree *comb,
550 struct pointer_map_t **cache)
553 aff_tree to_add, current, curre;
557 struct name_expansion *exp;
559 tree_to_aff_combination (expr, type, comb);
560 aff_combination_zero (&to_add, type);
561 for (i = 0; i < comb->n; i++)
563 e = comb->elts[i].val;
564 if (TREE_CODE (e) != SSA_NAME)
566 def = SSA_NAME_DEF_STMT (e);
567 if (TREE_CODE (def) != GIMPLE_MODIFY_STMT
568 || GIMPLE_STMT_OPERAND (def, 0) != e)
571 rhs = GIMPLE_STMT_OPERAND (def, 1);
572 if (TREE_CODE (rhs) != SSA_NAME
574 && !is_gimple_min_invariant (rhs))
577 /* We do not know whether the reference retains its value at the
578 place where the expansion is used. */
579 if (REFERENCE_CLASS_P (rhs))
583 *cache = pointer_map_create ();
584 slot = pointer_map_insert (*cache, e);
589 exp = XNEW (struct name_expansion);
590 exp->in_progress = 1;
592 tree_to_aff_combination_expand (rhs, type, ¤t, cache);
593 exp->expansion = current;
594 exp->in_progress = 0;
598 /* Since we follow the definitions in the SSA form, we should not
599 enter a cycle unless we pass through a phi node. */
600 gcc_assert (!exp->in_progress);
601 current = exp->expansion;
604 /* Accumulate the new terms to TO_ADD, so that we do not modify
605 COMB while traversing it; include the term -coef * E, to remove
607 scale = comb->elts[i].coef;
608 aff_combination_zero (&curre, type);
609 aff_combination_add_elt (&curre, e, double_int_neg (scale));
610 aff_combination_scale (¤t, scale);
611 aff_combination_add (&to_add, ¤t);
612 aff_combination_add (&to_add, &curre);
614 aff_combination_add (comb, &to_add);
617 /* Frees memory occupied by struct name_expansion in *VALUE. Callback for
618 pointer_map_traverse. */
621 free_name_expansion (void *key ATTRIBUTE_UNUSED, void **value,
622 void *data ATTRIBUTE_UNUSED)
624 struct name_expansion *exp = *value;
630 /* Frees memory allocated for the CACHE used by
631 tree_to_aff_combination_expand. */
634 free_affine_expand_cache (struct pointer_map_t **cache)
639 pointer_map_traverse (*cache, free_name_expansion, NULL);
640 pointer_map_destroy (*cache);
644 /* If VAL != CST * DIV for any constant CST, returns false.
645 Otherwise, if VAL != 0 (and hence CST != 0), and *MULT_SET is true,
646 additionally compares CST and MULT, and if they are different,
647 returns false. Finally, if neither of these two cases occur,
648 true is returned, and if CST != 0, CST is stored to MULT and
649 MULT_SET is set to true. */
652 double_int_constant_multiple_p (double_int val, double_int div,
653 bool *mult_set, double_int *mult)
657 if (double_int_zero_p (val))
660 if (double_int_zero_p (div))
663 cst = double_int_sdivmod (val, div, FLOOR_DIV_EXPR, &rem);
664 if (!double_int_zero_p (rem))
667 if (*mult_set && !double_int_equal_p (*mult, cst))
675 /* Returns true if VAL = X * DIV for some constant X. If this is the case,
676 X is stored to MULT. */
679 aff_combination_constant_multiple_p (aff_tree *val, aff_tree *div,
682 bool mult_set = false;
685 if (val->n == 0 && double_int_zero_p (val->offset))
687 *mult = double_int_zero;
690 if (val->n != div->n)
693 if (val->rest || div->rest)
696 if (!double_int_constant_multiple_p (val->offset, div->offset,
700 for (i = 0; i < div->n; i++)
702 struct aff_comb_elt *elt
703 = aff_combination_find_elt (val, div->elts[i].val, NULL);
706 if (!double_int_constant_multiple_p (elt->coef, div->elts[i].coef,
711 gcc_assert (mult_set);