Reverting merge from trunk
[official-gcc.git] / gcc / tree-ssa-address.c
blobcf245675796b66685212ed86e1c82b45ca61cf06
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
9 later version.
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
14 for more details.
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. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "basic-block.h"
30 #include "tree-pretty-print.h"
31 #include "gimple.h"
32 #include "gimple-iterator.h"
33 #include "gimplify-me.h"
34 #include "tree-ssanames.h"
35 #include "tree-ssa-loop-ivopts.h"
36 #include "tree-dfa.h"
37 #include "dumpfile.h"
38 #include "flags.h"
39 #include "tree-inline.h"
40 #include "tree-affine.h"
42 /* FIXME: We compute address costs using RTL. */
43 #include "insn-config.h"
44 #include "rtl.h"
45 #include "recog.h"
46 #include "expr.h"
47 #include "ggc.h"
48 #include "target.h"
49 #include "expmed.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
74 precise results. */
76 /* A "template" for memory address, used to determine whether the address is
77 valid for mode. */
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
82 filled in. */
83 rtx * GTY ((skip)) off_p; /* The point in template where the offset should
84 be filled in. */
85 } mem_addr_template;
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) \
95 (((int) (AS) << 5) \
96 | ((SYMBOL != 0) << 4) \
97 | ((BASE != 0) << 3) \
98 | ((INDEX != 0) << 2) \
99 | ((STEP != 0) << 1) \
100 | (OFFSET != 0))
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. */
106 static void
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)
111 rtx act_elem;
113 *addr = NULL_RTX;
114 if (step_p)
115 *step_p = NULL;
116 if (offset_p)
117 *offset_p = NULL;
119 if (index)
121 act_elem = index;
122 if (step)
124 act_elem = gen_rtx_MULT (address_mode, act_elem, step);
126 if (step_p)
127 *step_p = &XEXP (act_elem, 1);
130 *addr = act_elem;
133 if (base && base != const0_rtx)
135 if (*addr)
136 *addr = simplify_gen_binary (PLUS, address_mode, base, *addr);
137 else
138 *addr = base;
141 if (symbol)
143 act_elem = symbol;
144 if (offset)
146 act_elem = gen_rtx_PLUS (address_mode, act_elem, offset);
148 if (offset_p)
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);
157 if (*addr)
158 *addr = gen_rtx_PLUS (address_mode, *addr, act_elem);
159 else
160 *addr = act_elem;
162 else if (offset)
164 if (*addr)
166 *addr = gen_rtx_PLUS (address_mode, *addr, offset);
167 if (offset_p)
168 *offset_p = &XEXP (*addr, 1);
170 else
172 *addr = offset;
173 if (offset_p)
174 *offset_p = addr;
178 if (!*addr)
179 *addr = const0_rtx;
182 /* Description of a memory address. */
184 struct mem_address
186 tree symbol, base, index, step, offset;
189 /* Returns address for TARGET_MEM_REF with parameters given by ADDR
190 in address space AS.
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,
197 bool really_expand)
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);
206 else
207 st = NULL_RTX;
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))),
213 pointer_mode);
214 else
215 off = NULL_RTX;
217 if (!really_expand)
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];
227 if (!templ->ref)
229 sym = (addr->symbol ?
230 gen_rtx_SYMBOL_REF (pointer_mode, ggc_strdup ("test_symbol"))
231 : NULL_RTX);
232 bse = (addr->base ?
233 gen_raw_REG (pointer_mode, LAST_VIRTUAL_REGISTER + 1)
234 : NULL_RTX);
235 idx = (addr->index ?
236 gen_raw_REG (pointer_mode, LAST_VIRTUAL_REGISTER + 2)
237 : NULL_RTX);
239 gen_addr_rtx (pointer_mode, sym, bse, idx,
240 st? const0_rtx : NULL_RTX,
241 off? const0_rtx : NULL_RTX,
242 &templ->ref,
243 &templ->step_p,
244 &templ->off_p);
247 if (st)
248 *templ->step_p = st;
249 if (off)
250 *templ->off_p = off;
252 return templ->ref;
255 /* Otherwise really expand the expressions. */
256 sym = (addr->symbol
257 ? expand_expr (addr->symbol, NULL_RTX, pointer_mode, EXPAND_NORMAL)
258 : NULL_RTX);
259 bse = (addr->base
260 ? expand_expr (addr->base, NULL_RTX, pointer_mode, EXPAND_NORMAL)
261 : NULL_RTX);
262 idx = (addr->index
263 ? expand_expr (addr->index, NULL_RTX, pointer_mode, EXPAND_NORMAL)
264 : NULL_RTX);
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);
269 return 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. */
285 tree
286 tree_mem_ref_addr (tree type, tree mem_ref)
288 tree addr;
289 tree act_elem;
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);
296 if (act_elem)
298 if (step)
299 act_elem = fold_build2 (MULT_EXPR, TREE_TYPE (act_elem),
300 act_elem, step);
301 addr_off = act_elem;
304 act_elem = TMR_INDEX2 (mem_ref);
305 if (act_elem)
307 if (addr_off)
308 addr_off = fold_build2 (PLUS_EXPR, TREE_TYPE (addr_off),
309 addr_off, act_elem);
310 else
311 addr_off = act_elem;
314 if (offset && !integer_zerop (offset))
316 if (addr_off)
317 addr_off = fold_build2 (PLUS_EXPR, TREE_TYPE (addr_off), addr_off,
318 fold_convert (TREE_TYPE (addr_off), offset));
319 else
320 addr_off = offset;
323 if (addr_off)
324 addr = fold_build_pointer_plus (addr_base, addr_off);
325 else
326 addr = addr_base;
328 return addr;
331 /* Returns true if a memory reference in MODE and with parameters given by
332 ADDR is valid on the current target. */
334 static bool
335 valid_mem_ref_p (enum machine_mode mode, addr_space_t as,
336 struct mem_address *addr)
338 rtx address;
340 address = addr_for_mem_ref (addr, as, false);
341 if (!address)
342 return 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. */
351 static tree
352 create_mem_ref_raw (tree type, tree alias_ptr_type, struct mem_address *addr,
353 bool verify)
355 tree base, index2;
357 if (verify
358 && !valid_mem_ref_p (TYPE_MODE (type), TYPE_ADDR_SPACE (type), addr))
359 return NULL_TREE;
361 if (addr->step && integer_onep (addr->step))
362 addr->step = NULL_TREE;
364 if (addr->offset)
365 addr->offset = fold_convert (alias_ptr_type, addr->offset);
366 else
367 addr->offset = build_int_cst (alias_ptr_type, 0);
369 if (addr->symbol)
371 base = addr->symbol;
372 index2 = addr->base;
374 else if (addr->base
375 && POINTER_TYPE_P (TREE_TYPE (addr->base)))
377 base = addr->base;
378 index2 = NULL_TREE;
380 else
382 base = build_int_cst (ptr_type_node, 0);
383 index2 = addr->base;
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
389 base is valid. */
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. */
401 static bool
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. */
413 static void
414 move_fixed_address_to_symbol (struct mem_address *parts, aff_tree *addr)
416 unsigned i;
417 tree val = NULL_TREE;
419 for (i = 0; i < addr->n; i++)
421 if (!addr->elts[i].coef.is_one ())
422 continue;
424 val = addr->elts[i].val;
425 if (TREE_CODE (val) == ADDR_EXPR
426 && fixed_address_object_p (TREE_OPERAND (val, 0)))
427 break;
430 if (i == addr->n)
431 return;
433 parts->symbol = val;
434 aff_combination_remove_elt (addr, i);
437 /* If ADDR contains an instance of BASE_HINT, move it to PARTS->base. */
439 static void
440 move_hint_to_base (tree type, struct mem_address *parts, tree base_hint,
441 aff_tree *addr)
443 unsigned i;
444 tree val = NULL_TREE;
445 int qual;
447 for (i = 0; i < addr->n; i++)
449 if (!addr->elts[i].coef.is_one ())
450 continue;
452 val = addr->elts[i].val;
453 if (operand_equal_p (val, base_hint, 0))
454 break;
457 if (i == addr->n)
458 return;
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
471 PARTS->base. */
473 static void
474 move_pointer_to_base (struct mem_address *parts, aff_tree *addr)
476 unsigned i;
477 tree val = NULL_TREE;
479 for (i = 0; i < addr->n; i++)
481 if (!addr->elts[i].coef.is_one ())
482 continue;
484 val = addr->elts[i].val;
485 if (POINTER_TYPE_P (TREE_TYPE (val)))
486 break;
489 if (i == addr->n)
490 return;
492 parts->base = val;
493 aff_combination_remove_elt (addr, i);
496 /* Moves the loop variant part V in linear address ADDR to be the index
497 of PARTS. */
499 static void
500 move_variant_to_index (struct mem_address *parts, aff_tree *addr, tree v)
502 unsigned i;
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))
510 break;
513 if (i == addr->n)
514 return;
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. */
523 static void
524 add_to_parts (struct mem_address *parts, tree elt)
526 tree type;
528 if (!parts->index)
530 parts->index = fold_convert (sizetype, elt);
531 return;
534 if (!parts->base)
536 parts->base = elt;
537 return;
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);
544 else
545 parts->base = fold_build2 (PLUS_EXPR, type,
546 parts->base, elt);
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. */
553 static void
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);
559 HOST_WIDE_INT coef;
560 double_int best_mult, amult, amult_neg;
561 unsigned best_mult_cost = 0, acost;
562 tree mult_elt = NULL_TREE, elt;
563 unsigned i, j;
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 ())
570 continue;
572 coef = addr->elts[i].coef.to_shwi ();
573 if (coef == 1
574 || !multiplier_allowed_in_address_p (coef, TYPE_MODE (type), as))
575 continue;
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;
586 if (!best_mult_cost)
587 return;
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)
596 op_code = PLUS_EXPR;
597 else if (amult_neg == best_mult)
598 op_code = MINUS_EXPR;
599 else
601 addr->elts[j] = addr->elts[i];
602 j++;
603 continue;
606 elt = fold_convert (sizetype, addr->elts[i].val);
607 if (mult_elt)
608 mult_elt = fold_build2 (op_code, sizetype, mult_elt, elt);
609 else if (op_code == PLUS_EXPR)
610 mult_elt = elt;
611 else
612 mult_elt = fold_build1 (NEGATE_EXPR, sizetype, elt);
614 addr->n = j;
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. */
632 static void
633 addr_to_parts (tree type, aff_tree *addr, tree iv_cand,
634 tree base_hint, struct mem_address *parts,
635 bool speed)
637 tree part;
638 unsigned i;
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);
647 else
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
655 exposed. */
657 if (!base_hint && (addr->n > 2))
658 move_variant_to_index (parts, addr, iv_cand);
660 /* First move the most expensive feasible multiplication
661 to index. */
662 if (!parts->index)
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);
682 if (addr->rest)
683 add_to_parts (parts, fold_convert (sizetype, addr->rest));
686 /* Force the PARTS to register. */
688 static void
689 gimplify_mem_ref_parts (gimple_stmt_iterator *gsi, struct mem_address *parts)
691 if (parts->base)
692 parts->base = force_gimple_operand_gsi_1 (gsi, parts->base,
693 is_gimple_mem_ref_addr, NULL_TREE,
694 true, GSI_SAME_STMT);
695 if (parts->index)
696 parts->index = force_gimple_operand_gsi (gsi, parts->index,
697 true, NULL_TREE,
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
705 object. */
707 tree
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)
711 tree mem_ref, tmp;
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);
717 if (mem_ref)
718 return mem_ref;
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);
733 if (mem_ref)
734 return mem_ref;
737 if (parts.symbol)
739 tmp = parts.symbol;
740 gcc_assert (is_gimple_val (tmp));
742 /* Add the symbol to base, eventually forcing it to register. */
743 if (parts.base)
745 gcc_assert (useless_type_conversion_p
746 (sizetype, TREE_TYPE (parts.base)));
748 if (parts.index)
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);
754 else
756 parts.index = parts.base;
757 parts.base = tmp;
760 else
761 parts.base = tmp;
762 parts.symbol = NULL_TREE;
764 mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
765 if (mem_ref)
766 return mem_ref;
769 if (parts.index)
771 /* Add index to base. */
772 if (parts.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);
778 else
779 parts.base = parts.index;
780 parts.index = NULL_TREE;
782 mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
783 if (mem_ref)
784 return mem_ref;
787 if (parts.offset && !integer_zerop (parts.offset))
789 /* Try adding offset to base. */
790 if (parts.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);
796 else
797 parts.base = parts.offset;
799 parts.offset = NULL_TREE;
801 mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
802 if (mem_ref)
803 return mem_ref;
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));
813 gcc_unreachable ();
816 /* Copies components of the address from OP to ADDR. */
818 void
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);
826 else
828 addr->symbol = NULL_TREE;
829 if (TMR_INDEX2 (op))
831 gcc_assert (integer_zerop (TMR_BASE (op)));
832 addr->base = TMR_INDEX2 (op);
834 else
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. */
845 void
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. */
860 if (new_ptr_base
861 && TREE_CODE (new_ptr_base) == SSA_NAME
862 && !SSA_NAME_PTR_INFO (new_ptr_base))
864 tree base = get_base_address (old_ref);
865 if (!base)
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))
885 < align)))))
887 unsigned int inc = (mem_ref_offset (old_ref)
888 - mem_ref_offset (new_ref)).low;
889 adjust_ptr_info_misalignment (new_pi, inc);
891 else
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. */
907 tree
908 maybe_fold_tmr (tree ref)
910 struct mem_address addr;
911 bool changed = false;
912 tree new_ref, off;
914 get_address_description (ref, &addr);
916 if (addr.base
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;
924 changed = true;
927 if (addr.symbol
928 && TREE_CODE (TREE_OPERAND (addr.symbol, 0)) == MEM_REF)
930 addr.offset = fold_binary_to_constant
931 (PLUS_EXPR, TREE_TYPE (addr.offset),
932 addr.offset,
933 TREE_OPERAND (TREE_OPERAND (addr.symbol, 0), 1));
934 addr.symbol = TREE_OPERAND (TREE_OPERAND (addr.symbol, 0), 0);
935 changed = true;
937 else if (addr.symbol
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));
946 changed = true;
949 if (addr.index && TREE_CODE (addr.index) == INTEGER_CST)
951 off = addr.index;
952 if (addr.step)
954 off = fold_binary_to_constant (MULT_EXPR, sizetype,
955 off, addr.step);
956 addr.step = NULL_TREE;
959 addr.offset = fold_binary_to_constant (PLUS_EXPR,
960 TREE_TYPE (addr.offset),
961 addr.offset, off);
962 addr.index = NULL_TREE;
963 changed = true;
966 if (!changed)
967 return 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);
977 return new_ref;
980 /* Dump PARTS to FILE. */
982 extern void dump_mem_address (FILE *, struct mem_address *);
983 void
984 dump_mem_address (FILE *file, struct mem_address *parts)
986 if (parts->symbol)
988 fprintf (file, "symbol: ");
989 print_generic_expr (file, TREE_OPERAND (parts->symbol, 0), TDF_SLIM);
990 fprintf (file, "\n");
992 if (parts->base)
994 fprintf (file, "base: ");
995 print_generic_expr (file, parts->base, TDF_SLIM);
996 fprintf (file, "\n");
998 if (parts->index)
1000 fprintf (file, "index: ");
1001 print_generic_expr (file, parts->index, TDF_SLIM);
1002 fprintf (file, "\n");
1004 if (parts->step)
1006 fprintf (file, "step: ");
1007 print_generic_expr (file, parts->step, TDF_SLIM);
1008 fprintf (file, "\n");
1010 if (parts->offset)
1012 fprintf (file, "offset: ");
1013 print_generic_expr (file, parts->offset, TDF_SLIM);
1014 fprintf (file, "\n");
1018 #include "gt-tree-ssa-address.h"