* lib/scanasm.exp (hidden-scan-for): Add XCOFF support.
[official-gcc.git] / gcc / gimple-ssa-store-merging.c
blob57b855671dbbda59c206403ce3adb4acf65a3cad
1 /* GIMPLE store merging pass.
2 Copyright (C) 2016 Free Software Foundation, Inc.
3 Contributed by ARM Ltd.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* The purpose of this pass is to combine multiple memory stores of
22 constant values to consecutive memory locations into fewer wider stores.
23 For example, if we have a sequence peforming four byte stores to
24 consecutive memory locations:
25 [p ] := imm1;
26 [p + 1B] := imm2;
27 [p + 2B] := imm3;
28 [p + 3B] := imm4;
29 we can transform this into a single 4-byte store if the target supports it:
30 [p] := imm1:imm2:imm3:imm4 //concatenated immediates according to endianness.
32 The algorithm is applied to each basic block in three phases:
34 1) Scan through the basic block recording constant assignments to
35 destinations that can be expressed as a store to memory of a certain size
36 at a certain bit offset. Record store chains to different bases in a
37 hash_map (m_stores) and make sure to terminate such chains when appropriate
38 (for example when when the stored values get used subsequently).
39 These stores can be a result of structure element initializers, array stores
40 etc. A store_immediate_info object is recorded for every such store.
41 Record as many such assignments to a single base as possible until a
42 statement that interferes with the store sequence is encountered.
44 2) Analyze the chain of stores recorded in phase 1) (i.e. the vector of
45 store_immediate_info objects) and coalesce contiguous stores into
46 merged_store_group objects.
48 For example, given the stores:
49 [p ] := 0;
50 [p + 1B] := 1;
51 [p + 3B] := 0;
52 [p + 4B] := 1;
53 [p + 5B] := 0;
54 [p + 6B] := 0;
55 This phase would produce two merged_store_group objects, one recording the
56 two bytes stored in the memory region [p : p + 1] and another
57 recording the four bytes stored in the memory region [p + 3 : p + 6].
59 3) The merged_store_group objects produced in phase 2) are processed
60 to generate the sequence of wider stores that set the contiguous memory
61 regions to the sequence of bytes that correspond to it. This may emit
62 multiple stores per store group to handle contiguous stores that are not
63 of a size that is a power of 2. For example it can try to emit a 40-bit
64 store as a 32-bit store followed by an 8-bit store.
65 We try to emit as wide stores as we can while respecting STRICT_ALIGNMENT or
66 SLOW_UNALIGNED_ACCESS rules.
68 Note on endianness and example:
69 Consider 2 contiguous 16-bit stores followed by 2 contiguous 8-bit stores:
70 [p ] := 0x1234;
71 [p + 2B] := 0x5678;
72 [p + 4B] := 0xab;
73 [p + 5B] := 0xcd;
75 The memory layout for little-endian (LE) and big-endian (BE) must be:
76 p |LE|BE|
77 ---------
78 0 |34|12|
79 1 |12|34|
80 2 |78|56|
81 3 |56|78|
82 4 |ab|ab|
83 5 |cd|cd|
85 To merge these into a single 48-bit merged value 'val' in phase 2)
86 on little-endian we insert stores to higher (consecutive) bitpositions
87 into the most significant bits of the merged value.
88 The final merged value would be: 0xcdab56781234
90 For big-endian we insert stores to higher bitpositions into the least
91 significant bits of the merged value.
92 The final merged value would be: 0x12345678abcd
94 Then, in phase 3), we want to emit this 48-bit value as a 32-bit store
95 followed by a 16-bit store. Again, we must consider endianness when
96 breaking down the 48-bit value 'val' computed above.
97 For little endian we emit:
98 [p] (32-bit) := 0x56781234; // val & 0x0000ffffffff;
99 [p + 4B] (16-bit) := 0xcdab; // (val & 0xffff00000000) >> 32;
101 Whereas for big-endian we emit:
102 [p] (32-bit) := 0x12345678; // (val & 0xffffffff0000) >> 16;
103 [p + 4B] (16-bit) := 0xabcd; // val & 0x00000000ffff; */
105 #include "config.h"
106 #include "system.h"
107 #include "coretypes.h"
108 #include "backend.h"
109 #include "tree.h"
110 #include "gimple.h"
111 #include "builtins.h"
112 #include "fold-const.h"
113 #include "tree-pass.h"
114 #include "ssa.h"
115 #include "gimple-pretty-print.h"
116 #include "alias.h"
117 #include "fold-const.h"
118 #include "params.h"
119 #include "print-tree.h"
120 #include "tree-hash-traits.h"
121 #include "gimple-iterator.h"
122 #include "gimplify.h"
123 #include "stor-layout.h"
124 #include "timevar.h"
125 #include "tree-cfg.h"
126 #include "tree-eh.h"
127 #include "target.h"
128 #include "gimplify-me.h"
130 /* The maximum size (in bits) of the stores this pass should generate. */
131 #define MAX_STORE_BITSIZE (BITS_PER_WORD)
132 #define MAX_STORE_BYTES (MAX_STORE_BITSIZE / BITS_PER_UNIT)
134 namespace {
136 /* Struct recording the information about a single store of an immediate
137 to memory. These are created in the first phase and coalesced into
138 merged_store_group objects in the second phase. */
140 struct store_immediate_info
142 unsigned HOST_WIDE_INT bitsize;
143 unsigned HOST_WIDE_INT bitpos;
144 gimple *stmt;
145 unsigned int order;
146 store_immediate_info (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
147 gimple *, unsigned int);
150 store_immediate_info::store_immediate_info (unsigned HOST_WIDE_INT bs,
151 unsigned HOST_WIDE_INT bp,
152 gimple *st,
153 unsigned int ord)
154 : bitsize (bs), bitpos (bp), stmt (st), order (ord)
158 /* Struct representing a group of stores to contiguous memory locations.
159 These are produced by the second phase (coalescing) and consumed in the
160 third phase that outputs the widened stores. */
162 struct merged_store_group
164 unsigned HOST_WIDE_INT start;
165 unsigned HOST_WIDE_INT width;
166 /* The size of the allocated memory for val. */
167 unsigned HOST_WIDE_INT buf_size;
169 unsigned int align;
170 unsigned int first_order;
171 unsigned int last_order;
173 auto_vec<struct store_immediate_info *> stores;
174 /* We record the first and last original statements in the sequence because
175 we'll need their vuse/vdef and replacement position. It's easier to keep
176 track of them separately as 'stores' is reordered by apply_stores. */
177 gimple *last_stmt;
178 gimple *first_stmt;
179 unsigned char *val;
181 merged_store_group (store_immediate_info *);
182 ~merged_store_group ();
183 void merge_into (store_immediate_info *);
184 void merge_overlapping (store_immediate_info *);
185 bool apply_stores ();
188 /* Debug helper. Dump LEN elements of byte array PTR to FD in hex. */
190 static void
191 dump_char_array (FILE *fd, unsigned char *ptr, unsigned int len)
193 if (!fd)
194 return;
196 for (unsigned int i = 0; i < len; i++)
197 fprintf (fd, "%x ", ptr[i]);
198 fprintf (fd, "\n");
201 /* Fill a byte array PTR of SZ elements with zeroes. This is to be used by
202 encode_tree_to_bitpos to zero-initialize most likely small arrays but
203 with a non-compile-time-constant size. */
205 static inline void
206 zero_char_buf (unsigned char *ptr, unsigned int sz)
208 for (unsigned int i = 0; i < sz; i++)
209 ptr[i] = 0;
212 /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
213 bits between adjacent elements. AMNT should be within
214 [0, BITS_PER_UNIT).
215 Example, AMNT = 2:
216 00011111|11100000 << 2 = 01111111|10000000
217 PTR[1] | PTR[0] PTR[1] | PTR[0]. */
219 static void
220 shift_bytes_in_array (unsigned char *ptr, unsigned int sz, unsigned int amnt)
222 if (amnt == 0)
223 return;
225 unsigned char carry_over = 0U;
226 unsigned char carry_mask = (~0U) << ((unsigned char)(BITS_PER_UNIT - amnt));
227 unsigned char clear_mask = (~0U) << amnt;
229 for (unsigned int i = 0; i < sz; i++)
231 unsigned prev_carry_over = carry_over;
232 carry_over
233 = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
235 ptr[i] <<= amnt;
236 if (i != 0)
238 ptr[i] &= clear_mask;
239 ptr[i] |= prev_carry_over;
244 /* Like shift_bytes_in_array but for big-endian.
245 Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
246 bits between adjacent elements. AMNT should be within
247 [0, BITS_PER_UNIT).
248 Example, AMNT = 2:
249 00011111|11100000 >> 2 = 00000111|11111000
250 PTR[0] | PTR[1] PTR[0] | PTR[1]. */
252 static void
253 shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
254 unsigned int amnt)
256 if (amnt == 0)
257 return;
259 unsigned char carry_over = 0U;
260 unsigned char carry_mask = ~(~0U << amnt);
262 for (unsigned int i = 0; i < sz; i++)
264 unsigned prev_carry_over = carry_over;
265 carry_over
266 = (ptr[i] & carry_mask);
268 carry_over <<= ((unsigned char)BITS_PER_UNIT - amnt);
269 ptr[i] >>= amnt;
270 ptr[i] |= prev_carry_over;
274 /* Clear out LEN bits starting from bit START in the byte array
275 PTR. This clears the bits to the *right* from START.
276 START must be within [0, BITS_PER_UNIT) and counts starting from
277 the least significant bit. */
279 static void
280 clear_bit_region_be (unsigned char *ptr, unsigned int start,
281 unsigned int len)
283 if (len == 0)
284 return;
285 /* Clear len bits to the right of start. */
286 else if (len <= start + 1)
288 unsigned char mask = (~(~0U << len));
289 mask = mask << (start + 1U - len);
290 ptr[0] &= ~mask;
292 else if (start != BITS_PER_UNIT - 1)
294 clear_bit_region_be (ptr, start, (start % BITS_PER_UNIT) + 1);
295 clear_bit_region_be (ptr + 1, BITS_PER_UNIT - 1,
296 len - (start % BITS_PER_UNIT) - 1);
298 else if (start == BITS_PER_UNIT - 1
299 && len > BITS_PER_UNIT)
301 unsigned int nbytes = len / BITS_PER_UNIT;
302 for (unsigned int i = 0; i < nbytes; i++)
303 ptr[i] = 0U;
304 if (len % BITS_PER_UNIT != 0)
305 clear_bit_region_be (ptr + nbytes, BITS_PER_UNIT - 1,
306 len % BITS_PER_UNIT);
308 else
309 gcc_unreachable ();
312 /* In the byte array PTR clear the bit region starting at bit
313 START and is LEN bits wide.
314 For regions spanning multiple bytes do this recursively until we reach
315 zero LEN or a region contained within a single byte. */
317 static void
318 clear_bit_region (unsigned char *ptr, unsigned int start,
319 unsigned int len)
321 /* Degenerate base case. */
322 if (len == 0)
323 return;
324 else if (start >= BITS_PER_UNIT)
325 clear_bit_region (ptr + 1, start - BITS_PER_UNIT, len);
326 /* Second base case. */
327 else if ((start + len) <= BITS_PER_UNIT)
329 unsigned char mask = (~0U) << ((unsigned char)(BITS_PER_UNIT - len));
330 mask >>= BITS_PER_UNIT - (start + len);
332 ptr[0] &= ~mask;
334 return;
336 /* Clear most significant bits in a byte and proceed with the next byte. */
337 else if (start != 0)
339 clear_bit_region (ptr, start, BITS_PER_UNIT - start);
340 clear_bit_region (ptr + 1, 0, len - (BITS_PER_UNIT - start) + 1);
342 /* Whole bytes need to be cleared. */
343 else if (start == 0 && len > BITS_PER_UNIT)
345 unsigned int nbytes = len / BITS_PER_UNIT;
346 /* We could recurse on each byte but do the loop here to avoid
347 recursing too deep. */
348 for (unsigned int i = 0; i < nbytes; i++)
349 ptr[i] = 0U;
350 /* Clear the remaining sub-byte region if there is one. */
351 if (len % BITS_PER_UNIT != 0)
352 clear_bit_region (ptr + nbytes, 0, len % BITS_PER_UNIT);
354 else
355 gcc_unreachable ();
358 /* Write BITLEN bits of EXPR to the byte array PTR at
359 bit position BITPOS. PTR should contain TOTAL_BYTES elements.
360 Return true if the operation succeeded. */
362 static bool
363 encode_tree_to_bitpos (tree expr, unsigned char *ptr, int bitlen, int bitpos,
364 unsigned int total_bytes)
366 unsigned int first_byte = bitpos / BITS_PER_UNIT;
367 tree tmp_int = expr;
368 bool sub_byte_op_p = (bitlen % BITS_PER_UNIT) || (bitpos % BITS_PER_UNIT)
369 || mode_for_size (bitlen, MODE_INT, 0) == BLKmode;
371 if (!sub_byte_op_p)
372 return native_encode_expr (tmp_int, ptr + first_byte, total_bytes, 0)
373 != 0;
375 /* LITTLE-ENDIAN
376 We are writing a non byte-sized quantity or at a position that is not
377 at a byte boundary.
378 |--------|--------|--------| ptr + first_byte
380 xxx xxxxxxxx xxx< bp>
381 |______EXPR____|
383 First native_encode_expr EPXR into a temporary buffer and shift each
384 byte in the buffer by 'bp' (carrying the bits over as necessary).
385 |00000000|00xxxxxx|xxxxxxxx| << bp = |000xxxxx|xxxxxxxx|xxx00000|
386 <------bitlen---->< bp>
387 Then we clear the destination bits:
388 |---00000|00000000|000-----| ptr + first_byte
389 <-------bitlen--->< bp>
391 Finally we ORR the bytes of the shifted EXPR into the cleared region:
392 |---xxxxx||xxxxxxxx||xxx-----| ptr + first_byte.
394 BIG-ENDIAN
395 We are writing a non byte-sized quantity or at a position that is not
396 at a byte boundary.
397 ptr + first_byte |--------|--------|--------|
399 <bp >xxx xxxxxxxx xxx
400 |_____EXPR_____|
402 First native_encode_expr EPXR into a temporary buffer and shift each
403 byte in the buffer to the right by (carrying the bits over as necessary).
404 We shift by as much as needed to align the most significant bit of EXPR
405 with bitpos:
406 |00xxxxxx|xxxxxxxx| >> 3 = |00000xxx|xxxxxxxx|xxxxx000|
407 <---bitlen----> <bp ><-----bitlen----->
408 Then we clear the destination bits:
409 ptr + first_byte |-----000||00000000||00000---|
410 <bp ><-------bitlen----->
412 Finally we ORR the bytes of the shifted EXPR into the cleared region:
413 ptr + first_byte |---xxxxx||xxxxxxxx||xxx-----|.
414 The awkwardness comes from the fact that bitpos is counted from the
415 most significant bit of a byte. */
417 /* Allocate an extra byte so that we have space to shift into. */
418 unsigned int byte_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr))) + 1;
419 unsigned char *tmpbuf = XALLOCAVEC (unsigned char, byte_size);
420 zero_char_buf (tmpbuf, byte_size);
421 /* The store detection code should only have allowed constants that are
422 accepted by native_encode_expr. */
423 if (native_encode_expr (expr, tmpbuf, byte_size, 0) == 0)
424 gcc_unreachable ();
426 /* The native_encode_expr machinery uses TYPE_MODE to determine how many
427 bytes to write. This means it can write more than
428 ROUND_UP (bitlen, BITS_PER_UNIT) / BITS_PER_UNIT bytes (for example
429 write 8 bytes for a bitlen of 40). Skip the bytes that are not within
430 bitlen and zero out the bits that are not relevant as well (that may
431 contain a sign bit due to sign-extension). */
432 unsigned int padding
433 = byte_size - ROUND_UP (bitlen, BITS_PER_UNIT) / BITS_PER_UNIT - 1;
434 if (padding != 0
435 || bitlen % BITS_PER_UNIT != 0)
437 /* On big-endian the padding is at the 'front' so just skip the initial
438 bytes. */
439 if (BYTES_BIG_ENDIAN)
440 tmpbuf += padding;
442 byte_size -= padding;
443 if (bitlen % BITS_PER_UNIT != 0)
445 if (BYTES_BIG_ENDIAN)
446 clear_bit_region_be (tmpbuf, BITS_PER_UNIT - 1,
447 BITS_PER_UNIT - (bitlen % BITS_PER_UNIT));
448 else
449 clear_bit_region (tmpbuf, bitlen,
450 byte_size * BITS_PER_UNIT - bitlen);
454 /* Clear the bit region in PTR where the bits from TMPBUF will be
455 inerted into. */
456 if (BYTES_BIG_ENDIAN)
457 clear_bit_region_be (ptr + first_byte,
458 BITS_PER_UNIT - 1 - (bitpos % BITS_PER_UNIT), bitlen);
459 else
460 clear_bit_region (ptr + first_byte, bitpos % BITS_PER_UNIT, bitlen);
462 int shift_amnt;
463 int bitlen_mod = bitlen % BITS_PER_UNIT;
464 int bitpos_mod = bitpos % BITS_PER_UNIT;
466 bool skip_byte = false;
467 if (BYTES_BIG_ENDIAN)
469 /* BITPOS and BITLEN are exactly aligned and no shifting
470 is necessary. */
471 if (bitpos_mod + bitlen_mod == BITS_PER_UNIT
472 || (bitpos_mod == 0 && bitlen_mod == 0))
473 shift_amnt = 0;
474 /* |. . . . . . . .|
475 <bp > <blen >.
476 We always shift right for BYTES_BIG_ENDIAN so shift the beginning
477 of the value until it aligns with 'bp' in the next byte over. */
478 else if (bitpos_mod + bitlen_mod < BITS_PER_UNIT)
480 shift_amnt = bitlen_mod + bitpos_mod;
481 skip_byte = bitlen_mod != 0;
483 /* |. . . . . . . .|
484 <----bp--->
485 <---blen---->.
486 Shift the value right within the same byte so it aligns with 'bp'. */
487 else
488 shift_amnt = bitlen_mod + bitpos_mod - BITS_PER_UNIT;
490 else
491 shift_amnt = bitpos % BITS_PER_UNIT;
493 /* Create the shifted version of EXPR. */
494 if (!BYTES_BIG_ENDIAN)
495 shift_bytes_in_array (tmpbuf, byte_size, shift_amnt);
496 else
498 gcc_assert (BYTES_BIG_ENDIAN);
499 shift_bytes_in_array_right (tmpbuf, byte_size, shift_amnt);
500 /* If shifting right forced us to move into the next byte skip the now
501 empty byte. */
502 if (skip_byte)
504 tmpbuf++;
505 byte_size--;
509 /* Insert the bits from TMPBUF. */
510 for (unsigned int i = 0; i < byte_size; i++)
511 ptr[first_byte + i] |= tmpbuf[i];
513 return true;
516 /* Sorting function for store_immediate_info objects.
517 Sorts them by bitposition. */
519 static int
520 sort_by_bitpos (const void *x, const void *y)
522 store_immediate_info *const *tmp = (store_immediate_info * const *) x;
523 store_immediate_info *const *tmp2 = (store_immediate_info * const *) y;
525 if ((*tmp)->bitpos <= (*tmp2)->bitpos)
526 return -1;
527 else if ((*tmp)->bitpos > (*tmp2)->bitpos)
528 return 1;
530 gcc_unreachable ();
533 /* Sorting function for store_immediate_info objects.
534 Sorts them by the order field. */
536 static int
537 sort_by_order (const void *x, const void *y)
539 store_immediate_info *const *tmp = (store_immediate_info * const *) x;
540 store_immediate_info *const *tmp2 = (store_immediate_info * const *) y;
542 if ((*tmp)->order < (*tmp2)->order)
543 return -1;
544 else if ((*tmp)->order > (*tmp2)->order)
545 return 1;
547 gcc_unreachable ();
550 /* Initialize a merged_store_group object from a store_immediate_info
551 object. */
553 merged_store_group::merged_store_group (store_immediate_info *info)
555 start = info->bitpos;
556 width = info->bitsize;
557 /* VAL has memory allocated for it in apply_stores once the group
558 width has been finalized. */
559 val = NULL;
560 align = get_object_alignment (gimple_assign_lhs (info->stmt));
561 stores.create (1);
562 stores.safe_push (info);
563 last_stmt = info->stmt;
564 last_order = info->order;
565 first_stmt = last_stmt;
566 first_order = last_order;
567 buf_size = 0;
570 merged_store_group::~merged_store_group ()
572 if (val)
573 XDELETEVEC (val);
576 /* Merge a store recorded by INFO into this merged store.
577 The store is not overlapping with the existing recorded
578 stores. */
580 void
581 merged_store_group::merge_into (store_immediate_info *info)
583 unsigned HOST_WIDE_INT wid = info->bitsize;
584 /* Make sure we're inserting in the position we think we're inserting. */
585 gcc_assert (info->bitpos == start + width);
587 width += wid;
588 gimple *stmt = info->stmt;
589 stores.safe_push (info);
590 if (info->order > last_order)
592 last_order = info->order;
593 last_stmt = stmt;
595 else if (info->order < first_order)
597 first_order = info->order;
598 first_stmt = stmt;
602 /* Merge a store described by INFO into this merged store.
603 INFO overlaps in some way with the current store (i.e. it's not contiguous
604 which is handled by merged_store_group::merge_into). */
606 void
607 merged_store_group::merge_overlapping (store_immediate_info *info)
609 gimple *stmt = info->stmt;
610 stores.safe_push (info);
612 /* If the store extends the size of the group, extend the width. */
613 if ((info->bitpos + info->bitsize) > (start + width))
614 width += info->bitpos + info->bitsize - (start + width);
616 if (info->order > last_order)
618 last_order = info->order;
619 last_stmt = stmt;
621 else if (info->order < first_order)
623 first_order = info->order;
624 first_stmt = stmt;
628 /* Go through all the recorded stores in this group in program order and
629 apply their values to the VAL byte array to create the final merged
630 value. Return true if the operation succeeded. */
632 bool
633 merged_store_group::apply_stores ()
635 /* The total width of the stores must add up to a whole number of bytes
636 and start at a byte boundary. We don't support emitting bitfield
637 references for now. Also, make sure we have more than one store
638 in the group, otherwise we cannot merge anything. */
639 if (width % BITS_PER_UNIT != 0
640 || start % BITS_PER_UNIT != 0
641 || stores.length () == 1)
642 return false;
644 stores.qsort (sort_by_order);
645 struct store_immediate_info *info;
646 unsigned int i;
647 /* Create a buffer of a size that is 2 times the number of bytes we're
648 storing. That way native_encode_expr can write power-of-2-sized
649 chunks without overrunning. */
650 buf_size
651 = 2 * (ROUND_UP (width, BITS_PER_UNIT) / BITS_PER_UNIT);
652 val = XCNEWVEC (unsigned char, buf_size);
654 FOR_EACH_VEC_ELT (stores, i, info)
656 unsigned int pos_in_buffer = info->bitpos - start;
657 bool ret = encode_tree_to_bitpos (gimple_assign_rhs1 (info->stmt),
658 val, info->bitsize,
659 pos_in_buffer, buf_size);
660 if (dump_file && (dump_flags & TDF_DETAILS))
662 if (ret)
664 fprintf (dump_file, "After writing ");
665 print_generic_expr (dump_file,
666 gimple_assign_rhs1 (info->stmt), 0);
667 fprintf (dump_file, " of size " HOST_WIDE_INT_PRINT_DEC
668 " at position %d the merged region contains:\n",
669 info->bitsize, pos_in_buffer);
670 dump_char_array (dump_file, val, buf_size);
672 else
673 fprintf (dump_file, "Failed to merge stores\n");
675 if (!ret)
676 return false;
678 return true;
681 /* Structure describing the store chain. */
683 struct imm_store_chain_info
685 tree base_addr;
686 auto_vec<struct store_immediate_info *> m_store_info;
687 auto_vec<merged_store_group *> m_merged_store_groups;
689 imm_store_chain_info (tree b_a) : base_addr (b_a) {}
690 bool terminate_and_process_chain ();
691 bool coalesce_immediate_stores ();
692 bool output_merged_store (merged_store_group *);
693 bool output_merged_stores ();
696 const pass_data pass_data_tree_store_merging = {
697 GIMPLE_PASS, /* type */
698 "store-merging", /* name */
699 OPTGROUP_NONE, /* optinfo_flags */
700 TV_GIMPLE_STORE_MERGING, /* tv_id */
701 PROP_ssa, /* properties_required */
702 0, /* properties_provided */
703 0, /* properties_destroyed */
704 0, /* todo_flags_start */
705 TODO_update_ssa, /* todo_flags_finish */
708 class pass_store_merging : public gimple_opt_pass
710 public:
711 pass_store_merging (gcc::context *ctxt)
712 : gimple_opt_pass (pass_data_tree_store_merging, ctxt)
716 /* Pass not supported for PDP-endianness. */
717 virtual bool
718 gate (function *)
720 return flag_store_merging && (WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN);
723 virtual unsigned int execute (function *);
725 private:
726 hash_map<tree_operand_hash, struct imm_store_chain_info *> m_stores;
728 bool terminate_and_process_all_chains ();
729 bool terminate_all_aliasing_chains (imm_store_chain_info **,
730 bool, gimple *);
731 bool terminate_and_release_chain (imm_store_chain_info *);
732 }; // class pass_store_merging
734 /* Terminate and process all recorded chains. Return true if any changes
735 were made. */
737 bool
738 pass_store_merging::terminate_and_process_all_chains ()
740 hash_map<tree_operand_hash, struct imm_store_chain_info *>::iterator iter
741 = m_stores.begin ();
742 bool ret = false;
743 for (; iter != m_stores.end (); ++iter)
744 ret |= terminate_and_release_chain ((*iter).second);
746 return ret;
749 /* Terminate all chains that are affected by the assignment to DEST, appearing
750 in statement STMT and ultimately points to the object BASE. Return true if
751 at least one aliasing chain was terminated. BASE and DEST are allowed to
752 be NULL_TREE. In that case the aliasing checks are performed on the whole
753 statement rather than a particular operand in it. VAR_OFFSET_P signifies
754 whether STMT represents a store to BASE offset by a variable amount.
755 If that is the case we have to terminate any chain anchored at BASE. */
757 bool
758 pass_store_merging::terminate_all_aliasing_chains (imm_store_chain_info
759 **chain_info,
760 bool var_offset_p,
761 gimple *stmt)
763 bool ret = false;
765 /* If the statement doesn't touch memory it can't alias. */
766 if (!gimple_vuse (stmt))
767 return false;
769 /* Check if the assignment destination (BASE) is part of a store chain.
770 This is to catch non-constant stores to destinations that may be part
771 of a chain. */
772 if (chain_info)
774 /* We have a chain at BASE and we're writing to [BASE + <variable>].
775 This can interfere with any of the stores so terminate
776 the chain. */
777 if (var_offset_p)
779 terminate_and_release_chain (*chain_info);
780 ret = true;
782 /* Otherwise go through every store in the chain to see if it
783 aliases with any of them. */
784 else
786 struct store_immediate_info *info;
787 unsigned int i;
788 FOR_EACH_VEC_ELT ((*chain_info)->m_store_info, i, info)
790 if (ref_maybe_used_by_stmt_p (stmt,
791 gimple_assign_lhs (info->stmt))
792 || stmt_may_clobber_ref_p (stmt,
793 gimple_assign_lhs (info->stmt)))
795 if (dump_file && (dump_flags & TDF_DETAILS))
797 fprintf (dump_file,
798 "stmt causes chain termination:\n");
799 print_gimple_stmt (dump_file, stmt, 0, 0);
801 terminate_and_release_chain (*chain_info);
802 ret = true;
803 break;
809 hash_map<tree_operand_hash, struct imm_store_chain_info *>::iterator iter
810 = m_stores.begin ();
812 /* Check for aliasing with all other store chains. */
813 for (; iter != m_stores.end (); ++iter)
815 /* We already checked all the stores in chain_info and terminated the
816 chain if necessary. Skip it here. */
817 if (chain_info && (*chain_info) == (*iter).second)
818 continue;
820 /* We can't use the base object here as that does not reliably exist.
821 Build a ao_ref from the base object address (if we know the
822 minimum and maximum offset and the maximum size we could improve
823 things here). */
824 ao_ref chain_ref;
825 ao_ref_init_from_ptr_and_size (&chain_ref, (*iter).first, NULL_TREE);
826 if (ref_maybe_used_by_stmt_p (stmt, &chain_ref)
827 || stmt_may_clobber_ref_p_1 (stmt, &chain_ref))
829 terminate_and_release_chain ((*iter).second);
830 ret = true;
834 return ret;
837 /* Helper function. Terminate the recorded chain storing to base object
838 BASE. Return true if the merging and output was successful. The m_stores
839 entry is removed after the processing in any case. */
841 bool
842 pass_store_merging::terminate_and_release_chain (imm_store_chain_info *chain_info)
844 bool ret = chain_info->terminate_and_process_chain ();
845 m_stores.remove (chain_info->base_addr);
846 delete chain_info;
847 return ret;
850 /* Go through the candidate stores recorded in m_store_info and merge them
851 into merged_store_group objects recorded into m_merged_store_groups
852 representing the widened stores. Return true if coalescing was successful
853 and the number of widened stores is fewer than the original number
854 of stores. */
856 bool
857 imm_store_chain_info::coalesce_immediate_stores ()
859 /* Anything less can't be processed. */
860 if (m_store_info.length () < 2)
861 return false;
863 if (dump_file && (dump_flags & TDF_DETAILS))
864 fprintf (dump_file, "Attempting to coalesce %u stores in chain.\n",
865 m_store_info.length ());
867 store_immediate_info *info;
868 unsigned int i;
870 /* Order the stores by the bitposition they write to. */
871 m_store_info.qsort (sort_by_bitpos);
873 info = m_store_info[0];
874 merged_store_group *merged_store = new merged_store_group (info);
876 FOR_EACH_VEC_ELT (m_store_info, i, info)
878 if (dump_file && (dump_flags & TDF_DETAILS))
880 fprintf (dump_file, "Store %u:\nbitsize:" HOST_WIDE_INT_PRINT_DEC
881 " bitpos:" HOST_WIDE_INT_PRINT_DEC " val:\n",
882 i, info->bitsize, info->bitpos);
883 print_generic_expr (dump_file, gimple_assign_rhs1 (info->stmt), 0);
884 fprintf (dump_file, "\n------------\n");
887 if (i == 0)
888 continue;
890 /* |---store 1---|
891 |---store 2---|
892 Overlapping stores. */
893 unsigned HOST_WIDE_INT start = info->bitpos;
894 if (IN_RANGE (start, merged_store->start,
895 merged_store->start + merged_store->width - 1))
897 merged_store->merge_overlapping (info);
898 continue;
901 /* |---store 1---| <gap> |---store 2---|.
902 Gap between stores. Start a new group. */
903 if (start != merged_store->start + merged_store->width)
905 /* Try to apply all the stores recorded for the group to determine
906 the bitpattern they write and discard it if that fails.
907 This will also reject single-store groups. */
908 if (!merged_store->apply_stores ())
909 delete merged_store;
910 else
911 m_merged_store_groups.safe_push (merged_store);
913 merged_store = new merged_store_group (info);
915 continue;
918 /* |---store 1---||---store 2---|
919 This store is consecutive to the previous one.
920 Merge it into the current store group. */
921 merged_store->merge_into (info);
924 /* Record or discard the last store group. */
925 if (!merged_store->apply_stores ())
926 delete merged_store;
927 else
928 m_merged_store_groups.safe_push (merged_store);
930 gcc_assert (m_merged_store_groups.length () <= m_store_info.length ());
931 bool success
932 = !m_merged_store_groups.is_empty ()
933 && m_merged_store_groups.length () < m_store_info.length ();
935 if (success && dump_file)
936 fprintf (dump_file, "Coalescing successful!\n"
937 "Merged into %u stores\n",
938 m_merged_store_groups.length ());
940 return success;
943 /* Return the type to use for the merged stores described by STMTS.
944 This is needed to get the alias sets right. */
946 static tree
947 get_alias_type_for_stmts (auto_vec<gimple *> &stmts)
949 gimple *stmt;
950 unsigned int i;
951 tree lhs = gimple_assign_lhs (stmts[0]);
952 tree type = reference_alias_ptr_type (lhs);
954 FOR_EACH_VEC_ELT (stmts, i, stmt)
956 if (i == 0)
957 continue;
959 lhs = gimple_assign_lhs (stmt);
960 tree type1 = reference_alias_ptr_type (lhs);
961 if (!alias_ptr_types_compatible_p (type, type1))
962 return ptr_type_node;
964 return type;
967 /* Return the location_t information we can find among the statements
968 in STMTS. */
970 static location_t
971 get_location_for_stmts (auto_vec<gimple *> &stmts)
973 gimple *stmt;
974 unsigned int i;
976 FOR_EACH_VEC_ELT (stmts, i, stmt)
977 if (gimple_has_location (stmt))
978 return gimple_location (stmt);
980 return UNKNOWN_LOCATION;
983 /* Used to decribe a store resulting from splitting a wide store in smaller
984 regularly-sized stores in split_group. */
986 struct split_store
988 unsigned HOST_WIDE_INT bytepos;
989 unsigned HOST_WIDE_INT size;
990 unsigned HOST_WIDE_INT align;
991 auto_vec<gimple *> orig_stmts;
992 split_store (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
993 unsigned HOST_WIDE_INT);
996 /* Simple constructor. */
998 split_store::split_store (unsigned HOST_WIDE_INT bp,
999 unsigned HOST_WIDE_INT sz,
1000 unsigned HOST_WIDE_INT al)
1001 : bytepos (bp), size (sz), align (al)
1003 orig_stmts.create (0);
1006 /* Record all statements corresponding to stores in GROUP that write to
1007 the region starting at BITPOS and is of size BITSIZE. Record such
1008 statements in STMTS. The stores in GROUP must be sorted by
1009 bitposition. */
1011 static void
1012 find_constituent_stmts (struct merged_store_group *group,
1013 auto_vec<gimple *> &stmts,
1014 unsigned HOST_WIDE_INT bitpos,
1015 unsigned HOST_WIDE_INT bitsize)
1017 struct store_immediate_info *info;
1018 unsigned int i;
1019 unsigned HOST_WIDE_INT end = bitpos + bitsize;
1020 FOR_EACH_VEC_ELT (group->stores, i, info)
1022 unsigned HOST_WIDE_INT stmt_start = info->bitpos;
1023 unsigned HOST_WIDE_INT stmt_end = stmt_start + info->bitsize;
1024 if (stmt_end < bitpos)
1025 continue;
1026 /* The stores in GROUP are ordered by bitposition so if we're past
1027 the region for this group return early. */
1028 if (stmt_start > end)
1029 return;
1031 if (IN_RANGE (stmt_start, bitpos, bitpos + bitsize)
1032 || IN_RANGE (stmt_end, bitpos, end)
1033 /* The statement writes a region that completely encloses the region
1034 that this group writes. Unlikely to occur but let's
1035 handle it. */
1036 || IN_RANGE (bitpos, stmt_start, stmt_end))
1037 stmts.safe_push (info->stmt);
1041 /* Split a merged store described by GROUP by populating the SPLIT_STORES
1042 vector with split_store structs describing the byte offset (from the base),
1043 the bit size and alignment of each store as well as the original statements
1044 involved in each such split group.
1045 This is to separate the splitting strategy from the statement
1046 building/emission/linking done in output_merged_store.
1047 At the moment just start with the widest possible size and keep emitting
1048 the widest we can until we have emitted all the bytes, halving the size
1049 when appropriate. */
1051 static bool
1052 split_group (merged_store_group *group,
1053 auto_vec<struct split_store *> &split_stores)
1055 unsigned HOST_WIDE_INT pos = group->start;
1056 unsigned HOST_WIDE_INT size = group->width;
1057 unsigned HOST_WIDE_INT bytepos = pos / BITS_PER_UNIT;
1058 unsigned HOST_WIDE_INT align = group->align;
1060 /* We don't handle partial bitfields for now. We shouldn't have
1061 reached this far. */
1062 gcc_assert ((size % BITS_PER_UNIT == 0) && (pos % BITS_PER_UNIT == 0));
1064 bool allow_unaligned
1065 = !STRICT_ALIGNMENT && PARAM_VALUE (PARAM_STORE_MERGING_ALLOW_UNALIGNED);
1067 unsigned int try_size = MAX_STORE_BITSIZE;
1068 while (try_size > size
1069 || (!allow_unaligned
1070 && try_size > align))
1072 try_size /= 2;
1073 if (try_size < BITS_PER_UNIT)
1074 return false;
1077 unsigned HOST_WIDE_INT try_pos = bytepos;
1078 group->stores.qsort (sort_by_bitpos);
1080 while (size > 0)
1082 struct split_store *store = new split_store (try_pos, try_size, align);
1083 unsigned HOST_WIDE_INT try_bitpos = try_pos * BITS_PER_UNIT;
1084 find_constituent_stmts (group, store->orig_stmts, try_bitpos, try_size);
1085 split_stores.safe_push (store);
1087 try_pos += try_size / BITS_PER_UNIT;
1089 size -= try_size;
1090 align = try_size;
1091 while (size < try_size)
1092 try_size /= 2;
1094 return true;
1097 /* Given a merged store group GROUP output the widened version of it.
1098 The store chain is against the base object BASE.
1099 Try store sizes of at most MAX_STORE_BITSIZE bits wide and don't output
1100 unaligned stores for STRICT_ALIGNMENT targets or if it's too expensive.
1101 Make sure that the number of statements output is less than the number of
1102 original statements. If a better sequence is possible emit it and
1103 return true. */
1105 bool
1106 imm_store_chain_info::output_merged_store (merged_store_group *group)
1108 unsigned HOST_WIDE_INT start_byte_pos = group->start / BITS_PER_UNIT;
1110 unsigned int orig_num_stmts = group->stores.length ();
1111 if (orig_num_stmts < 2)
1112 return false;
1114 auto_vec<struct split_store *> split_stores;
1115 split_stores.create (0);
1116 if (!split_group (group, split_stores))
1117 return false;
1119 gimple_stmt_iterator last_gsi = gsi_for_stmt (group->last_stmt);
1120 gimple_seq seq = NULL;
1121 unsigned int num_stmts = 0;
1122 tree last_vdef, new_vuse;
1123 last_vdef = gimple_vdef (group->last_stmt);
1124 new_vuse = gimple_vuse (group->last_stmt);
1126 gimple *stmt = NULL;
1127 /* The new SSA names created. Keep track of them so that we can free them
1128 if we decide to not use the new sequence. */
1129 auto_vec<tree> new_ssa_names;
1130 split_store *split_store;
1131 unsigned int i;
1132 bool fail = false;
1134 tree addr = force_gimple_operand_1 (unshare_expr (base_addr), &seq,
1135 is_gimple_mem_ref_addr, NULL_TREE);
1136 FOR_EACH_VEC_ELT (split_stores, i, split_store)
1138 unsigned HOST_WIDE_INT try_size = split_store->size;
1139 unsigned HOST_WIDE_INT try_pos = split_store->bytepos;
1140 unsigned HOST_WIDE_INT align = split_store->align;
1141 tree offset_type = get_alias_type_for_stmts (split_store->orig_stmts);
1142 location_t loc = get_location_for_stmts (split_store->orig_stmts);
1144 tree int_type = build_nonstandard_integer_type (try_size, UNSIGNED);
1145 int_type = build_aligned_type (int_type, align);
1146 tree dest = fold_build2 (MEM_REF, int_type, addr,
1147 build_int_cst (offset_type, try_pos));
1149 tree src = native_interpret_expr (int_type,
1150 group->val + try_pos - start_byte_pos,
1151 group->buf_size);
1153 stmt = gimple_build_assign (dest, src);
1154 gimple_set_location (stmt, loc);
1155 gimple_set_vuse (stmt, new_vuse);
1156 gimple_seq_add_stmt_without_update (&seq, stmt);
1158 /* We didn't manage to reduce the number of statements. Bail out. */
1159 if (++num_stmts == orig_num_stmts)
1161 if (dump_file && (dump_flags & TDF_DETAILS))
1163 fprintf (dump_file, "Exceeded original number of stmts (%u)."
1164 " Not profitable to emit new sequence.\n",
1165 orig_num_stmts);
1167 unsigned int ssa_count;
1168 tree ssa_name;
1169 /* Don't forget to cleanup the temporary SSA names. */
1170 FOR_EACH_VEC_ELT (new_ssa_names, ssa_count, ssa_name)
1171 release_ssa_name (ssa_name);
1173 fail = true;
1174 break;
1177 tree new_vdef;
1178 if (i < split_stores.length () - 1)
1180 new_vdef = make_ssa_name (gimple_vop (cfun), stmt);
1181 new_ssa_names.safe_push (new_vdef);
1183 else
1184 new_vdef = last_vdef;
1186 gimple_set_vdef (stmt, new_vdef);
1187 SSA_NAME_DEF_STMT (new_vdef) = stmt;
1188 new_vuse = new_vdef;
1191 FOR_EACH_VEC_ELT (split_stores, i, split_store)
1192 delete split_store;
1194 if (fail)
1195 return false;
1197 gcc_assert (seq);
1198 if (dump_file)
1200 fprintf (dump_file,
1201 "New sequence of %u stmts to replace old one of %u stmts\n",
1202 num_stmts, orig_num_stmts);
1203 if (dump_flags & TDF_DETAILS)
1204 print_gimple_seq (dump_file, seq, 0, TDF_VOPS | TDF_MEMSYMS);
1206 gsi_insert_seq_after (&last_gsi, seq, GSI_SAME_STMT);
1208 return true;
1211 /* Process the merged_store_group objects created in the coalescing phase.
1212 The stores are all against the base object BASE.
1213 Try to output the widened stores and delete the original statements if
1214 successful. Return true iff any changes were made. */
1216 bool
1217 imm_store_chain_info::output_merged_stores ()
1219 unsigned int i;
1220 merged_store_group *merged_store;
1221 bool ret = false;
1222 FOR_EACH_VEC_ELT (m_merged_store_groups, i, merged_store)
1224 if (output_merged_store (merged_store))
1226 unsigned int j;
1227 store_immediate_info *store;
1228 FOR_EACH_VEC_ELT (merged_store->stores, j, store)
1230 gimple *stmt = store->stmt;
1231 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1232 gsi_remove (&gsi, true);
1233 if (stmt != merged_store->last_stmt)
1235 unlink_stmt_vdef (stmt);
1236 release_defs (stmt);
1239 ret = true;
1242 if (ret && dump_file)
1243 fprintf (dump_file, "Merging successful!\n");
1245 return ret;
1248 /* Coalesce the store_immediate_info objects recorded against the base object
1249 BASE in the first phase and output them.
1250 Delete the allocated structures.
1251 Return true if any changes were made. */
1253 bool
1254 imm_store_chain_info::terminate_and_process_chain ()
1256 /* Process store chain. */
1257 bool ret = false;
1258 if (m_store_info.length () > 1)
1260 ret = coalesce_immediate_stores ();
1261 if (ret)
1262 ret = output_merged_stores ();
1265 /* Delete all the entries we allocated ourselves. */
1266 store_immediate_info *info;
1267 unsigned int i;
1268 FOR_EACH_VEC_ELT (m_store_info, i, info)
1269 delete info;
1271 merged_store_group *merged_info;
1272 FOR_EACH_VEC_ELT (m_merged_store_groups, i, merged_info)
1273 delete merged_info;
1275 return ret;
1278 /* Return true iff LHS is a destination potentially interesting for
1279 store merging. In practice these are the codes that get_inner_reference
1280 can process. */
1282 static bool
1283 lhs_valid_for_store_merging_p (tree lhs)
1285 tree_code code = TREE_CODE (lhs);
1287 if (code == ARRAY_REF || code == ARRAY_RANGE_REF || code == MEM_REF
1288 || code == COMPONENT_REF || code == BIT_FIELD_REF)
1289 return true;
1291 return false;
1294 /* Return true if the tree RHS is a constant we want to consider
1295 during store merging. In practice accept all codes that
1296 native_encode_expr accepts. */
1298 static bool
1299 rhs_valid_for_store_merging_p (tree rhs)
1301 tree type = TREE_TYPE (rhs);
1302 if (TREE_CODE_CLASS (TREE_CODE (rhs)) != tcc_constant
1303 || !can_native_encode_type_p (type))
1304 return false;
1306 return true;
1309 /* Entry point for the pass. Go over each basic block recording chains of
1310 immediate stores. Upon encountering a terminating statement (as defined
1311 by stmt_terminates_chain_p) process the recorded stores and emit the widened
1312 variants. */
1314 unsigned int
1315 pass_store_merging::execute (function *fun)
1317 basic_block bb;
1318 hash_set<gimple *> orig_stmts;
1320 FOR_EACH_BB_FN (bb, fun)
1322 gimple_stmt_iterator gsi;
1323 unsigned HOST_WIDE_INT num_statements = 0;
1324 /* Record the original statements so that we can keep track of
1325 statements emitted in this pass and not re-process new
1326 statements. */
1327 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1329 if (is_gimple_debug (gsi_stmt (gsi)))
1330 continue;
1332 if (++num_statements > 2)
1333 break;
1336 if (num_statements < 2)
1337 continue;
1339 if (dump_file && (dump_flags & TDF_DETAILS))
1340 fprintf (dump_file, "Processing basic block <%d>:\n", bb->index);
1342 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1344 gimple *stmt = gsi_stmt (gsi);
1346 if (gimple_has_volatile_ops (stmt))
1348 /* Terminate all chains. */
1349 if (dump_file && (dump_flags & TDF_DETAILS))
1350 fprintf (dump_file, "Volatile access terminates "
1351 "all chains\n");
1352 terminate_and_process_all_chains ();
1353 continue;
1356 if (is_gimple_debug (stmt))
1357 continue;
1359 if (gimple_assign_single_p (stmt) && gimple_vdef (stmt)
1360 && !stmt_can_throw_internal (stmt)
1361 && lhs_valid_for_store_merging_p (gimple_assign_lhs (stmt)))
1363 tree lhs = gimple_assign_lhs (stmt);
1364 tree rhs = gimple_assign_rhs1 (stmt);
1366 HOST_WIDE_INT bitsize, bitpos;
1367 machine_mode mode;
1368 int unsignedp = 0, reversep = 0, volatilep = 0;
1369 tree offset, base_addr;
1370 base_addr
1371 = get_inner_reference (lhs, &bitsize, &bitpos, &offset, &mode,
1372 &unsignedp, &reversep, &volatilep);
1373 /* As a future enhancement we could handle stores with the same
1374 base and offset. */
1375 bool invalid = reversep
1376 || ((bitsize > MAX_BITSIZE_MODE_ANY_INT)
1377 && (TREE_CODE (rhs) != INTEGER_CST))
1378 || !rhs_valid_for_store_merging_p (rhs);
1380 /* We do not want to rewrite TARGET_MEM_REFs. */
1381 if (TREE_CODE (base_addr) == TARGET_MEM_REF)
1382 invalid = true;
1383 /* In some cases get_inner_reference may return a
1384 MEM_REF [ptr + byteoffset]. For the purposes of this pass
1385 canonicalize the base_addr to MEM_REF [ptr] and take
1386 byteoffset into account in the bitpos. This occurs in
1387 PR 23684 and this way we can catch more chains. */
1388 else if (TREE_CODE (base_addr) == MEM_REF)
1390 offset_int bit_off, byte_off = mem_ref_offset (base_addr);
1391 bit_off = byte_off << LOG2_BITS_PER_UNIT;
1392 bit_off += bitpos;
1393 if (!wi::neg_p (bit_off) && wi::fits_shwi_p (bit_off))
1394 bitpos = bit_off.to_shwi ();
1395 else
1396 invalid = true;
1397 base_addr = TREE_OPERAND (base_addr, 0);
1399 /* get_inner_reference returns the base object, get at its
1400 address now. */
1401 else
1403 if (bitpos < 0)
1404 invalid = true;
1405 base_addr = build_fold_addr_expr (base_addr);
1408 if (! invalid
1409 && offset != NULL_TREE)
1411 /* If the access is variable offset then a base
1412 decl has to be address-taken to be able to
1413 emit pointer-based stores to it.
1414 ??? We might be able to get away with
1415 re-using the original base up to the first
1416 variable part and then wrapping that inside
1417 a BIT_FIELD_REF. */
1418 tree base = get_base_address (base_addr);
1419 if (! base
1420 || (DECL_P (base)
1421 && ! TREE_ADDRESSABLE (base)))
1422 invalid = true;
1423 else
1424 base_addr = build2 (POINTER_PLUS_EXPR,
1425 TREE_TYPE (base_addr),
1426 base_addr, offset);
1429 struct imm_store_chain_info **chain_info
1430 = m_stores.get (base_addr);
1432 if (!invalid)
1434 store_immediate_info *info;
1435 if (chain_info)
1437 info = new store_immediate_info (
1438 bitsize, bitpos, stmt,
1439 (*chain_info)->m_store_info.length ());
1440 if (dump_file && (dump_flags & TDF_DETAILS))
1442 fprintf (dump_file,
1443 "Recording immediate store from stmt:\n");
1444 print_gimple_stmt (dump_file, stmt, 0, 0);
1446 (*chain_info)->m_store_info.safe_push (info);
1447 /* If we reach the limit of stores to merge in a chain
1448 terminate and process the chain now. */
1449 if ((*chain_info)->m_store_info.length ()
1450 == (unsigned int)
1451 PARAM_VALUE (PARAM_MAX_STORES_TO_MERGE))
1453 if (dump_file && (dump_flags & TDF_DETAILS))
1454 fprintf (dump_file,
1455 "Reached maximum number of statements"
1456 " to merge:\n");
1457 terminate_and_release_chain (*chain_info);
1459 continue;
1462 /* Store aliases any existing chain? */
1463 terminate_all_aliasing_chains (chain_info, false, stmt);
1464 /* Start a new chain. */
1465 struct imm_store_chain_info *new_chain
1466 = new imm_store_chain_info (base_addr);
1467 info = new store_immediate_info (bitsize, bitpos,
1468 stmt, 0);
1469 new_chain->m_store_info.safe_push (info);
1470 m_stores.put (base_addr, new_chain);
1471 if (dump_file && (dump_flags & TDF_DETAILS))
1473 fprintf (dump_file,
1474 "Starting new chain with statement:\n");
1475 print_gimple_stmt (dump_file, stmt, 0, 0);
1476 fprintf (dump_file, "The base object is:\n");
1477 print_generic_expr (dump_file, base_addr, 0);
1478 fprintf (dump_file, "\n");
1481 else
1482 terminate_all_aliasing_chains (chain_info,
1483 offset != NULL_TREE, stmt);
1485 continue;
1488 terminate_all_aliasing_chains (NULL, false, stmt);
1490 terminate_and_process_all_chains ();
1492 return 0;
1495 } // anon namespace
1497 /* Construct and return a store merging pass object. */
1499 gimple_opt_pass *
1500 make_pass_store_merging (gcc::context *ctxt)
1502 return new pass_store_merging (ctxt);