i386: move alignment defaults to processor_costs.
[official-gcc.git] / gcc / gimple-ssa-store-merging.c
blobc8031e185704b4efa9a8046f8a6d2223c0563ddb
1 /* GIMPLE store merging and byte swapping passes.
2 Copyright (C) 2009-2018 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 the store merging pass is to combine multiple memory stores
22 of constant values, values loaded from memory, bitwise operations on those,
23 or bit-field values, to consecutive locations, into fewer wider stores.
25 For example, if we have a sequence peforming four byte stores to
26 consecutive memory locations:
27 [p ] := imm1;
28 [p + 1B] := imm2;
29 [p + 2B] := imm3;
30 [p + 3B] := imm4;
31 we can transform this into a single 4-byte store if the target supports it:
32 [p] := imm1:imm2:imm3:imm4 concatenated according to endianness.
34 Or:
35 [p ] := [q ];
36 [p + 1B] := [q + 1B];
37 [p + 2B] := [q + 2B];
38 [p + 3B] := [q + 3B];
39 if there is no overlap can be transformed into a single 4-byte
40 load followed by single 4-byte store.
42 Or:
43 [p ] := [q ] ^ imm1;
44 [p + 1B] := [q + 1B] ^ imm2;
45 [p + 2B] := [q + 2B] ^ imm3;
46 [p + 3B] := [q + 3B] ^ imm4;
47 if there is no overlap can be transformed into a single 4-byte
48 load, xored with imm1:imm2:imm3:imm4 and stored using a single 4-byte store.
50 Or:
51 [p:1 ] := imm;
52 [p:31] := val & 0x7FFFFFFF;
53 we can transform this into a single 4-byte store if the target supports it:
54 [p] := imm:(val & 0x7FFFFFFF) concatenated according to endianness.
56 The algorithm is applied to each basic block in three phases:
58 1) Scan through the basic block and record assignments to destinations
59 that can be expressed as a store to memory of a certain size at a certain
60 bit offset from base expressions we can handle. For bit-fields we also
61 record the surrounding bit region, i.e. bits that could be stored in
62 a read-modify-write operation when storing the bit-field. Record store
63 chains to different bases in a hash_map (m_stores) and make sure to
64 terminate such chains when appropriate (for example when when the stored
65 values get used subsequently).
66 These stores can be a result of structure element initializers, array stores
67 etc. A store_immediate_info object is recorded for every such store.
68 Record as many such assignments to a single base as possible until a
69 statement that interferes with the store sequence is encountered.
70 Each store has up to 2 operands, which can be a either constant, a memory
71 load or an SSA name, from which the value to be stored can be computed.
72 At most one of the operands can be a constant. The operands are recorded
73 in store_operand_info struct.
75 2) Analyze the chains of stores recorded in phase 1) (i.e. the vector of
76 store_immediate_info objects) and coalesce contiguous stores into
77 merged_store_group objects. For bit-field stores, we don't need to
78 require the stores to be contiguous, just their surrounding bit regions
79 have to be contiguous. If the expression being stored is different
80 between adjacent stores, such as one store storing a constant and
81 following storing a value loaded from memory, or if the loaded memory
82 objects are not adjacent, a new merged_store_group is created as well.
84 For example, given the stores:
85 [p ] := 0;
86 [p + 1B] := 1;
87 [p + 3B] := 0;
88 [p + 4B] := 1;
89 [p + 5B] := 0;
90 [p + 6B] := 0;
91 This phase would produce two merged_store_group objects, one recording the
92 two bytes stored in the memory region [p : p + 1] and another
93 recording the four bytes stored in the memory region [p + 3 : p + 6].
95 3) The merged_store_group objects produced in phase 2) are processed
96 to generate the sequence of wider stores that set the contiguous memory
97 regions to the sequence of bytes that correspond to it. This may emit
98 multiple stores per store group to handle contiguous stores that are not
99 of a size that is a power of 2. For example it can try to emit a 40-bit
100 store as a 32-bit store followed by an 8-bit store.
101 We try to emit as wide stores as we can while respecting STRICT_ALIGNMENT
102 or TARGET_SLOW_UNALIGNED_ACCESS settings.
104 Note on endianness and example:
105 Consider 2 contiguous 16-bit stores followed by 2 contiguous 8-bit stores:
106 [p ] := 0x1234;
107 [p + 2B] := 0x5678;
108 [p + 4B] := 0xab;
109 [p + 5B] := 0xcd;
111 The memory layout for little-endian (LE) and big-endian (BE) must be:
112 p |LE|BE|
113 ---------
114 0 |34|12|
115 1 |12|34|
116 2 |78|56|
117 3 |56|78|
118 4 |ab|ab|
119 5 |cd|cd|
121 To merge these into a single 48-bit merged value 'val' in phase 2)
122 on little-endian we insert stores to higher (consecutive) bitpositions
123 into the most significant bits of the merged value.
124 The final merged value would be: 0xcdab56781234
126 For big-endian we insert stores to higher bitpositions into the least
127 significant bits of the merged value.
128 The final merged value would be: 0x12345678abcd
130 Then, in phase 3), we want to emit this 48-bit value as a 32-bit store
131 followed by a 16-bit store. Again, we must consider endianness when
132 breaking down the 48-bit value 'val' computed above.
133 For little endian we emit:
134 [p] (32-bit) := 0x56781234; // val & 0x0000ffffffff;
135 [p + 4B] (16-bit) := 0xcdab; // (val & 0xffff00000000) >> 32;
137 Whereas for big-endian we emit:
138 [p] (32-bit) := 0x12345678; // (val & 0xffffffff0000) >> 16;
139 [p + 4B] (16-bit) := 0xabcd; // val & 0x00000000ffff; */
141 #include "config.h"
142 #include "system.h"
143 #include "coretypes.h"
144 #include "backend.h"
145 #include "tree.h"
146 #include "gimple.h"
147 #include "builtins.h"
148 #include "fold-const.h"
149 #include "tree-pass.h"
150 #include "ssa.h"
151 #include "gimple-pretty-print.h"
152 #include "alias.h"
153 #include "fold-const.h"
154 #include "params.h"
155 #include "print-tree.h"
156 #include "tree-hash-traits.h"
157 #include "gimple-iterator.h"
158 #include "gimplify.h"
159 #include "gimple-fold.h"
160 #include "stor-layout.h"
161 #include "timevar.h"
162 #include "tree-cfg.h"
163 #include "tree-eh.h"
164 #include "target.h"
165 #include "gimplify-me.h"
166 #include "rtl.h"
167 #include "expr.h" /* For get_bit_range. */
168 #include "optabs-tree.h"
169 #include "selftest.h"
171 /* The maximum size (in bits) of the stores this pass should generate. */
172 #define MAX_STORE_BITSIZE (BITS_PER_WORD)
173 #define MAX_STORE_BYTES (MAX_STORE_BITSIZE / BITS_PER_UNIT)
175 /* Limit to bound the number of aliasing checks for loads with the same
176 vuse as the corresponding store. */
177 #define MAX_STORE_ALIAS_CHECKS 64
179 namespace {
181 struct bswap_stat
183 /* Number of hand-written 16-bit nop / bswaps found. */
184 int found_16bit;
186 /* Number of hand-written 32-bit nop / bswaps found. */
187 int found_32bit;
189 /* Number of hand-written 64-bit nop / bswaps found. */
190 int found_64bit;
191 } nop_stats, bswap_stats;
193 /* A symbolic number structure is used to detect byte permutation and selection
194 patterns of a source. To achieve that, its field N contains an artificial
195 number consisting of BITS_PER_MARKER sized markers tracking where does each
196 byte come from in the source:
198 0 - target byte has the value 0
199 FF - target byte has an unknown value (eg. due to sign extension)
200 1..size - marker value is the byte index in the source (0 for lsb).
202 To detect permutations on memory sources (arrays and structures), a symbolic
203 number is also associated:
204 - a base address BASE_ADDR and an OFFSET giving the address of the source;
205 - a range which gives the difference between the highest and lowest accessed
206 memory location to make such a symbolic number;
207 - the address SRC of the source element of lowest address as a convenience
208 to easily get BASE_ADDR + offset + lowest bytepos;
209 - number of expressions N_OPS bitwise ored together to represent
210 approximate cost of the computation.
212 Note 1: the range is different from size as size reflects the size of the
213 type of the current expression. For instance, for an array char a[],
214 (short) a[0] | (short) a[3] would have a size of 2 but a range of 4 while
215 (short) a[0] | ((short) a[0] << 1) would still have a size of 2 but this
216 time a range of 1.
218 Note 2: for non-memory sources, range holds the same value as size.
220 Note 3: SRC points to the SSA_NAME in case of non-memory source. */
222 struct symbolic_number {
223 uint64_t n;
224 tree type;
225 tree base_addr;
226 tree offset;
227 poly_int64_pod bytepos;
228 tree src;
229 tree alias_set;
230 tree vuse;
231 unsigned HOST_WIDE_INT range;
232 int n_ops;
235 #define BITS_PER_MARKER 8
236 #define MARKER_MASK ((1 << BITS_PER_MARKER) - 1)
237 #define MARKER_BYTE_UNKNOWN MARKER_MASK
238 #define HEAD_MARKER(n, size) \
239 ((n) & ((uint64_t) MARKER_MASK << (((size) - 1) * BITS_PER_MARKER)))
241 /* The number which the find_bswap_or_nop_1 result should match in
242 order to have a nop. The number is masked according to the size of
243 the symbolic number before using it. */
244 #define CMPNOP (sizeof (int64_t) < 8 ? 0 : \
245 (uint64_t)0x08070605 << 32 | 0x04030201)
247 /* The number which the find_bswap_or_nop_1 result should match in
248 order to have a byte swap. The number is masked according to the
249 size of the symbolic number before using it. */
250 #define CMPXCHG (sizeof (int64_t) < 8 ? 0 : \
251 (uint64_t)0x01020304 << 32 | 0x05060708)
253 /* Perform a SHIFT or ROTATE operation by COUNT bits on symbolic
254 number N. Return false if the requested operation is not permitted
255 on a symbolic number. */
257 inline bool
258 do_shift_rotate (enum tree_code code,
259 struct symbolic_number *n,
260 int count)
262 int i, size = TYPE_PRECISION (n->type) / BITS_PER_UNIT;
263 unsigned head_marker;
265 if (count % BITS_PER_UNIT != 0)
266 return false;
267 count = (count / BITS_PER_UNIT) * BITS_PER_MARKER;
269 /* Zero out the extra bits of N in order to avoid them being shifted
270 into the significant bits. */
271 if (size < 64 / BITS_PER_MARKER)
272 n->n &= ((uint64_t) 1 << (size * BITS_PER_MARKER)) - 1;
274 switch (code)
276 case LSHIFT_EXPR:
277 n->n <<= count;
278 break;
279 case RSHIFT_EXPR:
280 head_marker = HEAD_MARKER (n->n, size);
281 n->n >>= count;
282 /* Arithmetic shift of signed type: result is dependent on the value. */
283 if (!TYPE_UNSIGNED (n->type) && head_marker)
284 for (i = 0; i < count / BITS_PER_MARKER; i++)
285 n->n |= (uint64_t) MARKER_BYTE_UNKNOWN
286 << ((size - 1 - i) * BITS_PER_MARKER);
287 break;
288 case LROTATE_EXPR:
289 n->n = (n->n << count) | (n->n >> ((size * BITS_PER_MARKER) - count));
290 break;
291 case RROTATE_EXPR:
292 n->n = (n->n >> count) | (n->n << ((size * BITS_PER_MARKER) - count));
293 break;
294 default:
295 return false;
297 /* Zero unused bits for size. */
298 if (size < 64 / BITS_PER_MARKER)
299 n->n &= ((uint64_t) 1 << (size * BITS_PER_MARKER)) - 1;
300 return true;
303 /* Perform sanity checking for the symbolic number N and the gimple
304 statement STMT. */
306 inline bool
307 verify_symbolic_number_p (struct symbolic_number *n, gimple *stmt)
309 tree lhs_type;
311 lhs_type = gimple_expr_type (stmt);
313 if (TREE_CODE (lhs_type) != INTEGER_TYPE)
314 return false;
316 if (TYPE_PRECISION (lhs_type) != TYPE_PRECISION (n->type))
317 return false;
319 return true;
322 /* Initialize the symbolic number N for the bswap pass from the base element
323 SRC manipulated by the bitwise OR expression. */
325 bool
326 init_symbolic_number (struct symbolic_number *n, tree src)
328 int size;
330 if (! INTEGRAL_TYPE_P (TREE_TYPE (src)))
331 return false;
333 n->base_addr = n->offset = n->alias_set = n->vuse = NULL_TREE;
334 n->src = src;
336 /* Set up the symbolic number N by setting each byte to a value between 1 and
337 the byte size of rhs1. The highest order byte is set to n->size and the
338 lowest order byte to 1. */
339 n->type = TREE_TYPE (src);
340 size = TYPE_PRECISION (n->type);
341 if (size % BITS_PER_UNIT != 0)
342 return false;
343 size /= BITS_PER_UNIT;
344 if (size > 64 / BITS_PER_MARKER)
345 return false;
346 n->range = size;
347 n->n = CMPNOP;
348 n->n_ops = 1;
350 if (size < 64 / BITS_PER_MARKER)
351 n->n &= ((uint64_t) 1 << (size * BITS_PER_MARKER)) - 1;
353 return true;
356 /* Check if STMT might be a byte swap or a nop from a memory source and returns
357 the answer. If so, REF is that memory source and the base of the memory area
358 accessed and the offset of the access from that base are recorded in N. */
360 bool
361 find_bswap_or_nop_load (gimple *stmt, tree ref, struct symbolic_number *n)
363 /* Leaf node is an array or component ref. Memorize its base and
364 offset from base to compare to other such leaf node. */
365 poly_int64 bitsize, bitpos, bytepos;
366 machine_mode mode;
367 int unsignedp, reversep, volatilep;
368 tree offset, base_addr;
370 /* Not prepared to handle PDP endian. */
371 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
372 return false;
374 if (!gimple_assign_load_p (stmt) || gimple_has_volatile_ops (stmt))
375 return false;
377 base_addr = get_inner_reference (ref, &bitsize, &bitpos, &offset, &mode,
378 &unsignedp, &reversep, &volatilep);
380 if (TREE_CODE (base_addr) == TARGET_MEM_REF)
381 /* Do not rewrite TARGET_MEM_REF. */
382 return false;
383 else if (TREE_CODE (base_addr) == MEM_REF)
385 poly_offset_int bit_offset = 0;
386 tree off = TREE_OPERAND (base_addr, 1);
388 if (!integer_zerop (off))
390 poly_offset_int boff = mem_ref_offset (base_addr);
391 boff <<= LOG2_BITS_PER_UNIT;
392 bit_offset += boff;
395 base_addr = TREE_OPERAND (base_addr, 0);
397 /* Avoid returning a negative bitpos as this may wreak havoc later. */
398 if (maybe_lt (bit_offset, 0))
400 tree byte_offset = wide_int_to_tree
401 (sizetype, bits_to_bytes_round_down (bit_offset));
402 bit_offset = num_trailing_bits (bit_offset);
403 if (offset)
404 offset = size_binop (PLUS_EXPR, offset, byte_offset);
405 else
406 offset = byte_offset;
409 bitpos += bit_offset.force_shwi ();
411 else
412 base_addr = build_fold_addr_expr (base_addr);
414 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
415 return false;
416 if (!multiple_p (bitsize, BITS_PER_UNIT))
417 return false;
418 if (reversep)
419 return false;
421 if (!init_symbolic_number (n, ref))
422 return false;
423 n->base_addr = base_addr;
424 n->offset = offset;
425 n->bytepos = bytepos;
426 n->alias_set = reference_alias_ptr_type (ref);
427 n->vuse = gimple_vuse (stmt);
428 return true;
431 /* Compute the symbolic number N representing the result of a bitwise OR on 2
432 symbolic number N1 and N2 whose source statements are respectively
433 SOURCE_STMT1 and SOURCE_STMT2. */
435 gimple *
436 perform_symbolic_merge (gimple *source_stmt1, struct symbolic_number *n1,
437 gimple *source_stmt2, struct symbolic_number *n2,
438 struct symbolic_number *n)
440 int i, size;
441 uint64_t mask;
442 gimple *source_stmt;
443 struct symbolic_number *n_start;
445 tree rhs1 = gimple_assign_rhs1 (source_stmt1);
446 if (TREE_CODE (rhs1) == BIT_FIELD_REF
447 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
448 rhs1 = TREE_OPERAND (rhs1, 0);
449 tree rhs2 = gimple_assign_rhs1 (source_stmt2);
450 if (TREE_CODE (rhs2) == BIT_FIELD_REF
451 && TREE_CODE (TREE_OPERAND (rhs2, 0)) == SSA_NAME)
452 rhs2 = TREE_OPERAND (rhs2, 0);
454 /* Sources are different, cancel bswap if they are not memory location with
455 the same base (array, structure, ...). */
456 if (rhs1 != rhs2)
458 uint64_t inc;
459 HOST_WIDE_INT start1, start2, start_sub, end_sub, end1, end2, end;
460 struct symbolic_number *toinc_n_ptr, *n_end;
461 basic_block bb1, bb2;
463 if (!n1->base_addr || !n2->base_addr
464 || !operand_equal_p (n1->base_addr, n2->base_addr, 0))
465 return NULL;
467 if (!n1->offset != !n2->offset
468 || (n1->offset && !operand_equal_p (n1->offset, n2->offset, 0)))
469 return NULL;
471 start1 = 0;
472 if (!(n2->bytepos - n1->bytepos).is_constant (&start2))
473 return NULL;
475 if (start1 < start2)
477 n_start = n1;
478 start_sub = start2 - start1;
480 else
482 n_start = n2;
483 start_sub = start1 - start2;
486 bb1 = gimple_bb (source_stmt1);
487 bb2 = gimple_bb (source_stmt2);
488 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
489 source_stmt = source_stmt1;
490 else
491 source_stmt = source_stmt2;
493 /* Find the highest address at which a load is performed and
494 compute related info. */
495 end1 = start1 + (n1->range - 1);
496 end2 = start2 + (n2->range - 1);
497 if (end1 < end2)
499 end = end2;
500 end_sub = end2 - end1;
502 else
504 end = end1;
505 end_sub = end1 - end2;
507 n_end = (end2 > end1) ? n2 : n1;
509 /* Find symbolic number whose lsb is the most significant. */
510 if (BYTES_BIG_ENDIAN)
511 toinc_n_ptr = (n_end == n1) ? n2 : n1;
512 else
513 toinc_n_ptr = (n_start == n1) ? n2 : n1;
515 n->range = end - MIN (start1, start2) + 1;
517 /* Check that the range of memory covered can be represented by
518 a symbolic number. */
519 if (n->range > 64 / BITS_PER_MARKER)
520 return NULL;
522 /* Reinterpret byte marks in symbolic number holding the value of
523 bigger weight according to target endianness. */
524 inc = BYTES_BIG_ENDIAN ? end_sub : start_sub;
525 size = TYPE_PRECISION (n1->type) / BITS_PER_UNIT;
526 for (i = 0; i < size; i++, inc <<= BITS_PER_MARKER)
528 unsigned marker
529 = (toinc_n_ptr->n >> (i * BITS_PER_MARKER)) & MARKER_MASK;
530 if (marker && marker != MARKER_BYTE_UNKNOWN)
531 toinc_n_ptr->n += inc;
534 else
536 n->range = n1->range;
537 n_start = n1;
538 source_stmt = source_stmt1;
541 if (!n1->alias_set
542 || alias_ptr_types_compatible_p (n1->alias_set, n2->alias_set))
543 n->alias_set = n1->alias_set;
544 else
545 n->alias_set = ptr_type_node;
546 n->vuse = n_start->vuse;
547 n->base_addr = n_start->base_addr;
548 n->offset = n_start->offset;
549 n->src = n_start->src;
550 n->bytepos = n_start->bytepos;
551 n->type = n_start->type;
552 size = TYPE_PRECISION (n->type) / BITS_PER_UNIT;
554 for (i = 0, mask = MARKER_MASK; i < size; i++, mask <<= BITS_PER_MARKER)
556 uint64_t masked1, masked2;
558 masked1 = n1->n & mask;
559 masked2 = n2->n & mask;
560 if (masked1 && masked2 && masked1 != masked2)
561 return NULL;
563 n->n = n1->n | n2->n;
564 n->n_ops = n1->n_ops + n2->n_ops;
566 return source_stmt;
569 /* find_bswap_or_nop_1 invokes itself recursively with N and tries to perform
570 the operation given by the rhs of STMT on the result. If the operation
571 could successfully be executed the function returns a gimple stmt whose
572 rhs's first tree is the expression of the source operand and NULL
573 otherwise. */
575 gimple *
576 find_bswap_or_nop_1 (gimple *stmt, struct symbolic_number *n, int limit)
578 enum tree_code code;
579 tree rhs1, rhs2 = NULL;
580 gimple *rhs1_stmt, *rhs2_stmt, *source_stmt1;
581 enum gimple_rhs_class rhs_class;
583 if (!limit || !is_gimple_assign (stmt))
584 return NULL;
586 rhs1 = gimple_assign_rhs1 (stmt);
588 if (find_bswap_or_nop_load (stmt, rhs1, n))
589 return stmt;
591 /* Handle BIT_FIELD_REF. */
592 if (TREE_CODE (rhs1) == BIT_FIELD_REF
593 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
595 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (TREE_OPERAND (rhs1, 1));
596 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (TREE_OPERAND (rhs1, 2));
597 if (bitpos % BITS_PER_UNIT == 0
598 && bitsize % BITS_PER_UNIT == 0
599 && init_symbolic_number (n, TREE_OPERAND (rhs1, 0)))
601 /* Handle big-endian bit numbering in BIT_FIELD_REF. */
602 if (BYTES_BIG_ENDIAN)
603 bitpos = TYPE_PRECISION (n->type) - bitpos - bitsize;
605 /* Shift. */
606 if (!do_shift_rotate (RSHIFT_EXPR, n, bitpos))
607 return NULL;
609 /* Mask. */
610 uint64_t mask = 0;
611 uint64_t tmp = (1 << BITS_PER_UNIT) - 1;
612 for (unsigned i = 0; i < bitsize / BITS_PER_UNIT;
613 i++, tmp <<= BITS_PER_UNIT)
614 mask |= (uint64_t) MARKER_MASK << (i * BITS_PER_MARKER);
615 n->n &= mask;
617 /* Convert. */
618 n->type = TREE_TYPE (rhs1);
619 if (!n->base_addr)
620 n->range = TYPE_PRECISION (n->type) / BITS_PER_UNIT;
622 return verify_symbolic_number_p (n, stmt) ? stmt : NULL;
625 return NULL;
628 if (TREE_CODE (rhs1) != SSA_NAME)
629 return NULL;
631 code = gimple_assign_rhs_code (stmt);
632 rhs_class = gimple_assign_rhs_class (stmt);
633 rhs1_stmt = SSA_NAME_DEF_STMT (rhs1);
635 if (rhs_class == GIMPLE_BINARY_RHS)
636 rhs2 = gimple_assign_rhs2 (stmt);
638 /* Handle unary rhs and binary rhs with integer constants as second
639 operand. */
641 if (rhs_class == GIMPLE_UNARY_RHS
642 || (rhs_class == GIMPLE_BINARY_RHS
643 && TREE_CODE (rhs2) == INTEGER_CST))
645 if (code != BIT_AND_EXPR
646 && code != LSHIFT_EXPR
647 && code != RSHIFT_EXPR
648 && code != LROTATE_EXPR
649 && code != RROTATE_EXPR
650 && !CONVERT_EXPR_CODE_P (code))
651 return NULL;
653 source_stmt1 = find_bswap_or_nop_1 (rhs1_stmt, n, limit - 1);
655 /* If find_bswap_or_nop_1 returned NULL, STMT is a leaf node and
656 we have to initialize the symbolic number. */
657 if (!source_stmt1)
659 if (gimple_assign_load_p (stmt)
660 || !init_symbolic_number (n, rhs1))
661 return NULL;
662 source_stmt1 = stmt;
665 switch (code)
667 case BIT_AND_EXPR:
669 int i, size = TYPE_PRECISION (n->type) / BITS_PER_UNIT;
670 uint64_t val = int_cst_value (rhs2), mask = 0;
671 uint64_t tmp = (1 << BITS_PER_UNIT) - 1;
673 /* Only constants masking full bytes are allowed. */
674 for (i = 0; i < size; i++, tmp <<= BITS_PER_UNIT)
675 if ((val & tmp) != 0 && (val & tmp) != tmp)
676 return NULL;
677 else if (val & tmp)
678 mask |= (uint64_t) MARKER_MASK << (i * BITS_PER_MARKER);
680 n->n &= mask;
682 break;
683 case LSHIFT_EXPR:
684 case RSHIFT_EXPR:
685 case LROTATE_EXPR:
686 case RROTATE_EXPR:
687 if (!do_shift_rotate (code, n, (int) TREE_INT_CST_LOW (rhs2)))
688 return NULL;
689 break;
690 CASE_CONVERT:
692 int i, type_size, old_type_size;
693 tree type;
695 type = gimple_expr_type (stmt);
696 type_size = TYPE_PRECISION (type);
697 if (type_size % BITS_PER_UNIT != 0)
698 return NULL;
699 type_size /= BITS_PER_UNIT;
700 if (type_size > 64 / BITS_PER_MARKER)
701 return NULL;
703 /* Sign extension: result is dependent on the value. */
704 old_type_size = TYPE_PRECISION (n->type) / BITS_PER_UNIT;
705 if (!TYPE_UNSIGNED (n->type) && type_size > old_type_size
706 && HEAD_MARKER (n->n, old_type_size))
707 for (i = 0; i < type_size - old_type_size; i++)
708 n->n |= (uint64_t) MARKER_BYTE_UNKNOWN
709 << ((type_size - 1 - i) * BITS_PER_MARKER);
711 if (type_size < 64 / BITS_PER_MARKER)
713 /* If STMT casts to a smaller type mask out the bits not
714 belonging to the target type. */
715 n->n &= ((uint64_t) 1 << (type_size * BITS_PER_MARKER)) - 1;
717 n->type = type;
718 if (!n->base_addr)
719 n->range = type_size;
721 break;
722 default:
723 return NULL;
725 return verify_symbolic_number_p (n, stmt) ? source_stmt1 : NULL;
728 /* Handle binary rhs. */
730 if (rhs_class == GIMPLE_BINARY_RHS)
732 struct symbolic_number n1, n2;
733 gimple *source_stmt, *source_stmt2;
735 if (code != BIT_IOR_EXPR)
736 return NULL;
738 if (TREE_CODE (rhs2) != SSA_NAME)
739 return NULL;
741 rhs2_stmt = SSA_NAME_DEF_STMT (rhs2);
743 switch (code)
745 case BIT_IOR_EXPR:
746 source_stmt1 = find_bswap_or_nop_1 (rhs1_stmt, &n1, limit - 1);
748 if (!source_stmt1)
749 return NULL;
751 source_stmt2 = find_bswap_or_nop_1 (rhs2_stmt, &n2, limit - 1);
753 if (!source_stmt2)
754 return NULL;
756 if (TYPE_PRECISION (n1.type) != TYPE_PRECISION (n2.type))
757 return NULL;
759 if (n1.vuse != n2.vuse)
760 return NULL;
762 source_stmt
763 = perform_symbolic_merge (source_stmt1, &n1, source_stmt2, &n2, n);
765 if (!source_stmt)
766 return NULL;
768 if (!verify_symbolic_number_p (n, stmt))
769 return NULL;
771 break;
772 default:
773 return NULL;
775 return source_stmt;
777 return NULL;
780 /* Helper for find_bswap_or_nop and try_coalesce_bswap to compute
781 *CMPXCHG, *CMPNOP and adjust *N. */
783 void
784 find_bswap_or_nop_finalize (struct symbolic_number *n, uint64_t *cmpxchg,
785 uint64_t *cmpnop)
787 unsigned rsize;
788 uint64_t tmpn, mask;
790 /* The number which the find_bswap_or_nop_1 result should match in order
791 to have a full byte swap. The number is shifted to the right
792 according to the size of the symbolic number before using it. */
793 *cmpxchg = CMPXCHG;
794 *cmpnop = CMPNOP;
796 /* Find real size of result (highest non-zero byte). */
797 if (n->base_addr)
798 for (tmpn = n->n, rsize = 0; tmpn; tmpn >>= BITS_PER_MARKER, rsize++);
799 else
800 rsize = n->range;
802 /* Zero out the bits corresponding to untouched bytes in original gimple
803 expression. */
804 if (n->range < (int) sizeof (int64_t))
806 mask = ((uint64_t) 1 << (n->range * BITS_PER_MARKER)) - 1;
807 *cmpxchg >>= (64 / BITS_PER_MARKER - n->range) * BITS_PER_MARKER;
808 *cmpnop &= mask;
811 /* Zero out the bits corresponding to unused bytes in the result of the
812 gimple expression. */
813 if (rsize < n->range)
815 if (BYTES_BIG_ENDIAN)
817 mask = ((uint64_t) 1 << (rsize * BITS_PER_MARKER)) - 1;
818 *cmpxchg &= mask;
819 *cmpnop >>= (n->range - rsize) * BITS_PER_MARKER;
821 else
823 mask = ((uint64_t) 1 << (rsize * BITS_PER_MARKER)) - 1;
824 *cmpxchg >>= (n->range - rsize) * BITS_PER_MARKER;
825 *cmpnop &= mask;
827 n->range = rsize;
830 n->range *= BITS_PER_UNIT;
833 /* Check if STMT completes a bswap implementation or a read in a given
834 endianness consisting of ORs, SHIFTs and ANDs and sets *BSWAP
835 accordingly. It also sets N to represent the kind of operations
836 performed: size of the resulting expression and whether it works on
837 a memory source, and if so alias-set and vuse. At last, the
838 function returns a stmt whose rhs's first tree is the source
839 expression. */
841 gimple *
842 find_bswap_or_nop (gimple *stmt, struct symbolic_number *n, bool *bswap)
844 /* The last parameter determines the depth search limit. It usually
845 correlates directly to the number n of bytes to be touched. We
846 increase that number by log2(n) + 1 here in order to also
847 cover signed -> unsigned conversions of the src operand as can be seen
848 in libgcc, and for initial shift/and operation of the src operand. */
849 int limit = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (gimple_expr_type (stmt)));
850 limit += 1 + (int) ceil_log2 ((unsigned HOST_WIDE_INT) limit);
851 gimple *ins_stmt = find_bswap_or_nop_1 (stmt, n, limit);
853 if (!ins_stmt)
854 return NULL;
856 uint64_t cmpxchg, cmpnop;
857 find_bswap_or_nop_finalize (n, &cmpxchg, &cmpnop);
859 /* A complete byte swap should make the symbolic number to start with
860 the largest digit in the highest order byte. Unchanged symbolic
861 number indicates a read with same endianness as target architecture. */
862 if (n->n == cmpnop)
863 *bswap = false;
864 else if (n->n == cmpxchg)
865 *bswap = true;
866 else
867 return NULL;
869 /* Useless bit manipulation performed by code. */
870 if (!n->base_addr && n->n == cmpnop && n->n_ops == 1)
871 return NULL;
873 return ins_stmt;
876 const pass_data pass_data_optimize_bswap =
878 GIMPLE_PASS, /* type */
879 "bswap", /* name */
880 OPTGROUP_NONE, /* optinfo_flags */
881 TV_NONE, /* tv_id */
882 PROP_ssa, /* properties_required */
883 0, /* properties_provided */
884 0, /* properties_destroyed */
885 0, /* todo_flags_start */
886 0, /* todo_flags_finish */
889 class pass_optimize_bswap : public gimple_opt_pass
891 public:
892 pass_optimize_bswap (gcc::context *ctxt)
893 : gimple_opt_pass (pass_data_optimize_bswap, ctxt)
896 /* opt_pass methods: */
897 virtual bool gate (function *)
899 return flag_expensive_optimizations && optimize && BITS_PER_UNIT == 8;
902 virtual unsigned int execute (function *);
904 }; // class pass_optimize_bswap
906 /* Perform the bswap optimization: replace the expression computed in the rhs
907 of gsi_stmt (GSI) (or if NULL add instead of replace) by an equivalent
908 bswap, load or load + bswap expression.
909 Which of these alternatives replace the rhs is given by N->base_addr (non
910 null if a load is needed) and BSWAP. The type, VUSE and set-alias of the
911 load to perform are also given in N while the builtin bswap invoke is given
912 in FNDEL. Finally, if a load is involved, INS_STMT refers to one of the
913 load statements involved to construct the rhs in gsi_stmt (GSI) and
914 N->range gives the size of the rhs expression for maintaining some
915 statistics.
917 Note that if the replacement involve a load and if gsi_stmt (GSI) is
918 non-NULL, that stmt is moved just after INS_STMT to do the load with the
919 same VUSE which can lead to gsi_stmt (GSI) changing of basic block. */
921 tree
922 bswap_replace (gimple_stmt_iterator gsi, gimple *ins_stmt, tree fndecl,
923 tree bswap_type, tree load_type, struct symbolic_number *n,
924 bool bswap)
926 tree src, tmp, tgt = NULL_TREE;
927 gimple *bswap_stmt;
929 gimple *cur_stmt = gsi_stmt (gsi);
930 src = n->src;
931 if (cur_stmt)
932 tgt = gimple_assign_lhs (cur_stmt);
934 /* Need to load the value from memory first. */
935 if (n->base_addr)
937 gimple_stmt_iterator gsi_ins = gsi;
938 if (ins_stmt)
939 gsi_ins = gsi_for_stmt (ins_stmt);
940 tree addr_expr, addr_tmp, val_expr, val_tmp;
941 tree load_offset_ptr, aligned_load_type;
942 gimple *load_stmt;
943 unsigned align = get_object_alignment (src);
944 poly_int64 load_offset = 0;
946 if (cur_stmt)
948 basic_block ins_bb = gimple_bb (ins_stmt);
949 basic_block cur_bb = gimple_bb (cur_stmt);
950 if (!dominated_by_p (CDI_DOMINATORS, cur_bb, ins_bb))
951 return NULL_TREE;
953 /* Move cur_stmt just before one of the load of the original
954 to ensure it has the same VUSE. See PR61517 for what could
955 go wrong. */
956 if (gimple_bb (cur_stmt) != gimple_bb (ins_stmt))
957 reset_flow_sensitive_info (gimple_assign_lhs (cur_stmt));
958 gsi_move_before (&gsi, &gsi_ins);
959 gsi = gsi_for_stmt (cur_stmt);
961 else
962 gsi = gsi_ins;
964 /* Compute address to load from and cast according to the size
965 of the load. */
966 addr_expr = build_fold_addr_expr (src);
967 if (is_gimple_mem_ref_addr (addr_expr))
968 addr_tmp = unshare_expr (addr_expr);
969 else
971 addr_tmp = unshare_expr (n->base_addr);
972 if (!is_gimple_mem_ref_addr (addr_tmp))
973 addr_tmp = force_gimple_operand_gsi_1 (&gsi, addr_tmp,
974 is_gimple_mem_ref_addr,
975 NULL_TREE, true,
976 GSI_SAME_STMT);
977 load_offset = n->bytepos;
978 if (n->offset)
980 tree off
981 = force_gimple_operand_gsi (&gsi, unshare_expr (n->offset),
982 true, NULL_TREE, true,
983 GSI_SAME_STMT);
984 gimple *stmt
985 = gimple_build_assign (make_ssa_name (TREE_TYPE (addr_tmp)),
986 POINTER_PLUS_EXPR, addr_tmp, off);
987 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
988 addr_tmp = gimple_assign_lhs (stmt);
992 /* Perform the load. */
993 aligned_load_type = load_type;
994 if (align < TYPE_ALIGN (load_type))
995 aligned_load_type = build_aligned_type (load_type, align);
996 load_offset_ptr = build_int_cst (n->alias_set, load_offset);
997 val_expr = fold_build2 (MEM_REF, aligned_load_type, addr_tmp,
998 load_offset_ptr);
1000 if (!bswap)
1002 if (n->range == 16)
1003 nop_stats.found_16bit++;
1004 else if (n->range == 32)
1005 nop_stats.found_32bit++;
1006 else
1008 gcc_assert (n->range == 64);
1009 nop_stats.found_64bit++;
1012 /* Convert the result of load if necessary. */
1013 if (tgt && !useless_type_conversion_p (TREE_TYPE (tgt), load_type))
1015 val_tmp = make_temp_ssa_name (aligned_load_type, NULL,
1016 "load_dst");
1017 load_stmt = gimple_build_assign (val_tmp, val_expr);
1018 gimple_set_vuse (load_stmt, n->vuse);
1019 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1020 gimple_assign_set_rhs_with_ops (&gsi, NOP_EXPR, val_tmp);
1021 update_stmt (cur_stmt);
1023 else if (cur_stmt)
1025 gimple_assign_set_rhs_with_ops (&gsi, MEM_REF, val_expr);
1026 gimple_set_vuse (cur_stmt, n->vuse);
1027 update_stmt (cur_stmt);
1029 else
1031 tgt = make_ssa_name (load_type);
1032 cur_stmt = gimple_build_assign (tgt, MEM_REF, val_expr);
1033 gimple_set_vuse (cur_stmt, n->vuse);
1034 gsi_insert_before (&gsi, cur_stmt, GSI_SAME_STMT);
1037 if (dump_file)
1039 fprintf (dump_file,
1040 "%d bit load in target endianness found at: ",
1041 (int) n->range);
1042 print_gimple_stmt (dump_file, cur_stmt, 0);
1044 return tgt;
1046 else
1048 val_tmp = make_temp_ssa_name (aligned_load_type, NULL, "load_dst");
1049 load_stmt = gimple_build_assign (val_tmp, val_expr);
1050 gimple_set_vuse (load_stmt, n->vuse);
1051 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1053 src = val_tmp;
1055 else if (!bswap)
1057 gimple *g = NULL;
1058 if (tgt && !useless_type_conversion_p (TREE_TYPE (tgt), TREE_TYPE (src)))
1060 if (!is_gimple_val (src))
1061 return NULL_TREE;
1062 g = gimple_build_assign (tgt, NOP_EXPR, src);
1064 else if (cur_stmt)
1065 g = gimple_build_assign (tgt, src);
1066 else
1067 tgt = src;
1068 if (n->range == 16)
1069 nop_stats.found_16bit++;
1070 else if (n->range == 32)
1071 nop_stats.found_32bit++;
1072 else
1074 gcc_assert (n->range == 64);
1075 nop_stats.found_64bit++;
1077 if (dump_file)
1079 fprintf (dump_file,
1080 "%d bit reshuffle in target endianness found at: ",
1081 (int) n->range);
1082 if (cur_stmt)
1083 print_gimple_stmt (dump_file, cur_stmt, 0);
1084 else
1086 print_generic_expr (dump_file, tgt, TDF_NONE);
1087 fprintf (dump_file, "\n");
1090 if (cur_stmt)
1091 gsi_replace (&gsi, g, true);
1092 return tgt;
1094 else if (TREE_CODE (src) == BIT_FIELD_REF)
1095 src = TREE_OPERAND (src, 0);
1097 if (n->range == 16)
1098 bswap_stats.found_16bit++;
1099 else if (n->range == 32)
1100 bswap_stats.found_32bit++;
1101 else
1103 gcc_assert (n->range == 64);
1104 bswap_stats.found_64bit++;
1107 tmp = src;
1109 /* Convert the src expression if necessary. */
1110 if (!useless_type_conversion_p (TREE_TYPE (tmp), bswap_type))
1112 gimple *convert_stmt;
1114 tmp = make_temp_ssa_name (bswap_type, NULL, "bswapsrc");
1115 convert_stmt = gimple_build_assign (tmp, NOP_EXPR, src);
1116 gsi_insert_before (&gsi, convert_stmt, GSI_SAME_STMT);
1119 /* Canonical form for 16 bit bswap is a rotate expression. Only 16bit values
1120 are considered as rotation of 2N bit values by N bits is generally not
1121 equivalent to a bswap. Consider for instance 0x01020304 r>> 16 which
1122 gives 0x03040102 while a bswap for that value is 0x04030201. */
1123 if (bswap && n->range == 16)
1125 tree count = build_int_cst (NULL, BITS_PER_UNIT);
1126 src = fold_build2 (LROTATE_EXPR, bswap_type, tmp, count);
1127 bswap_stmt = gimple_build_assign (NULL, src);
1129 else
1130 bswap_stmt = gimple_build_call (fndecl, 1, tmp);
1132 if (tgt == NULL_TREE)
1133 tgt = make_ssa_name (bswap_type);
1134 tmp = tgt;
1136 /* Convert the result if necessary. */
1137 if (!useless_type_conversion_p (TREE_TYPE (tgt), bswap_type))
1139 gimple *convert_stmt;
1141 tmp = make_temp_ssa_name (bswap_type, NULL, "bswapdst");
1142 convert_stmt = gimple_build_assign (tgt, NOP_EXPR, tmp);
1143 gsi_insert_after (&gsi, convert_stmt, GSI_SAME_STMT);
1146 gimple_set_lhs (bswap_stmt, tmp);
1148 if (dump_file)
1150 fprintf (dump_file, "%d bit bswap implementation found at: ",
1151 (int) n->range);
1152 if (cur_stmt)
1153 print_gimple_stmt (dump_file, cur_stmt, 0);
1154 else
1156 print_generic_expr (dump_file, tgt, TDF_NONE);
1157 fprintf (dump_file, "\n");
1161 if (cur_stmt)
1163 gsi_insert_after (&gsi, bswap_stmt, GSI_SAME_STMT);
1164 gsi_remove (&gsi, true);
1166 else
1167 gsi_insert_before (&gsi, bswap_stmt, GSI_SAME_STMT);
1168 return tgt;
1171 /* Find manual byte swap implementations as well as load in a given
1172 endianness. Byte swaps are turned into a bswap builtin invokation
1173 while endian loads are converted to bswap builtin invokation or
1174 simple load according to the target endianness. */
1176 unsigned int
1177 pass_optimize_bswap::execute (function *fun)
1179 basic_block bb;
1180 bool bswap32_p, bswap64_p;
1181 bool changed = false;
1182 tree bswap32_type = NULL_TREE, bswap64_type = NULL_TREE;
1184 bswap32_p = (builtin_decl_explicit_p (BUILT_IN_BSWAP32)
1185 && optab_handler (bswap_optab, SImode) != CODE_FOR_nothing);
1186 bswap64_p = (builtin_decl_explicit_p (BUILT_IN_BSWAP64)
1187 && (optab_handler (bswap_optab, DImode) != CODE_FOR_nothing
1188 || (bswap32_p && word_mode == SImode)));
1190 /* Determine the argument type of the builtins. The code later on
1191 assumes that the return and argument type are the same. */
1192 if (bswap32_p)
1194 tree fndecl = builtin_decl_explicit (BUILT_IN_BSWAP32);
1195 bswap32_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
1198 if (bswap64_p)
1200 tree fndecl = builtin_decl_explicit (BUILT_IN_BSWAP64);
1201 bswap64_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
1204 memset (&nop_stats, 0, sizeof (nop_stats));
1205 memset (&bswap_stats, 0, sizeof (bswap_stats));
1206 calculate_dominance_info (CDI_DOMINATORS);
1208 FOR_EACH_BB_FN (bb, fun)
1210 gimple_stmt_iterator gsi;
1212 /* We do a reverse scan for bswap patterns to make sure we get the
1213 widest match. As bswap pattern matching doesn't handle previously
1214 inserted smaller bswap replacements as sub-patterns, the wider
1215 variant wouldn't be detected. */
1216 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
1218 gimple *ins_stmt, *cur_stmt = gsi_stmt (gsi);
1219 tree fndecl = NULL_TREE, bswap_type = NULL_TREE, load_type;
1220 enum tree_code code;
1221 struct symbolic_number n;
1222 bool bswap;
1224 /* This gsi_prev (&gsi) is not part of the for loop because cur_stmt
1225 might be moved to a different basic block by bswap_replace and gsi
1226 must not points to it if that's the case. Moving the gsi_prev
1227 there make sure that gsi points to the statement previous to
1228 cur_stmt while still making sure that all statements are
1229 considered in this basic block. */
1230 gsi_prev (&gsi);
1232 if (!is_gimple_assign (cur_stmt))
1233 continue;
1235 code = gimple_assign_rhs_code (cur_stmt);
1236 switch (code)
1238 case LROTATE_EXPR:
1239 case RROTATE_EXPR:
1240 if (!tree_fits_uhwi_p (gimple_assign_rhs2 (cur_stmt))
1241 || tree_to_uhwi (gimple_assign_rhs2 (cur_stmt))
1242 % BITS_PER_UNIT)
1243 continue;
1244 /* Fall through. */
1245 case BIT_IOR_EXPR:
1246 break;
1247 default:
1248 continue;
1251 ins_stmt = find_bswap_or_nop (cur_stmt, &n, &bswap);
1253 if (!ins_stmt)
1254 continue;
1256 switch (n.range)
1258 case 16:
1259 /* Already in canonical form, nothing to do. */
1260 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
1261 continue;
1262 load_type = bswap_type = uint16_type_node;
1263 break;
1264 case 32:
1265 load_type = uint32_type_node;
1266 if (bswap32_p)
1268 fndecl = builtin_decl_explicit (BUILT_IN_BSWAP32);
1269 bswap_type = bswap32_type;
1271 break;
1272 case 64:
1273 load_type = uint64_type_node;
1274 if (bswap64_p)
1276 fndecl = builtin_decl_explicit (BUILT_IN_BSWAP64);
1277 bswap_type = bswap64_type;
1279 break;
1280 default:
1281 continue;
1284 if (bswap && !fndecl && n.range != 16)
1285 continue;
1287 if (bswap_replace (gsi_for_stmt (cur_stmt), ins_stmt, fndecl,
1288 bswap_type, load_type, &n, bswap))
1289 changed = true;
1293 statistics_counter_event (fun, "16-bit nop implementations found",
1294 nop_stats.found_16bit);
1295 statistics_counter_event (fun, "32-bit nop implementations found",
1296 nop_stats.found_32bit);
1297 statistics_counter_event (fun, "64-bit nop implementations found",
1298 nop_stats.found_64bit);
1299 statistics_counter_event (fun, "16-bit bswap implementations found",
1300 bswap_stats.found_16bit);
1301 statistics_counter_event (fun, "32-bit bswap implementations found",
1302 bswap_stats.found_32bit);
1303 statistics_counter_event (fun, "64-bit bswap implementations found",
1304 bswap_stats.found_64bit);
1306 return (changed ? TODO_update_ssa : 0);
1309 } // anon namespace
1311 gimple_opt_pass *
1312 make_pass_optimize_bswap (gcc::context *ctxt)
1314 return new pass_optimize_bswap (ctxt);
1317 namespace {
1319 /* Struct recording one operand for the store, which is either a constant,
1320 then VAL represents the constant and all the other fields are zero, or
1321 a memory load, then VAL represents the reference, BASE_ADDR is non-NULL
1322 and the other fields also reflect the memory load, or an SSA name, then
1323 VAL represents the SSA name and all the other fields are zero, */
1325 struct store_operand_info
1327 tree val;
1328 tree base_addr;
1329 poly_uint64 bitsize;
1330 poly_uint64 bitpos;
1331 poly_uint64 bitregion_start;
1332 poly_uint64 bitregion_end;
1333 gimple *stmt;
1334 bool bit_not_p;
1335 store_operand_info ();
1338 store_operand_info::store_operand_info ()
1339 : val (NULL_TREE), base_addr (NULL_TREE), bitsize (0), bitpos (0),
1340 bitregion_start (0), bitregion_end (0), stmt (NULL), bit_not_p (false)
1344 /* Struct recording the information about a single store of an immediate
1345 to memory. These are created in the first phase and coalesced into
1346 merged_store_group objects in the second phase. */
1348 struct store_immediate_info
1350 unsigned HOST_WIDE_INT bitsize;
1351 unsigned HOST_WIDE_INT bitpos;
1352 unsigned HOST_WIDE_INT bitregion_start;
1353 /* This is one past the last bit of the bit region. */
1354 unsigned HOST_WIDE_INT bitregion_end;
1355 gimple *stmt;
1356 unsigned int order;
1357 /* INTEGER_CST for constant stores, MEM_REF for memory copy,
1358 BIT_*_EXPR for logical bitwise operation, BIT_INSERT_EXPR
1359 for bit insertion.
1360 LROTATE_EXPR if it can be only bswap optimized and
1361 ops are not really meaningful.
1362 NOP_EXPR if bswap optimization detected identity, ops
1363 are not meaningful. */
1364 enum tree_code rhs_code;
1365 /* Two fields for bswap optimization purposes. */
1366 struct symbolic_number n;
1367 gimple *ins_stmt;
1368 /* True if BIT_{AND,IOR,XOR}_EXPR result is inverted before storing. */
1369 bool bit_not_p;
1370 /* True if ops have been swapped and thus ops[1] represents
1371 rhs1 of BIT_{AND,IOR,XOR}_EXPR and ops[0] represents rhs2. */
1372 bool ops_swapped_p;
1373 /* Operands. For BIT_*_EXPR rhs_code both operands are used, otherwise
1374 just the first one. */
1375 store_operand_info ops[2];
1376 store_immediate_info (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
1377 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
1378 gimple *, unsigned int, enum tree_code,
1379 struct symbolic_number &, gimple *, bool,
1380 const store_operand_info &,
1381 const store_operand_info &);
1384 store_immediate_info::store_immediate_info (unsigned HOST_WIDE_INT bs,
1385 unsigned HOST_WIDE_INT bp,
1386 unsigned HOST_WIDE_INT brs,
1387 unsigned HOST_WIDE_INT bre,
1388 gimple *st,
1389 unsigned int ord,
1390 enum tree_code rhscode,
1391 struct symbolic_number &nr,
1392 gimple *ins_stmtp,
1393 bool bitnotp,
1394 const store_operand_info &op0r,
1395 const store_operand_info &op1r)
1396 : bitsize (bs), bitpos (bp), bitregion_start (brs), bitregion_end (bre),
1397 stmt (st), order (ord), rhs_code (rhscode), n (nr),
1398 ins_stmt (ins_stmtp), bit_not_p (bitnotp), ops_swapped_p (false)
1399 #if __cplusplus >= 201103L
1400 , ops { op0r, op1r }
1403 #else
1405 ops[0] = op0r;
1406 ops[1] = op1r;
1408 #endif
1410 /* Struct representing a group of stores to contiguous memory locations.
1411 These are produced by the second phase (coalescing) and consumed in the
1412 third phase that outputs the widened stores. */
1414 struct merged_store_group
1416 unsigned HOST_WIDE_INT start;
1417 unsigned HOST_WIDE_INT width;
1418 unsigned HOST_WIDE_INT bitregion_start;
1419 unsigned HOST_WIDE_INT bitregion_end;
1420 /* The size of the allocated memory for val and mask. */
1421 unsigned HOST_WIDE_INT buf_size;
1422 unsigned HOST_WIDE_INT align_base;
1423 poly_uint64 load_align_base[2];
1425 unsigned int align;
1426 unsigned int load_align[2];
1427 unsigned int first_order;
1428 unsigned int last_order;
1429 bool bit_insertion;
1431 auto_vec<store_immediate_info *> stores;
1432 /* We record the first and last original statements in the sequence because
1433 we'll need their vuse/vdef and replacement position. It's easier to keep
1434 track of them separately as 'stores' is reordered by apply_stores. */
1435 gimple *last_stmt;
1436 gimple *first_stmt;
1437 unsigned char *val;
1438 unsigned char *mask;
1440 merged_store_group (store_immediate_info *);
1441 ~merged_store_group ();
1442 bool can_be_merged_into (store_immediate_info *);
1443 void merge_into (store_immediate_info *);
1444 void merge_overlapping (store_immediate_info *);
1445 bool apply_stores ();
1446 private:
1447 void do_merge (store_immediate_info *);
1450 /* Debug helper. Dump LEN elements of byte array PTR to FD in hex. */
1452 static void
1453 dump_char_array (FILE *fd, unsigned char *ptr, unsigned int len)
1455 if (!fd)
1456 return;
1458 for (unsigned int i = 0; i < len; i++)
1459 fprintf (fd, "%02x ", ptr[i]);
1460 fprintf (fd, "\n");
1463 /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
1464 bits between adjacent elements. AMNT should be within
1465 [0, BITS_PER_UNIT).
1466 Example, AMNT = 2:
1467 00011111|11100000 << 2 = 01111111|10000000
1468 PTR[1] | PTR[0] PTR[1] | PTR[0]. */
1470 static void
1471 shift_bytes_in_array (unsigned char *ptr, unsigned int sz, unsigned int amnt)
1473 if (amnt == 0)
1474 return;
1476 unsigned char carry_over = 0U;
1477 unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
1478 unsigned char clear_mask = (~0U) << amnt;
1480 for (unsigned int i = 0; i < sz; i++)
1482 unsigned prev_carry_over = carry_over;
1483 carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
1485 ptr[i] <<= amnt;
1486 if (i != 0)
1488 ptr[i] &= clear_mask;
1489 ptr[i] |= prev_carry_over;
1494 /* Like shift_bytes_in_array but for big-endian.
1495 Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
1496 bits between adjacent elements. AMNT should be within
1497 [0, BITS_PER_UNIT).
1498 Example, AMNT = 2:
1499 00011111|11100000 >> 2 = 00000111|11111000
1500 PTR[0] | PTR[1] PTR[0] | PTR[1]. */
1502 static void
1503 shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
1504 unsigned int amnt)
1506 if (amnt == 0)
1507 return;
1509 unsigned char carry_over = 0U;
1510 unsigned char carry_mask = ~(~0U << amnt);
1512 for (unsigned int i = 0; i < sz; i++)
1514 unsigned prev_carry_over = carry_over;
1515 carry_over = ptr[i] & carry_mask;
1517 carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
1518 ptr[i] >>= amnt;
1519 ptr[i] |= prev_carry_over;
1523 /* Clear out LEN bits starting from bit START in the byte array
1524 PTR. This clears the bits to the *right* from START.
1525 START must be within [0, BITS_PER_UNIT) and counts starting from
1526 the least significant bit. */
1528 static void
1529 clear_bit_region_be (unsigned char *ptr, unsigned int start,
1530 unsigned int len)
1532 if (len == 0)
1533 return;
1534 /* Clear len bits to the right of start. */
1535 else if (len <= start + 1)
1537 unsigned char mask = (~(~0U << len));
1538 mask = mask << (start + 1U - len);
1539 ptr[0] &= ~mask;
1541 else if (start != BITS_PER_UNIT - 1)
1543 clear_bit_region_be (ptr, start, (start % BITS_PER_UNIT) + 1);
1544 clear_bit_region_be (ptr + 1, BITS_PER_UNIT - 1,
1545 len - (start % BITS_PER_UNIT) - 1);
1547 else if (start == BITS_PER_UNIT - 1
1548 && len > BITS_PER_UNIT)
1550 unsigned int nbytes = len / BITS_PER_UNIT;
1551 memset (ptr, 0, nbytes);
1552 if (len % BITS_PER_UNIT != 0)
1553 clear_bit_region_be (ptr + nbytes, BITS_PER_UNIT - 1,
1554 len % BITS_PER_UNIT);
1556 else
1557 gcc_unreachable ();
1560 /* In the byte array PTR clear the bit region starting at bit
1561 START and is LEN bits wide.
1562 For regions spanning multiple bytes do this recursively until we reach
1563 zero LEN or a region contained within a single byte. */
1565 static void
1566 clear_bit_region (unsigned char *ptr, unsigned int start,
1567 unsigned int len)
1569 /* Degenerate base case. */
1570 if (len == 0)
1571 return;
1572 else if (start >= BITS_PER_UNIT)
1573 clear_bit_region (ptr + 1, start - BITS_PER_UNIT, len);
1574 /* Second base case. */
1575 else if ((start + len) <= BITS_PER_UNIT)
1577 unsigned char mask = (~0U) << (unsigned char) (BITS_PER_UNIT - len);
1578 mask >>= BITS_PER_UNIT - (start + len);
1580 ptr[0] &= ~mask;
1582 return;
1584 /* Clear most significant bits in a byte and proceed with the next byte. */
1585 else if (start != 0)
1587 clear_bit_region (ptr, start, BITS_PER_UNIT - start);
1588 clear_bit_region (ptr + 1, 0, len - (BITS_PER_UNIT - start));
1590 /* Whole bytes need to be cleared. */
1591 else if (start == 0 && len > BITS_PER_UNIT)
1593 unsigned int nbytes = len / BITS_PER_UNIT;
1594 /* We could recurse on each byte but we clear whole bytes, so a simple
1595 memset will do. */
1596 memset (ptr, '\0', nbytes);
1597 /* Clear the remaining sub-byte region if there is one. */
1598 if (len % BITS_PER_UNIT != 0)
1599 clear_bit_region (ptr + nbytes, 0, len % BITS_PER_UNIT);
1601 else
1602 gcc_unreachable ();
1605 /* Write BITLEN bits of EXPR to the byte array PTR at
1606 bit position BITPOS. PTR should contain TOTAL_BYTES elements.
1607 Return true if the operation succeeded. */
1609 static bool
1610 encode_tree_to_bitpos (tree expr, unsigned char *ptr, int bitlen, int bitpos,
1611 unsigned int total_bytes)
1613 unsigned int first_byte = bitpos / BITS_PER_UNIT;
1614 tree tmp_int = expr;
1615 bool sub_byte_op_p = ((bitlen % BITS_PER_UNIT)
1616 || (bitpos % BITS_PER_UNIT)
1617 || !int_mode_for_size (bitlen, 0).exists ());
1619 if (!sub_byte_op_p)
1620 return native_encode_expr (tmp_int, ptr + first_byte, total_bytes) != 0;
1622 /* LITTLE-ENDIAN
1623 We are writing a non byte-sized quantity or at a position that is not
1624 at a byte boundary.
1625 |--------|--------|--------| ptr + first_byte
1627 xxx xxxxxxxx xxx< bp>
1628 |______EXPR____|
1630 First native_encode_expr EXPR into a temporary buffer and shift each
1631 byte in the buffer by 'bp' (carrying the bits over as necessary).
1632 |00000000|00xxxxxx|xxxxxxxx| << bp = |000xxxxx|xxxxxxxx|xxx00000|
1633 <------bitlen---->< bp>
1634 Then we clear the destination bits:
1635 |---00000|00000000|000-----| ptr + first_byte
1636 <-------bitlen--->< bp>
1638 Finally we ORR the bytes of the shifted EXPR into the cleared region:
1639 |---xxxxx||xxxxxxxx||xxx-----| ptr + first_byte.
1641 BIG-ENDIAN
1642 We are writing a non byte-sized quantity or at a position that is not
1643 at a byte boundary.
1644 ptr + first_byte |--------|--------|--------|
1646 <bp >xxx xxxxxxxx xxx
1647 |_____EXPR_____|
1649 First native_encode_expr EXPR into a temporary buffer and shift each
1650 byte in the buffer to the right by (carrying the bits over as necessary).
1651 We shift by as much as needed to align the most significant bit of EXPR
1652 with bitpos:
1653 |00xxxxxx|xxxxxxxx| >> 3 = |00000xxx|xxxxxxxx|xxxxx000|
1654 <---bitlen----> <bp ><-----bitlen----->
1655 Then we clear the destination bits:
1656 ptr + first_byte |-----000||00000000||00000---|
1657 <bp ><-------bitlen----->
1659 Finally we ORR the bytes of the shifted EXPR into the cleared region:
1660 ptr + first_byte |---xxxxx||xxxxxxxx||xxx-----|.
1661 The awkwardness comes from the fact that bitpos is counted from the
1662 most significant bit of a byte. */
1664 /* We must be dealing with fixed-size data at this point, since the
1665 total size is also fixed. */
1666 fixed_size_mode mode = as_a <fixed_size_mode> (TYPE_MODE (TREE_TYPE (expr)));
1667 /* Allocate an extra byte so that we have space to shift into. */
1668 unsigned int byte_size = GET_MODE_SIZE (mode) + 1;
1669 unsigned char *tmpbuf = XALLOCAVEC (unsigned char, byte_size);
1670 memset (tmpbuf, '\0', byte_size);
1671 /* The store detection code should only have allowed constants that are
1672 accepted by native_encode_expr. */
1673 if (native_encode_expr (expr, tmpbuf, byte_size - 1) == 0)
1674 gcc_unreachable ();
1676 /* The native_encode_expr machinery uses TYPE_MODE to determine how many
1677 bytes to write. This means it can write more than
1678 ROUND_UP (bitlen, BITS_PER_UNIT) / BITS_PER_UNIT bytes (for example
1679 write 8 bytes for a bitlen of 40). Skip the bytes that are not within
1680 bitlen and zero out the bits that are not relevant as well (that may
1681 contain a sign bit due to sign-extension). */
1682 unsigned int padding
1683 = byte_size - ROUND_UP (bitlen, BITS_PER_UNIT) / BITS_PER_UNIT - 1;
1684 /* On big-endian the padding is at the 'front' so just skip the initial
1685 bytes. */
1686 if (BYTES_BIG_ENDIAN)
1687 tmpbuf += padding;
1689 byte_size -= padding;
1691 if (bitlen % BITS_PER_UNIT != 0)
1693 if (BYTES_BIG_ENDIAN)
1694 clear_bit_region_be (tmpbuf, BITS_PER_UNIT - 1,
1695 BITS_PER_UNIT - (bitlen % BITS_PER_UNIT));
1696 else
1697 clear_bit_region (tmpbuf, bitlen,
1698 byte_size * BITS_PER_UNIT - bitlen);
1700 /* Left shifting relies on the last byte being clear if bitlen is
1701 a multiple of BITS_PER_UNIT, which might not be clear if
1702 there are padding bytes. */
1703 else if (!BYTES_BIG_ENDIAN)
1704 tmpbuf[byte_size - 1] = '\0';
1706 /* Clear the bit region in PTR where the bits from TMPBUF will be
1707 inserted into. */
1708 if (BYTES_BIG_ENDIAN)
1709 clear_bit_region_be (ptr + first_byte,
1710 BITS_PER_UNIT - 1 - (bitpos % BITS_PER_UNIT), bitlen);
1711 else
1712 clear_bit_region (ptr + first_byte, bitpos % BITS_PER_UNIT, bitlen);
1714 int shift_amnt;
1715 int bitlen_mod = bitlen % BITS_PER_UNIT;
1716 int bitpos_mod = bitpos % BITS_PER_UNIT;
1718 bool skip_byte = false;
1719 if (BYTES_BIG_ENDIAN)
1721 /* BITPOS and BITLEN are exactly aligned and no shifting
1722 is necessary. */
1723 if (bitpos_mod + bitlen_mod == BITS_PER_UNIT
1724 || (bitpos_mod == 0 && bitlen_mod == 0))
1725 shift_amnt = 0;
1726 /* |. . . . . . . .|
1727 <bp > <blen >.
1728 We always shift right for BYTES_BIG_ENDIAN so shift the beginning
1729 of the value until it aligns with 'bp' in the next byte over. */
1730 else if (bitpos_mod + bitlen_mod < BITS_PER_UNIT)
1732 shift_amnt = bitlen_mod + bitpos_mod;
1733 skip_byte = bitlen_mod != 0;
1735 /* |. . . . . . . .|
1736 <----bp--->
1737 <---blen---->.
1738 Shift the value right within the same byte so it aligns with 'bp'. */
1739 else
1740 shift_amnt = bitlen_mod + bitpos_mod - BITS_PER_UNIT;
1742 else
1743 shift_amnt = bitpos % BITS_PER_UNIT;
1745 /* Create the shifted version of EXPR. */
1746 if (!BYTES_BIG_ENDIAN)
1748 shift_bytes_in_array (tmpbuf, byte_size, shift_amnt);
1749 if (shift_amnt == 0)
1750 byte_size--;
1752 else
1754 gcc_assert (BYTES_BIG_ENDIAN);
1755 shift_bytes_in_array_right (tmpbuf, byte_size, shift_amnt);
1756 /* If shifting right forced us to move into the next byte skip the now
1757 empty byte. */
1758 if (skip_byte)
1760 tmpbuf++;
1761 byte_size--;
1765 /* Insert the bits from TMPBUF. */
1766 for (unsigned int i = 0; i < byte_size; i++)
1767 ptr[first_byte + i] |= tmpbuf[i];
1769 return true;
1772 /* Sorting function for store_immediate_info objects.
1773 Sorts them by bitposition. */
1775 static int
1776 sort_by_bitpos (const void *x, const void *y)
1778 store_immediate_info *const *tmp = (store_immediate_info * const *) x;
1779 store_immediate_info *const *tmp2 = (store_immediate_info * const *) y;
1781 if ((*tmp)->bitpos < (*tmp2)->bitpos)
1782 return -1;
1783 else if ((*tmp)->bitpos > (*tmp2)->bitpos)
1784 return 1;
1785 else
1786 /* If they are the same let's use the order which is guaranteed to
1787 be different. */
1788 return (*tmp)->order - (*tmp2)->order;
1791 /* Sorting function for store_immediate_info objects.
1792 Sorts them by the order field. */
1794 static int
1795 sort_by_order (const void *x, const void *y)
1797 store_immediate_info *const *tmp = (store_immediate_info * const *) x;
1798 store_immediate_info *const *tmp2 = (store_immediate_info * const *) y;
1800 if ((*tmp)->order < (*tmp2)->order)
1801 return -1;
1802 else if ((*tmp)->order > (*tmp2)->order)
1803 return 1;
1805 gcc_unreachable ();
1808 /* Initialize a merged_store_group object from a store_immediate_info
1809 object. */
1811 merged_store_group::merged_store_group (store_immediate_info *info)
1813 start = info->bitpos;
1814 width = info->bitsize;
1815 bitregion_start = info->bitregion_start;
1816 bitregion_end = info->bitregion_end;
1817 /* VAL has memory allocated for it in apply_stores once the group
1818 width has been finalized. */
1819 val = NULL;
1820 mask = NULL;
1821 bit_insertion = false;
1822 unsigned HOST_WIDE_INT align_bitpos = 0;
1823 get_object_alignment_1 (gimple_assign_lhs (info->stmt),
1824 &align, &align_bitpos);
1825 align_base = start - align_bitpos;
1826 for (int i = 0; i < 2; ++i)
1828 store_operand_info &op = info->ops[i];
1829 if (op.base_addr == NULL_TREE)
1831 load_align[i] = 0;
1832 load_align_base[i] = 0;
1834 else
1836 get_object_alignment_1 (op.val, &load_align[i], &align_bitpos);
1837 load_align_base[i] = op.bitpos - align_bitpos;
1840 stores.create (1);
1841 stores.safe_push (info);
1842 last_stmt = info->stmt;
1843 last_order = info->order;
1844 first_stmt = last_stmt;
1845 first_order = last_order;
1846 buf_size = 0;
1849 merged_store_group::~merged_store_group ()
1851 if (val)
1852 XDELETEVEC (val);
1855 /* Return true if the store described by INFO can be merged into the group. */
1857 bool
1858 merged_store_group::can_be_merged_into (store_immediate_info *info)
1860 /* Do not merge bswap patterns. */
1861 if (info->rhs_code == LROTATE_EXPR)
1862 return false;
1864 /* The canonical case. */
1865 if (info->rhs_code == stores[0]->rhs_code)
1866 return true;
1868 /* BIT_INSERT_EXPR is compatible with INTEGER_CST. */
1869 if (info->rhs_code == BIT_INSERT_EXPR && stores[0]->rhs_code == INTEGER_CST)
1870 return true;
1872 if (stores[0]->rhs_code == BIT_INSERT_EXPR && info->rhs_code == INTEGER_CST)
1873 return true;
1875 /* We can turn MEM_REF into BIT_INSERT_EXPR for bit-field stores. */
1876 if (info->rhs_code == MEM_REF
1877 && (stores[0]->rhs_code == INTEGER_CST
1878 || stores[0]->rhs_code == BIT_INSERT_EXPR)
1879 && info->bitregion_start == stores[0]->bitregion_start
1880 && info->bitregion_end == stores[0]->bitregion_end)
1881 return true;
1883 if (stores[0]->rhs_code == MEM_REF
1884 && (info->rhs_code == INTEGER_CST
1885 || info->rhs_code == BIT_INSERT_EXPR)
1886 && info->bitregion_start == stores[0]->bitregion_start
1887 && info->bitregion_end == stores[0]->bitregion_end)
1888 return true;
1890 return false;
1893 /* Helper method for merge_into and merge_overlapping to do
1894 the common part. */
1896 void
1897 merged_store_group::do_merge (store_immediate_info *info)
1899 bitregion_start = MIN (bitregion_start, info->bitregion_start);
1900 bitregion_end = MAX (bitregion_end, info->bitregion_end);
1902 unsigned int this_align;
1903 unsigned HOST_WIDE_INT align_bitpos = 0;
1904 get_object_alignment_1 (gimple_assign_lhs (info->stmt),
1905 &this_align, &align_bitpos);
1906 if (this_align > align)
1908 align = this_align;
1909 align_base = info->bitpos - align_bitpos;
1911 for (int i = 0; i < 2; ++i)
1913 store_operand_info &op = info->ops[i];
1914 if (!op.base_addr)
1915 continue;
1917 get_object_alignment_1 (op.val, &this_align, &align_bitpos);
1918 if (this_align > load_align[i])
1920 load_align[i] = this_align;
1921 load_align_base[i] = op.bitpos - align_bitpos;
1925 gimple *stmt = info->stmt;
1926 stores.safe_push (info);
1927 if (info->order > last_order)
1929 last_order = info->order;
1930 last_stmt = stmt;
1932 else if (info->order < first_order)
1934 first_order = info->order;
1935 first_stmt = stmt;
1939 /* Merge a store recorded by INFO into this merged store.
1940 The store is not overlapping with the existing recorded
1941 stores. */
1943 void
1944 merged_store_group::merge_into (store_immediate_info *info)
1946 /* Make sure we're inserting in the position we think we're inserting. */
1947 gcc_assert (info->bitpos >= start + width
1948 && info->bitregion_start <= bitregion_end);
1950 width = info->bitpos + info->bitsize - start;
1951 do_merge (info);
1954 /* Merge a store described by INFO into this merged store.
1955 INFO overlaps in some way with the current store (i.e. it's not contiguous
1956 which is handled by merged_store_group::merge_into). */
1958 void
1959 merged_store_group::merge_overlapping (store_immediate_info *info)
1961 /* If the store extends the size of the group, extend the width. */
1962 if (info->bitpos + info->bitsize > start + width)
1963 width = info->bitpos + info->bitsize - start;
1965 do_merge (info);
1968 /* Go through all the recorded stores in this group in program order and
1969 apply their values to the VAL byte array to create the final merged
1970 value. Return true if the operation succeeded. */
1972 bool
1973 merged_store_group::apply_stores ()
1975 /* Make sure we have more than one store in the group, otherwise we cannot
1976 merge anything. */
1977 if (bitregion_start % BITS_PER_UNIT != 0
1978 || bitregion_end % BITS_PER_UNIT != 0
1979 || stores.length () == 1)
1980 return false;
1982 stores.qsort (sort_by_order);
1983 store_immediate_info *info;
1984 unsigned int i;
1985 /* Create a power-of-2-sized buffer for native_encode_expr. */
1986 buf_size = 1 << ceil_log2 ((bitregion_end - bitregion_start) / BITS_PER_UNIT);
1987 val = XNEWVEC (unsigned char, 2 * buf_size);
1988 mask = val + buf_size;
1989 memset (val, 0, buf_size);
1990 memset (mask, ~0U, buf_size);
1992 FOR_EACH_VEC_ELT (stores, i, info)
1994 unsigned int pos_in_buffer = info->bitpos - bitregion_start;
1995 tree cst;
1996 if (info->ops[0].val && info->ops[0].base_addr == NULL_TREE)
1997 cst = info->ops[0].val;
1998 else if (info->ops[1].val && info->ops[1].base_addr == NULL_TREE)
1999 cst = info->ops[1].val;
2000 else
2001 cst = NULL_TREE;
2002 bool ret = true;
2003 if (cst)
2005 if (info->rhs_code == BIT_INSERT_EXPR)
2006 bit_insertion = true;
2007 else
2008 ret = encode_tree_to_bitpos (cst, val, info->bitsize,
2009 pos_in_buffer, buf_size);
2011 unsigned char *m = mask + (pos_in_buffer / BITS_PER_UNIT);
2012 if (BYTES_BIG_ENDIAN)
2013 clear_bit_region_be (m, (BITS_PER_UNIT - 1
2014 - (pos_in_buffer % BITS_PER_UNIT)),
2015 info->bitsize);
2016 else
2017 clear_bit_region (m, pos_in_buffer % BITS_PER_UNIT, info->bitsize);
2018 if (cst && dump_file && (dump_flags & TDF_DETAILS))
2020 if (ret)
2022 fputs ("After writing ", dump_file);
2023 print_generic_expr (dump_file, cst, TDF_NONE);
2024 fprintf (dump_file, " of size " HOST_WIDE_INT_PRINT_DEC
2025 " at position %d\n", info->bitsize, pos_in_buffer);
2026 fputs (" the merged value contains ", dump_file);
2027 dump_char_array (dump_file, val, buf_size);
2028 fputs (" the merged mask contains ", dump_file);
2029 dump_char_array (dump_file, mask, buf_size);
2030 if (bit_insertion)
2031 fputs (" bit insertion is required\n", dump_file);
2033 else
2034 fprintf (dump_file, "Failed to merge stores\n");
2036 if (!ret)
2037 return false;
2039 stores.qsort (sort_by_bitpos);
2040 return true;
2043 /* Structure describing the store chain. */
2045 struct imm_store_chain_info
2047 /* Doubly-linked list that imposes an order on chain processing.
2048 PNXP (prev's next pointer) points to the head of a list, or to
2049 the next field in the previous chain in the list.
2050 See pass_store_merging::m_stores_head for more rationale. */
2051 imm_store_chain_info *next, **pnxp;
2052 tree base_addr;
2053 auto_vec<store_immediate_info *> m_store_info;
2054 auto_vec<merged_store_group *> m_merged_store_groups;
2056 imm_store_chain_info (imm_store_chain_info *&inspt, tree b_a)
2057 : next (inspt), pnxp (&inspt), base_addr (b_a)
2059 inspt = this;
2060 if (next)
2062 gcc_checking_assert (pnxp == next->pnxp);
2063 next->pnxp = &next;
2066 ~imm_store_chain_info ()
2068 *pnxp = next;
2069 if (next)
2071 gcc_checking_assert (&next == next->pnxp);
2072 next->pnxp = pnxp;
2075 bool terminate_and_process_chain ();
2076 bool try_coalesce_bswap (merged_store_group *, unsigned int, unsigned int);
2077 bool coalesce_immediate_stores ();
2078 bool output_merged_store (merged_store_group *);
2079 bool output_merged_stores ();
2082 const pass_data pass_data_tree_store_merging = {
2083 GIMPLE_PASS, /* type */
2084 "store-merging", /* name */
2085 OPTGROUP_NONE, /* optinfo_flags */
2086 TV_GIMPLE_STORE_MERGING, /* tv_id */
2087 PROP_ssa, /* properties_required */
2088 0, /* properties_provided */
2089 0, /* properties_destroyed */
2090 0, /* todo_flags_start */
2091 TODO_update_ssa, /* todo_flags_finish */
2094 class pass_store_merging : public gimple_opt_pass
2096 public:
2097 pass_store_merging (gcc::context *ctxt)
2098 : gimple_opt_pass (pass_data_tree_store_merging, ctxt), m_stores_head ()
2102 /* Pass not supported for PDP-endian, nor for insane hosts or
2103 target character sizes where native_{encode,interpret}_expr
2104 doesn't work properly. */
2105 virtual bool
2106 gate (function *)
2108 return flag_store_merging
2109 && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
2110 && CHAR_BIT == 8
2111 && BITS_PER_UNIT == 8;
2114 virtual unsigned int execute (function *);
2116 private:
2117 hash_map<tree_operand_hash, struct imm_store_chain_info *> m_stores;
2119 /* Form a doubly-linked stack of the elements of m_stores, so that
2120 we can iterate over them in a predictable way. Using this order
2121 avoids extraneous differences in the compiler output just because
2122 of tree pointer variations (e.g. different chains end up in
2123 different positions of m_stores, so they are handled in different
2124 orders, so they allocate or release SSA names in different
2125 orders, and when they get reused, subsequent passes end up
2126 getting different SSA names, which may ultimately change
2127 decisions when going out of SSA). */
2128 imm_store_chain_info *m_stores_head;
2130 void process_store (gimple *);
2131 bool terminate_and_process_all_chains ();
2132 bool terminate_all_aliasing_chains (imm_store_chain_info **, gimple *);
2133 bool terminate_and_release_chain (imm_store_chain_info *);
2134 }; // class pass_store_merging
2136 /* Terminate and process all recorded chains. Return true if any changes
2137 were made. */
2139 bool
2140 pass_store_merging::terminate_and_process_all_chains ()
2142 bool ret = false;
2143 while (m_stores_head)
2144 ret |= terminate_and_release_chain (m_stores_head);
2145 gcc_assert (m_stores.elements () == 0);
2146 gcc_assert (m_stores_head == NULL);
2148 return ret;
2151 /* Terminate all chains that are affected by the statement STMT.
2152 CHAIN_INFO is the chain we should ignore from the checks if
2153 non-NULL. */
2155 bool
2156 pass_store_merging::terminate_all_aliasing_chains (imm_store_chain_info
2157 **chain_info,
2158 gimple *stmt)
2160 bool ret = false;
2162 /* If the statement doesn't touch memory it can't alias. */
2163 if (!gimple_vuse (stmt))
2164 return false;
2166 tree store_lhs = gimple_store_p (stmt) ? gimple_get_lhs (stmt) : NULL_TREE;
2167 for (imm_store_chain_info *next = m_stores_head, *cur = next; cur; cur = next)
2169 next = cur->next;
2171 /* We already checked all the stores in chain_info and terminated the
2172 chain if necessary. Skip it here. */
2173 if (chain_info && *chain_info == cur)
2174 continue;
2176 store_immediate_info *info;
2177 unsigned int i;
2178 FOR_EACH_VEC_ELT (cur->m_store_info, i, info)
2180 tree lhs = gimple_assign_lhs (info->stmt);
2181 if (ref_maybe_used_by_stmt_p (stmt, lhs)
2182 || stmt_may_clobber_ref_p (stmt, lhs)
2183 || (store_lhs && refs_output_dependent_p (store_lhs, lhs)))
2185 if (dump_file && (dump_flags & TDF_DETAILS))
2187 fprintf (dump_file, "stmt causes chain termination:\n");
2188 print_gimple_stmt (dump_file, stmt, 0);
2190 terminate_and_release_chain (cur);
2191 ret = true;
2192 break;
2197 return ret;
2200 /* Helper function. Terminate the recorded chain storing to base object
2201 BASE. Return true if the merging and output was successful. The m_stores
2202 entry is removed after the processing in any case. */
2204 bool
2205 pass_store_merging::terminate_and_release_chain (imm_store_chain_info *chain_info)
2207 bool ret = chain_info->terminate_and_process_chain ();
2208 m_stores.remove (chain_info->base_addr);
2209 delete chain_info;
2210 return ret;
2213 /* Return true if stmts in between FIRST (inclusive) and LAST (exclusive)
2214 may clobber REF. FIRST and LAST must be in the same basic block and
2215 have non-NULL vdef. We want to be able to sink load of REF across
2216 stores between FIRST and LAST, up to right before LAST. */
2218 bool
2219 stmts_may_clobber_ref_p (gimple *first, gimple *last, tree ref)
2221 ao_ref r;
2222 ao_ref_init (&r, ref);
2223 unsigned int count = 0;
2224 tree vop = gimple_vdef (last);
2225 gimple *stmt;
2227 gcc_checking_assert (gimple_bb (first) == gimple_bb (last));
2230 stmt = SSA_NAME_DEF_STMT (vop);
2231 if (stmt_may_clobber_ref_p_1 (stmt, &r))
2232 return true;
2233 if (gimple_store_p (stmt)
2234 && refs_anti_dependent_p (ref, gimple_get_lhs (stmt)))
2235 return true;
2236 /* Avoid quadratic compile time by bounding the number of checks
2237 we perform. */
2238 if (++count > MAX_STORE_ALIAS_CHECKS)
2239 return true;
2240 vop = gimple_vuse (stmt);
2242 while (stmt != first);
2243 return false;
2246 /* Return true if INFO->ops[IDX] is mergeable with the
2247 corresponding loads already in MERGED_STORE group.
2248 BASE_ADDR is the base address of the whole store group. */
2250 bool
2251 compatible_load_p (merged_store_group *merged_store,
2252 store_immediate_info *info,
2253 tree base_addr, int idx)
2255 store_immediate_info *infof = merged_store->stores[0];
2256 if (!info->ops[idx].base_addr
2257 || maybe_ne (info->ops[idx].bitpos - infof->ops[idx].bitpos,
2258 info->bitpos - infof->bitpos)
2259 || !operand_equal_p (info->ops[idx].base_addr,
2260 infof->ops[idx].base_addr, 0))
2261 return false;
2263 store_immediate_info *infol = merged_store->stores.last ();
2264 tree load_vuse = gimple_vuse (info->ops[idx].stmt);
2265 /* In this case all vuses should be the same, e.g.
2266 _1 = s.a; _2 = s.b; _3 = _1 | 1; t.a = _3; _4 = _2 | 2; t.b = _4;
2268 _1 = s.a; _2 = s.b; t.a = _1; t.b = _2;
2269 and we can emit the coalesced load next to any of those loads. */
2270 if (gimple_vuse (infof->ops[idx].stmt) == load_vuse
2271 && gimple_vuse (infol->ops[idx].stmt) == load_vuse)
2272 return true;
2274 /* Otherwise, at least for now require that the load has the same
2275 vuse as the store. See following examples. */
2276 if (gimple_vuse (info->stmt) != load_vuse)
2277 return false;
2279 if (gimple_vuse (infof->stmt) != gimple_vuse (infof->ops[idx].stmt)
2280 || (infof != infol
2281 && gimple_vuse (infol->stmt) != gimple_vuse (infol->ops[idx].stmt)))
2282 return false;
2284 /* If the load is from the same location as the store, already
2285 the construction of the immediate chain info guarantees no intervening
2286 stores, so no further checks are needed. Example:
2287 _1 = s.a; _2 = _1 & -7; s.a = _2; _3 = s.b; _4 = _3 & -7; s.b = _4; */
2288 if (known_eq (info->ops[idx].bitpos, info->bitpos)
2289 && operand_equal_p (info->ops[idx].base_addr, base_addr, 0))
2290 return true;
2292 /* Otherwise, we need to punt if any of the loads can be clobbered by any
2293 of the stores in the group, or any other stores in between those.
2294 Previous calls to compatible_load_p ensured that for all the
2295 merged_store->stores IDX loads, no stmts starting with
2296 merged_store->first_stmt and ending right before merged_store->last_stmt
2297 clobbers those loads. */
2298 gimple *first = merged_store->first_stmt;
2299 gimple *last = merged_store->last_stmt;
2300 unsigned int i;
2301 store_immediate_info *infoc;
2302 /* The stores are sorted by increasing store bitpos, so if info->stmt store
2303 comes before the so far first load, we'll be changing
2304 merged_store->first_stmt. In that case we need to give up if
2305 any of the earlier processed loads clobber with the stmts in the new
2306 range. */
2307 if (info->order < merged_store->first_order)
2309 FOR_EACH_VEC_ELT (merged_store->stores, i, infoc)
2310 if (stmts_may_clobber_ref_p (info->stmt, first, infoc->ops[idx].val))
2311 return false;
2312 first = info->stmt;
2314 /* Similarly, we could change merged_store->last_stmt, so ensure
2315 in that case no stmts in the new range clobber any of the earlier
2316 processed loads. */
2317 else if (info->order > merged_store->last_order)
2319 FOR_EACH_VEC_ELT (merged_store->stores, i, infoc)
2320 if (stmts_may_clobber_ref_p (last, info->stmt, infoc->ops[idx].val))
2321 return false;
2322 last = info->stmt;
2324 /* And finally, we'd be adding a new load to the set, ensure it isn't
2325 clobbered in the new range. */
2326 if (stmts_may_clobber_ref_p (first, last, info->ops[idx].val))
2327 return false;
2329 /* Otherwise, we are looking for:
2330 _1 = s.a; _2 = _1 ^ 15; t.a = _2; _3 = s.b; _4 = _3 ^ 15; t.b = _4;
2332 _1 = s.a; t.a = _1; _2 = s.b; t.b = _2; */
2333 return true;
2336 /* Add all refs loaded to compute VAL to REFS vector. */
2338 void
2339 gather_bswap_load_refs (vec<tree> *refs, tree val)
2341 if (TREE_CODE (val) != SSA_NAME)
2342 return;
2344 gimple *stmt = SSA_NAME_DEF_STMT (val);
2345 if (!is_gimple_assign (stmt))
2346 return;
2348 if (gimple_assign_load_p (stmt))
2350 refs->safe_push (gimple_assign_rhs1 (stmt));
2351 return;
2354 switch (gimple_assign_rhs_class (stmt))
2356 case GIMPLE_BINARY_RHS:
2357 gather_bswap_load_refs (refs, gimple_assign_rhs2 (stmt));
2358 /* FALLTHRU */
2359 case GIMPLE_UNARY_RHS:
2360 gather_bswap_load_refs (refs, gimple_assign_rhs1 (stmt));
2361 break;
2362 default:
2363 gcc_unreachable ();
2367 /* Check if there are any stores in M_STORE_INFO after index I
2368 (where M_STORE_INFO must be sorted by sort_by_bitpos) that overlap
2369 a potential group ending with END that have their order
2370 smaller than LAST_ORDER. RHS_CODE is the kind of store in the
2371 group. Return true if there are no such stores.
2372 Consider:
2373 MEM[(long long int *)p_28] = 0;
2374 MEM[(long long int *)p_28 + 8B] = 0;
2375 MEM[(long long int *)p_28 + 16B] = 0;
2376 MEM[(long long int *)p_28 + 24B] = 0;
2377 _129 = (int) _130;
2378 MEM[(int *)p_28 + 8B] = _129;
2379 MEM[(int *)p_28].a = -1;
2380 We already have
2381 MEM[(long long int *)p_28] = 0;
2382 MEM[(int *)p_28].a = -1;
2383 stmts in the current group and need to consider if it is safe to
2384 add MEM[(long long int *)p_28 + 8B] = 0; store into the same group.
2385 There is an overlap between that store and the MEM[(int *)p_28 + 8B] = _129;
2386 store though, so if we add the MEM[(long long int *)p_28 + 8B] = 0;
2387 into the group and merging of those 3 stores is successful, merged
2388 stmts will be emitted at the latest store from that group, i.e.
2389 LAST_ORDER, which is the MEM[(int *)p_28].a = -1; store.
2390 The MEM[(int *)p_28 + 8B] = _129; store that originally follows
2391 the MEM[(long long int *)p_28 + 8B] = 0; would now be before it,
2392 so we need to refuse merging MEM[(long long int *)p_28 + 8B] = 0;
2393 into the group. That way it will be its own store group and will
2394 not be touched. If RHS_CODE is INTEGER_CST and there are overlapping
2395 INTEGER_CST stores, those are mergeable using merge_overlapping,
2396 so don't return false for those. */
2398 static bool
2399 check_no_overlap (vec<store_immediate_info *> m_store_info, unsigned int i,
2400 enum tree_code rhs_code, unsigned int last_order,
2401 unsigned HOST_WIDE_INT end)
2403 unsigned int len = m_store_info.length ();
2404 for (++i; i < len; ++i)
2406 store_immediate_info *info = m_store_info[i];
2407 if (info->bitpos >= end)
2408 break;
2409 if (info->order < last_order
2410 && (rhs_code != INTEGER_CST || info->rhs_code != INTEGER_CST))
2411 return false;
2413 return true;
2416 /* Return true if m_store_info[first] and at least one following store
2417 form a group which store try_size bitsize value which is byte swapped
2418 from a memory load or some value, or identity from some value.
2419 This uses the bswap pass APIs. */
2421 bool
2422 imm_store_chain_info::try_coalesce_bswap (merged_store_group *merged_store,
2423 unsigned int first,
2424 unsigned int try_size)
2426 unsigned int len = m_store_info.length (), last = first;
2427 unsigned HOST_WIDE_INT width = m_store_info[first]->bitsize;
2428 if (width >= try_size)
2429 return false;
2430 for (unsigned int i = first + 1; i < len; ++i)
2432 if (m_store_info[i]->bitpos != m_store_info[first]->bitpos + width
2433 || m_store_info[i]->ins_stmt == NULL)
2434 return false;
2435 width += m_store_info[i]->bitsize;
2436 if (width >= try_size)
2438 last = i;
2439 break;
2442 if (width != try_size)
2443 return false;
2445 bool allow_unaligned
2446 = !STRICT_ALIGNMENT && PARAM_VALUE (PARAM_STORE_MERGING_ALLOW_UNALIGNED);
2447 /* Punt if the combined store would not be aligned and we need alignment. */
2448 if (!allow_unaligned)
2450 unsigned int align = merged_store->align;
2451 unsigned HOST_WIDE_INT align_base = merged_store->align_base;
2452 for (unsigned int i = first + 1; i <= last; ++i)
2454 unsigned int this_align;
2455 unsigned HOST_WIDE_INT align_bitpos = 0;
2456 get_object_alignment_1 (gimple_assign_lhs (m_store_info[i]->stmt),
2457 &this_align, &align_bitpos);
2458 if (this_align > align)
2460 align = this_align;
2461 align_base = m_store_info[i]->bitpos - align_bitpos;
2464 unsigned HOST_WIDE_INT align_bitpos
2465 = (m_store_info[first]->bitpos - align_base) & (align - 1);
2466 if (align_bitpos)
2467 align = least_bit_hwi (align_bitpos);
2468 if (align < try_size)
2469 return false;
2472 tree type;
2473 switch (try_size)
2475 case 16: type = uint16_type_node; break;
2476 case 32: type = uint32_type_node; break;
2477 case 64: type = uint64_type_node; break;
2478 default: gcc_unreachable ();
2480 struct symbolic_number n;
2481 gimple *ins_stmt = NULL;
2482 int vuse_store = -1;
2483 unsigned int first_order = merged_store->first_order;
2484 unsigned int last_order = merged_store->last_order;
2485 gimple *first_stmt = merged_store->first_stmt;
2486 gimple *last_stmt = merged_store->last_stmt;
2487 unsigned HOST_WIDE_INT end = merged_store->start + merged_store->width;
2488 store_immediate_info *infof = m_store_info[first];
2490 for (unsigned int i = first; i <= last; ++i)
2492 store_immediate_info *info = m_store_info[i];
2493 struct symbolic_number this_n = info->n;
2494 this_n.type = type;
2495 if (!this_n.base_addr)
2496 this_n.range = try_size / BITS_PER_UNIT;
2497 else
2498 /* Update vuse in case it has changed by output_merged_stores. */
2499 this_n.vuse = gimple_vuse (info->ins_stmt);
2500 unsigned int bitpos = info->bitpos - infof->bitpos;
2501 if (!do_shift_rotate (LSHIFT_EXPR, &this_n,
2502 BYTES_BIG_ENDIAN
2503 ? try_size - info->bitsize - bitpos
2504 : bitpos))
2505 return false;
2506 if (this_n.base_addr && vuse_store)
2508 unsigned int j;
2509 for (j = first; j <= last; ++j)
2510 if (this_n.vuse == gimple_vuse (m_store_info[j]->stmt))
2511 break;
2512 if (j > last)
2514 if (vuse_store == 1)
2515 return false;
2516 vuse_store = 0;
2519 if (i == first)
2521 n = this_n;
2522 ins_stmt = info->ins_stmt;
2524 else
2526 if (n.base_addr && n.vuse != this_n.vuse)
2528 if (vuse_store == 0)
2529 return false;
2530 vuse_store = 1;
2532 if (info->order > last_order)
2534 last_order = info->order;
2535 last_stmt = info->stmt;
2537 else if (info->order < first_order)
2539 first_order = info->order;
2540 first_stmt = info->stmt;
2542 end = MAX (end, info->bitpos + info->bitsize);
2544 ins_stmt = perform_symbolic_merge (ins_stmt, &n, info->ins_stmt,
2545 &this_n, &n);
2546 if (ins_stmt == NULL)
2547 return false;
2551 uint64_t cmpxchg, cmpnop;
2552 find_bswap_or_nop_finalize (&n, &cmpxchg, &cmpnop);
2554 /* A complete byte swap should make the symbolic number to start with
2555 the largest digit in the highest order byte. Unchanged symbolic
2556 number indicates a read with same endianness as target architecture. */
2557 if (n.n != cmpnop && n.n != cmpxchg)
2558 return false;
2560 if (n.base_addr == NULL_TREE && !is_gimple_val (n.src))
2561 return false;
2563 if (!check_no_overlap (m_store_info, last, LROTATE_EXPR, last_order, end))
2564 return false;
2566 /* Don't handle memory copy this way if normal non-bswap processing
2567 would handle it too. */
2568 if (n.n == cmpnop && (unsigned) n.n_ops == last - first + 1)
2570 unsigned int i;
2571 for (i = first; i <= last; ++i)
2572 if (m_store_info[i]->rhs_code != MEM_REF)
2573 break;
2574 if (i == last + 1)
2575 return false;
2578 if (n.n == cmpxchg)
2579 switch (try_size)
2581 case 16:
2582 /* Will emit LROTATE_EXPR. */
2583 break;
2584 case 32:
2585 if (builtin_decl_explicit_p (BUILT_IN_BSWAP32)
2586 && optab_handler (bswap_optab, SImode) != CODE_FOR_nothing)
2587 break;
2588 return false;
2589 case 64:
2590 if (builtin_decl_explicit_p (BUILT_IN_BSWAP64)
2591 && optab_handler (bswap_optab, DImode) != CODE_FOR_nothing)
2592 break;
2593 return false;
2594 default:
2595 gcc_unreachable ();
2598 if (!allow_unaligned && n.base_addr)
2600 unsigned int align = get_object_alignment (n.src);
2601 if (align < try_size)
2602 return false;
2605 /* If each load has vuse of the corresponding store, need to verify
2606 the loads can be sunk right before the last store. */
2607 if (vuse_store == 1)
2609 auto_vec<tree, 64> refs;
2610 for (unsigned int i = first; i <= last; ++i)
2611 gather_bswap_load_refs (&refs,
2612 gimple_assign_rhs1 (m_store_info[i]->stmt));
2614 unsigned int i;
2615 tree ref;
2616 FOR_EACH_VEC_ELT (refs, i, ref)
2617 if (stmts_may_clobber_ref_p (first_stmt, last_stmt, ref))
2618 return false;
2619 n.vuse = NULL_TREE;
2622 infof->n = n;
2623 infof->ins_stmt = ins_stmt;
2624 for (unsigned int i = first; i <= last; ++i)
2626 m_store_info[i]->rhs_code = n.n == cmpxchg ? LROTATE_EXPR : NOP_EXPR;
2627 m_store_info[i]->ops[0].base_addr = NULL_TREE;
2628 m_store_info[i]->ops[1].base_addr = NULL_TREE;
2629 if (i != first)
2630 merged_store->merge_into (m_store_info[i]);
2633 return true;
2636 /* Go through the candidate stores recorded in m_store_info and merge them
2637 into merged_store_group objects recorded into m_merged_store_groups
2638 representing the widened stores. Return true if coalescing was successful
2639 and the number of widened stores is fewer than the original number
2640 of stores. */
2642 bool
2643 imm_store_chain_info::coalesce_immediate_stores ()
2645 /* Anything less can't be processed. */
2646 if (m_store_info.length () < 2)
2647 return false;
2649 if (dump_file && (dump_flags & TDF_DETAILS))
2650 fprintf (dump_file, "Attempting to coalesce %u stores in chain\n",
2651 m_store_info.length ());
2653 store_immediate_info *info;
2654 unsigned int i, ignore = 0;
2656 /* Order the stores by the bitposition they write to. */
2657 m_store_info.qsort (sort_by_bitpos);
2659 info = m_store_info[0];
2660 merged_store_group *merged_store = new merged_store_group (info);
2661 if (dump_file && (dump_flags & TDF_DETAILS))
2662 fputs ("New store group\n", dump_file);
2664 FOR_EACH_VEC_ELT (m_store_info, i, info)
2666 if (i <= ignore)
2667 goto done;
2669 /* First try to handle group of stores like:
2670 p[0] = data >> 24;
2671 p[1] = data >> 16;
2672 p[2] = data >> 8;
2673 p[3] = data;
2674 using the bswap framework. */
2675 if (info->bitpos == merged_store->start + merged_store->width
2676 && merged_store->stores.length () == 1
2677 && merged_store->stores[0]->ins_stmt != NULL
2678 && info->ins_stmt != NULL)
2680 unsigned int try_size;
2681 for (try_size = 64; try_size >= 16; try_size >>= 1)
2682 if (try_coalesce_bswap (merged_store, i - 1, try_size))
2683 break;
2685 if (try_size >= 16)
2687 ignore = i + merged_store->stores.length () - 1;
2688 m_merged_store_groups.safe_push (merged_store);
2689 if (ignore < m_store_info.length ())
2690 merged_store = new merged_store_group (m_store_info[ignore]);
2691 else
2692 merged_store = NULL;
2693 goto done;
2697 /* |---store 1---|
2698 |---store 2---|
2699 Overlapping stores. */
2700 if (IN_RANGE (info->bitpos, merged_store->start,
2701 merged_store->start + merged_store->width - 1))
2703 /* Only allow overlapping stores of constants. */
2704 if (info->rhs_code == INTEGER_CST
2705 && merged_store->stores[0]->rhs_code == INTEGER_CST)
2707 unsigned int last_order
2708 = MAX (merged_store->last_order, info->order);
2709 unsigned HOST_WIDE_INT end
2710 = MAX (merged_store->start + merged_store->width,
2711 info->bitpos + info->bitsize);
2712 if (check_no_overlap (m_store_info, i, INTEGER_CST,
2713 last_order, end))
2715 /* check_no_overlap call above made sure there are no
2716 overlapping stores with non-INTEGER_CST rhs_code
2717 in between the first and last of the stores we've
2718 just merged. If there are any INTEGER_CST rhs_code
2719 stores in between, we need to merge_overlapping them
2720 even if in the sort_by_bitpos order there are other
2721 overlapping stores in between. Keep those stores as is.
2722 Example:
2723 MEM[(int *)p_28] = 0;
2724 MEM[(char *)p_28 + 3B] = 1;
2725 MEM[(char *)p_28 + 1B] = 2;
2726 MEM[(char *)p_28 + 2B] = MEM[(char *)p_28 + 6B];
2727 We can't merge the zero store with the store of two and
2728 not merge anything else, because the store of one is
2729 in the original order in between those two, but in
2730 store_by_bitpos order it comes after the last store that
2731 we can't merge with them. We can merge the first 3 stores
2732 and keep the last store as is though. */
2733 unsigned int len = m_store_info.length (), k = i;
2734 for (unsigned int j = i + 1; j < len; ++j)
2736 store_immediate_info *info2 = m_store_info[j];
2737 if (info2->bitpos >= end)
2738 break;
2739 if (info2->order < last_order)
2741 if (info2->rhs_code != INTEGER_CST)
2743 /* Normally check_no_overlap makes sure this
2744 doesn't happen, but if end grows below, then
2745 we need to process more stores than
2746 check_no_overlap verified. Example:
2747 MEM[(int *)p_5] = 0;
2748 MEM[(short *)p_5 + 3B] = 1;
2749 MEM[(char *)p_5 + 4B] = _9;
2750 MEM[(char *)p_5 + 2B] = 2; */
2751 k = 0;
2752 break;
2754 k = j;
2755 end = MAX (end, info2->bitpos + info2->bitsize);
2759 if (k != 0)
2761 merged_store->merge_overlapping (info);
2763 for (unsigned int j = i + 1; j <= k; j++)
2765 store_immediate_info *info2 = m_store_info[j];
2766 gcc_assert (info2->bitpos < end);
2767 if (info2->order < last_order)
2769 gcc_assert (info2->rhs_code == INTEGER_CST);
2770 merged_store->merge_overlapping (info2);
2772 /* Other stores are kept and not merged in any
2773 way. */
2775 ignore = k;
2776 goto done;
2781 /* |---store 1---||---store 2---|
2782 This store is consecutive to the previous one.
2783 Merge it into the current store group. There can be gaps in between
2784 the stores, but there can't be gaps in between bitregions. */
2785 else if (info->bitregion_start <= merged_store->bitregion_end
2786 && merged_store->can_be_merged_into (info))
2788 store_immediate_info *infof = merged_store->stores[0];
2790 /* All the rhs_code ops that take 2 operands are commutative,
2791 swap the operands if it could make the operands compatible. */
2792 if (infof->ops[0].base_addr
2793 && infof->ops[1].base_addr
2794 && info->ops[0].base_addr
2795 && info->ops[1].base_addr
2796 && known_eq (info->ops[1].bitpos - infof->ops[0].bitpos,
2797 info->bitpos - infof->bitpos)
2798 && operand_equal_p (info->ops[1].base_addr,
2799 infof->ops[0].base_addr, 0))
2801 std::swap (info->ops[0], info->ops[1]);
2802 info->ops_swapped_p = true;
2804 if (check_no_overlap (m_store_info, i, info->rhs_code,
2805 MAX (merged_store->last_order, info->order),
2806 MAX (merged_store->start + merged_store->width,
2807 info->bitpos + info->bitsize)))
2809 /* Turn MEM_REF into BIT_INSERT_EXPR for bit-field stores. */
2810 if (info->rhs_code == MEM_REF && infof->rhs_code != MEM_REF)
2812 info->rhs_code = BIT_INSERT_EXPR;
2813 info->ops[0].val = gimple_assign_rhs1 (info->stmt);
2814 info->ops[0].base_addr = NULL_TREE;
2816 else if (infof->rhs_code == MEM_REF && info->rhs_code != MEM_REF)
2818 store_immediate_info *infoj;
2819 unsigned int j;
2820 FOR_EACH_VEC_ELT (merged_store->stores, j, infoj)
2822 infoj->rhs_code = BIT_INSERT_EXPR;
2823 infoj->ops[0].val = gimple_assign_rhs1 (infoj->stmt);
2824 infoj->ops[0].base_addr = NULL_TREE;
2827 if ((infof->ops[0].base_addr
2828 ? compatible_load_p (merged_store, info, base_addr, 0)
2829 : !info->ops[0].base_addr)
2830 && (infof->ops[1].base_addr
2831 ? compatible_load_p (merged_store, info, base_addr, 1)
2832 : !info->ops[1].base_addr))
2834 merged_store->merge_into (info);
2835 goto done;
2840 /* |---store 1---| <gap> |---store 2---|.
2841 Gap between stores or the rhs not compatible. Start a new group. */
2843 /* Try to apply all the stores recorded for the group to determine
2844 the bitpattern they write and discard it if that fails.
2845 This will also reject single-store groups. */
2846 if (merged_store->apply_stores ())
2847 m_merged_store_groups.safe_push (merged_store);
2848 else
2849 delete merged_store;
2851 merged_store = new merged_store_group (info);
2852 if (dump_file && (dump_flags & TDF_DETAILS))
2853 fputs ("New store group\n", dump_file);
2855 done:
2856 if (dump_file && (dump_flags & TDF_DETAILS))
2858 fprintf (dump_file, "Store %u:\nbitsize:" HOST_WIDE_INT_PRINT_DEC
2859 " bitpos:" HOST_WIDE_INT_PRINT_DEC " val:",
2860 i, info->bitsize, info->bitpos);
2861 print_generic_expr (dump_file, gimple_assign_rhs1 (info->stmt));
2862 fputc ('\n', dump_file);
2866 /* Record or discard the last store group. */
2867 if (merged_store)
2869 if (merged_store->apply_stores ())
2870 m_merged_store_groups.safe_push (merged_store);
2871 else
2872 delete merged_store;
2875 gcc_assert (m_merged_store_groups.length () <= m_store_info.length ());
2877 bool success
2878 = !m_merged_store_groups.is_empty ()
2879 && m_merged_store_groups.length () < m_store_info.length ();
2881 if (success && dump_file)
2882 fprintf (dump_file, "Coalescing successful!\nMerged into %u stores\n",
2883 m_merged_store_groups.length ());
2885 return success;
2888 /* Return the type to use for the merged stores or loads described by STMTS.
2889 This is needed to get the alias sets right. If IS_LOAD, look for rhs,
2890 otherwise lhs. Additionally set *CLIQUEP and *BASEP to MR_DEPENDENCE_*
2891 of the MEM_REFs if any. */
2893 static tree
2894 get_alias_type_for_stmts (vec<gimple *> &stmts, bool is_load,
2895 unsigned short *cliquep, unsigned short *basep)
2897 gimple *stmt;
2898 unsigned int i;
2899 tree type = NULL_TREE;
2900 tree ret = NULL_TREE;
2901 *cliquep = 0;
2902 *basep = 0;
2904 FOR_EACH_VEC_ELT (stmts, i, stmt)
2906 tree ref = is_load ? gimple_assign_rhs1 (stmt)
2907 : gimple_assign_lhs (stmt);
2908 tree type1 = reference_alias_ptr_type (ref);
2909 tree base = get_base_address (ref);
2911 if (i == 0)
2913 if (TREE_CODE (base) == MEM_REF)
2915 *cliquep = MR_DEPENDENCE_CLIQUE (base);
2916 *basep = MR_DEPENDENCE_BASE (base);
2918 ret = type = type1;
2919 continue;
2921 if (!alias_ptr_types_compatible_p (type, type1))
2922 ret = ptr_type_node;
2923 if (TREE_CODE (base) != MEM_REF
2924 || *cliquep != MR_DEPENDENCE_CLIQUE (base)
2925 || *basep != MR_DEPENDENCE_BASE (base))
2927 *cliquep = 0;
2928 *basep = 0;
2931 return ret;
2934 /* Return the location_t information we can find among the statements
2935 in STMTS. */
2937 static location_t
2938 get_location_for_stmts (vec<gimple *> &stmts)
2940 gimple *stmt;
2941 unsigned int i;
2943 FOR_EACH_VEC_ELT (stmts, i, stmt)
2944 if (gimple_has_location (stmt))
2945 return gimple_location (stmt);
2947 return UNKNOWN_LOCATION;
2950 /* Used to decribe a store resulting from splitting a wide store in smaller
2951 regularly-sized stores in split_group. */
2953 struct split_store
2955 unsigned HOST_WIDE_INT bytepos;
2956 unsigned HOST_WIDE_INT size;
2957 unsigned HOST_WIDE_INT align;
2958 auto_vec<store_immediate_info *> orig_stores;
2959 /* True if there is a single orig stmt covering the whole split store. */
2960 bool orig;
2961 split_store (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
2962 unsigned HOST_WIDE_INT);
2965 /* Simple constructor. */
2967 split_store::split_store (unsigned HOST_WIDE_INT bp,
2968 unsigned HOST_WIDE_INT sz,
2969 unsigned HOST_WIDE_INT al)
2970 : bytepos (bp), size (sz), align (al), orig (false)
2972 orig_stores.create (0);
2975 /* Record all stores in GROUP that write to the region starting at BITPOS and
2976 is of size BITSIZE. Record infos for such statements in STORES if
2977 non-NULL. The stores in GROUP must be sorted by bitposition. Return INFO
2978 if there is exactly one original store in the range. */
2980 static store_immediate_info *
2981 find_constituent_stores (struct merged_store_group *group,
2982 vec<store_immediate_info *> *stores,
2983 unsigned int *first,
2984 unsigned HOST_WIDE_INT bitpos,
2985 unsigned HOST_WIDE_INT bitsize)
2987 store_immediate_info *info, *ret = NULL;
2988 unsigned int i;
2989 bool second = false;
2990 bool update_first = true;
2991 unsigned HOST_WIDE_INT end = bitpos + bitsize;
2992 for (i = *first; group->stores.iterate (i, &info); ++i)
2994 unsigned HOST_WIDE_INT stmt_start = info->bitpos;
2995 unsigned HOST_WIDE_INT stmt_end = stmt_start + info->bitsize;
2996 if (stmt_end <= bitpos)
2998 /* BITPOS passed to this function never decreases from within the
2999 same split_group call, so optimize and don't scan info records
3000 which are known to end before or at BITPOS next time.
3001 Only do it if all stores before this one also pass this. */
3002 if (update_first)
3003 *first = i + 1;
3004 continue;
3006 else
3007 update_first = false;
3009 /* The stores in GROUP are ordered by bitposition so if we're past
3010 the region for this group return early. */
3011 if (stmt_start >= end)
3012 return ret;
3014 if (stores)
3016 stores->safe_push (info);
3017 if (ret)
3019 ret = NULL;
3020 second = true;
3023 else if (ret)
3024 return NULL;
3025 if (!second)
3026 ret = info;
3028 return ret;
3031 /* Return how many SSA_NAMEs used to compute value to store in the INFO
3032 store have multiple uses. If any SSA_NAME has multiple uses, also
3033 count statements needed to compute it. */
3035 static unsigned
3036 count_multiple_uses (store_immediate_info *info)
3038 gimple *stmt = info->stmt;
3039 unsigned ret = 0;
3040 switch (info->rhs_code)
3042 case INTEGER_CST:
3043 return 0;
3044 case BIT_AND_EXPR:
3045 case BIT_IOR_EXPR:
3046 case BIT_XOR_EXPR:
3047 if (info->bit_not_p)
3049 if (!has_single_use (gimple_assign_rhs1 (stmt)))
3050 ret = 1; /* Fall through below to return
3051 the BIT_NOT_EXPR stmt and then
3052 BIT_{AND,IOR,XOR}_EXPR and anything it
3053 uses. */
3054 else
3055 /* stmt is after this the BIT_NOT_EXPR. */
3056 stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
3058 if (!has_single_use (gimple_assign_rhs1 (stmt)))
3060 ret += 1 + info->ops[0].bit_not_p;
3061 if (info->ops[1].base_addr)
3062 ret += 1 + info->ops[1].bit_not_p;
3063 return ret + 1;
3065 stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
3066 /* stmt is now the BIT_*_EXPR. */
3067 if (!has_single_use (gimple_assign_rhs1 (stmt)))
3068 ret += 1 + info->ops[info->ops_swapped_p].bit_not_p;
3069 else if (info->ops[info->ops_swapped_p].bit_not_p)
3071 gimple *stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
3072 if (!has_single_use (gimple_assign_rhs1 (stmt2)))
3073 ++ret;
3075 if (info->ops[1].base_addr == NULL_TREE)
3077 gcc_checking_assert (!info->ops_swapped_p);
3078 return ret;
3080 if (!has_single_use (gimple_assign_rhs2 (stmt)))
3081 ret += 1 + info->ops[1 - info->ops_swapped_p].bit_not_p;
3082 else if (info->ops[1 - info->ops_swapped_p].bit_not_p)
3084 gimple *stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt));
3085 if (!has_single_use (gimple_assign_rhs1 (stmt2)))
3086 ++ret;
3088 return ret;
3089 case MEM_REF:
3090 if (!has_single_use (gimple_assign_rhs1 (stmt)))
3091 return 1 + info->ops[0].bit_not_p;
3092 else if (info->ops[0].bit_not_p)
3094 stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
3095 if (!has_single_use (gimple_assign_rhs1 (stmt)))
3096 return 1;
3098 return 0;
3099 case BIT_INSERT_EXPR:
3100 return has_single_use (gimple_assign_rhs1 (stmt)) ? 0 : 1;
3101 default:
3102 gcc_unreachable ();
3106 /* Split a merged store described by GROUP by populating the SPLIT_STORES
3107 vector (if non-NULL) with split_store structs describing the byte offset
3108 (from the base), the bit size and alignment of each store as well as the
3109 original statements involved in each such split group.
3110 This is to separate the splitting strategy from the statement
3111 building/emission/linking done in output_merged_store.
3112 Return number of new stores.
3113 If ALLOW_UNALIGNED_STORE is false, then all stores must be aligned.
3114 If ALLOW_UNALIGNED_LOAD is false, then all loads must be aligned.
3115 If SPLIT_STORES is NULL, it is just a dry run to count number of
3116 new stores. */
3118 static unsigned int
3119 split_group (merged_store_group *group, bool allow_unaligned_store,
3120 bool allow_unaligned_load,
3121 vec<struct split_store *> *split_stores,
3122 unsigned *total_orig,
3123 unsigned *total_new)
3125 unsigned HOST_WIDE_INT pos = group->bitregion_start;
3126 unsigned HOST_WIDE_INT size = group->bitregion_end - pos;
3127 unsigned HOST_WIDE_INT bytepos = pos / BITS_PER_UNIT;
3128 unsigned HOST_WIDE_INT group_align = group->align;
3129 unsigned HOST_WIDE_INT align_base = group->align_base;
3130 unsigned HOST_WIDE_INT group_load_align = group_align;
3131 bool any_orig = false;
3133 gcc_assert ((size % BITS_PER_UNIT == 0) && (pos % BITS_PER_UNIT == 0));
3135 if (group->stores[0]->rhs_code == LROTATE_EXPR
3136 || group->stores[0]->rhs_code == NOP_EXPR)
3138 /* For bswap framework using sets of stores, all the checking
3139 has been done earlier in try_coalesce_bswap and needs to be
3140 emitted as a single store. */
3141 if (total_orig)
3143 /* Avoid the old/new stmt count heuristics. It should be
3144 always beneficial. */
3145 total_new[0] = 1;
3146 total_orig[0] = 2;
3149 if (split_stores)
3151 unsigned HOST_WIDE_INT align_bitpos
3152 = (group->start - align_base) & (group_align - 1);
3153 unsigned HOST_WIDE_INT align = group_align;
3154 if (align_bitpos)
3155 align = least_bit_hwi (align_bitpos);
3156 bytepos = group->start / BITS_PER_UNIT;
3157 struct split_store *store
3158 = new split_store (bytepos, group->width, align);
3159 unsigned int first = 0;
3160 find_constituent_stores (group, &store->orig_stores,
3161 &first, group->start, group->width);
3162 split_stores->safe_push (store);
3165 return 1;
3168 unsigned int ret = 0, first = 0;
3169 unsigned HOST_WIDE_INT try_pos = bytepos;
3171 if (total_orig)
3173 unsigned int i;
3174 store_immediate_info *info = group->stores[0];
3176 total_new[0] = 0;
3177 total_orig[0] = 1; /* The orig store. */
3178 info = group->stores[0];
3179 if (info->ops[0].base_addr)
3180 total_orig[0]++;
3181 if (info->ops[1].base_addr)
3182 total_orig[0]++;
3183 switch (info->rhs_code)
3185 case BIT_AND_EXPR:
3186 case BIT_IOR_EXPR:
3187 case BIT_XOR_EXPR:
3188 total_orig[0]++; /* The orig BIT_*_EXPR stmt. */
3189 break;
3190 default:
3191 break;
3193 total_orig[0] *= group->stores.length ();
3195 FOR_EACH_VEC_ELT (group->stores, i, info)
3197 total_new[0] += count_multiple_uses (info);
3198 total_orig[0] += (info->bit_not_p
3199 + info->ops[0].bit_not_p
3200 + info->ops[1].bit_not_p);
3204 if (!allow_unaligned_load)
3205 for (int i = 0; i < 2; ++i)
3206 if (group->load_align[i])
3207 group_load_align = MIN (group_load_align, group->load_align[i]);
3209 while (size > 0)
3211 if ((allow_unaligned_store || group_align <= BITS_PER_UNIT)
3212 && group->mask[try_pos - bytepos] == (unsigned char) ~0U)
3214 /* Skip padding bytes. */
3215 ++try_pos;
3216 size -= BITS_PER_UNIT;
3217 continue;
3220 unsigned HOST_WIDE_INT try_bitpos = try_pos * BITS_PER_UNIT;
3221 unsigned int try_size = MAX_STORE_BITSIZE, nonmasked;
3222 unsigned HOST_WIDE_INT align_bitpos
3223 = (try_bitpos - align_base) & (group_align - 1);
3224 unsigned HOST_WIDE_INT align = group_align;
3225 if (align_bitpos)
3226 align = least_bit_hwi (align_bitpos);
3227 if (!allow_unaligned_store)
3228 try_size = MIN (try_size, align);
3229 if (!allow_unaligned_load)
3231 /* If we can't do or don't want to do unaligned stores
3232 as well as loads, we need to take the loads into account
3233 as well. */
3234 unsigned HOST_WIDE_INT load_align = group_load_align;
3235 align_bitpos = (try_bitpos - align_base) & (load_align - 1);
3236 if (align_bitpos)
3237 load_align = least_bit_hwi (align_bitpos);
3238 for (int i = 0; i < 2; ++i)
3239 if (group->load_align[i])
3241 align_bitpos
3242 = known_alignment (try_bitpos
3243 - group->stores[0]->bitpos
3244 + group->stores[0]->ops[i].bitpos
3245 - group->load_align_base[i]);
3246 if (align_bitpos & (group_load_align - 1))
3248 unsigned HOST_WIDE_INT a = least_bit_hwi (align_bitpos);
3249 load_align = MIN (load_align, a);
3252 try_size = MIN (try_size, load_align);
3254 store_immediate_info *info
3255 = find_constituent_stores (group, NULL, &first, try_bitpos, try_size);
3256 if (info)
3258 /* If there is just one original statement for the range, see if
3259 we can just reuse the original store which could be even larger
3260 than try_size. */
3261 unsigned HOST_WIDE_INT stmt_end
3262 = ROUND_UP (info->bitpos + info->bitsize, BITS_PER_UNIT);
3263 info = find_constituent_stores (group, NULL, &first, try_bitpos,
3264 stmt_end - try_bitpos);
3265 if (info && info->bitpos >= try_bitpos)
3267 try_size = stmt_end - try_bitpos;
3268 goto found;
3272 /* Approximate store bitsize for the case when there are no padding
3273 bits. */
3274 while (try_size > size)
3275 try_size /= 2;
3276 /* Now look for whole padding bytes at the end of that bitsize. */
3277 for (nonmasked = try_size / BITS_PER_UNIT; nonmasked > 0; --nonmasked)
3278 if (group->mask[try_pos - bytepos + nonmasked - 1]
3279 != (unsigned char) ~0U)
3280 break;
3281 if (nonmasked == 0)
3283 /* If entire try_size range is padding, skip it. */
3284 try_pos += try_size / BITS_PER_UNIT;
3285 size -= try_size;
3286 continue;
3288 /* Otherwise try to decrease try_size if second half, last 3 quarters
3289 etc. are padding. */
3290 nonmasked *= BITS_PER_UNIT;
3291 while (nonmasked <= try_size / 2)
3292 try_size /= 2;
3293 if (!allow_unaligned_store && group_align > BITS_PER_UNIT)
3295 /* Now look for whole padding bytes at the start of that bitsize. */
3296 unsigned int try_bytesize = try_size / BITS_PER_UNIT, masked;
3297 for (masked = 0; masked < try_bytesize; ++masked)
3298 if (group->mask[try_pos - bytepos + masked] != (unsigned char) ~0U)
3299 break;
3300 masked *= BITS_PER_UNIT;
3301 gcc_assert (masked < try_size);
3302 if (masked >= try_size / 2)
3304 while (masked >= try_size / 2)
3306 try_size /= 2;
3307 try_pos += try_size / BITS_PER_UNIT;
3308 size -= try_size;
3309 masked -= try_size;
3311 /* Need to recompute the alignment, so just retry at the new
3312 position. */
3313 continue;
3317 found:
3318 ++ret;
3320 if (split_stores)
3322 struct split_store *store
3323 = new split_store (try_pos, try_size, align);
3324 info = find_constituent_stores (group, &store->orig_stores,
3325 &first, try_bitpos, try_size);
3326 if (info
3327 && info->bitpos >= try_bitpos
3328 && info->bitpos + info->bitsize <= try_bitpos + try_size)
3330 store->orig = true;
3331 any_orig = true;
3333 split_stores->safe_push (store);
3336 try_pos += try_size / BITS_PER_UNIT;
3337 size -= try_size;
3340 if (total_orig)
3342 unsigned int i;
3343 struct split_store *store;
3344 /* If we are reusing some original stores and any of the
3345 original SSA_NAMEs had multiple uses, we need to subtract
3346 those now before we add the new ones. */
3347 if (total_new[0] && any_orig)
3349 FOR_EACH_VEC_ELT (*split_stores, i, store)
3350 if (store->orig)
3351 total_new[0] -= count_multiple_uses (store->orig_stores[0]);
3353 total_new[0] += ret; /* The new store. */
3354 store_immediate_info *info = group->stores[0];
3355 if (info->ops[0].base_addr)
3356 total_new[0] += ret;
3357 if (info->ops[1].base_addr)
3358 total_new[0] += ret;
3359 switch (info->rhs_code)
3361 case BIT_AND_EXPR:
3362 case BIT_IOR_EXPR:
3363 case BIT_XOR_EXPR:
3364 total_new[0] += ret; /* The new BIT_*_EXPR stmt. */
3365 break;
3366 default:
3367 break;
3369 FOR_EACH_VEC_ELT (*split_stores, i, store)
3371 unsigned int j;
3372 bool bit_not_p[3] = { false, false, false };
3373 /* If all orig_stores have certain bit_not_p set, then
3374 we'd use a BIT_NOT_EXPR stmt and need to account for it.
3375 If some orig_stores have certain bit_not_p set, then
3376 we'd use a BIT_XOR_EXPR with a mask and need to account for
3377 it. */
3378 FOR_EACH_VEC_ELT (store->orig_stores, j, info)
3380 if (info->ops[0].bit_not_p)
3381 bit_not_p[0] = true;
3382 if (info->ops[1].bit_not_p)
3383 bit_not_p[1] = true;
3384 if (info->bit_not_p)
3385 bit_not_p[2] = true;
3387 total_new[0] += bit_not_p[0] + bit_not_p[1] + bit_not_p[2];
3392 return ret;
3395 /* Return the operation through which the operand IDX (if < 2) or
3396 result (IDX == 2) should be inverted. If NOP_EXPR, no inversion
3397 is done, if BIT_NOT_EXPR, all bits are inverted, if BIT_XOR_EXPR,
3398 the bits should be xored with mask. */
3400 static enum tree_code
3401 invert_op (split_store *split_store, int idx, tree int_type, tree &mask)
3403 unsigned int i;
3404 store_immediate_info *info;
3405 unsigned int cnt = 0;
3406 bool any_paddings = false;
3407 FOR_EACH_VEC_ELT (split_store->orig_stores, i, info)
3409 bool bit_not_p = idx < 2 ? info->ops[idx].bit_not_p : info->bit_not_p;
3410 if (bit_not_p)
3412 ++cnt;
3413 tree lhs = gimple_assign_lhs (info->stmt);
3414 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
3415 && TYPE_PRECISION (TREE_TYPE (lhs)) < info->bitsize)
3416 any_paddings = true;
3419 mask = NULL_TREE;
3420 if (cnt == 0)
3421 return NOP_EXPR;
3422 if (cnt == split_store->orig_stores.length () && !any_paddings)
3423 return BIT_NOT_EXPR;
3425 unsigned HOST_WIDE_INT try_bitpos = split_store->bytepos * BITS_PER_UNIT;
3426 unsigned buf_size = split_store->size / BITS_PER_UNIT;
3427 unsigned char *buf
3428 = XALLOCAVEC (unsigned char, buf_size);
3429 memset (buf, ~0U, buf_size);
3430 FOR_EACH_VEC_ELT (split_store->orig_stores, i, info)
3432 bool bit_not_p = idx < 2 ? info->ops[idx].bit_not_p : info->bit_not_p;
3433 if (!bit_not_p)
3434 continue;
3435 /* Clear regions with bit_not_p and invert afterwards, rather than
3436 clear regions with !bit_not_p, so that gaps in between stores aren't
3437 set in the mask. */
3438 unsigned HOST_WIDE_INT bitsize = info->bitsize;
3439 unsigned HOST_WIDE_INT prec = bitsize;
3440 unsigned int pos_in_buffer = 0;
3441 if (any_paddings)
3443 tree lhs = gimple_assign_lhs (info->stmt);
3444 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
3445 && TYPE_PRECISION (TREE_TYPE (lhs)) < bitsize)
3446 prec = TYPE_PRECISION (TREE_TYPE (lhs));
3448 if (info->bitpos < try_bitpos)
3450 gcc_assert (info->bitpos + bitsize > try_bitpos);
3451 if (!BYTES_BIG_ENDIAN)
3453 if (prec <= try_bitpos - info->bitpos)
3454 continue;
3455 prec -= try_bitpos - info->bitpos;
3457 bitsize -= try_bitpos - info->bitpos;
3458 if (BYTES_BIG_ENDIAN && prec > bitsize)
3459 prec = bitsize;
3461 else
3462 pos_in_buffer = info->bitpos - try_bitpos;
3463 if (prec < bitsize)
3465 /* If this is a bool inversion, invert just the least significant
3466 prec bits rather than all bits of it. */
3467 if (BYTES_BIG_ENDIAN)
3469 pos_in_buffer += bitsize - prec;
3470 if (pos_in_buffer >= split_store->size)
3471 continue;
3473 bitsize = prec;
3475 if (pos_in_buffer + bitsize > split_store->size)
3476 bitsize = split_store->size - pos_in_buffer;
3477 unsigned char *p = buf + (pos_in_buffer / BITS_PER_UNIT);
3478 if (BYTES_BIG_ENDIAN)
3479 clear_bit_region_be (p, (BITS_PER_UNIT - 1
3480 - (pos_in_buffer % BITS_PER_UNIT)), bitsize);
3481 else
3482 clear_bit_region (p, pos_in_buffer % BITS_PER_UNIT, bitsize);
3484 for (unsigned int i = 0; i < buf_size; ++i)
3485 buf[i] = ~buf[i];
3486 mask = native_interpret_expr (int_type, buf, buf_size);
3487 return BIT_XOR_EXPR;
3490 /* Given a merged store group GROUP output the widened version of it.
3491 The store chain is against the base object BASE.
3492 Try store sizes of at most MAX_STORE_BITSIZE bits wide and don't output
3493 unaligned stores for STRICT_ALIGNMENT targets or if it's too expensive.
3494 Make sure that the number of statements output is less than the number of
3495 original statements. If a better sequence is possible emit it and
3496 return true. */
3498 bool
3499 imm_store_chain_info::output_merged_store (merged_store_group *group)
3501 split_store *split_store;
3502 unsigned int i;
3503 unsigned HOST_WIDE_INT start_byte_pos
3504 = group->bitregion_start / BITS_PER_UNIT;
3506 unsigned int orig_num_stmts = group->stores.length ();
3507 if (orig_num_stmts < 2)
3508 return false;
3510 auto_vec<struct split_store *, 32> split_stores;
3511 bool allow_unaligned_store
3512 = !STRICT_ALIGNMENT && PARAM_VALUE (PARAM_STORE_MERGING_ALLOW_UNALIGNED);
3513 bool allow_unaligned_load = allow_unaligned_store;
3514 if (allow_unaligned_store)
3516 /* If unaligned stores are allowed, see how many stores we'd emit
3517 for unaligned and how many stores we'd emit for aligned stores.
3518 Only use unaligned stores if it allows fewer stores than aligned. */
3519 unsigned aligned_cnt
3520 = split_group (group, false, allow_unaligned_load, NULL, NULL, NULL);
3521 unsigned unaligned_cnt
3522 = split_group (group, true, allow_unaligned_load, NULL, NULL, NULL);
3523 if (aligned_cnt <= unaligned_cnt)
3524 allow_unaligned_store = false;
3526 unsigned total_orig, total_new;
3527 split_group (group, allow_unaligned_store, allow_unaligned_load,
3528 &split_stores, &total_orig, &total_new);
3530 if (split_stores.length () >= orig_num_stmts)
3532 /* We didn't manage to reduce the number of statements. Bail out. */
3533 if (dump_file && (dump_flags & TDF_DETAILS))
3534 fprintf (dump_file, "Exceeded original number of stmts (%u)."
3535 " Not profitable to emit new sequence.\n",
3536 orig_num_stmts);
3537 FOR_EACH_VEC_ELT (split_stores, i, split_store)
3538 delete split_store;
3539 return false;
3541 if (total_orig <= total_new)
3543 /* If number of estimated new statements is above estimated original
3544 statements, bail out too. */
3545 if (dump_file && (dump_flags & TDF_DETAILS))
3546 fprintf (dump_file, "Estimated number of original stmts (%u)"
3547 " not larger than estimated number of new"
3548 " stmts (%u).\n",
3549 total_orig, total_new);
3550 FOR_EACH_VEC_ELT (split_stores, i, split_store)
3551 delete split_store;
3552 return false;
3555 gimple_stmt_iterator last_gsi = gsi_for_stmt (group->last_stmt);
3556 gimple_seq seq = NULL;
3557 tree last_vdef, new_vuse;
3558 last_vdef = gimple_vdef (group->last_stmt);
3559 new_vuse = gimple_vuse (group->last_stmt);
3560 tree bswap_res = NULL_TREE;
3562 if (group->stores[0]->rhs_code == LROTATE_EXPR
3563 || group->stores[0]->rhs_code == NOP_EXPR)
3565 tree fndecl = NULL_TREE, bswap_type = NULL_TREE, load_type;
3566 gimple *ins_stmt = group->stores[0]->ins_stmt;
3567 struct symbolic_number *n = &group->stores[0]->n;
3568 bool bswap = group->stores[0]->rhs_code == LROTATE_EXPR;
3570 switch (n->range)
3572 case 16:
3573 load_type = bswap_type = uint16_type_node;
3574 break;
3575 case 32:
3576 load_type = uint32_type_node;
3577 if (bswap)
3579 fndecl = builtin_decl_explicit (BUILT_IN_BSWAP32);
3580 bswap_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
3582 break;
3583 case 64:
3584 load_type = uint64_type_node;
3585 if (bswap)
3587 fndecl = builtin_decl_explicit (BUILT_IN_BSWAP64);
3588 bswap_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
3590 break;
3591 default:
3592 gcc_unreachable ();
3595 /* If the loads have each vuse of the corresponding store,
3596 we've checked the aliasing already in try_coalesce_bswap and
3597 we want to sink the need load into seq. So need to use new_vuse
3598 on the load. */
3599 if (n->base_addr)
3601 if (n->vuse == NULL)
3603 n->vuse = new_vuse;
3604 ins_stmt = NULL;
3606 else
3607 /* Update vuse in case it has changed by output_merged_stores. */
3608 n->vuse = gimple_vuse (ins_stmt);
3610 bswap_res = bswap_replace (gsi_start (seq), ins_stmt, fndecl,
3611 bswap_type, load_type, n, bswap);
3612 gcc_assert (bswap_res);
3615 gimple *stmt = NULL;
3616 auto_vec<gimple *, 32> orig_stmts;
3617 gimple_seq this_seq;
3618 tree addr = force_gimple_operand_1 (unshare_expr (base_addr), &this_seq,
3619 is_gimple_mem_ref_addr, NULL_TREE);
3620 gimple_seq_add_seq_without_update (&seq, this_seq);
3622 tree load_addr[2] = { NULL_TREE, NULL_TREE };
3623 gimple_seq load_seq[2] = { NULL, NULL };
3624 gimple_stmt_iterator load_gsi[2] = { gsi_none (), gsi_none () };
3625 for (int j = 0; j < 2; ++j)
3627 store_operand_info &op = group->stores[0]->ops[j];
3628 if (op.base_addr == NULL_TREE)
3629 continue;
3631 store_immediate_info *infol = group->stores.last ();
3632 if (gimple_vuse (op.stmt) == gimple_vuse (infol->ops[j].stmt))
3634 /* We can't pick the location randomly; while we've verified
3635 all the loads have the same vuse, they can be still in different
3636 basic blocks and we need to pick the one from the last bb:
3637 int x = q[0];
3638 if (x == N) return;
3639 int y = q[1];
3640 p[0] = x;
3641 p[1] = y;
3642 otherwise if we put the wider load at the q[0] load, we might
3643 segfault if q[1] is not mapped. */
3644 basic_block bb = gimple_bb (op.stmt);
3645 gimple *ostmt = op.stmt;
3646 store_immediate_info *info;
3647 FOR_EACH_VEC_ELT (group->stores, i, info)
3649 gimple *tstmt = info->ops[j].stmt;
3650 basic_block tbb = gimple_bb (tstmt);
3651 if (dominated_by_p (CDI_DOMINATORS, tbb, bb))
3653 ostmt = tstmt;
3654 bb = tbb;
3657 load_gsi[j] = gsi_for_stmt (ostmt);
3658 load_addr[j]
3659 = force_gimple_operand_1 (unshare_expr (op.base_addr),
3660 &load_seq[j], is_gimple_mem_ref_addr,
3661 NULL_TREE);
3663 else if (operand_equal_p (base_addr, op.base_addr, 0))
3664 load_addr[j] = addr;
3665 else
3667 load_addr[j]
3668 = force_gimple_operand_1 (unshare_expr (op.base_addr),
3669 &this_seq, is_gimple_mem_ref_addr,
3670 NULL_TREE);
3671 gimple_seq_add_seq_without_update (&seq, this_seq);
3675 FOR_EACH_VEC_ELT (split_stores, i, split_store)
3677 unsigned HOST_WIDE_INT try_size = split_store->size;
3678 unsigned HOST_WIDE_INT try_pos = split_store->bytepos;
3679 unsigned HOST_WIDE_INT try_bitpos = try_pos * BITS_PER_UNIT;
3680 unsigned HOST_WIDE_INT align = split_store->align;
3681 tree dest, src;
3682 location_t loc;
3683 if (split_store->orig)
3685 /* If there is just a single constituent store which covers
3686 the whole area, just reuse the lhs and rhs. */
3687 gimple *orig_stmt = split_store->orig_stores[0]->stmt;
3688 dest = gimple_assign_lhs (orig_stmt);
3689 src = gimple_assign_rhs1 (orig_stmt);
3690 loc = gimple_location (orig_stmt);
3692 else
3694 store_immediate_info *info;
3695 unsigned short clique, base;
3696 unsigned int k;
3697 FOR_EACH_VEC_ELT (split_store->orig_stores, k, info)
3698 orig_stmts.safe_push (info->stmt);
3699 tree offset_type
3700 = get_alias_type_for_stmts (orig_stmts, false, &clique, &base);
3701 loc = get_location_for_stmts (orig_stmts);
3702 orig_stmts.truncate (0);
3704 tree int_type = build_nonstandard_integer_type (try_size, UNSIGNED);
3705 int_type = build_aligned_type (int_type, align);
3706 dest = fold_build2 (MEM_REF, int_type, addr,
3707 build_int_cst (offset_type, try_pos));
3708 if (TREE_CODE (dest) == MEM_REF)
3710 MR_DEPENDENCE_CLIQUE (dest) = clique;
3711 MR_DEPENDENCE_BASE (dest) = base;
3714 tree mask;
3715 if (bswap_res)
3716 mask = integer_zero_node;
3717 else
3718 mask = native_interpret_expr (int_type,
3719 group->mask + try_pos
3720 - start_byte_pos,
3721 group->buf_size);
3723 tree ops[2];
3724 for (int j = 0;
3725 j < 1 + (split_store->orig_stores[0]->ops[1].val != NULL_TREE);
3726 ++j)
3728 store_operand_info &op = split_store->orig_stores[0]->ops[j];
3729 if (bswap_res)
3730 ops[j] = bswap_res;
3731 else if (op.base_addr)
3733 FOR_EACH_VEC_ELT (split_store->orig_stores, k, info)
3734 orig_stmts.safe_push (info->ops[j].stmt);
3736 offset_type = get_alias_type_for_stmts (orig_stmts, true,
3737 &clique, &base);
3738 location_t load_loc = get_location_for_stmts (orig_stmts);
3739 orig_stmts.truncate (0);
3741 unsigned HOST_WIDE_INT load_align = group->load_align[j];
3742 unsigned HOST_WIDE_INT align_bitpos
3743 = known_alignment (try_bitpos
3744 - split_store->orig_stores[0]->bitpos
3745 + op.bitpos);
3746 if (align_bitpos & (load_align - 1))
3747 load_align = least_bit_hwi (align_bitpos);
3749 tree load_int_type
3750 = build_nonstandard_integer_type (try_size, UNSIGNED);
3751 load_int_type
3752 = build_aligned_type (load_int_type, load_align);
3754 poly_uint64 load_pos
3755 = exact_div (try_bitpos
3756 - split_store->orig_stores[0]->bitpos
3757 + op.bitpos,
3758 BITS_PER_UNIT);
3759 ops[j] = fold_build2 (MEM_REF, load_int_type, load_addr[j],
3760 build_int_cst (offset_type, load_pos));
3761 if (TREE_CODE (ops[j]) == MEM_REF)
3763 MR_DEPENDENCE_CLIQUE (ops[j]) = clique;
3764 MR_DEPENDENCE_BASE (ops[j]) = base;
3766 if (!integer_zerop (mask))
3767 /* The load might load some bits (that will be masked off
3768 later on) uninitialized, avoid -W*uninitialized
3769 warnings in that case. */
3770 TREE_NO_WARNING (ops[j]) = 1;
3772 stmt = gimple_build_assign (make_ssa_name (int_type),
3773 ops[j]);
3774 gimple_set_location (stmt, load_loc);
3775 if (gsi_bb (load_gsi[j]))
3777 gimple_set_vuse (stmt, gimple_vuse (op.stmt));
3778 gimple_seq_add_stmt_without_update (&load_seq[j], stmt);
3780 else
3782 gimple_set_vuse (stmt, new_vuse);
3783 gimple_seq_add_stmt_without_update (&seq, stmt);
3785 ops[j] = gimple_assign_lhs (stmt);
3786 tree xor_mask;
3787 enum tree_code inv_op
3788 = invert_op (split_store, j, int_type, xor_mask);
3789 if (inv_op != NOP_EXPR)
3791 stmt = gimple_build_assign (make_ssa_name (int_type),
3792 inv_op, ops[j], xor_mask);
3793 gimple_set_location (stmt, load_loc);
3794 ops[j] = gimple_assign_lhs (stmt);
3796 if (gsi_bb (load_gsi[j]))
3797 gimple_seq_add_stmt_without_update (&load_seq[j],
3798 stmt);
3799 else
3800 gimple_seq_add_stmt_without_update (&seq, stmt);
3803 else
3804 ops[j] = native_interpret_expr (int_type,
3805 group->val + try_pos
3806 - start_byte_pos,
3807 group->buf_size);
3810 switch (split_store->orig_stores[0]->rhs_code)
3812 case BIT_AND_EXPR:
3813 case BIT_IOR_EXPR:
3814 case BIT_XOR_EXPR:
3815 FOR_EACH_VEC_ELT (split_store->orig_stores, k, info)
3817 tree rhs1 = gimple_assign_rhs1 (info->stmt);
3818 orig_stmts.safe_push (SSA_NAME_DEF_STMT (rhs1));
3820 location_t bit_loc;
3821 bit_loc = get_location_for_stmts (orig_stmts);
3822 orig_stmts.truncate (0);
3824 stmt
3825 = gimple_build_assign (make_ssa_name (int_type),
3826 split_store->orig_stores[0]->rhs_code,
3827 ops[0], ops[1]);
3828 gimple_set_location (stmt, bit_loc);
3829 /* If there is just one load and there is a separate
3830 load_seq[0], emit the bitwise op right after it. */
3831 if (load_addr[1] == NULL_TREE && gsi_bb (load_gsi[0]))
3832 gimple_seq_add_stmt_without_update (&load_seq[0], stmt);
3833 /* Otherwise, if at least one load is in seq, we need to
3834 emit the bitwise op right before the store. If there
3835 are two loads and are emitted somewhere else, it would
3836 be better to emit the bitwise op as early as possible;
3837 we don't track where that would be possible right now
3838 though. */
3839 else
3840 gimple_seq_add_stmt_without_update (&seq, stmt);
3841 src = gimple_assign_lhs (stmt);
3842 tree xor_mask;
3843 enum tree_code inv_op;
3844 inv_op = invert_op (split_store, 2, int_type, xor_mask);
3845 if (inv_op != NOP_EXPR)
3847 stmt = gimple_build_assign (make_ssa_name (int_type),
3848 inv_op, src, xor_mask);
3849 gimple_set_location (stmt, bit_loc);
3850 if (load_addr[1] == NULL_TREE && gsi_bb (load_gsi[0]))
3851 gimple_seq_add_stmt_without_update (&load_seq[0], stmt);
3852 else
3853 gimple_seq_add_stmt_without_update (&seq, stmt);
3854 src = gimple_assign_lhs (stmt);
3856 break;
3857 case LROTATE_EXPR:
3858 case NOP_EXPR:
3859 src = ops[0];
3860 if (!is_gimple_val (src))
3862 stmt = gimple_build_assign (make_ssa_name (TREE_TYPE (src)),
3863 src);
3864 gimple_seq_add_stmt_without_update (&seq, stmt);
3865 src = gimple_assign_lhs (stmt);
3867 if (!useless_type_conversion_p (int_type, TREE_TYPE (src)))
3869 stmt = gimple_build_assign (make_ssa_name (int_type),
3870 NOP_EXPR, src);
3871 gimple_seq_add_stmt_without_update (&seq, stmt);
3872 src = gimple_assign_lhs (stmt);
3874 inv_op = invert_op (split_store, 2, int_type, xor_mask);
3875 if (inv_op != NOP_EXPR)
3877 stmt = gimple_build_assign (make_ssa_name (int_type),
3878 inv_op, src, xor_mask);
3879 gimple_set_location (stmt, loc);
3880 gimple_seq_add_stmt_without_update (&seq, stmt);
3881 src = gimple_assign_lhs (stmt);
3883 break;
3884 default:
3885 src = ops[0];
3886 break;
3889 /* If bit insertion is required, we use the source as an accumulator
3890 into which the successive bit-field values are manually inserted.
3891 FIXME: perhaps use BIT_INSERT_EXPR instead in some cases? */
3892 if (group->bit_insertion)
3893 FOR_EACH_VEC_ELT (split_store->orig_stores, k, info)
3894 if (info->rhs_code == BIT_INSERT_EXPR
3895 && info->bitpos < try_bitpos + try_size
3896 && info->bitpos + info->bitsize > try_bitpos)
3898 /* Mask, truncate, convert to final type, shift and ior into
3899 the accumulator. Note that every step can be a no-op. */
3900 const HOST_WIDE_INT start_gap = info->bitpos - try_bitpos;
3901 const HOST_WIDE_INT end_gap
3902 = (try_bitpos + try_size) - (info->bitpos + info->bitsize);
3903 tree tem = info->ops[0].val;
3904 if (TYPE_PRECISION (TREE_TYPE (tem)) <= info->bitsize)
3906 tree bitfield_type
3907 = build_nonstandard_integer_type (info->bitsize,
3908 UNSIGNED);
3909 tem = gimple_convert (&seq, loc, bitfield_type, tem);
3911 else if ((BYTES_BIG_ENDIAN ? start_gap : end_gap) > 0)
3913 const unsigned HOST_WIDE_INT imask
3914 = (HOST_WIDE_INT_1U << info->bitsize) - 1;
3915 tem = gimple_build (&seq, loc,
3916 BIT_AND_EXPR, TREE_TYPE (tem), tem,
3917 build_int_cst (TREE_TYPE (tem),
3918 imask));
3920 const HOST_WIDE_INT shift
3921 = (BYTES_BIG_ENDIAN ? end_gap : start_gap);
3922 if (shift < 0)
3923 tem = gimple_build (&seq, loc,
3924 RSHIFT_EXPR, TREE_TYPE (tem), tem,
3925 build_int_cst (NULL_TREE, -shift));
3926 tem = gimple_convert (&seq, loc, int_type, tem);
3927 if (shift > 0)
3928 tem = gimple_build (&seq, loc,
3929 LSHIFT_EXPR, int_type, tem,
3930 build_int_cst (NULL_TREE, shift));
3931 src = gimple_build (&seq, loc,
3932 BIT_IOR_EXPR, int_type, tem, src);
3935 if (!integer_zerop (mask))
3937 tree tem = make_ssa_name (int_type);
3938 tree load_src = unshare_expr (dest);
3939 /* The load might load some or all bits uninitialized,
3940 avoid -W*uninitialized warnings in that case.
3941 As optimization, it would be nice if all the bits are
3942 provably uninitialized (no stores at all yet or previous
3943 store a CLOBBER) we'd optimize away the load and replace
3944 it e.g. with 0. */
3945 TREE_NO_WARNING (load_src) = 1;
3946 stmt = gimple_build_assign (tem, load_src);
3947 gimple_set_location (stmt, loc);
3948 gimple_set_vuse (stmt, new_vuse);
3949 gimple_seq_add_stmt_without_update (&seq, stmt);
3951 /* FIXME: If there is a single chunk of zero bits in mask,
3952 perhaps use BIT_INSERT_EXPR instead? */
3953 stmt = gimple_build_assign (make_ssa_name (int_type),
3954 BIT_AND_EXPR, tem, mask);
3955 gimple_set_location (stmt, loc);
3956 gimple_seq_add_stmt_without_update (&seq, stmt);
3957 tem = gimple_assign_lhs (stmt);
3959 if (TREE_CODE (src) == INTEGER_CST)
3960 src = wide_int_to_tree (int_type,
3961 wi::bit_and_not (wi::to_wide (src),
3962 wi::to_wide (mask)));
3963 else
3965 tree nmask
3966 = wide_int_to_tree (int_type,
3967 wi::bit_not (wi::to_wide (mask)));
3968 stmt = gimple_build_assign (make_ssa_name (int_type),
3969 BIT_AND_EXPR, src, nmask);
3970 gimple_set_location (stmt, loc);
3971 gimple_seq_add_stmt_without_update (&seq, stmt);
3972 src = gimple_assign_lhs (stmt);
3974 stmt = gimple_build_assign (make_ssa_name (int_type),
3975 BIT_IOR_EXPR, tem, src);
3976 gimple_set_location (stmt, loc);
3977 gimple_seq_add_stmt_without_update (&seq, stmt);
3978 src = gimple_assign_lhs (stmt);
3982 stmt = gimple_build_assign (dest, src);
3983 gimple_set_location (stmt, loc);
3984 gimple_set_vuse (stmt, new_vuse);
3985 gimple_seq_add_stmt_without_update (&seq, stmt);
3987 tree new_vdef;
3988 if (i < split_stores.length () - 1)
3989 new_vdef = make_ssa_name (gimple_vop (cfun), stmt);
3990 else
3991 new_vdef = last_vdef;
3993 gimple_set_vdef (stmt, new_vdef);
3994 SSA_NAME_DEF_STMT (new_vdef) = stmt;
3995 new_vuse = new_vdef;
3998 FOR_EACH_VEC_ELT (split_stores, i, split_store)
3999 delete split_store;
4001 gcc_assert (seq);
4002 if (dump_file)
4004 fprintf (dump_file,
4005 "New sequence of %u stores to replace old one of %u stores\n",
4006 split_stores.length (), orig_num_stmts);
4007 if (dump_flags & TDF_DETAILS)
4008 print_gimple_seq (dump_file, seq, 0, TDF_VOPS | TDF_MEMSYMS);
4010 gsi_insert_seq_after (&last_gsi, seq, GSI_SAME_STMT);
4011 for (int j = 0; j < 2; ++j)
4012 if (load_seq[j])
4013 gsi_insert_seq_after (&load_gsi[j], load_seq[j], GSI_SAME_STMT);
4015 return true;
4018 /* Process the merged_store_group objects created in the coalescing phase.
4019 The stores are all against the base object BASE.
4020 Try to output the widened stores and delete the original statements if
4021 successful. Return true iff any changes were made. */
4023 bool
4024 imm_store_chain_info::output_merged_stores ()
4026 unsigned int i;
4027 merged_store_group *merged_store;
4028 bool ret = false;
4029 FOR_EACH_VEC_ELT (m_merged_store_groups, i, merged_store)
4031 if (output_merged_store (merged_store))
4033 unsigned int j;
4034 store_immediate_info *store;
4035 FOR_EACH_VEC_ELT (merged_store->stores, j, store)
4037 gimple *stmt = store->stmt;
4038 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4039 gsi_remove (&gsi, true);
4040 if (stmt != merged_store->last_stmt)
4042 unlink_stmt_vdef (stmt);
4043 release_defs (stmt);
4046 ret = true;
4049 if (ret && dump_file)
4050 fprintf (dump_file, "Merging successful!\n");
4052 return ret;
4055 /* Coalesce the store_immediate_info objects recorded against the base object
4056 BASE in the first phase and output them.
4057 Delete the allocated structures.
4058 Return true if any changes were made. */
4060 bool
4061 imm_store_chain_info::terminate_and_process_chain ()
4063 /* Process store chain. */
4064 bool ret = false;
4065 if (m_store_info.length () > 1)
4067 ret = coalesce_immediate_stores ();
4068 if (ret)
4069 ret = output_merged_stores ();
4072 /* Delete all the entries we allocated ourselves. */
4073 store_immediate_info *info;
4074 unsigned int i;
4075 FOR_EACH_VEC_ELT (m_store_info, i, info)
4076 delete info;
4078 merged_store_group *merged_info;
4079 FOR_EACH_VEC_ELT (m_merged_store_groups, i, merged_info)
4080 delete merged_info;
4082 return ret;
4085 /* Return true iff LHS is a destination potentially interesting for
4086 store merging. In practice these are the codes that get_inner_reference
4087 can process. */
4089 static bool
4090 lhs_valid_for_store_merging_p (tree lhs)
4092 tree_code code = TREE_CODE (lhs);
4094 if (code == ARRAY_REF || code == ARRAY_RANGE_REF || code == MEM_REF
4095 || code == COMPONENT_REF || code == BIT_FIELD_REF)
4096 return true;
4098 return false;
4101 /* Return true if the tree RHS is a constant we want to consider
4102 during store merging. In practice accept all codes that
4103 native_encode_expr accepts. */
4105 static bool
4106 rhs_valid_for_store_merging_p (tree rhs)
4108 unsigned HOST_WIDE_INT size;
4109 return (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (rhs))).is_constant (&size)
4110 && native_encode_expr (rhs, NULL, size) != 0);
4113 /* If MEM is a memory reference usable for store merging (either as
4114 store destination or for loads), return the non-NULL base_addr
4115 and set *PBITSIZE, *PBITPOS, *PBITREGION_START and *PBITREGION_END.
4116 Otherwise return NULL, *PBITPOS should be still valid even for that
4117 case. */
4119 static tree
4120 mem_valid_for_store_merging (tree mem, poly_uint64 *pbitsize,
4121 poly_uint64 *pbitpos,
4122 poly_uint64 *pbitregion_start,
4123 poly_uint64 *pbitregion_end)
4125 poly_int64 bitsize, bitpos;
4126 poly_uint64 bitregion_start = 0, bitregion_end = 0;
4127 machine_mode mode;
4128 int unsignedp = 0, reversep = 0, volatilep = 0;
4129 tree offset;
4130 tree base_addr = get_inner_reference (mem, &bitsize, &bitpos, &offset, &mode,
4131 &unsignedp, &reversep, &volatilep);
4132 *pbitsize = bitsize;
4133 if (known_eq (bitsize, 0))
4134 return NULL_TREE;
4136 if (TREE_CODE (mem) == COMPONENT_REF
4137 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (mem, 1)))
4139 get_bit_range (&bitregion_start, &bitregion_end, mem, &bitpos, &offset);
4140 if (maybe_ne (bitregion_end, 0U))
4141 bitregion_end += 1;
4144 if (reversep)
4145 return NULL_TREE;
4147 /* We do not want to rewrite TARGET_MEM_REFs. */
4148 if (TREE_CODE (base_addr) == TARGET_MEM_REF)
4149 return NULL_TREE;
4150 /* In some cases get_inner_reference may return a
4151 MEM_REF [ptr + byteoffset]. For the purposes of this pass
4152 canonicalize the base_addr to MEM_REF [ptr] and take
4153 byteoffset into account in the bitpos. This occurs in
4154 PR 23684 and this way we can catch more chains. */
4155 else if (TREE_CODE (base_addr) == MEM_REF)
4157 poly_offset_int byte_off = mem_ref_offset (base_addr);
4158 poly_offset_int bit_off = byte_off << LOG2_BITS_PER_UNIT;
4159 bit_off += bitpos;
4160 if (known_ge (bit_off, 0) && bit_off.to_shwi (&bitpos))
4162 if (maybe_ne (bitregion_end, 0U))
4164 bit_off = byte_off << LOG2_BITS_PER_UNIT;
4165 bit_off += bitregion_start;
4166 if (bit_off.to_uhwi (&bitregion_start))
4168 bit_off = byte_off << LOG2_BITS_PER_UNIT;
4169 bit_off += bitregion_end;
4170 if (!bit_off.to_uhwi (&bitregion_end))
4171 bitregion_end = 0;
4173 else
4174 bitregion_end = 0;
4177 else
4178 return NULL_TREE;
4179 base_addr = TREE_OPERAND (base_addr, 0);
4181 /* get_inner_reference returns the base object, get at its
4182 address now. */
4183 else
4185 if (maybe_lt (bitpos, 0))
4186 return NULL_TREE;
4187 base_addr = build_fold_addr_expr (base_addr);
4190 if (known_eq (bitregion_end, 0U))
4192 bitregion_start = round_down_to_byte_boundary (bitpos);
4193 bitregion_end = bitpos;
4194 bitregion_end = round_up_to_byte_boundary (bitregion_end + bitsize);
4197 if (offset != NULL_TREE)
4199 /* If the access is variable offset then a base decl has to be
4200 address-taken to be able to emit pointer-based stores to it.
4201 ??? We might be able to get away with re-using the original
4202 base up to the first variable part and then wrapping that inside
4203 a BIT_FIELD_REF. */
4204 tree base = get_base_address (base_addr);
4205 if (! base
4206 || (DECL_P (base) && ! TREE_ADDRESSABLE (base)))
4207 return NULL_TREE;
4209 base_addr = build2 (POINTER_PLUS_EXPR, TREE_TYPE (base_addr),
4210 base_addr, offset);
4213 *pbitsize = bitsize;
4214 *pbitpos = bitpos;
4215 *pbitregion_start = bitregion_start;
4216 *pbitregion_end = bitregion_end;
4217 return base_addr;
4220 /* Return true if STMT is a load that can be used for store merging.
4221 In that case fill in *OP. BITSIZE, BITPOS, BITREGION_START and
4222 BITREGION_END are properties of the corresponding store. */
4224 static bool
4225 handled_load (gimple *stmt, store_operand_info *op,
4226 poly_uint64 bitsize, poly_uint64 bitpos,
4227 poly_uint64 bitregion_start, poly_uint64 bitregion_end)
4229 if (!is_gimple_assign (stmt))
4230 return false;
4231 if (gimple_assign_rhs_code (stmt) == BIT_NOT_EXPR)
4233 tree rhs1 = gimple_assign_rhs1 (stmt);
4234 if (TREE_CODE (rhs1) == SSA_NAME
4235 && handled_load (SSA_NAME_DEF_STMT (rhs1), op, bitsize, bitpos,
4236 bitregion_start, bitregion_end))
4238 /* Don't allow _1 = load; _2 = ~1; _3 = ~_2; which should have
4239 been optimized earlier, but if allowed here, would confuse the
4240 multiple uses counting. */
4241 if (op->bit_not_p)
4242 return false;
4243 op->bit_not_p = !op->bit_not_p;
4244 return true;
4246 return false;
4248 if (gimple_vuse (stmt)
4249 && gimple_assign_load_p (stmt)
4250 && !stmt_can_throw_internal (stmt)
4251 && !gimple_has_volatile_ops (stmt))
4253 tree mem = gimple_assign_rhs1 (stmt);
4254 op->base_addr
4255 = mem_valid_for_store_merging (mem, &op->bitsize, &op->bitpos,
4256 &op->bitregion_start,
4257 &op->bitregion_end);
4258 if (op->base_addr != NULL_TREE
4259 && known_eq (op->bitsize, bitsize)
4260 && multiple_p (op->bitpos - bitpos, BITS_PER_UNIT)
4261 && known_ge (op->bitpos - op->bitregion_start,
4262 bitpos - bitregion_start)
4263 && known_ge (op->bitregion_end - op->bitpos,
4264 bitregion_end - bitpos))
4266 op->stmt = stmt;
4267 op->val = mem;
4268 op->bit_not_p = false;
4269 return true;
4272 return false;
4275 /* Record the store STMT for store merging optimization if it can be
4276 optimized. */
4278 void
4279 pass_store_merging::process_store (gimple *stmt)
4281 tree lhs = gimple_assign_lhs (stmt);
4282 tree rhs = gimple_assign_rhs1 (stmt);
4283 poly_uint64 bitsize, bitpos;
4284 poly_uint64 bitregion_start, bitregion_end;
4285 tree base_addr
4286 = mem_valid_for_store_merging (lhs, &bitsize, &bitpos,
4287 &bitregion_start, &bitregion_end);
4288 if (known_eq (bitsize, 0U))
4289 return;
4291 bool invalid = (base_addr == NULL_TREE
4292 || (maybe_gt (bitsize,
4293 (unsigned int) MAX_BITSIZE_MODE_ANY_INT)
4294 && (TREE_CODE (rhs) != INTEGER_CST)));
4295 enum tree_code rhs_code = ERROR_MARK;
4296 bool bit_not_p = false;
4297 struct symbolic_number n;
4298 gimple *ins_stmt = NULL;
4299 store_operand_info ops[2];
4300 if (invalid)
4302 else if (rhs_valid_for_store_merging_p (rhs))
4304 rhs_code = INTEGER_CST;
4305 ops[0].val = rhs;
4307 else if (TREE_CODE (rhs) != SSA_NAME)
4308 invalid = true;
4309 else
4311 gimple *def_stmt = SSA_NAME_DEF_STMT (rhs), *def_stmt1, *def_stmt2;
4312 if (!is_gimple_assign (def_stmt))
4313 invalid = true;
4314 else if (handled_load (def_stmt, &ops[0], bitsize, bitpos,
4315 bitregion_start, bitregion_end))
4316 rhs_code = MEM_REF;
4317 else if (gimple_assign_rhs_code (def_stmt) == BIT_NOT_EXPR)
4319 tree rhs1 = gimple_assign_rhs1 (def_stmt);
4320 if (TREE_CODE (rhs1) == SSA_NAME
4321 && is_gimple_assign (SSA_NAME_DEF_STMT (rhs1)))
4323 bit_not_p = true;
4324 def_stmt = SSA_NAME_DEF_STMT (rhs1);
4328 if (rhs_code == ERROR_MARK && !invalid)
4329 switch ((rhs_code = gimple_assign_rhs_code (def_stmt)))
4331 case BIT_AND_EXPR:
4332 case BIT_IOR_EXPR:
4333 case BIT_XOR_EXPR:
4334 tree rhs1, rhs2;
4335 rhs1 = gimple_assign_rhs1 (def_stmt);
4336 rhs2 = gimple_assign_rhs2 (def_stmt);
4337 invalid = true;
4338 if (TREE_CODE (rhs1) != SSA_NAME)
4339 break;
4340 def_stmt1 = SSA_NAME_DEF_STMT (rhs1);
4341 if (!is_gimple_assign (def_stmt1)
4342 || !handled_load (def_stmt1, &ops[0], bitsize, bitpos,
4343 bitregion_start, bitregion_end))
4344 break;
4345 if (rhs_valid_for_store_merging_p (rhs2))
4346 ops[1].val = rhs2;
4347 else if (TREE_CODE (rhs2) != SSA_NAME)
4348 break;
4349 else
4351 def_stmt2 = SSA_NAME_DEF_STMT (rhs2);
4352 if (!is_gimple_assign (def_stmt2))
4353 break;
4354 else if (!handled_load (def_stmt2, &ops[1], bitsize, bitpos,
4355 bitregion_start, bitregion_end))
4356 break;
4358 invalid = false;
4359 break;
4360 default:
4361 invalid = true;
4362 break;
4365 unsigned HOST_WIDE_INT const_bitsize;
4366 if (bitsize.is_constant (&const_bitsize)
4367 && (const_bitsize % BITS_PER_UNIT) == 0
4368 && const_bitsize <= 64
4369 && multiple_p (bitpos, BITS_PER_UNIT))
4371 ins_stmt = find_bswap_or_nop_1 (def_stmt, &n, 12);
4372 if (ins_stmt)
4374 uint64_t nn = n.n;
4375 for (unsigned HOST_WIDE_INT i = 0;
4376 i < const_bitsize;
4377 i += BITS_PER_UNIT, nn >>= BITS_PER_MARKER)
4378 if ((nn & MARKER_MASK) == 0
4379 || (nn & MARKER_MASK) == MARKER_BYTE_UNKNOWN)
4381 ins_stmt = NULL;
4382 break;
4384 if (ins_stmt)
4386 if (invalid)
4388 rhs_code = LROTATE_EXPR;
4389 ops[0].base_addr = NULL_TREE;
4390 ops[1].base_addr = NULL_TREE;
4392 invalid = false;
4397 if (invalid
4398 && bitsize.is_constant (&const_bitsize)
4399 && ((const_bitsize % BITS_PER_UNIT) != 0
4400 || !multiple_p (bitpos, BITS_PER_UNIT))
4401 && const_bitsize <= 64)
4403 /* Bypass a conversion to the bit-field type. */
4404 if (!bit_not_p
4405 && is_gimple_assign (def_stmt)
4406 && CONVERT_EXPR_CODE_P (rhs_code))
4408 tree rhs1 = gimple_assign_rhs1 (def_stmt);
4409 if (TREE_CODE (rhs1) == SSA_NAME
4410 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4411 rhs = rhs1;
4413 rhs_code = BIT_INSERT_EXPR;
4414 bit_not_p = false;
4415 ops[0].val = rhs;
4416 ops[0].base_addr = NULL_TREE;
4417 ops[1].base_addr = NULL_TREE;
4418 invalid = false;
4422 unsigned HOST_WIDE_INT const_bitsize, const_bitpos;
4423 unsigned HOST_WIDE_INT const_bitregion_start, const_bitregion_end;
4424 if (invalid
4425 || !bitsize.is_constant (&const_bitsize)
4426 || !bitpos.is_constant (&const_bitpos)
4427 || !bitregion_start.is_constant (&const_bitregion_start)
4428 || !bitregion_end.is_constant (&const_bitregion_end))
4430 terminate_all_aliasing_chains (NULL, stmt);
4431 return;
4434 if (!ins_stmt)
4435 memset (&n, 0, sizeof (n));
4437 struct imm_store_chain_info **chain_info = NULL;
4438 if (base_addr)
4439 chain_info = m_stores.get (base_addr);
4441 store_immediate_info *info;
4442 if (chain_info)
4444 unsigned int ord = (*chain_info)->m_store_info.length ();
4445 info = new store_immediate_info (const_bitsize, const_bitpos,
4446 const_bitregion_start,
4447 const_bitregion_end,
4448 stmt, ord, rhs_code, n, ins_stmt,
4449 bit_not_p, ops[0], ops[1]);
4450 if (dump_file && (dump_flags & TDF_DETAILS))
4452 fprintf (dump_file, "Recording immediate store from stmt:\n");
4453 print_gimple_stmt (dump_file, stmt, 0);
4455 (*chain_info)->m_store_info.safe_push (info);
4456 terminate_all_aliasing_chains (chain_info, stmt);
4457 /* If we reach the limit of stores to merge in a chain terminate and
4458 process the chain now. */
4459 if ((*chain_info)->m_store_info.length ()
4460 == (unsigned int) PARAM_VALUE (PARAM_MAX_STORES_TO_MERGE))
4462 if (dump_file && (dump_flags & TDF_DETAILS))
4463 fprintf (dump_file,
4464 "Reached maximum number of statements to merge:\n");
4465 terminate_and_release_chain (*chain_info);
4467 return;
4470 /* Store aliases any existing chain? */
4471 terminate_all_aliasing_chains (NULL, stmt);
4472 /* Start a new chain. */
4473 struct imm_store_chain_info *new_chain
4474 = new imm_store_chain_info (m_stores_head, base_addr);
4475 info = new store_immediate_info (const_bitsize, const_bitpos,
4476 const_bitregion_start,
4477 const_bitregion_end,
4478 stmt, 0, rhs_code, n, ins_stmt,
4479 bit_not_p, ops[0], ops[1]);
4480 new_chain->m_store_info.safe_push (info);
4481 m_stores.put (base_addr, new_chain);
4482 if (dump_file && (dump_flags & TDF_DETAILS))
4484 fprintf (dump_file, "Starting new chain with statement:\n");
4485 print_gimple_stmt (dump_file, stmt, 0);
4486 fprintf (dump_file, "The base object is:\n");
4487 print_generic_expr (dump_file, base_addr);
4488 fprintf (dump_file, "\n");
4492 /* Entry point for the pass. Go over each basic block recording chains of
4493 immediate stores. Upon encountering a terminating statement (as defined
4494 by stmt_terminates_chain_p) process the recorded stores and emit the widened
4495 variants. */
4497 unsigned int
4498 pass_store_merging::execute (function *fun)
4500 basic_block bb;
4501 hash_set<gimple *> orig_stmts;
4503 calculate_dominance_info (CDI_DOMINATORS);
4505 FOR_EACH_BB_FN (bb, fun)
4507 gimple_stmt_iterator gsi;
4508 unsigned HOST_WIDE_INT num_statements = 0;
4509 /* Record the original statements so that we can keep track of
4510 statements emitted in this pass and not re-process new
4511 statements. */
4512 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4514 if (is_gimple_debug (gsi_stmt (gsi)))
4515 continue;
4517 if (++num_statements >= 2)
4518 break;
4521 if (num_statements < 2)
4522 continue;
4524 if (dump_file && (dump_flags & TDF_DETAILS))
4525 fprintf (dump_file, "Processing basic block <%d>:\n", bb->index);
4527 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4529 gimple *stmt = gsi_stmt (gsi);
4531 if (is_gimple_debug (stmt))
4532 continue;
4534 if (gimple_has_volatile_ops (stmt))
4536 /* Terminate all chains. */
4537 if (dump_file && (dump_flags & TDF_DETAILS))
4538 fprintf (dump_file, "Volatile access terminates "
4539 "all chains\n");
4540 terminate_and_process_all_chains ();
4541 continue;
4544 if (gimple_assign_single_p (stmt) && gimple_vdef (stmt)
4545 && !stmt_can_throw_internal (stmt)
4546 && lhs_valid_for_store_merging_p (gimple_assign_lhs (stmt)))
4547 process_store (stmt);
4548 else
4549 terminate_all_aliasing_chains (NULL, stmt);
4551 terminate_and_process_all_chains ();
4553 return 0;
4556 } // anon namespace
4558 /* Construct and return a store merging pass object. */
4560 gimple_opt_pass *
4561 make_pass_store_merging (gcc::context *ctxt)
4563 return new pass_store_merging (ctxt);
4566 #if CHECKING_P
4568 namespace selftest {
4570 /* Selftests for store merging helpers. */
4572 /* Assert that all elements of the byte arrays X and Y, both of length N
4573 are equal. */
4575 static void
4576 verify_array_eq (unsigned char *x, unsigned char *y, unsigned int n)
4578 for (unsigned int i = 0; i < n; i++)
4580 if (x[i] != y[i])
4582 fprintf (stderr, "Arrays do not match. X:\n");
4583 dump_char_array (stderr, x, n);
4584 fprintf (stderr, "Y:\n");
4585 dump_char_array (stderr, y, n);
4587 ASSERT_EQ (x[i], y[i]);
4591 /* Test shift_bytes_in_array and that it carries bits across between
4592 bytes correctly. */
4594 static void
4595 verify_shift_bytes_in_array (void)
4597 /* byte 1 | byte 0
4598 00011111 | 11100000. */
4599 unsigned char orig[2] = { 0xe0, 0x1f };
4600 unsigned char in[2];
4601 memcpy (in, orig, sizeof orig);
4603 unsigned char expected[2] = { 0x80, 0x7f };
4604 shift_bytes_in_array (in, sizeof (in), 2);
4605 verify_array_eq (in, expected, sizeof (in));
4607 memcpy (in, orig, sizeof orig);
4608 memcpy (expected, orig, sizeof orig);
4609 /* Check that shifting by zero doesn't change anything. */
4610 shift_bytes_in_array (in, sizeof (in), 0);
4611 verify_array_eq (in, expected, sizeof (in));
4615 /* Test shift_bytes_in_array_right and that it carries bits across between
4616 bytes correctly. */
4618 static void
4619 verify_shift_bytes_in_array_right (void)
4621 /* byte 1 | byte 0
4622 00011111 | 11100000. */
4623 unsigned char orig[2] = { 0x1f, 0xe0};
4624 unsigned char in[2];
4625 memcpy (in, orig, sizeof orig);
4626 unsigned char expected[2] = { 0x07, 0xf8};
4627 shift_bytes_in_array_right (in, sizeof (in), 2);
4628 verify_array_eq (in, expected, sizeof (in));
4630 memcpy (in, orig, sizeof orig);
4631 memcpy (expected, orig, sizeof orig);
4632 /* Check that shifting by zero doesn't change anything. */
4633 shift_bytes_in_array_right (in, sizeof (in), 0);
4634 verify_array_eq (in, expected, sizeof (in));
4637 /* Test clear_bit_region that it clears exactly the bits asked and
4638 nothing more. */
4640 static void
4641 verify_clear_bit_region (void)
4643 /* Start with all bits set and test clearing various patterns in them. */
4644 unsigned char orig[3] = { 0xff, 0xff, 0xff};
4645 unsigned char in[3];
4646 unsigned char expected[3];
4647 memcpy (in, orig, sizeof in);
4649 /* Check zeroing out all the bits. */
4650 clear_bit_region (in, 0, 3 * BITS_PER_UNIT);
4651 expected[0] = expected[1] = expected[2] = 0;
4652 verify_array_eq (in, expected, sizeof in);
4654 memcpy (in, orig, sizeof in);
4655 /* Leave the first and last bits intact. */
4656 clear_bit_region (in, 1, 3 * BITS_PER_UNIT - 2);
4657 expected[0] = 0x1;
4658 expected[1] = 0;
4659 expected[2] = 0x80;
4660 verify_array_eq (in, expected, sizeof in);
4663 /* Test verify_clear_bit_region_be that it clears exactly the bits asked and
4664 nothing more. */
4666 static void
4667 verify_clear_bit_region_be (void)
4669 /* Start with all bits set and test clearing various patterns in them. */
4670 unsigned char orig[3] = { 0xff, 0xff, 0xff};
4671 unsigned char in[3];
4672 unsigned char expected[3];
4673 memcpy (in, orig, sizeof in);
4675 /* Check zeroing out all the bits. */
4676 clear_bit_region_be (in, BITS_PER_UNIT - 1, 3 * BITS_PER_UNIT);
4677 expected[0] = expected[1] = expected[2] = 0;
4678 verify_array_eq (in, expected, sizeof in);
4680 memcpy (in, orig, sizeof in);
4681 /* Leave the first and last bits intact. */
4682 clear_bit_region_be (in, BITS_PER_UNIT - 2, 3 * BITS_PER_UNIT - 2);
4683 expected[0] = 0x80;
4684 expected[1] = 0;
4685 expected[2] = 0x1;
4686 verify_array_eq (in, expected, sizeof in);
4690 /* Run all of the selftests within this file. */
4692 void
4693 store_merging_c_tests (void)
4695 verify_shift_bytes_in_array ();
4696 verify_shift_bytes_in_array_right ();
4697 verify_clear_bit_region ();
4698 verify_clear_bit_region_be ();
4701 } // namespace selftest
4702 #endif /* CHECKING_P. */