]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-ssa-address.c
gimplify-be.h: New file.
[thirdparty/gcc.git] / gcc / tree-ssa-address.c
1 /* Memory address lowering and addressing mode selection.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 /* Utility functions for manipulation with TARGET_MEM_REFs -- tree expressions
21 that directly map to addressing modes of the target. */
22
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"
41
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"
51
52 /* TODO -- handling of symbols (according to Richard Hendersons
53 comments, http://gcc.gnu.org/ml/gcc-patches/2005-04/msg00949.html):
54
55 There are at least 5 different kinds of symbols that we can run up against:
56
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
62
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"))).
66
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).
70
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. */
75
76 /* A "template" for memory address, used to determine whether the address is
77 valid for mode. */
78
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;
86
87
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. */
91
92 static GTY(()) vec<mem_addr_template, va_gc> *mem_addr_template_list;
93
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))
101
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. */
105
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)
110 {
111 rtx act_elem;
112
113 *addr = NULL_RTX;
114 if (step_p)
115 *step_p = NULL;
116 if (offset_p)
117 *offset_p = NULL;
118
119 if (index)
120 {
121 act_elem = index;
122 if (step)
123 {
124 act_elem = gen_rtx_MULT (address_mode, act_elem, step);
125
126 if (step_p)
127 *step_p = &XEXP (act_elem, 1);
128 }
129
130 *addr = act_elem;
131 }
132
133 if (base && base != const0_rtx)
134 {
135 if (*addr)
136 *addr = simplify_gen_binary (PLUS, address_mode, base, *addr);
137 else
138 *addr = base;
139 }
140
141 if (symbol)
142 {
143 act_elem = symbol;
144 if (offset)
145 {
146 act_elem = gen_rtx_PLUS (address_mode, act_elem, offset);
147
148 if (offset_p)
149 *offset_p = &XEXP (act_elem, 1);
150
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);
155 }
156
157 if (*addr)
158 *addr = gen_rtx_PLUS (address_mode, *addr, act_elem);
159 else
160 *addr = act_elem;
161 }
162 else if (offset)
163 {
164 if (*addr)
165 {
166 *addr = gen_rtx_PLUS (address_mode, *addr, offset);
167 if (offset_p)
168 *offset_p = &XEXP (*addr, 1);
169 }
170 else
171 {
172 *addr = offset;
173 if (offset_p)
174 *offset_p = addr;
175 }
176 }
177
178 if (!*addr)
179 *addr = const0_rtx;
180 }
181
182 /* Description of a memory address. */
183
184 struct mem_address
185 {
186 tree symbol, base, index, step, offset;
187 };
188
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". */
194
195 rtx
196 addr_for_mem_ref (struct mem_address *addr, addr_space_t as,
197 bool really_expand)
198 {
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;
203
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;
208
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;
216
217 if (!really_expand)
218 {
219 unsigned int templ_index
220 = TEMPL_IDX (as, addr->symbol, addr->base, addr->index, st, off);
221
222 if (templ_index >= vec_safe_length (mem_addr_template_list))
223 vec_safe_grow_cleared (mem_addr_template_list, templ_index + 1);
224
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)
228 {
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);
238
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);
245 }
246
247 if (st)
248 *templ->step_p = st;
249 if (off)
250 *templ->off_p = off;
251
252 return templ->ref;
253 }
254
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);
265
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;
270 }
271
272 /* implement addr_for_mem_ref() directly from a tree, which avoids exporting
273 the mem_address structure. */
274
275 rtx
276 addr_for_mem_ref (tree exp, addr_space_t as, bool really_expand)
277 {
278 struct mem_address addr;
279 get_address_description (exp, &addr);
280 return addr_for_mem_ref (&addr, as, really_expand);
281 }
282
283 /* Returns address of MEM_REF in TYPE. */
284
285 tree
286 tree_mem_ref_addr (tree type, tree mem_ref)
287 {
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;
292
293 addr_base = fold_convert (type, TMR_BASE (mem_ref));
294
295 act_elem = TMR_INDEX (mem_ref);
296 if (act_elem)
297 {
298 if (step)
299 act_elem = fold_build2 (MULT_EXPR, TREE_TYPE (act_elem),
300 act_elem, step);
301 addr_off = act_elem;
302 }
303
304 act_elem = TMR_INDEX2 (mem_ref);
305 if (act_elem)
306 {
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;
312 }
313
314 if (offset && !integer_zerop (offset))
315 {
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;
321 }
322
323 if (addr_off)
324 addr = fold_build_pointer_plus (addr_base, addr_off);
325 else
326 addr = addr_base;
327
328 return addr;
329 }
330
331 /* Returns true if a memory reference in MODE and with parameters given by
332 ADDR is valid on the current target. */
333
334 static bool
335 valid_mem_ref_p (enum machine_mode mode, addr_space_t as,
336 struct mem_address *addr)
337 {
338 rtx address;
339
340 address = addr_for_mem_ref (addr, as, false);
341 if (!address)
342 return false;
343
344 return memory_address_addr_space_p (mode, address, as);
345 }
346
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. */
350
351 static tree
352 create_mem_ref_raw (tree type, tree alias_ptr_type, struct mem_address *addr,
353 bool verify)
354 {
355 tree base, index2;
356
357 if (verify
358 && !valid_mem_ref_p (TYPE_MODE (type), TYPE_ADDR_SPACE (type), addr))
359 return NULL_TREE;
360
361 if (addr->step && integer_onep (addr->step))
362 addr->step = NULL_TREE;
363
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);
368
369 if (addr->symbol)
370 {
371 base = addr->symbol;
372 index2 = addr->base;
373 }
374 else if (addr->base
375 && POINTER_TYPE_P (TREE_TYPE (addr->base)))
376 {
377 base = addr->base;
378 index2 = NULL_TREE;
379 }
380 else
381 {
382 base = build_int_cst (ptr_type_node, 0);
383 index2 = addr->base;
384 }
385
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);
394
395 return build5 (TARGET_MEM_REF, type,
396 base, addr->offset, addr->index, addr->step, index2);
397 }
398
399 /* Returns true if OBJ is an object whose address is a link time constant. */
400
401 static bool
402 fixed_address_object_p (tree obj)
403 {
404 return (TREE_CODE (obj) == VAR_DECL
405 && (TREE_STATIC (obj)
406 || DECL_EXTERNAL (obj))
407 && ! DECL_DLLIMPORT_P (obj));
408 }
409
410 /* If ADDR contains an address of object that is a link time constant,
411 move it to PARTS->symbol. */
412
413 static void
414 move_fixed_address_to_symbol (struct mem_address *parts, aff_tree *addr)
415 {
416 unsigned i;
417 tree val = NULL_TREE;
418
419 for (i = 0; i < addr->n; i++)
420 {
421 if (!addr->elts[i].coef.is_one ())
422 continue;
423
424 val = addr->elts[i].val;
425 if (TREE_CODE (val) == ADDR_EXPR
426 && fixed_address_object_p (TREE_OPERAND (val, 0)))
427 break;
428 }
429
430 if (i == addr->n)
431 return;
432
433 parts->symbol = val;
434 aff_combination_remove_elt (addr, i);
435 }
436
437 /* If ADDR contains an instance of BASE_HINT, move it to PARTS->base. */
438
439 static void
440 move_hint_to_base (tree type, struct mem_address *parts, tree base_hint,
441 aff_tree *addr)
442 {
443 unsigned i;
444 tree val = NULL_TREE;
445 int qual;
446
447 for (i = 0; i < addr->n; i++)
448 {
449 if (!addr->elts[i].coef.is_one ())
450 continue;
451
452 val = addr->elts[i].val;
453 if (operand_equal_p (val, base_hint, 0))
454 break;
455 }
456
457 if (i == addr->n)
458 return;
459
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);
468 }
469
470 /* If ADDR contains an address of a dereferenced pointer, move it to
471 PARTS->base. */
472
473 static void
474 move_pointer_to_base (struct mem_address *parts, aff_tree *addr)
475 {
476 unsigned i;
477 tree val = NULL_TREE;
478
479 for (i = 0; i < addr->n; i++)
480 {
481 if (!addr->elts[i].coef.is_one ())
482 continue;
483
484 val = addr->elts[i].val;
485 if (POINTER_TYPE_P (TREE_TYPE (val)))
486 break;
487 }
488
489 if (i == addr->n)
490 return;
491
492 parts->base = val;
493 aff_combination_remove_elt (addr, i);
494 }
495
496 /* Moves the loop variant part V in linear address ADDR to be the index
497 of PARTS. */
498
499 static void
500 move_variant_to_index (struct mem_address *parts, aff_tree *addr, tree v)
501 {
502 unsigned i;
503 tree val = NULL_TREE;
504
505 gcc_assert (!parts->index);
506 for (i = 0; i < addr->n; i++)
507 {
508 val = addr->elts[i].val;
509 if (operand_equal_p (val, v, 0))
510 break;
511 }
512
513 if (i == addr->n)
514 return;
515
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);
519 }
520
521 /* Adds ELT to PARTS. */
522
523 static void
524 add_to_parts (struct mem_address *parts, tree elt)
525 {
526 tree type;
527
528 if (!parts->index)
529 {
530 parts->index = fold_convert (sizetype, elt);
531 return;
532 }
533
534 if (!parts->base)
535 {
536 parts->base = elt;
537 return;
538 }
539
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);
547 }
548
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. */
552
553 static void
554 most_expensive_mult_to_index (tree type, struct mem_address *parts,
555 aff_tree *addr, bool speed)
556 {
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;
565
566 best_mult = double_int_zero;
567 for (i = 0; i < addr->n; i++)
568 {
569 if (!addr->elts[i].coef.fits_shwi ())
570 continue;
571
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;
576
577 acost = mult_by_coeff_cost (coef, address_mode, speed);
578
579 if (acost > best_mult_cost)
580 {
581 best_mult_cost = acost;
582 best_mult = addr->elts[i].coef;
583 }
584 }
585
586 if (!best_mult_cost)
587 return;
588
589 /* Collect elements multiplied by best_mult. */
590 for (i = j = 0; i < addr->n; i++)
591 {
592 amult = addr->elts[i].coef;
593 amult_neg = double_int_ext_for_comb (-amult, addr);
594
595 if (amult == best_mult)
596 op_code = PLUS_EXPR;
597 else if (amult_neg == best_mult)
598 op_code = MINUS_EXPR;
599 else
600 {
601 addr->elts[j] = addr->elts[i];
602 j++;
603 continue;
604 }
605
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);
613 }
614 addr->n = j;
615
616 parts->index = mult_elt;
617 parts->step = double_int_to_tree (sizetype, best_mult);
618 }
619
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.
624
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. */
631
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)
636 {
637 tree part;
638 unsigned i;
639
640 parts->symbol = NULL_TREE;
641 parts->base = NULL_TREE;
642 parts->index = NULL_TREE;
643 parts->step = NULL_TREE;
644
645 if (!addr->offset.is_zero ())
646 parts->offset = double_int_to_tree (sizetype, addr->offset);
647 else
648 parts->offset = NULL_TREE;
649
650 /* Try to find a symbol. */
651 move_fixed_address_to_symbol (parts, addr);
652
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. */
656
657 if (!base_hint && (addr->n > 2))
658 move_variant_to_index (parts, addr, iv_cand);
659
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);
664
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);
672
673 /* Then try to process the remaining elements. */
674 for (i = 0; i < addr->n; i++)
675 {
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);
681 }
682 if (addr->rest)
683 add_to_parts (parts, fold_convert (sizetype, addr->rest));
684 }
685
686 /* Force the PARTS to register. */
687
688 static void
689 gimplify_mem_ref_parts (gimple_stmt_iterator *gsi, struct mem_address *parts)
690 {
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);
699 }
700
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. */
706
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)
710 {
711 tree mem_ref, tmp;
712 struct mem_address parts;
713
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;
719
720 /* The expression is too complicated. Try making it simpler. */
721
722 if (parts.step && !integer_onep (parts.step))
723 {
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;
731
732 mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
733 if (mem_ref)
734 return mem_ref;
735 }
736
737 if (parts.symbol)
738 {
739 tmp = parts.symbol;
740 gcc_assert (is_gimple_val (tmp));
741
742 /* Add the symbol to base, eventually forcing it to register. */
743 if (parts.base)
744 {
745 gcc_assert (useless_type_conversion_p
746 (sizetype, TREE_TYPE (parts.base)));
747
748 if (parts.index)
749 {
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);
753 }
754 else
755 {
756 parts.index = parts.base;
757 parts.base = tmp;
758 }
759 }
760 else
761 parts.base = tmp;
762 parts.symbol = NULL_TREE;
763
764 mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
765 if (mem_ref)
766 return mem_ref;
767 }
768
769 if (parts.index)
770 {
771 /* Add index to base. */
772 if (parts.base)
773 {
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);
777 }
778 else
779 parts.base = parts.index;
780 parts.index = NULL_TREE;
781
782 mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
783 if (mem_ref)
784 return mem_ref;
785 }
786
787 if (parts.offset && !integer_zerop (parts.offset))
788 {
789 /* Try adding offset to base. */
790 if (parts.base)
791 {
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);
795 }
796 else
797 parts.base = parts.offset;
798
799 parts.offset = NULL_TREE;
800
801 mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
802 if (mem_ref)
803 return mem_ref;
804 }
805
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 ();
814 }
815
816 /* Copies components of the address from OP to ADDR. */
817
818 void
819 get_address_description (tree op, struct mem_address *addr)
820 {
821 if (TREE_CODE (TMR_BASE (op)) == ADDR_EXPR)
822 {
823 addr->symbol = TMR_BASE (op);
824 addr->base = TMR_INDEX2 (op);
825 }
826 else
827 {
828 addr->symbol = NULL_TREE;
829 if (TMR_INDEX2 (op))
830 {
831 gcc_assert (integer_zerop (TMR_BASE (op)));
832 addr->base = TMR_INDEX2 (op);
833 }
834 else
835 addr->base = TMR_BASE (op);
836 }
837 addr->index = TMR_INDEX (op);
838 addr->step = TMR_STEP (op);
839 addr->offset = TMR_OFFSET (op);
840 }
841
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. */
844
845 void
846 copy_ref_info (tree new_ref, tree old_ref)
847 {
848 tree new_ptr_base = NULL_TREE;
849
850 gcc_assert (TREE_CODE (new_ref) == MEM_REF
851 || TREE_CODE (new_ref) == TARGET_MEM_REF);
852
853 TREE_SIDE_EFFECTS (new_ref) = TREE_SIDE_EFFECTS (old_ref);
854 TREE_THIS_VOLATILE (new_ref) = TREE_THIS_VOLATILE (old_ref);
855
856 new_ptr_base = TREE_OPERAND (new_ref, 0);
857
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))
863 {
864 tree base = get_base_address (old_ref);
865 if (!base)
866 ;
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)))
871 {
872 struct ptr_info_def *new_pi;
873 unsigned int align, misalign;
874
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)))))
886 {
887 unsigned int inc = (mem_ref_offset (old_ref)
888 - mem_ref_offset (new_ref)).low;
889 adjust_ptr_info_misalignment (new_pi, inc);
890 }
891 else
892 mark_ptr_info_alignment_unknown (new_pi);
893 }
894 else if (TREE_CODE (base) == VAR_DECL
895 || TREE_CODE (base) == PARM_DECL
896 || TREE_CODE (base) == RESULT_DECL)
897 {
898 struct ptr_info_def *pi = get_ptr_info (new_ptr_base);
899 pt_solution_set_var (&pi->pt, base);
900 }
901 }
902 }
903
904 /* Move constants in target_mem_ref REF to offset. Returns the new target
905 mem ref if anything changes, NULL_TREE otherwise. */
906
907 tree
908 maybe_fold_tmr (tree ref)
909 {
910 struct mem_address addr;
911 bool changed = false;
912 tree new_ref, off;
913
914 get_address_description (ref, &addr);
915
916 if (addr.base
917 && TREE_CODE (addr.base) == INTEGER_CST
918 && !integer_zerop (addr.base))
919 {
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;
925 }
926
927 if (addr.symbol
928 && TREE_CODE (TREE_OPERAND (addr.symbol, 0)) == MEM_REF)
929 {
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;
936 }
937 else if (addr.symbol
938 && handled_component_p (TREE_OPERAND (addr.symbol, 0)))
939 {
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;
947 }
948
949 if (addr.index && TREE_CODE (addr.index) == INTEGER_CST)
950 {
951 off = addr.index;
952 if (addr.step)
953 {
954 off = fold_binary_to_constant (MULT_EXPR, sizetype,
955 off, addr.step);
956 addr.step = NULL_TREE;
957 }
958
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;
964 }
965
966 if (!changed)
967 return NULL_TREE;
968
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;
978 }
979
980 /* Dump PARTS to FILE. */
981
982 extern void dump_mem_address (FILE *, struct mem_address *);
983 void
984 dump_mem_address (FILE *file, struct mem_address *parts)
985 {
986 if (parts->symbol)
987 {
988 fprintf (file, "symbol: ");
989 print_generic_expr (file, TREE_OPERAND (parts->symbol, 0), TDF_SLIM);
990 fprintf (file, "\n");
991 }
992 if (parts->base)
993 {
994 fprintf (file, "base: ");
995 print_generic_expr (file, parts->base, TDF_SLIM);
996 fprintf (file, "\n");
997 }
998 if (parts->index)
999 {
1000 fprintf (file, "index: ");
1001 print_generic_expr (file, parts->index, TDF_SLIM);
1002 fprintf (file, "\n");
1003 }
1004 if (parts->step)
1005 {
1006 fprintf (file, "step: ");
1007 print_generic_expr (file, parts->step, TDF_SLIM);
1008 fprintf (file, "\n");
1009 }
1010 if (parts->offset)
1011 {
1012 fprintf (file, "offset: ");
1013 print_generic_expr (file, parts->offset, TDF_SLIM);
1014 fprintf (file, "\n");
1015 }
1016 }
1017
1018 #include "gt-tree-ssa-address.h"