1 /* Memory address lowering and addressing mode selection.
2 Copyright (C) 2004-2013 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/>. */
20 /* Utility functions for manipulation with TARGET_MEM_REFs -- tree expressions
21 that directly map to addressing modes of the target. */
25 #include "coretypes.h"
29 #include "basic-block.h"
30 #include "tree-pretty-print.h"
32 #include "gimple-iterator.h"
33 #include "gimplify-me.h"
34 #include "tree-ssanames.h"
35 #include "tree-ssa-loop-ivopts.h"
39 #include "tree-inline.h"
40 #include "tree-affine.h"
42 /* FIXME: We compute address costs using RTL. */
43 #include "insn-config.h"
50 #include "tree-ssa-address.h"
52 /* TODO -- handling of symbols (according to Richard Hendersons
53 comments, http://gcc.gnu.org/ml/gcc-patches/2005-04/msg00949.html):
55 There are at least 5 different kinds of symbols that we can run up against:
57 (1) binds_local_p, small data area.
58 (2) binds_local_p, eg local statics
59 (3) !binds_local_p, eg global variables
60 (4) thread local, local_exec
61 (5) thread local, !local_exec
63 Now, (1) won't appear often in an array context, but it certainly can.
64 All you have to do is set -GN high enough, or explicitly mark any
65 random object __attribute__((section (".sdata"))).
67 All of these affect whether or not a symbol is in fact a valid address.
68 The only one tested here is (3). And that result may very well
69 be incorrect for (4) or (5).
71 An incorrect result here does not cause incorrect results out the
72 back end, because the expander in expr.c validizes the address. However
73 it would be nice to improve the handling here in order to produce more
76 /* A "template" for memory address, used to determine whether the address is
79 typedef struct GTY (()) mem_addr_template
{
80 rtx ref
; /* The template. */
81 rtx
* GTY ((skip
)) step_p
; /* The point in template where the step should be
83 rtx
* GTY ((skip
)) off_p
; /* The point in template where the offset should
88 /* The templates. Each of the low five bits of the index corresponds to one
89 component of TARGET_MEM_REF being present, while the high bits identify
90 the address space. See TEMPL_IDX. */
92 static GTY(()) vec
<mem_addr_template
, va_gc
> *mem_addr_template_list
;
94 #define TEMPL_IDX(AS, SYMBOL, BASE, INDEX, STEP, OFFSET) \
96 | ((SYMBOL != 0) << 4) \
97 | ((BASE != 0) << 3) \
98 | ((INDEX != 0) << 2) \
99 | ((STEP != 0) << 1) \
102 /* Stores address for memory reference with parameters SYMBOL, BASE, INDEX,
103 STEP and OFFSET to *ADDR using address mode ADDRESS_MODE. Stores pointers
104 to where step is placed to *STEP_P and offset to *OFFSET_P. */
107 gen_addr_rtx (enum machine_mode address_mode
,
108 rtx symbol
, rtx base
, rtx index
, rtx step
, rtx offset
,
109 rtx
*addr
, rtx
**step_p
, rtx
**offset_p
)
124 act_elem
= gen_rtx_MULT (address_mode
, act_elem
, step
);
127 *step_p
= &XEXP (act_elem
, 1);
133 if (base
&& base
!= const0_rtx
)
136 *addr
= simplify_gen_binary (PLUS
, address_mode
, base
, *addr
);
146 act_elem
= gen_rtx_PLUS (address_mode
, act_elem
, offset
);
149 *offset_p
= &XEXP (act_elem
, 1);
151 if (GET_CODE (symbol
) == SYMBOL_REF
152 || GET_CODE (symbol
) == LABEL_REF
153 || GET_CODE (symbol
) == CONST
)
154 act_elem
= gen_rtx_CONST (address_mode
, act_elem
);
158 *addr
= gen_rtx_PLUS (address_mode
, *addr
, act_elem
);
166 *addr
= gen_rtx_PLUS (address_mode
, *addr
, offset
);
168 *offset_p
= &XEXP (*addr
, 1);
182 /* Description of a memory address. */
186 tree symbol
, base
, index
, step
, offset
;
189 /* Returns address for TARGET_MEM_REF with parameters given by ADDR
191 If REALLY_EXPAND is false, just make fake registers instead
192 of really expanding the operands, and perform the expansion in-place
193 by using one of the "templates". */
196 addr_for_mem_ref (struct mem_address
*addr
, addr_space_t as
,
199 enum machine_mode address_mode
= targetm
.addr_space
.address_mode (as
);
200 enum machine_mode pointer_mode
= targetm
.addr_space
.pointer_mode (as
);
201 rtx address
, sym
, bse
, idx
, st
, off
;
202 struct mem_addr_template
*templ
;
204 if (addr
->step
&& !integer_onep (addr
->step
))
205 st
= immed_double_int_const (tree_to_double_int (addr
->step
), pointer_mode
);
209 if (addr
->offset
&& !integer_zerop (addr
->offset
))
210 off
= immed_double_int_const
211 (tree_to_double_int (addr
->offset
)
212 .sext (TYPE_PRECISION (TREE_TYPE (addr
->offset
))),
219 unsigned int templ_index
220 = TEMPL_IDX (as
, addr
->symbol
, addr
->base
, addr
->index
, st
, off
);
222 if (templ_index
>= vec_safe_length (mem_addr_template_list
))
223 vec_safe_grow_cleared (mem_addr_template_list
, templ_index
+ 1);
225 /* Reuse the templates for addresses, so that we do not waste memory. */
226 templ
= &(*mem_addr_template_list
)[templ_index
];
229 sym
= (addr
->symbol
?
230 gen_rtx_SYMBOL_REF (pointer_mode
, ggc_strdup ("test_symbol"))
233 gen_raw_REG (pointer_mode
, LAST_VIRTUAL_REGISTER
+ 1)
236 gen_raw_REG (pointer_mode
, LAST_VIRTUAL_REGISTER
+ 2)
239 gen_addr_rtx (pointer_mode
, sym
, bse
, idx
,
240 st
? const0_rtx
: NULL_RTX
,
241 off
? const0_rtx
: NULL_RTX
,
255 /* Otherwise really expand the expressions. */
257 ? expand_expr (addr
->symbol
, NULL_RTX
, pointer_mode
, EXPAND_NORMAL
)
260 ? expand_expr (addr
->base
, NULL_RTX
, pointer_mode
, EXPAND_NORMAL
)
263 ? expand_expr (addr
->index
, NULL_RTX
, pointer_mode
, EXPAND_NORMAL
)
266 gen_addr_rtx (pointer_mode
, sym
, bse
, idx
, st
, off
, &address
, NULL
, NULL
);
267 if (pointer_mode
!= address_mode
)
268 address
= convert_memory_address (address_mode
, address
);
272 /* implement addr_for_mem_ref() directly from a tree, which avoids exporting
273 the mem_address structure. */
276 addr_for_mem_ref (tree exp
, addr_space_t as
, bool really_expand
)
278 struct mem_address addr
;
279 get_address_description (exp
, &addr
);
280 return addr_for_mem_ref (&addr
, as
, really_expand
);
283 /* Returns address of MEM_REF in TYPE. */
286 tree_mem_ref_addr (tree type
, tree mem_ref
)
290 tree step
= TMR_STEP (mem_ref
), offset
= TMR_OFFSET (mem_ref
);
291 tree addr_base
= NULL_TREE
, addr_off
= NULL_TREE
;
293 addr_base
= fold_convert (type
, TMR_BASE (mem_ref
));
295 act_elem
= TMR_INDEX (mem_ref
);
299 act_elem
= fold_build2 (MULT_EXPR
, TREE_TYPE (act_elem
),
304 act_elem
= TMR_INDEX2 (mem_ref
);
308 addr_off
= fold_build2 (PLUS_EXPR
, TREE_TYPE (addr_off
),
314 if (offset
&& !integer_zerop (offset
))
317 addr_off
= fold_build2 (PLUS_EXPR
, TREE_TYPE (addr_off
), addr_off
,
318 fold_convert (TREE_TYPE (addr_off
), offset
));
324 addr
= fold_build_pointer_plus (addr_base
, addr_off
);
331 /* Returns true if a memory reference in MODE and with parameters given by
332 ADDR is valid on the current target. */
335 valid_mem_ref_p (enum machine_mode mode
, addr_space_t as
,
336 struct mem_address
*addr
)
340 address
= addr_for_mem_ref (addr
, as
, false);
344 return memory_address_addr_space_p (mode
, address
, as
);
347 /* Checks whether a TARGET_MEM_REF with type TYPE and parameters given by ADDR
348 is valid on the current target and if so, creates and returns the
349 TARGET_MEM_REF. If VERIFY is false omit the verification step. */
352 create_mem_ref_raw (tree type
, tree alias_ptr_type
, struct mem_address
*addr
,
358 && !valid_mem_ref_p (TYPE_MODE (type
), TYPE_ADDR_SPACE (type
), addr
))
361 if (addr
->step
&& integer_onep (addr
->step
))
362 addr
->step
= NULL_TREE
;
365 addr
->offset
= fold_convert (alias_ptr_type
, addr
->offset
);
367 addr
->offset
= build_int_cst (alias_ptr_type
, 0);
375 && POINTER_TYPE_P (TREE_TYPE (addr
->base
)))
382 base
= build_int_cst (ptr_type_node
, 0);
386 /* If possible use a plain MEM_REF instead of a TARGET_MEM_REF.
387 ??? As IVOPTs does not follow restrictions to where the base
388 pointer may point to create a MEM_REF only if we know that
390 if ((TREE_CODE (base
) == ADDR_EXPR
|| TREE_CODE (base
) == INTEGER_CST
)
391 && (!index2
|| integer_zerop (index2
))
392 && (!addr
->index
|| integer_zerop (addr
->index
)))
393 return fold_build2 (MEM_REF
, type
, base
, addr
->offset
);
395 return build5 (TARGET_MEM_REF
, type
,
396 base
, addr
->offset
, addr
->index
, addr
->step
, index2
);
399 /* Returns true if OBJ is an object whose address is a link time constant. */
402 fixed_address_object_p (tree obj
)
404 return (TREE_CODE (obj
) == VAR_DECL
405 && (TREE_STATIC (obj
)
406 || DECL_EXTERNAL (obj
))
407 && ! DECL_DLLIMPORT_P (obj
));
410 /* If ADDR contains an address of object that is a link time constant,
411 move it to PARTS->symbol. */
414 move_fixed_address_to_symbol (struct mem_address
*parts
, aff_tree
*addr
)
417 tree val
= NULL_TREE
;
419 for (i
= 0; i
< addr
->n
; i
++)
421 if (!addr
->elts
[i
].coef
.is_one ())
424 val
= addr
->elts
[i
].val
;
425 if (TREE_CODE (val
) == ADDR_EXPR
426 && fixed_address_object_p (TREE_OPERAND (val
, 0)))
434 aff_combination_remove_elt (addr
, i
);
437 /* If ADDR contains an instance of BASE_HINT, move it to PARTS->base. */
440 move_hint_to_base (tree type
, struct mem_address
*parts
, tree base_hint
,
444 tree val
= NULL_TREE
;
447 for (i
= 0; i
< addr
->n
; i
++)
449 if (!addr
->elts
[i
].coef
.is_one ())
452 val
= addr
->elts
[i
].val
;
453 if (operand_equal_p (val
, base_hint
, 0))
460 /* Cast value to appropriate pointer type. We cannot use a pointer
461 to TYPE directly, as the back-end will assume registers of pointer
462 type are aligned, and just the base itself may not actually be.
463 We use void pointer to the type's address space instead. */
464 qual
= ENCODE_QUAL_ADDR_SPACE (TYPE_ADDR_SPACE (type
));
465 type
= build_qualified_type (void_type_node
, qual
);
466 parts
->base
= fold_convert (build_pointer_type (type
), val
);
467 aff_combination_remove_elt (addr
, i
);
470 /* If ADDR contains an address of a dereferenced pointer, move it to
474 move_pointer_to_base (struct mem_address
*parts
, aff_tree
*addr
)
477 tree val
= NULL_TREE
;
479 for (i
= 0; i
< addr
->n
; i
++)
481 if (!addr
->elts
[i
].coef
.is_one ())
484 val
= addr
->elts
[i
].val
;
485 if (POINTER_TYPE_P (TREE_TYPE (val
)))
493 aff_combination_remove_elt (addr
, i
);
496 /* Moves the loop variant part V in linear address ADDR to be the index
500 move_variant_to_index (struct mem_address
*parts
, aff_tree
*addr
, tree v
)
503 tree val
= NULL_TREE
;
505 gcc_assert (!parts
->index
);
506 for (i
= 0; i
< addr
->n
; i
++)
508 val
= addr
->elts
[i
].val
;
509 if (operand_equal_p (val
, v
, 0))
516 parts
->index
= fold_convert (sizetype
, val
);
517 parts
->step
= double_int_to_tree (sizetype
, addr
->elts
[i
].coef
);
518 aff_combination_remove_elt (addr
, i
);
521 /* Adds ELT to PARTS. */
524 add_to_parts (struct mem_address
*parts
, tree elt
)
530 parts
->index
= fold_convert (sizetype
, elt
);
540 /* Add ELT to base. */
541 type
= TREE_TYPE (parts
->base
);
542 if (POINTER_TYPE_P (type
))
543 parts
->base
= fold_build_pointer_plus (parts
->base
, elt
);
545 parts
->base
= fold_build2 (PLUS_EXPR
, type
,
549 /* Finds the most expensive multiplication in ADDR that can be
550 expressed in an addressing mode and move the corresponding
551 element(s) to PARTS. */
554 most_expensive_mult_to_index (tree type
, struct mem_address
*parts
,
555 aff_tree
*addr
, bool speed
)
557 addr_space_t as
= TYPE_ADDR_SPACE (type
);
558 enum machine_mode address_mode
= targetm
.addr_space
.address_mode (as
);
560 double_int best_mult
, amult
, amult_neg
;
561 unsigned best_mult_cost
= 0, acost
;
562 tree mult_elt
= NULL_TREE
, elt
;
564 enum tree_code op_code
;
566 best_mult
= double_int_zero
;
567 for (i
= 0; i
< addr
->n
; i
++)
569 if (!addr
->elts
[i
].coef
.fits_shwi ())
572 coef
= addr
->elts
[i
].coef
.to_shwi ();
574 || !multiplier_allowed_in_address_p (coef
, TYPE_MODE (type
), as
))
577 acost
= mult_by_coeff_cost (coef
, address_mode
, speed
);
579 if (acost
> best_mult_cost
)
581 best_mult_cost
= acost
;
582 best_mult
= addr
->elts
[i
].coef
;
589 /* Collect elements multiplied by best_mult. */
590 for (i
= j
= 0; i
< addr
->n
; i
++)
592 amult
= addr
->elts
[i
].coef
;
593 amult_neg
= double_int_ext_for_comb (-amult
, addr
);
595 if (amult
== best_mult
)
597 else if (amult_neg
== best_mult
)
598 op_code
= MINUS_EXPR
;
601 addr
->elts
[j
] = addr
->elts
[i
];
606 elt
= fold_convert (sizetype
, addr
->elts
[i
].val
);
608 mult_elt
= fold_build2 (op_code
, sizetype
, mult_elt
, elt
);
609 else if (op_code
== PLUS_EXPR
)
612 mult_elt
= fold_build1 (NEGATE_EXPR
, sizetype
, elt
);
616 parts
->index
= mult_elt
;
617 parts
->step
= double_int_to_tree (sizetype
, best_mult
);
620 /* Splits address ADDR for a memory access of type TYPE into PARTS.
621 If BASE_HINT is non-NULL, it specifies an SSA name to be used
622 preferentially as base of the reference, and IV_CAND is the selected
623 iv candidate used in ADDR.
625 TODO -- be more clever about the distribution of the elements of ADDR
626 to PARTS. Some architectures do not support anything but single
627 register in address, possibly with a small integer offset; while
628 create_mem_ref will simplify the address to an acceptable shape
629 later, it would be more efficient to know that asking for complicated
630 addressing modes is useless. */
633 addr_to_parts (tree type
, aff_tree
*addr
, tree iv_cand
,
634 tree base_hint
, struct mem_address
*parts
,
640 parts
->symbol
= NULL_TREE
;
641 parts
->base
= NULL_TREE
;
642 parts
->index
= NULL_TREE
;
643 parts
->step
= NULL_TREE
;
645 if (!addr
->offset
.is_zero ())
646 parts
->offset
= double_int_to_tree (sizetype
, addr
->offset
);
648 parts
->offset
= NULL_TREE
;
650 /* Try to find a symbol. */
651 move_fixed_address_to_symbol (parts
, addr
);
653 /* No need to do address parts reassociation if the number of parts
654 is <= 2 -- in that case, no loop invariant code motion can be
657 if (!base_hint
&& (addr
->n
> 2))
658 move_variant_to_index (parts
, addr
, iv_cand
);
660 /* First move the most expensive feasible multiplication
663 most_expensive_mult_to_index (type
, parts
, addr
, speed
);
665 /* Try to find a base of the reference. Since at the moment
666 there is no reliable way how to distinguish between pointer and its
667 offset, this is just a guess. */
668 if (!parts
->symbol
&& base_hint
)
669 move_hint_to_base (type
, parts
, base_hint
, addr
);
670 if (!parts
->symbol
&& !parts
->base
)
671 move_pointer_to_base (parts
, addr
);
673 /* Then try to process the remaining elements. */
674 for (i
= 0; i
< addr
->n
; i
++)
676 part
= fold_convert (sizetype
, addr
->elts
[i
].val
);
677 if (!addr
->elts
[i
].coef
.is_one ())
678 part
= fold_build2 (MULT_EXPR
, sizetype
, part
,
679 double_int_to_tree (sizetype
, addr
->elts
[i
].coef
));
680 add_to_parts (parts
, part
);
683 add_to_parts (parts
, fold_convert (sizetype
, addr
->rest
));
686 /* Force the PARTS to register. */
689 gimplify_mem_ref_parts (gimple_stmt_iterator
*gsi
, struct mem_address
*parts
)
692 parts
->base
= force_gimple_operand_gsi_1 (gsi
, parts
->base
,
693 is_gimple_mem_ref_addr
, NULL_TREE
,
694 true, GSI_SAME_STMT
);
696 parts
->index
= force_gimple_operand_gsi (gsi
, parts
->index
,
698 true, GSI_SAME_STMT
);
701 /* Creates and returns a TARGET_MEM_REF for address ADDR. If necessary
702 computations are emitted in front of GSI. TYPE is the mode
703 of created memory reference. IV_CAND is the selected iv candidate in ADDR,
704 and BASE_HINT is non NULL if IV_CAND comes from a base address
708 create_mem_ref (gimple_stmt_iterator
*gsi
, tree type
, aff_tree
*addr
,
709 tree alias_ptr_type
, tree iv_cand
, tree base_hint
, bool speed
)
712 struct mem_address parts
;
714 addr_to_parts (type
, addr
, iv_cand
, base_hint
, &parts
, speed
);
715 gimplify_mem_ref_parts (gsi
, &parts
);
716 mem_ref
= create_mem_ref_raw (type
, alias_ptr_type
, &parts
, true);
720 /* The expression is too complicated. Try making it simpler. */
722 if (parts
.step
&& !integer_onep (parts
.step
))
724 /* Move the multiplication to index. */
725 gcc_assert (parts
.index
);
726 parts
.index
= force_gimple_operand_gsi (gsi
,
727 fold_build2 (MULT_EXPR
, sizetype
,
728 parts
.index
, parts
.step
),
729 true, NULL_TREE
, true, GSI_SAME_STMT
);
730 parts
.step
= NULL_TREE
;
732 mem_ref
= create_mem_ref_raw (type
, alias_ptr_type
, &parts
, true);
740 gcc_assert (is_gimple_val (tmp
));
742 /* Add the symbol to base, eventually forcing it to register. */
745 gcc_assert (useless_type_conversion_p
746 (sizetype
, TREE_TYPE (parts
.base
)));
750 parts
.base
= force_gimple_operand_gsi_1 (gsi
,
751 fold_build_pointer_plus (tmp
, parts
.base
),
752 is_gimple_mem_ref_addr
, NULL_TREE
, true, GSI_SAME_STMT
);
756 parts
.index
= parts
.base
;
762 parts
.symbol
= NULL_TREE
;
764 mem_ref
= create_mem_ref_raw (type
, alias_ptr_type
, &parts
, true);
771 /* Add index to base. */
774 parts
.base
= force_gimple_operand_gsi_1 (gsi
,
775 fold_build_pointer_plus (parts
.base
, parts
.index
),
776 is_gimple_mem_ref_addr
, NULL_TREE
, true, GSI_SAME_STMT
);
779 parts
.base
= parts
.index
;
780 parts
.index
= NULL_TREE
;
782 mem_ref
= create_mem_ref_raw (type
, alias_ptr_type
, &parts
, true);
787 if (parts
.offset
&& !integer_zerop (parts
.offset
))
789 /* Try adding offset to base. */
792 parts
.base
= force_gimple_operand_gsi_1 (gsi
,
793 fold_build_pointer_plus (parts
.base
, parts
.offset
),
794 is_gimple_mem_ref_addr
, NULL_TREE
, true, GSI_SAME_STMT
);
797 parts
.base
= parts
.offset
;
799 parts
.offset
= NULL_TREE
;
801 mem_ref
= create_mem_ref_raw (type
, alias_ptr_type
, &parts
, true);
806 /* Verify that the address is in the simplest possible shape
807 (only a register). If we cannot create such a memory reference,
808 something is really wrong. */
809 gcc_assert (parts
.symbol
== NULL_TREE
);
810 gcc_assert (parts
.index
== NULL_TREE
);
811 gcc_assert (!parts
.step
|| integer_onep (parts
.step
));
812 gcc_assert (!parts
.offset
|| integer_zerop (parts
.offset
));
816 /* Copies components of the address from OP to ADDR. */
819 get_address_description (tree op
, struct mem_address
*addr
)
821 if (TREE_CODE (TMR_BASE (op
)) == ADDR_EXPR
)
823 addr
->symbol
= TMR_BASE (op
);
824 addr
->base
= TMR_INDEX2 (op
);
828 addr
->symbol
= NULL_TREE
;
831 gcc_assert (integer_zerop (TMR_BASE (op
)));
832 addr
->base
= TMR_INDEX2 (op
);
835 addr
->base
= TMR_BASE (op
);
837 addr
->index
= TMR_INDEX (op
);
838 addr
->step
= TMR_STEP (op
);
839 addr
->offset
= TMR_OFFSET (op
);
842 /* Copies the reference information from OLD_REF to NEW_REF, where
843 NEW_REF should be either a MEM_REF or a TARGET_MEM_REF. */
846 copy_ref_info (tree new_ref
, tree old_ref
)
848 tree new_ptr_base
= NULL_TREE
;
850 gcc_assert (TREE_CODE (new_ref
) == MEM_REF
851 || TREE_CODE (new_ref
) == TARGET_MEM_REF
);
853 TREE_SIDE_EFFECTS (new_ref
) = TREE_SIDE_EFFECTS (old_ref
);
854 TREE_THIS_VOLATILE (new_ref
) = TREE_THIS_VOLATILE (old_ref
);
856 new_ptr_base
= TREE_OPERAND (new_ref
, 0);
858 /* We can transfer points-to information from an old pointer
859 or decl base to the new one. */
861 && TREE_CODE (new_ptr_base
) == SSA_NAME
862 && !SSA_NAME_PTR_INFO (new_ptr_base
))
864 tree base
= get_base_address (old_ref
);
867 else if ((TREE_CODE (base
) == MEM_REF
868 || TREE_CODE (base
) == TARGET_MEM_REF
)
869 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
870 && SSA_NAME_PTR_INFO (TREE_OPERAND (base
, 0)))
872 struct ptr_info_def
*new_pi
;
873 unsigned int align
, misalign
;
875 duplicate_ssa_name_ptr_info
876 (new_ptr_base
, SSA_NAME_PTR_INFO (TREE_OPERAND (base
, 0)));
877 new_pi
= SSA_NAME_PTR_INFO (new_ptr_base
);
878 /* We have to be careful about transferring alignment information. */
879 if (get_ptr_info_alignment (new_pi
, &align
, &misalign
)
880 && TREE_CODE (old_ref
) == MEM_REF
881 && !(TREE_CODE (new_ref
) == TARGET_MEM_REF
882 && (TMR_INDEX2 (new_ref
)
883 || (TMR_STEP (new_ref
)
884 && (TREE_INT_CST_LOW (TMR_STEP (new_ref
))
887 unsigned int inc
= (mem_ref_offset (old_ref
)
888 - mem_ref_offset (new_ref
)).low
;
889 adjust_ptr_info_misalignment (new_pi
, inc
);
892 mark_ptr_info_alignment_unknown (new_pi
);
894 else if (TREE_CODE (base
) == VAR_DECL
895 || TREE_CODE (base
) == PARM_DECL
896 || TREE_CODE (base
) == RESULT_DECL
)
898 struct ptr_info_def
*pi
= get_ptr_info (new_ptr_base
);
899 pt_solution_set_var (&pi
->pt
, base
);
904 /* Move constants in target_mem_ref REF to offset. Returns the new target
905 mem ref if anything changes, NULL_TREE otherwise. */
908 maybe_fold_tmr (tree ref
)
910 struct mem_address addr
;
911 bool changed
= false;
914 get_address_description (ref
, &addr
);
917 && TREE_CODE (addr
.base
) == INTEGER_CST
918 && !integer_zerop (addr
.base
))
920 addr
.offset
= fold_binary_to_constant (PLUS_EXPR
,
921 TREE_TYPE (addr
.offset
),
922 addr
.offset
, addr
.base
);
923 addr
.base
= NULL_TREE
;
928 && TREE_CODE (TREE_OPERAND (addr
.symbol
, 0)) == MEM_REF
)
930 addr
.offset
= fold_binary_to_constant
931 (PLUS_EXPR
, TREE_TYPE (addr
.offset
),
933 TREE_OPERAND (TREE_OPERAND (addr
.symbol
, 0), 1));
934 addr
.symbol
= TREE_OPERAND (TREE_OPERAND (addr
.symbol
, 0), 0);
938 && handled_component_p (TREE_OPERAND (addr
.symbol
, 0)))
940 HOST_WIDE_INT offset
;
941 addr
.symbol
= build_fold_addr_expr
942 (get_addr_base_and_unit_offset
943 (TREE_OPERAND (addr
.symbol
, 0), &offset
));
944 addr
.offset
= int_const_binop (PLUS_EXPR
,
945 addr
.offset
, size_int (offset
));
949 if (addr
.index
&& TREE_CODE (addr
.index
) == INTEGER_CST
)
954 off
= fold_binary_to_constant (MULT_EXPR
, sizetype
,
956 addr
.step
= NULL_TREE
;
959 addr
.offset
= fold_binary_to_constant (PLUS_EXPR
,
960 TREE_TYPE (addr
.offset
),
962 addr
.index
= NULL_TREE
;
969 /* If we have propagated something into this TARGET_MEM_REF and thus
970 ended up folding it, always create a new TARGET_MEM_REF regardless
971 if it is valid in this for on the target - the propagation result
972 wouldn't be anyway. */
973 new_ref
= create_mem_ref_raw (TREE_TYPE (ref
),
974 TREE_TYPE (addr
.offset
), &addr
, false);
975 TREE_SIDE_EFFECTS (new_ref
) = TREE_SIDE_EFFECTS (ref
);
976 TREE_THIS_VOLATILE (new_ref
) = TREE_THIS_VOLATILE (ref
);
980 /* Dump PARTS to FILE. */
982 extern void dump_mem_address (FILE *, struct mem_address
*);
984 dump_mem_address (FILE *file
, struct mem_address
*parts
)
988 fprintf (file
, "symbol: ");
989 print_generic_expr (file
, TREE_OPERAND (parts
->symbol
, 0), TDF_SLIM
);
990 fprintf (file
, "\n");
994 fprintf (file
, "base: ");
995 print_generic_expr (file
, parts
->base
, TDF_SLIM
);
996 fprintf (file
, "\n");
1000 fprintf (file
, "index: ");
1001 print_generic_expr (file
, parts
->index
, TDF_SLIM
);
1002 fprintf (file
, "\n");
1006 fprintf (file
, "step: ");
1007 print_generic_expr (file
, parts
->step
, TDF_SLIM
);
1008 fprintf (file
, "\n");
1012 fprintf (file
, "offset: ");
1013 print_generic_expr (file
, parts
->offset
, TDF_SLIM
);
1014 fprintf (file
, "\n");
1018 #include "gt-tree-ssa-address.h"