1 /* Operations with affine combinations of trees.
2 Copyright (C) 2005, 2007, 2008, 2010 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 3, 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
25 #include "diagnostic.h"
26 #include "tree-pretty-print.h"
27 #include "tree-dump.h"
28 #include "pointer-set.h"
29 #include "tree-affine.h"
33 /* Extends CST as appropriate for the affine combinations COMB. */
36 double_int_ext_for_comb (double_int cst
, aff_tree
*comb
)
38 return double_int_sext (cst
, TYPE_PRECISION (comb
->type
));
41 /* Initializes affine combination COMB so that its value is zero in TYPE. */
44 aff_combination_zero (aff_tree
*comb
, tree type
)
47 comb
->offset
= double_int_zero
;
49 comb
->rest
= NULL_TREE
;
52 /* Sets COMB to CST. */
55 aff_combination_const (aff_tree
*comb
, tree type
, double_int cst
)
57 aff_combination_zero (comb
, type
);
58 comb
->offset
= double_int_ext_for_comb (cst
, comb
);
61 /* Sets COMB to single element ELT. */
64 aff_combination_elt (aff_tree
*comb
, tree type
, tree elt
)
66 aff_combination_zero (comb
, type
);
69 comb
->elts
[0].val
= elt
;
70 comb
->elts
[0].coef
= double_int_one
;
73 /* Scales COMB by SCALE. */
76 aff_combination_scale (aff_tree
*comb
, double_int scale
)
80 scale
= double_int_ext_for_comb (scale
, comb
);
81 if (double_int_one_p (scale
))
84 if (double_int_zero_p (scale
))
86 aff_combination_zero (comb
, comb
->type
);
91 = double_int_ext_for_comb (double_int_mul (scale
, comb
->offset
), comb
);
92 for (i
= 0, j
= 0; i
< comb
->n
; i
++)
97 = double_int_ext_for_comb (double_int_mul (scale
, comb
->elts
[i
].coef
),
99 /* A coefficient may become zero due to overflow. Remove the zero
101 if (double_int_zero_p (new_coef
))
103 comb
->elts
[j
].coef
= new_coef
;
104 comb
->elts
[j
].val
= comb
->elts
[i
].val
;
111 tree type
= comb
->type
;
112 if (POINTER_TYPE_P (type
))
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
, type
, comb
->rest
,
123 double_int_to_tree (type
, scale
));
127 /* Adds ELT * SCALE to COMB. */
130 aff_combination_add_elt (aff_tree
*comb
, tree elt
, double_int scale
)
135 scale
= double_int_ext_for_comb (scale
, comb
);
136 if (double_int_zero_p (scale
))
139 for (i
= 0; i
< comb
->n
; i
++)
140 if (operand_equal_p (comb
->elts
[i
].val
, elt
, 0))
144 new_coef
= double_int_add (comb
->elts
[i
].coef
, scale
);
145 new_coef
= double_int_ext_for_comb (new_coef
, comb
);
146 if (!double_int_zero_p (new_coef
))
148 comb
->elts
[i
].coef
= new_coef
;
153 comb
->elts
[i
] = comb
->elts
[comb
->n
];
157 gcc_assert (comb
->n
== MAX_AFF_ELTS
- 1);
158 comb
->elts
[comb
->n
].coef
= double_int_one
;
159 comb
->elts
[comb
->n
].val
= comb
->rest
;
160 comb
->rest
= NULL_TREE
;
165 if (comb
->n
< MAX_AFF_ELTS
)
167 comb
->elts
[comb
->n
].coef
= scale
;
168 comb
->elts
[comb
->n
].val
= elt
;
174 if (POINTER_TYPE_P (type
))
177 if (double_int_one_p (scale
))
178 elt
= fold_convert (type
, elt
);
180 elt
= fold_build2 (MULT_EXPR
, type
,
181 fold_convert (type
, elt
),
182 double_int_to_tree (type
, scale
));
185 comb
->rest
= fold_build2 (PLUS_EXPR
, type
, comb
->rest
,
194 aff_combination_add_cst (aff_tree
*c
, double_int cst
)
196 c
->offset
= double_int_ext_for_comb (double_int_add (c
->offset
, cst
), c
);
199 /* Adds COMB2 to COMB1. */
202 aff_combination_add (aff_tree
*comb1
, aff_tree
*comb2
)
206 aff_combination_add_cst (comb1
, comb2
->offset
);
207 for (i
= 0; i
< comb2
->n
; i
++)
208 aff_combination_add_elt (comb1
, comb2
->elts
[i
].val
, comb2
->elts
[i
].coef
);
210 aff_combination_add_elt (comb1
, comb2
->rest
, double_int_one
);
213 /* Converts affine combination COMB to TYPE. */
216 aff_combination_convert (aff_tree
*comb
, tree type
)
219 tree comb_type
= comb
->type
;
221 if (TYPE_PRECISION (type
) > TYPE_PRECISION (comb_type
))
223 tree val
= fold_convert (type
, aff_combination_to_tree (comb
));
224 tree_to_aff_combination (val
, type
, comb
);
229 if (comb
->rest
&& !POINTER_TYPE_P (type
))
230 comb
->rest
= fold_convert (type
, comb
->rest
);
232 if (TYPE_PRECISION (type
) == TYPE_PRECISION (comb_type
))
235 comb
->offset
= double_int_ext_for_comb (comb
->offset
, comb
);
236 for (i
= j
= 0; i
< comb
->n
; i
++)
238 double_int new_coef
= double_int_ext_for_comb (comb
->elts
[i
].coef
, comb
);
239 if (double_int_zero_p (new_coef
))
241 comb
->elts
[j
].coef
= new_coef
;
242 comb
->elts
[j
].val
= fold_convert (type
, comb
->elts
[i
].val
);
247 if (comb
->n
< MAX_AFF_ELTS
&& comb
->rest
)
249 comb
->elts
[comb
->n
].coef
= double_int_one
;
250 comb
->elts
[comb
->n
].val
= comb
->rest
;
251 comb
->rest
= NULL_TREE
;
256 /* Splits EXPR into an affine combination of parts. */
259 tree_to_aff_combination (tree expr
, tree type
, aff_tree
*comb
)
263 tree cst
, core
, toffset
;
264 HOST_WIDE_INT bitpos
, bitsize
;
265 enum machine_mode mode
;
266 int unsignedp
, volatilep
;
270 code
= TREE_CODE (expr
);
274 aff_combination_const (comb
, type
, tree_to_double_int (expr
));
277 case POINTER_PLUS_EXPR
:
278 tree_to_aff_combination (TREE_OPERAND (expr
, 0), type
, comb
);
279 tree_to_aff_combination (TREE_OPERAND (expr
, 1), sizetype
, &tmp
);
280 aff_combination_add (comb
, &tmp
);
285 tree_to_aff_combination (TREE_OPERAND (expr
, 0), type
, comb
);
286 tree_to_aff_combination (TREE_OPERAND (expr
, 1), type
, &tmp
);
287 if (code
== MINUS_EXPR
)
288 aff_combination_scale (&tmp
, double_int_minus_one
);
289 aff_combination_add (comb
, &tmp
);
293 cst
= TREE_OPERAND (expr
, 1);
294 if (TREE_CODE (cst
) != INTEGER_CST
)
296 tree_to_aff_combination (TREE_OPERAND (expr
, 0), type
, comb
);
297 aff_combination_scale (comb
, tree_to_double_int (cst
));
301 tree_to_aff_combination (TREE_OPERAND (expr
, 0), type
, comb
);
302 aff_combination_scale (comb
, double_int_minus_one
);
307 tree_to_aff_combination (TREE_OPERAND (expr
, 0), type
, comb
);
308 aff_combination_scale (comb
, double_int_minus_one
);
309 aff_combination_add_cst (comb
, double_int_minus_one
);
313 core
= get_inner_reference (TREE_OPERAND (expr
, 0), &bitsize
, &bitpos
,
314 &toffset
, &mode
, &unsignedp
, &volatilep
,
316 if (bitpos
% BITS_PER_UNIT
!= 0)
318 aff_combination_const (comb
, type
,
319 uhwi_to_double_int (bitpos
/ BITS_PER_UNIT
));
320 core
= build_fold_addr_expr (core
);
321 if (TREE_CODE (core
) == ADDR_EXPR
)
322 aff_combination_add_elt (comb
, core
, double_int_one
);
325 tree_to_aff_combination (core
, type
, &tmp
);
326 aff_combination_add (comb
, &tmp
);
330 tree_to_aff_combination (toffset
, type
, &tmp
);
331 aff_combination_add (comb
, &tmp
);
339 aff_combination_elt (comb
, type
, expr
);
342 /* Creates EXPR + ELT * SCALE in TYPE. EXPR is taken from affine
346 add_elt_to_tree (tree expr
, tree type
, tree elt
, double_int scale
,
351 if (POINTER_TYPE_P (type
))
354 scale
= double_int_ext_for_comb (scale
, comb
);
355 elt
= fold_convert (type1
, elt
);
357 if (double_int_one_p (scale
))
360 return fold_convert (type
, elt
);
362 if (POINTER_TYPE_P (type
))
363 return fold_build2 (POINTER_PLUS_EXPR
, type
, expr
, elt
);
364 return fold_build2 (PLUS_EXPR
, type
, expr
, elt
);
367 if (double_int_minus_one_p (scale
))
370 return fold_convert (type
, fold_build1 (NEGATE_EXPR
, type1
, elt
));
372 if (POINTER_TYPE_P (type
))
374 elt
= fold_build1 (NEGATE_EXPR
, type1
, elt
);
375 return fold_build2 (POINTER_PLUS_EXPR
, type
, expr
, elt
);
377 return fold_build2 (MINUS_EXPR
, type
, expr
, elt
);
381 return fold_convert (type
,
382 fold_build2 (MULT_EXPR
, type1
, elt
,
383 double_int_to_tree (type1
, scale
)));
385 if (double_int_negative_p (scale
))
388 scale
= double_int_neg (scale
);
393 elt
= fold_build2 (MULT_EXPR
, type1
, elt
,
394 double_int_to_tree (type1
, scale
));
395 if (POINTER_TYPE_P (type
))
397 if (code
== MINUS_EXPR
)
398 elt
= fold_build1 (NEGATE_EXPR
, type1
, elt
);
399 return fold_build2 (POINTER_PLUS_EXPR
, type
, expr
, elt
);
401 return fold_build2 (code
, type
, expr
, elt
);
404 /* Makes tree from the affine combination COMB. */
407 aff_combination_to_tree (aff_tree
*comb
)
409 tree type
= comb
->type
;
410 tree expr
= comb
->rest
;
414 if (POINTER_TYPE_P (type
))
417 gcc_assert (comb
->n
== MAX_AFF_ELTS
|| comb
->rest
== NULL_TREE
);
419 for (i
= 0; i
< comb
->n
; i
++)
420 expr
= add_elt_to_tree (expr
, type
, comb
->elts
[i
].val
, comb
->elts
[i
].coef
,
423 /* Ensure that we get x - 1, not x + (-1) or x + 0xff..f if x is
425 if (double_int_negative_p (comb
->offset
))
427 off
= double_int_neg (comb
->offset
);
428 sgn
= double_int_minus_one
;
433 sgn
= double_int_one
;
435 return add_elt_to_tree (expr
, type
, double_int_to_tree (type1
, off
), sgn
,
439 /* Copies the tree elements of COMB to ensure that they are not shared. */
442 unshare_aff_combination (aff_tree
*comb
)
446 for (i
= 0; i
< comb
->n
; i
++)
447 comb
->elts
[i
].val
= unshare_expr (comb
->elts
[i
].val
);
449 comb
->rest
= unshare_expr (comb
->rest
);
452 /* Remove M-th element from COMB. */
455 aff_combination_remove_elt (aff_tree
*comb
, unsigned m
)
459 comb
->elts
[m
] = comb
->elts
[comb
->n
];
462 comb
->elts
[comb
->n
].coef
= double_int_one
;
463 comb
->elts
[comb
->n
].val
= comb
->rest
;
464 comb
->rest
= NULL_TREE
;
469 /* Adds C * COEF * VAL to R. VAL may be NULL, in that case only
470 C * COEF is added to R. */
474 aff_combination_add_product (aff_tree
*c
, double_int coef
, tree val
,
480 for (i
= 0; i
< c
->n
; i
++)
482 aval
= c
->elts
[i
].val
;
485 type
= TREE_TYPE (aval
);
486 aval
= fold_build2 (MULT_EXPR
, type
, aval
,
487 fold_convert (type
, val
));
490 aff_combination_add_elt (r
, aval
,
491 double_int_mul (coef
, c
->elts
[i
].coef
));
499 type
= TREE_TYPE (aval
);
500 aval
= fold_build2 (MULT_EXPR
, type
, aval
,
501 fold_convert (type
, val
));
504 aff_combination_add_elt (r
, aval
, coef
);
508 aff_combination_add_elt (r
, val
,
509 double_int_mul (coef
, c
->offset
));
511 aff_combination_add_cst (r
, double_int_mul (coef
, c
->offset
));
514 /* Multiplies C1 by C2, storing the result to R */
517 aff_combination_mult (aff_tree
*c1
, aff_tree
*c2
, aff_tree
*r
)
520 gcc_assert (TYPE_PRECISION (c1
->type
) == TYPE_PRECISION (c2
->type
));
522 aff_combination_zero (r
, c1
->type
);
524 for (i
= 0; i
< c2
->n
; i
++)
525 aff_combination_add_product (c1
, c2
->elts
[i
].coef
, c2
->elts
[i
].val
, r
);
527 aff_combination_add_product (c1
, double_int_one
, c2
->rest
, r
);
528 aff_combination_add_product (c1
, c2
->offset
, NULL
, r
);
531 /* Returns the element of COMB whose value is VAL, or NULL if no such
532 element exists. If IDX is not NULL, it is set to the index of VAL in
535 static struct aff_comb_elt
*
536 aff_combination_find_elt (aff_tree
*comb
, tree val
, unsigned *idx
)
540 for (i
= 0; i
< comb
->n
; i
++)
541 if (operand_equal_p (comb
->elts
[i
].val
, val
, 0))
546 return &comb
->elts
[i
];
552 /* Element of the cache that maps ssa name NAME to its expanded form
553 as an affine expression EXPANSION. */
555 struct name_expansion
559 /* True if the expansion for the name is just being generated. */
560 unsigned in_progress
: 1;
563 /* Expands SSA names in COMB recursively. CACHE is used to cache the
567 aff_combination_expand (aff_tree
*comb ATTRIBUTE_UNUSED
,
568 struct pointer_map_t
**cache ATTRIBUTE_UNUSED
)
571 aff_tree to_add
, current
, curre
;
576 struct name_expansion
*exp
;
578 aff_combination_zero (&to_add
, comb
->type
);
579 for (i
= 0; i
< comb
->n
; i
++)
584 e
= comb
->elts
[i
].val
;
585 type
= TREE_TYPE (e
);
587 /* Look through some conversions. */
588 if (TREE_CODE (e
) == NOP_EXPR
589 && (TYPE_PRECISION (type
)
590 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (e
, 0)))))
591 name
= TREE_OPERAND (e
, 0);
592 if (TREE_CODE (name
) != SSA_NAME
)
594 def
= SSA_NAME_DEF_STMT (name
);
595 if (!is_gimple_assign (def
) || gimple_assign_lhs (def
) != name
)
598 code
= gimple_assign_rhs_code (def
);
600 && !IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
))
601 && (get_gimple_rhs_class (code
) != GIMPLE_SINGLE_RHS
602 || !is_gimple_min_invariant (gimple_assign_rhs1 (def
))))
605 /* We do not know whether the reference retains its value at the
606 place where the expansion is used. */
607 if (TREE_CODE_CLASS (code
) == tcc_reference
)
611 *cache
= pointer_map_create ();
612 slot
= pointer_map_insert (*cache
, e
);
613 exp
= (struct name_expansion
*) *slot
;
617 exp
= XNEW (struct name_expansion
);
618 exp
->in_progress
= 1;
620 /* In principle this is a generally valid folding, but
621 it is not unconditionally an optimization, so do it
622 here and not in fold_unary. */
623 /* Convert (T1)(X *+- CST) into (T1)X *+- (T1)CST if T1 is wider
624 than the type of X and overflow for the type of X is
627 && INTEGRAL_TYPE_P (type
)
628 && INTEGRAL_TYPE_P (TREE_TYPE (name
))
629 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (name
))
630 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (name
))
631 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
|| code
== MULT_EXPR
)
632 && TREE_CODE (gimple_assign_rhs2 (def
)) == INTEGER_CST
)
633 rhs
= fold_build2 (code
, type
,
634 fold_convert (type
, gimple_assign_rhs1 (def
)),
635 fold_convert (type
, gimple_assign_rhs2 (def
)));
638 rhs
= gimple_assign_rhs_to_tree (def
);
640 rhs
= fold_convert (type
, rhs
);
642 tree_to_aff_combination_expand (rhs
, comb
->type
, ¤t
, cache
);
643 exp
->expansion
= current
;
644 exp
->in_progress
= 0;
648 /* Since we follow the definitions in the SSA form, we should not
649 enter a cycle unless we pass through a phi node. */
650 gcc_assert (!exp
->in_progress
);
651 current
= exp
->expansion
;
654 /* Accumulate the new terms to TO_ADD, so that we do not modify
655 COMB while traversing it; include the term -coef * E, to remove
657 scale
= comb
->elts
[i
].coef
;
658 aff_combination_zero (&curre
, comb
->type
);
659 aff_combination_add_elt (&curre
, e
, double_int_neg (scale
));
660 aff_combination_scale (¤t
, scale
);
661 aff_combination_add (&to_add
, ¤t
);
662 aff_combination_add (&to_add
, &curre
);
664 aff_combination_add (comb
, &to_add
);
667 /* Similar to tree_to_aff_combination, but follows SSA name definitions
668 and expands them recursively. CACHE is used to cache the expansions
669 of the ssa names, to avoid exponential time complexity for cases
678 tree_to_aff_combination_expand (tree expr
, tree type
, aff_tree
*comb
,
679 struct pointer_map_t
**cache
)
681 tree_to_aff_combination (expr
, type
, comb
);
682 aff_combination_expand (comb
, cache
);
685 /* Frees memory occupied by struct name_expansion in *VALUE. Callback for
686 pointer_map_traverse. */
689 free_name_expansion (const void *key ATTRIBUTE_UNUSED
, void **value
,
690 void *data ATTRIBUTE_UNUSED
)
692 struct name_expansion
*const exp
= (struct name_expansion
*) *value
;
698 /* Frees memory allocated for the CACHE used by
699 tree_to_aff_combination_expand. */
702 free_affine_expand_cache (struct pointer_map_t
**cache
)
707 pointer_map_traverse (*cache
, free_name_expansion
, NULL
);
708 pointer_map_destroy (*cache
);
712 /* If VAL != CST * DIV for any constant CST, returns false.
713 Otherwise, if VAL != 0 (and hence CST != 0), and *MULT_SET is true,
714 additionally compares CST and MULT, and if they are different,
715 returns false. Finally, if neither of these two cases occur,
716 true is returned, and if CST != 0, CST is stored to MULT and
717 MULT_SET is set to true. */
720 double_int_constant_multiple_p (double_int val
, double_int div
,
721 bool *mult_set
, double_int
*mult
)
725 if (double_int_zero_p (val
))
728 if (double_int_zero_p (div
))
731 cst
= double_int_sdivmod (val
, div
, FLOOR_DIV_EXPR
, &rem
);
732 if (!double_int_zero_p (rem
))
735 if (*mult_set
&& !double_int_equal_p (*mult
, cst
))
743 /* Returns true if VAL = X * DIV for some constant X. If this is the case,
744 X is stored to MULT. */
747 aff_combination_constant_multiple_p (aff_tree
*val
, aff_tree
*div
,
750 bool mult_set
= false;
753 if (val
->n
== 0 && double_int_zero_p (val
->offset
))
755 *mult
= double_int_zero
;
758 if (val
->n
!= div
->n
)
761 if (val
->rest
|| div
->rest
)
764 if (!double_int_constant_multiple_p (val
->offset
, div
->offset
,
768 for (i
= 0; i
< div
->n
; i
++)
770 struct aff_comb_elt
*elt
771 = aff_combination_find_elt (val
, div
->elts
[i
].val
, NULL
);
774 if (!double_int_constant_multiple_p (elt
->coef
, div
->elts
[i
].coef
,
779 gcc_assert (mult_set
);
783 /* Prints the affine VAL to the FILE. */
786 print_aff (FILE *file
, aff_tree
*val
)
789 bool uns
= TYPE_UNSIGNED (val
->type
);
790 if (POINTER_TYPE_P (val
->type
))
792 fprintf (file
, "{\n type = ");
793 print_generic_expr (file
, val
->type
, TDF_VOPS
|TDF_MEMSYMS
);
794 fprintf (file
, "\n offset = ");
795 dump_double_int (file
, val
->offset
, uns
);
798 fprintf (file
, "\n elements = {\n");
799 for (i
= 0; i
< val
->n
; i
++)
801 fprintf (file
, " [%d] = ", i
);
802 print_generic_expr (file
, val
->elts
[i
].val
, TDF_VOPS
|TDF_MEMSYMS
);
804 fprintf (file
, " * ");
805 dump_double_int (file
, val
->elts
[i
].coef
, uns
);
807 fprintf (file
, ", \n");
809 fprintf (file
, "\n }");
813 fprintf (file
, "\n rest = ");
814 print_generic_expr (file
, val
->rest
, TDF_VOPS
|TDF_MEMSYMS
);
816 fprintf (file
, "\n}");
819 /* Prints the affine VAL to the standard error, used for debugging. */
822 debug_aff (aff_tree
*val
)
824 print_aff (stderr
, val
);
825 fprintf (stderr
, "\n");
828 /* Returns address of the reference REF in ADDR. The size of the accessed
829 location is stored to SIZE. */
832 get_inner_reference_aff (tree ref
, aff_tree
*addr
, double_int
*size
)
834 HOST_WIDE_INT bitsize
, bitpos
;
836 enum machine_mode mode
;
839 tree base
= get_inner_reference (ref
, &bitsize
, &bitpos
, &toff
, &mode
,
841 tree base_addr
= build_fold_addr_expr (base
);
843 /* ADDR = &BASE + TOFF + BITPOS / BITS_PER_UNIT. */
845 tree_to_aff_combination (base_addr
, sizetype
, addr
);
849 tree_to_aff_combination (toff
, sizetype
, &tmp
);
850 aff_combination_add (addr
, &tmp
);
853 aff_combination_const (&tmp
, sizetype
,
854 shwi_to_double_int (bitpos
/ BITS_PER_UNIT
));
855 aff_combination_add (addr
, &tmp
);
857 *size
= shwi_to_double_int ((bitsize
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
);