]>
Commit | Line | Data |
---|---|---|
dffec8eb | 1 | /* GIMPLE store merging and byte swapping passes. |
85ec4feb | 2 | Copyright (C) 2009-2018 Free Software Foundation, Inc. |
f663d9ad KT |
3 | Contributed by ARM Ltd. |
4 | ||
5 | This file is part of GCC. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
20 | ||
c94c3532 EB |
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. | |
24 | ||
f663d9ad KT |
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: | |
c94c3532 | 32 | [p] := imm1:imm2:imm3:imm4 concatenated according to endianness. |
f663d9ad | 33 | |
245f6de1 JJ |
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. | |
41 | ||
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. | |
49 | ||
c94c3532 EB |
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. | |
55 | ||
f663d9ad KT |
56 | The algorithm is applied to each basic block in three phases: |
57 | ||
c94c3532 EB |
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 | |
245f6de1 JJ |
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). | |
f663d9ad KT |
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. | |
c94c3532 EB |
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. | |
245f6de1 JJ |
72 | At most one of the operands can be a constant. The operands are recorded |
73 | in store_operand_info struct. | |
f663d9ad | 74 | |
c94c3532 | 75 | 2) Analyze the chains of stores recorded in phase 1) (i.e. the vector of |
f663d9ad | 76 | store_immediate_info objects) and coalesce contiguous stores into |
c94c3532 | 77 | merged_store_group objects. For bit-field stores, we don't need to |
245f6de1 JJ |
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. | |
f663d9ad KT |
83 | |
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]. | |
94 | ||
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. | |
c94c3532 EB |
101 | We try to emit as wide stores as we can while respecting STRICT_ALIGNMENT |
102 | or TARGET_SLOW_UNALIGNED_ACCESS settings. | |
f663d9ad KT |
103 | |
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; | |
110 | ||
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| | |
120 | ||
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 | |
125 | ||
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 | |
129 | ||
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; | |
136 | ||
137 | Whereas for big-endian we emit: | |
138 | [p] (32-bit) := 0x12345678; // (val & 0xffffffff0000) >> 16; | |
139 | [p + 4B] (16-bit) := 0xabcd; // val & 0x00000000ffff; */ | |
140 | ||
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" | |
c94c3532 | 159 | #include "gimple-fold.h" |
f663d9ad KT |
160 | #include "stor-layout.h" |
161 | #include "timevar.h" | |
162 | #include "tree-cfg.h" | |
163 | #include "tree-eh.h" | |
164 | #include "target.h" | |
aa55dc0c | 165 | #include "gimplify-me.h" |
a62b3dc5 JJ |
166 | #include "rtl.h" |
167 | #include "expr.h" /* For get_bit_range. */ | |
dffec8eb | 168 | #include "optabs-tree.h" |
c22d8787 | 169 | #include "selftest.h" |
f663d9ad KT |
170 | |
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) | |
174 | ||
245f6de1 JJ |
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 | |
178 | ||
f663d9ad KT |
179 | namespace { |
180 | ||
bebadeca | 181 | struct bswap_stat |
dffec8eb JJ |
182 | { |
183 | /* Number of hand-written 16-bit nop / bswaps found. */ | |
184 | int found_16bit; | |
185 | ||
186 | /* Number of hand-written 32-bit nop / bswaps found. */ | |
187 | int found_32bit; | |
188 | ||
189 | /* Number of hand-written 64-bit nop / bswaps found. */ | |
190 | int found_64bit; | |
191 | } nop_stats, bswap_stats; | |
192 | ||
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: | |
197 | ||
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). | |
201 | ||
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. | |
211 | ||
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. | |
217 | ||
218 | Note 2: for non-memory sources, range holds the same value as size. | |
219 | ||
220 | Note 3: SRC points to the SSA_NAME in case of non-memory source. */ | |
221 | ||
222 | struct symbolic_number { | |
223 | uint64_t n; | |
224 | tree type; | |
225 | tree base_addr; | |
226 | tree offset; | |
4a022c70 | 227 | poly_int64_pod bytepos; |
dffec8eb JJ |
228 | tree src; |
229 | tree alias_set; | |
230 | tree vuse; | |
231 | unsigned HOST_WIDE_INT range; | |
232 | int n_ops; | |
233 | }; | |
234 | ||
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))) | |
240 | ||
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) | |
246 | ||
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) | |
252 | ||
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. */ | |
256 | ||
257 | inline bool | |
258 | do_shift_rotate (enum tree_code code, | |
259 | struct symbolic_number *n, | |
260 | int count) | |
261 | { | |
262 | int i, size = TYPE_PRECISION (n->type) / BITS_PER_UNIT; | |
263 | unsigned head_marker; | |
264 | ||
265 | if (count % BITS_PER_UNIT != 0) | |
266 | return false; | |
267 | count = (count / BITS_PER_UNIT) * BITS_PER_MARKER; | |
268 | ||
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; | |
273 | ||
274 | switch (code) | |
275 | { | |
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; | |
296 | } | |
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; | |
301 | } | |
302 | ||
303 | /* Perform sanity checking for the symbolic number N and the gimple | |
304 | statement STMT. */ | |
305 | ||
306 | inline bool | |
307 | verify_symbolic_number_p (struct symbolic_number *n, gimple *stmt) | |
308 | { | |
309 | tree lhs_type; | |
310 | ||
311 | lhs_type = gimple_expr_type (stmt); | |
312 | ||
313 | if (TREE_CODE (lhs_type) != INTEGER_TYPE) | |
314 | return false; | |
315 | ||
316 | if (TYPE_PRECISION (lhs_type) != TYPE_PRECISION (n->type)) | |
317 | return false; | |
318 | ||
319 | return true; | |
320 | } | |
321 | ||
322 | /* Initialize the symbolic number N for the bswap pass from the base element | |
323 | SRC manipulated by the bitwise OR expression. */ | |
324 | ||
325 | bool | |
326 | init_symbolic_number (struct symbolic_number *n, tree src) | |
327 | { | |
328 | int size; | |
329 | ||
330 | if (! INTEGRAL_TYPE_P (TREE_TYPE (src))) | |
331 | return false; | |
332 | ||
333 | n->base_addr = n->offset = n->alias_set = n->vuse = NULL_TREE; | |
334 | n->src = src; | |
335 | ||
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; | |
349 | ||
350 | if (size < 64 / BITS_PER_MARKER) | |
351 | n->n &= ((uint64_t) 1 << (size * BITS_PER_MARKER)) - 1; | |
352 | ||
353 | return true; | |
354 | } | |
355 | ||
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. */ | |
359 | ||
360 | bool | |
361 | find_bswap_or_nop_load (gimple *stmt, tree ref, struct symbolic_number *n) | |
362 | { | |
363 | /* Leaf node is an array or component ref. Memorize its base and | |
364 | offset from base to compare to other such leaf node. */ | |
f37fac2b | 365 | poly_int64 bitsize, bitpos, bytepos; |
dffec8eb JJ |
366 | machine_mode mode; |
367 | int unsignedp, reversep, volatilep; | |
368 | tree offset, base_addr; | |
369 | ||
370 | /* Not prepared to handle PDP endian. */ | |
371 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) | |
372 | return false; | |
373 | ||
374 | if (!gimple_assign_load_p (stmt) || gimple_has_volatile_ops (stmt)) | |
375 | return false; | |
376 | ||
377 | base_addr = get_inner_reference (ref, &bitsize, &bitpos, &offset, &mode, | |
378 | &unsignedp, &reversep, &volatilep); | |
379 | ||
4b84d9b8 JJ |
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) | |
dffec8eb | 384 | { |
3fed2ce9 | 385 | poly_offset_int bit_offset = 0; |
dffec8eb JJ |
386 | tree off = TREE_OPERAND (base_addr, 1); |
387 | ||
388 | if (!integer_zerop (off)) | |
389 | { | |
3fed2ce9 RS |
390 | poly_offset_int boff = mem_ref_offset (base_addr); |
391 | boff <<= LOG2_BITS_PER_UNIT; | |
dffec8eb JJ |
392 | bit_offset += boff; |
393 | } | |
394 | ||
395 | base_addr = TREE_OPERAND (base_addr, 0); | |
396 | ||
397 | /* Avoid returning a negative bitpos as this may wreak havoc later. */ | |
3fed2ce9 | 398 | if (maybe_lt (bit_offset, 0)) |
dffec8eb | 399 | { |
3fed2ce9 RS |
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); | |
dffec8eb | 403 | if (offset) |
3fed2ce9 | 404 | offset = size_binop (PLUS_EXPR, offset, byte_offset); |
dffec8eb | 405 | else |
3fed2ce9 | 406 | offset = byte_offset; |
dffec8eb JJ |
407 | } |
408 | ||
3fed2ce9 | 409 | bitpos += bit_offset.force_shwi (); |
dffec8eb | 410 | } |
4b84d9b8 JJ |
411 | else |
412 | base_addr = build_fold_addr_expr (base_addr); | |
dffec8eb | 413 | |
f37fac2b | 414 | if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)) |
dffec8eb | 415 | return false; |
f37fac2b | 416 | if (!multiple_p (bitsize, BITS_PER_UNIT)) |
dffec8eb JJ |
417 | return false; |
418 | if (reversep) | |
419 | return false; | |
420 | ||
421 | if (!init_symbolic_number (n, ref)) | |
422 | return false; | |
423 | n->base_addr = base_addr; | |
424 | n->offset = offset; | |
f37fac2b | 425 | n->bytepos = bytepos; |
dffec8eb JJ |
426 | n->alias_set = reference_alias_ptr_type (ref); |
427 | n->vuse = gimple_vuse (stmt); | |
428 | return true; | |
429 | } | |
430 | ||
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. */ | |
434 | ||
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) | |
439 | { | |
440 | int i, size; | |
441 | uint64_t mask; | |
442 | gimple *source_stmt; | |
443 | struct symbolic_number *n_start; | |
444 | ||
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); | |
453 | ||
454 | /* Sources are different, cancel bswap if they are not memory location with | |
455 | the same base (array, structure, ...). */ | |
456 | if (rhs1 != rhs2) | |
457 | { | |
458 | uint64_t inc; | |
4a022c70 | 459 | HOST_WIDE_INT start1, start2, start_sub, end_sub, end1, end2, end; |
dffec8eb JJ |
460 | struct symbolic_number *toinc_n_ptr, *n_end; |
461 | basic_block bb1, bb2; | |
462 | ||
463 | if (!n1->base_addr || !n2->base_addr | |
464 | || !operand_equal_p (n1->base_addr, n2->base_addr, 0)) | |
465 | return NULL; | |
466 | ||
467 | if (!n1->offset != !n2->offset | |
468 | || (n1->offset && !operand_equal_p (n1->offset, n2->offset, 0))) | |
469 | return NULL; | |
470 | ||
4a022c70 RS |
471 | start1 = 0; |
472 | if (!(n2->bytepos - n1->bytepos).is_constant (&start2)) | |
473 | return NULL; | |
474 | ||
475 | if (start1 < start2) | |
dffec8eb JJ |
476 | { |
477 | n_start = n1; | |
4a022c70 | 478 | start_sub = start2 - start1; |
dffec8eb JJ |
479 | } |
480 | else | |
481 | { | |
482 | n_start = n2; | |
4a022c70 | 483 | start_sub = start1 - start2; |
dffec8eb JJ |
484 | } |
485 | ||
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; | |
492 | ||
493 | /* Find the highest address at which a load is performed and | |
494 | compute related info. */ | |
4a022c70 RS |
495 | end1 = start1 + (n1->range - 1); |
496 | end2 = start2 + (n2->range - 1); | |
dffec8eb JJ |
497 | if (end1 < end2) |
498 | { | |
499 | end = end2; | |
500 | end_sub = end2 - end1; | |
501 | } | |
502 | else | |
503 | { | |
504 | end = end1; | |
505 | end_sub = end1 - end2; | |
506 | } | |
507 | n_end = (end2 > end1) ? n2 : n1; | |
508 | ||
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; | |
514 | ||
4a022c70 | 515 | n->range = end - MIN (start1, start2) + 1; |
dffec8eb JJ |
516 | |
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; | |
521 | ||
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) | |
527 | { | |
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; | |
532 | } | |
533 | } | |
534 | else | |
535 | { | |
536 | n->range = n1->range; | |
537 | n_start = n1; | |
538 | source_stmt = source_stmt1; | |
539 | } | |
540 | ||
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; | |
553 | ||
554 | for (i = 0, mask = MARKER_MASK; i < size; i++, mask <<= BITS_PER_MARKER) | |
555 | { | |
556 | uint64_t masked1, masked2; | |
557 | ||
558 | masked1 = n1->n & mask; | |
559 | masked2 = n2->n & mask; | |
560 | if (masked1 && masked2 && masked1 != masked2) | |
561 | return NULL; | |
562 | } | |
563 | n->n = n1->n | n2->n; | |
564 | n->n_ops = n1->n_ops + n2->n_ops; | |
565 | ||
566 | return source_stmt; | |
567 | } | |
568 | ||
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. */ | |
574 | ||
575 | gimple * | |
576 | find_bswap_or_nop_1 (gimple *stmt, struct symbolic_number *n, int limit) | |
577 | { | |
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; | |
582 | ||
583 | if (!limit || !is_gimple_assign (stmt)) | |
584 | return NULL; | |
585 | ||
586 | rhs1 = gimple_assign_rhs1 (stmt); | |
587 | ||
588 | if (find_bswap_or_nop_load (stmt, rhs1, n)) | |
589 | return stmt; | |
590 | ||
591 | /* Handle BIT_FIELD_REF. */ | |
592 | if (TREE_CODE (rhs1) == BIT_FIELD_REF | |
593 | && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME) | |
594 | { | |
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))) | |
600 | { | |
601 | /* Handle big-endian bit numbering in BIT_FIELD_REF. */ | |
602 | if (BYTES_BIG_ENDIAN) | |
603 | bitpos = TYPE_PRECISION (n->type) - bitpos - bitsize; | |
604 | ||
605 | /* Shift. */ | |
606 | if (!do_shift_rotate (RSHIFT_EXPR, n, bitpos)) | |
607 | return NULL; | |
608 | ||
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; | |
616 | ||
617 | /* Convert. */ | |
618 | n->type = TREE_TYPE (rhs1); | |
619 | if (!n->base_addr) | |
620 | n->range = TYPE_PRECISION (n->type) / BITS_PER_UNIT; | |
621 | ||
622 | return verify_symbolic_number_p (n, stmt) ? stmt : NULL; | |
623 | } | |
624 | ||
625 | return NULL; | |
626 | } | |
627 | ||
628 | if (TREE_CODE (rhs1) != SSA_NAME) | |
629 | return NULL; | |
630 | ||
631 | code = gimple_assign_rhs_code (stmt); | |
632 | rhs_class = gimple_assign_rhs_class (stmt); | |
633 | rhs1_stmt = SSA_NAME_DEF_STMT (rhs1); | |
634 | ||
635 | if (rhs_class == GIMPLE_BINARY_RHS) | |
636 | rhs2 = gimple_assign_rhs2 (stmt); | |
637 | ||
638 | /* Handle unary rhs and binary rhs with integer constants as second | |
639 | operand. */ | |
640 | ||
641 | if (rhs_class == GIMPLE_UNARY_RHS | |
642 | || (rhs_class == GIMPLE_BINARY_RHS | |
643 | && TREE_CODE (rhs2) == INTEGER_CST)) | |
644 | { | |
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; | |
652 | ||
653 | source_stmt1 = find_bswap_or_nop_1 (rhs1_stmt, n, limit - 1); | |
654 | ||
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) | |
658 | { | |
659 | if (gimple_assign_load_p (stmt) | |
660 | || !init_symbolic_number (n, rhs1)) | |
661 | return NULL; | |
662 | source_stmt1 = stmt; | |
663 | } | |
664 | ||
665 | switch (code) | |
666 | { | |
667 | case BIT_AND_EXPR: | |
668 | { | |
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; | |
672 | ||
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); | |
679 | ||
680 | n->n &= mask; | |
681 | } | |
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: | |
691 | { | |
692 | int i, type_size, old_type_size; | |
693 | tree type; | |
694 | ||
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; | |
702 | ||
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); | |
710 | ||
711 | if (type_size < 64 / BITS_PER_MARKER) | |
712 | { | |
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; | |
716 | } | |
717 | n->type = type; | |
718 | if (!n->base_addr) | |
719 | n->range = type_size; | |
720 | } | |
721 | break; | |
722 | default: | |
723 | return NULL; | |
724 | }; | |
725 | return verify_symbolic_number_p (n, stmt) ? source_stmt1 : NULL; | |
726 | } | |
727 | ||
728 | /* Handle binary rhs. */ | |
729 | ||
730 | if (rhs_class == GIMPLE_BINARY_RHS) | |
731 | { | |
732 | struct symbolic_number n1, n2; | |
733 | gimple *source_stmt, *source_stmt2; | |
734 | ||
735 | if (code != BIT_IOR_EXPR) | |
736 | return NULL; | |
737 | ||
738 | if (TREE_CODE (rhs2) != SSA_NAME) | |
739 | return NULL; | |
740 | ||
741 | rhs2_stmt = SSA_NAME_DEF_STMT (rhs2); | |
742 | ||
743 | switch (code) | |
744 | { | |
745 | case BIT_IOR_EXPR: | |
746 | source_stmt1 = find_bswap_or_nop_1 (rhs1_stmt, &n1, limit - 1); | |
747 | ||
748 | if (!source_stmt1) | |
749 | return NULL; | |
750 | ||
751 | source_stmt2 = find_bswap_or_nop_1 (rhs2_stmt, &n2, limit - 1); | |
752 | ||
753 | if (!source_stmt2) | |
754 | return NULL; | |
755 | ||
756 | if (TYPE_PRECISION (n1.type) != TYPE_PRECISION (n2.type)) | |
757 | return NULL; | |
758 | ||
4b84d9b8 | 759 | if (n1.vuse != n2.vuse) |
dffec8eb JJ |
760 | return NULL; |
761 | ||
762 | source_stmt | |
763 | = perform_symbolic_merge (source_stmt1, &n1, source_stmt2, &n2, n); | |
764 | ||
765 | if (!source_stmt) | |
766 | return NULL; | |
767 | ||
768 | if (!verify_symbolic_number_p (n, stmt)) | |
769 | return NULL; | |
770 | ||
771 | break; | |
772 | default: | |
773 | return NULL; | |
774 | } | |
775 | return source_stmt; | |
776 | } | |
777 | return NULL; | |
778 | } | |
779 | ||
4b84d9b8 JJ |
780 | /* Helper for find_bswap_or_nop and try_coalesce_bswap to compute |
781 | *CMPXCHG, *CMPNOP and adjust *N. */ | |
dffec8eb | 782 | |
4b84d9b8 JJ |
783 | void |
784 | find_bswap_or_nop_finalize (struct symbolic_number *n, uint64_t *cmpxchg, | |
785 | uint64_t *cmpnop) | |
dffec8eb JJ |
786 | { |
787 | unsigned rsize; | |
788 | uint64_t tmpn, mask; | |
dffec8eb | 789 | |
4b84d9b8 JJ |
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; | |
dffec8eb JJ |
795 | |
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; | |
801 | ||
802 | /* Zero out the bits corresponding to untouched bytes in original gimple | |
803 | expression. */ | |
804 | if (n->range < (int) sizeof (int64_t)) | |
805 | { | |
806 | mask = ((uint64_t) 1 << (n->range * BITS_PER_MARKER)) - 1; | |
4b84d9b8 JJ |
807 | *cmpxchg >>= (64 / BITS_PER_MARKER - n->range) * BITS_PER_MARKER; |
808 | *cmpnop &= mask; | |
dffec8eb JJ |
809 | } |
810 | ||
811 | /* Zero out the bits corresponding to unused bytes in the result of the | |
812 | gimple expression. */ | |
813 | if (rsize < n->range) | |
814 | { | |
815 | if (BYTES_BIG_ENDIAN) | |
816 | { | |
817 | mask = ((uint64_t) 1 << (rsize * BITS_PER_MARKER)) - 1; | |
4b84d9b8 JJ |
818 | *cmpxchg &= mask; |
819 | *cmpnop >>= (n->range - rsize) * BITS_PER_MARKER; | |
dffec8eb JJ |
820 | } |
821 | else | |
822 | { | |
823 | mask = ((uint64_t) 1 << (rsize * BITS_PER_MARKER)) - 1; | |
4b84d9b8 JJ |
824 | *cmpxchg >>= (n->range - rsize) * BITS_PER_MARKER; |
825 | *cmpnop &= mask; | |
dffec8eb JJ |
826 | } |
827 | n->range = rsize; | |
828 | } | |
829 | ||
4b84d9b8 JJ |
830 | n->range *= BITS_PER_UNIT; |
831 | } | |
832 | ||
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. */ | |
840 | ||
841 | gimple * | |
842 | find_bswap_or_nop (gimple *stmt, struct symbolic_number *n, bool *bswap) | |
843 | { | |
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); | |
852 | ||
853 | if (!ins_stmt) | |
854 | return NULL; | |
855 | ||
856 | uint64_t cmpxchg, cmpnop; | |
857 | find_bswap_or_nop_finalize (n, &cmpxchg, &cmpnop); | |
858 | ||
dffec8eb JJ |
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; | |
868 | ||
869 | /* Useless bit manipulation performed by code. */ | |
870 | if (!n->base_addr && n->n == cmpnop && n->n_ops == 1) | |
871 | return NULL; | |
872 | ||
dffec8eb JJ |
873 | return ins_stmt; |
874 | } | |
875 | ||
876 | const pass_data pass_data_optimize_bswap = | |
877 | { | |
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 */ | |
887 | }; | |
888 | ||
889 | class pass_optimize_bswap : public gimple_opt_pass | |
890 | { | |
891 | public: | |
892 | pass_optimize_bswap (gcc::context *ctxt) | |
893 | : gimple_opt_pass (pass_data_optimize_bswap, ctxt) | |
894 | {} | |
895 | ||
896 | /* opt_pass methods: */ | |
897 | virtual bool gate (function *) | |
898 | { | |
899 | return flag_expensive_optimizations && optimize && BITS_PER_UNIT == 8; | |
900 | } | |
901 | ||
902 | virtual unsigned int execute (function *); | |
903 | ||
904 | }; // class pass_optimize_bswap | |
905 | ||
906 | /* Perform the bswap optimization: replace the expression computed in the rhs | |
4b84d9b8 JJ |
907 | of gsi_stmt (GSI) (or if NULL add instead of replace) by an equivalent |
908 | bswap, load or load + bswap expression. | |
dffec8eb JJ |
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 | |
4b84d9b8 JJ |
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. | |
dffec8eb | 916 | |
4b84d9b8 JJ |
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. */ | |
dffec8eb | 920 | |
4b84d9b8 JJ |
921 | tree |
922 | bswap_replace (gimple_stmt_iterator gsi, gimple *ins_stmt, tree fndecl, | |
dffec8eb JJ |
923 | tree bswap_type, tree load_type, struct symbolic_number *n, |
924 | bool bswap) | |
925 | { | |
4b84d9b8 | 926 | tree src, tmp, tgt = NULL_TREE; |
dffec8eb JJ |
927 | gimple *bswap_stmt; |
928 | ||
4b84d9b8 | 929 | gimple *cur_stmt = gsi_stmt (gsi); |
dffec8eb | 930 | src = n->src; |
4b84d9b8 JJ |
931 | if (cur_stmt) |
932 | tgt = gimple_assign_lhs (cur_stmt); | |
dffec8eb JJ |
933 | |
934 | /* Need to load the value from memory first. */ | |
935 | if (n->base_addr) | |
936 | { | |
4b84d9b8 JJ |
937 | gimple_stmt_iterator gsi_ins = gsi; |
938 | if (ins_stmt) | |
939 | gsi_ins = gsi_for_stmt (ins_stmt); | |
dffec8eb JJ |
940 | tree addr_expr, addr_tmp, val_expr, val_tmp; |
941 | tree load_offset_ptr, aligned_load_type; | |
4b84d9b8 JJ |
942 | gimple *load_stmt; |
943 | unsigned align = get_object_alignment (src); | |
4a022c70 | 944 | poly_int64 load_offset = 0; |
dffec8eb | 945 | |
4b84d9b8 JJ |
946 | if (cur_stmt) |
947 | { | |
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; | |
952 | ||
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); | |
960 | } | |
961 | else | |
962 | gsi = gsi_ins; | |
dffec8eb JJ |
963 | |
964 | /* Compute address to load from and cast according to the size | |
965 | of the load. */ | |
4b84d9b8 | 966 | addr_expr = build_fold_addr_expr (src); |
dffec8eb | 967 | if (is_gimple_mem_ref_addr (addr_expr)) |
4b84d9b8 | 968 | addr_tmp = unshare_expr (addr_expr); |
dffec8eb JJ |
969 | else |
970 | { | |
4b84d9b8 JJ |
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) | |
979 | { | |
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); | |
989 | } | |
dffec8eb JJ |
990 | } |
991 | ||
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); | |
999 | ||
1000 | if (!bswap) | |
1001 | { | |
1002 | if (n->range == 16) | |
1003 | nop_stats.found_16bit++; | |
1004 | else if (n->range == 32) | |
1005 | nop_stats.found_32bit++; | |
1006 | else | |
1007 | { | |
1008 | gcc_assert (n->range == 64); | |
1009 | nop_stats.found_64bit++; | |
1010 | } | |
1011 | ||
1012 | /* Convert the result of load if necessary. */ | |
4b84d9b8 | 1013 | if (tgt && !useless_type_conversion_p (TREE_TYPE (tgt), load_type)) |
dffec8eb JJ |
1014 | { |
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); | |
4b84d9b8 | 1021 | update_stmt (cur_stmt); |
dffec8eb | 1022 | } |
4b84d9b8 | 1023 | else if (cur_stmt) |
dffec8eb JJ |
1024 | { |
1025 | gimple_assign_set_rhs_with_ops (&gsi, MEM_REF, val_expr); | |
1026 | gimple_set_vuse (cur_stmt, n->vuse); | |
4b84d9b8 JJ |
1027 | update_stmt (cur_stmt); |
1028 | } | |
1029 | else | |
1030 | { | |
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); | |
dffec8eb | 1035 | } |
dffec8eb JJ |
1036 | |
1037 | if (dump_file) | |
1038 | { | |
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); | |
1043 | } | |
4b84d9b8 | 1044 | return tgt; |
dffec8eb JJ |
1045 | } |
1046 | else | |
1047 | { | |
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); | |
1052 | } | |
1053 | src = val_tmp; | |
1054 | } | |
1055 | else if (!bswap) | |
1056 | { | |
4b84d9b8 JJ |
1057 | gimple *g = NULL; |
1058 | if (tgt && !useless_type_conversion_p (TREE_TYPE (tgt), TREE_TYPE (src))) | |
dffec8eb JJ |
1059 | { |
1060 | if (!is_gimple_val (src)) | |
4b84d9b8 | 1061 | return NULL_TREE; |
dffec8eb JJ |
1062 | g = gimple_build_assign (tgt, NOP_EXPR, src); |
1063 | } | |
4b84d9b8 | 1064 | else if (cur_stmt) |
dffec8eb | 1065 | g = gimple_build_assign (tgt, src); |
4b84d9b8 JJ |
1066 | else |
1067 | tgt = src; | |
dffec8eb JJ |
1068 | if (n->range == 16) |
1069 | nop_stats.found_16bit++; | |
1070 | else if (n->range == 32) | |
1071 | nop_stats.found_32bit++; | |
1072 | else | |
1073 | { | |
1074 | gcc_assert (n->range == 64); | |
1075 | nop_stats.found_64bit++; | |
1076 | } | |
1077 | if (dump_file) | |
1078 | { | |
1079 | fprintf (dump_file, | |
1080 | "%d bit reshuffle in target endianness found at: ", | |
1081 | (int) n->range); | |
4b84d9b8 JJ |
1082 | if (cur_stmt) |
1083 | print_gimple_stmt (dump_file, cur_stmt, 0); | |
1084 | else | |
1085 | { | |
1086 | print_generic_expr (dump_file, tgt, 0); | |
1087 | fprintf (dump_file, "\n"); | |
1088 | } | |
dffec8eb | 1089 | } |
4b84d9b8 JJ |
1090 | if (cur_stmt) |
1091 | gsi_replace (&gsi, g, true); | |
1092 | return tgt; | |
dffec8eb JJ |
1093 | } |
1094 | else if (TREE_CODE (src) == BIT_FIELD_REF) | |
1095 | src = TREE_OPERAND (src, 0); | |
1096 | ||
1097 | if (n->range == 16) | |
1098 | bswap_stats.found_16bit++; | |
1099 | else if (n->range == 32) | |
1100 | bswap_stats.found_32bit++; | |
1101 | else | |
1102 | { | |
1103 | gcc_assert (n->range == 64); | |
1104 | bswap_stats.found_64bit++; | |
1105 | } | |
1106 | ||
1107 | tmp = src; | |
1108 | ||
1109 | /* Convert the src expression if necessary. */ | |
1110 | if (!useless_type_conversion_p (TREE_TYPE (tmp), bswap_type)) | |
1111 | { | |
1112 | gimple *convert_stmt; | |
1113 | ||
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); | |
1117 | } | |
1118 | ||
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) | |
1124 | { | |
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); | |
1128 | } | |
1129 | else | |
1130 | bswap_stmt = gimple_build_call (fndecl, 1, tmp); | |
1131 | ||
4b84d9b8 JJ |
1132 | if (tgt == NULL_TREE) |
1133 | tgt = make_ssa_name (bswap_type); | |
dffec8eb JJ |
1134 | tmp = tgt; |
1135 | ||
1136 | /* Convert the result if necessary. */ | |
1137 | if (!useless_type_conversion_p (TREE_TYPE (tgt), bswap_type)) | |
1138 | { | |
1139 | gimple *convert_stmt; | |
1140 | ||
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); | |
1144 | } | |
1145 | ||
1146 | gimple_set_lhs (bswap_stmt, tmp); | |
1147 | ||
1148 | if (dump_file) | |
1149 | { | |
1150 | fprintf (dump_file, "%d bit bswap implementation found at: ", | |
1151 | (int) n->range); | |
4b84d9b8 JJ |
1152 | if (cur_stmt) |
1153 | print_gimple_stmt (dump_file, cur_stmt, 0); | |
1154 | else | |
1155 | { | |
1156 | print_generic_expr (dump_file, tgt, 0); | |
1157 | fprintf (dump_file, "\n"); | |
1158 | } | |
dffec8eb JJ |
1159 | } |
1160 | ||
4b84d9b8 JJ |
1161 | if (cur_stmt) |
1162 | { | |
1163 | gsi_insert_after (&gsi, bswap_stmt, GSI_SAME_STMT); | |
1164 | gsi_remove (&gsi, true); | |
1165 | } | |
1166 | else | |
1167 | gsi_insert_before (&gsi, bswap_stmt, GSI_SAME_STMT); | |
1168 | return tgt; | |
dffec8eb JJ |
1169 | } |
1170 | ||
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. */ | |
1175 | ||
1176 | unsigned int | |
1177 | pass_optimize_bswap::execute (function *fun) | |
1178 | { | |
1179 | basic_block bb; | |
1180 | bool bswap32_p, bswap64_p; | |
1181 | bool changed = false; | |
1182 | tree bswap32_type = NULL_TREE, bswap64_type = NULL_TREE; | |
1183 | ||
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))); | |
1189 | ||
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) | |
1193 | { | |
1194 | tree fndecl = builtin_decl_explicit (BUILT_IN_BSWAP32); | |
1195 | bswap32_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); | |
1196 | } | |
1197 | ||
1198 | if (bswap64_p) | |
1199 | { | |
1200 | tree fndecl = builtin_decl_explicit (BUILT_IN_BSWAP64); | |
1201 | bswap64_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); | |
1202 | } | |
1203 | ||
1204 | memset (&nop_stats, 0, sizeof (nop_stats)); | |
1205 | memset (&bswap_stats, 0, sizeof (bswap_stats)); | |
1206 | calculate_dominance_info (CDI_DOMINATORS); | |
1207 | ||
1208 | FOR_EACH_BB_FN (bb, fun) | |
1209 | { | |
1210 | gimple_stmt_iterator gsi; | |
1211 | ||
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);) | |
1217 | { | |
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; | |
1223 | ||
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); | |
1231 | ||
1232 | if (!is_gimple_assign (cur_stmt)) | |
1233 | continue; | |
1234 | ||
1235 | code = gimple_assign_rhs_code (cur_stmt); | |
1236 | switch (code) | |
1237 | { | |
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; | |
1249 | } | |
1250 | ||
1251 | ins_stmt = find_bswap_or_nop (cur_stmt, &n, &bswap); | |
1252 | ||
1253 | if (!ins_stmt) | |
1254 | continue; | |
1255 | ||
1256 | switch (n.range) | |
1257 | { | |
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) | |
1267 | { | |
1268 | fndecl = builtin_decl_explicit (BUILT_IN_BSWAP32); | |
1269 | bswap_type = bswap32_type; | |
1270 | } | |
1271 | break; | |
1272 | case 64: | |
1273 | load_type = uint64_type_node; | |
1274 | if (bswap64_p) | |
1275 | { | |
1276 | fndecl = builtin_decl_explicit (BUILT_IN_BSWAP64); | |
1277 | bswap_type = bswap64_type; | |
1278 | } | |
1279 | break; | |
1280 | default: | |
1281 | continue; | |
1282 | } | |
1283 | ||
1284 | if (bswap && !fndecl && n.range != 16) | |
1285 | continue; | |
1286 | ||
4b84d9b8 JJ |
1287 | if (bswap_replace (gsi_for_stmt (cur_stmt), ins_stmt, fndecl, |
1288 | bswap_type, load_type, &n, bswap)) | |
dffec8eb JJ |
1289 | changed = true; |
1290 | } | |
1291 | } | |
1292 | ||
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); | |
1305 | ||
1306 | return (changed ? TODO_update_ssa : 0); | |
1307 | } | |
1308 | ||
1309 | } // anon namespace | |
1310 | ||
1311 | gimple_opt_pass * | |
1312 | make_pass_optimize_bswap (gcc::context *ctxt) | |
1313 | { | |
1314 | return new pass_optimize_bswap (ctxt); | |
1315 | } | |
1316 | ||
1317 | namespace { | |
1318 | ||
245f6de1 | 1319 | /* Struct recording one operand for the store, which is either a constant, |
c94c3532 EB |
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, */ | |
245f6de1 JJ |
1324 | |
1325 | struct store_operand_info | |
1326 | { | |
1327 | tree val; | |
1328 | tree base_addr; | |
8a91d545 RS |
1329 | poly_uint64 bitsize; |
1330 | poly_uint64 bitpos; | |
1331 | poly_uint64 bitregion_start; | |
1332 | poly_uint64 bitregion_end; | |
245f6de1 | 1333 | gimple *stmt; |
383ac8dc | 1334 | bool bit_not_p; |
245f6de1 JJ |
1335 | store_operand_info (); |
1336 | }; | |
1337 | ||
1338 | store_operand_info::store_operand_info () | |
1339 | : val (NULL_TREE), base_addr (NULL_TREE), bitsize (0), bitpos (0), | |
383ac8dc | 1340 | bitregion_start (0), bitregion_end (0), stmt (NULL), bit_not_p (false) |
245f6de1 JJ |
1341 | { |
1342 | } | |
1343 | ||
f663d9ad KT |
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. */ | |
1347 | ||
1348 | struct store_immediate_info | |
1349 | { | |
1350 | unsigned HOST_WIDE_INT bitsize; | |
1351 | unsigned HOST_WIDE_INT bitpos; | |
a62b3dc5 JJ |
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; | |
f663d9ad KT |
1355 | gimple *stmt; |
1356 | unsigned int order; | |
c94c3532 EB |
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. | |
4b84d9b8 JJ |
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. */ | |
245f6de1 | 1364 | enum tree_code rhs_code; |
4b84d9b8 JJ |
1365 | /* Two fields for bswap optimization purposes. */ |
1366 | struct symbolic_number n; | |
1367 | gimple *ins_stmt; | |
127ef369 | 1368 | /* True if BIT_{AND,IOR,XOR}_EXPR result is inverted before storing. */ |
d60edaba | 1369 | bool bit_not_p; |
127ef369 JJ |
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; | |
245f6de1 JJ |
1373 | /* Operands. For BIT_*_EXPR rhs_code both operands are used, otherwise |
1374 | just the first one. */ | |
1375 | store_operand_info ops[2]; | |
b5926e23 | 1376 | store_immediate_info (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT, |
a62b3dc5 | 1377 | unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT, |
4b84d9b8 JJ |
1378 | gimple *, unsigned int, enum tree_code, |
1379 | struct symbolic_number &, gimple *, bool, | |
245f6de1 JJ |
1380 | const store_operand_info &, |
1381 | const store_operand_info &); | |
f663d9ad KT |
1382 | }; |
1383 | ||
1384 | store_immediate_info::store_immediate_info (unsigned HOST_WIDE_INT bs, | |
b5926e23 | 1385 | unsigned HOST_WIDE_INT bp, |
a62b3dc5 JJ |
1386 | unsigned HOST_WIDE_INT brs, |
1387 | unsigned HOST_WIDE_INT bre, | |
b5926e23 | 1388 | gimple *st, |
245f6de1 JJ |
1389 | unsigned int ord, |
1390 | enum tree_code rhscode, | |
4b84d9b8 JJ |
1391 | struct symbolic_number &nr, |
1392 | gimple *ins_stmtp, | |
d60edaba | 1393 | bool bitnotp, |
245f6de1 JJ |
1394 | const store_operand_info &op0r, |
1395 | const store_operand_info &op1r) | |
a62b3dc5 | 1396 | : bitsize (bs), bitpos (bp), bitregion_start (brs), bitregion_end (bre), |
4b84d9b8 JJ |
1397 | stmt (st), order (ord), rhs_code (rhscode), n (nr), |
1398 | ins_stmt (ins_stmtp), bit_not_p (bitnotp), ops_swapped_p (false) | |
245f6de1 JJ |
1399 | #if __cplusplus >= 201103L |
1400 | , ops { op0r, op1r } | |
1401 | { | |
1402 | } | |
1403 | #else | |
f663d9ad | 1404 | { |
245f6de1 JJ |
1405 | ops[0] = op0r; |
1406 | ops[1] = op1r; | |
f663d9ad | 1407 | } |
245f6de1 | 1408 | #endif |
f663d9ad KT |
1409 | |
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. */ | |
1413 | ||
1414 | struct merged_store_group | |
1415 | { | |
1416 | unsigned HOST_WIDE_INT start; | |
1417 | unsigned HOST_WIDE_INT width; | |
a62b3dc5 JJ |
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. */ | |
f663d9ad | 1421 | unsigned HOST_WIDE_INT buf_size; |
a62b3dc5 | 1422 | unsigned HOST_WIDE_INT align_base; |
8a91d545 | 1423 | poly_uint64 load_align_base[2]; |
f663d9ad KT |
1424 | |
1425 | unsigned int align; | |
245f6de1 | 1426 | unsigned int load_align[2]; |
f663d9ad KT |
1427 | unsigned int first_order; |
1428 | unsigned int last_order; | |
1429 | ||
a62b3dc5 | 1430 | auto_vec<store_immediate_info *> stores; |
f663d9ad KT |
1431 | /* We record the first and last original statements in the sequence because |
1432 | we'll need their vuse/vdef and replacement position. It's easier to keep | |
1433 | track of them separately as 'stores' is reordered by apply_stores. */ | |
1434 | gimple *last_stmt; | |
1435 | gimple *first_stmt; | |
1436 | unsigned char *val; | |
a62b3dc5 | 1437 | unsigned char *mask; |
c94c3532 | 1438 | bool bit_insertion; |
f663d9ad KT |
1439 | |
1440 | merged_store_group (store_immediate_info *); | |
1441 | ~merged_store_group (); | |
1442 | void merge_into (store_immediate_info *); | |
1443 | void merge_overlapping (store_immediate_info *); | |
1444 | bool apply_stores (); | |
a62b3dc5 JJ |
1445 | private: |
1446 | void do_merge (store_immediate_info *); | |
f663d9ad KT |
1447 | }; |
1448 | ||
1449 | /* Debug helper. Dump LEN elements of byte array PTR to FD in hex. */ | |
1450 | ||
1451 | static void | |
1452 | dump_char_array (FILE *fd, unsigned char *ptr, unsigned int len) | |
1453 | { | |
1454 | if (!fd) | |
1455 | return; | |
1456 | ||
1457 | for (unsigned int i = 0; i < len; i++) | |
c94c3532 | 1458 | fprintf (fd, "%02x ", ptr[i]); |
f663d9ad KT |
1459 | fprintf (fd, "\n"); |
1460 | } | |
1461 | ||
f663d9ad KT |
1462 | /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the |
1463 | bits between adjacent elements. AMNT should be within | |
1464 | [0, BITS_PER_UNIT). | |
1465 | Example, AMNT = 2: | |
1466 | 00011111|11100000 << 2 = 01111111|10000000 | |
1467 | PTR[1] | PTR[0] PTR[1] | PTR[0]. */ | |
1468 | ||
1469 | static void | |
1470 | shift_bytes_in_array (unsigned char *ptr, unsigned int sz, unsigned int amnt) | |
1471 | { | |
1472 | if (amnt == 0) | |
1473 | return; | |
1474 | ||
1475 | unsigned char carry_over = 0U; | |
46a61395 | 1476 | unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt); |
f663d9ad KT |
1477 | unsigned char clear_mask = (~0U) << amnt; |
1478 | ||
1479 | for (unsigned int i = 0; i < sz; i++) | |
1480 | { | |
1481 | unsigned prev_carry_over = carry_over; | |
46a61395 | 1482 | carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt); |
f663d9ad KT |
1483 | |
1484 | ptr[i] <<= amnt; | |
1485 | if (i != 0) | |
1486 | { | |
1487 | ptr[i] &= clear_mask; | |
1488 | ptr[i] |= prev_carry_over; | |
1489 | } | |
1490 | } | |
1491 | } | |
1492 | ||
1493 | /* Like shift_bytes_in_array but for big-endian. | |
1494 | Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the | |
1495 | bits between adjacent elements. AMNT should be within | |
1496 | [0, BITS_PER_UNIT). | |
1497 | Example, AMNT = 2: | |
1498 | 00011111|11100000 >> 2 = 00000111|11111000 | |
1499 | PTR[0] | PTR[1] PTR[0] | PTR[1]. */ | |
1500 | ||
1501 | static void | |
1502 | shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz, | |
1503 | unsigned int amnt) | |
1504 | { | |
1505 | if (amnt == 0) | |
1506 | return; | |
1507 | ||
1508 | unsigned char carry_over = 0U; | |
1509 | unsigned char carry_mask = ~(~0U << amnt); | |
1510 | ||
1511 | for (unsigned int i = 0; i < sz; i++) | |
1512 | { | |
1513 | unsigned prev_carry_over = carry_over; | |
46a61395 | 1514 | carry_over = ptr[i] & carry_mask; |
f663d9ad | 1515 | |
ad1de652 JJ |
1516 | carry_over <<= (unsigned char) BITS_PER_UNIT - amnt; |
1517 | ptr[i] >>= amnt; | |
1518 | ptr[i] |= prev_carry_over; | |
f663d9ad KT |
1519 | } |
1520 | } | |
1521 | ||
1522 | /* Clear out LEN bits starting from bit START in the byte array | |
1523 | PTR. This clears the bits to the *right* from START. | |
1524 | START must be within [0, BITS_PER_UNIT) and counts starting from | |
1525 | the least significant bit. */ | |
1526 | ||
1527 | static void | |
1528 | clear_bit_region_be (unsigned char *ptr, unsigned int start, | |
1529 | unsigned int len) | |
1530 | { | |
1531 | if (len == 0) | |
1532 | return; | |
1533 | /* Clear len bits to the right of start. */ | |
1534 | else if (len <= start + 1) | |
1535 | { | |
1536 | unsigned char mask = (~(~0U << len)); | |
1537 | mask = mask << (start + 1U - len); | |
1538 | ptr[0] &= ~mask; | |
1539 | } | |
1540 | else if (start != BITS_PER_UNIT - 1) | |
1541 | { | |
1542 | clear_bit_region_be (ptr, start, (start % BITS_PER_UNIT) + 1); | |
1543 | clear_bit_region_be (ptr + 1, BITS_PER_UNIT - 1, | |
1544 | len - (start % BITS_PER_UNIT) - 1); | |
1545 | } | |
1546 | else if (start == BITS_PER_UNIT - 1 | |
1547 | && len > BITS_PER_UNIT) | |
1548 | { | |
1549 | unsigned int nbytes = len / BITS_PER_UNIT; | |
a62b3dc5 | 1550 | memset (ptr, 0, nbytes); |
f663d9ad KT |
1551 | if (len % BITS_PER_UNIT != 0) |
1552 | clear_bit_region_be (ptr + nbytes, BITS_PER_UNIT - 1, | |
1553 | len % BITS_PER_UNIT); | |
1554 | } | |
1555 | else | |
1556 | gcc_unreachable (); | |
1557 | } | |
1558 | ||
1559 | /* In the byte array PTR clear the bit region starting at bit | |
1560 | START and is LEN bits wide. | |
1561 | For regions spanning multiple bytes do this recursively until we reach | |
1562 | zero LEN or a region contained within a single byte. */ | |
1563 | ||
1564 | static void | |
1565 | clear_bit_region (unsigned char *ptr, unsigned int start, | |
1566 | unsigned int len) | |
1567 | { | |
1568 | /* Degenerate base case. */ | |
1569 | if (len == 0) | |
1570 | return; | |
1571 | else if (start >= BITS_PER_UNIT) | |
1572 | clear_bit_region (ptr + 1, start - BITS_PER_UNIT, len); | |
1573 | /* Second base case. */ | |
1574 | else if ((start + len) <= BITS_PER_UNIT) | |
1575 | { | |
46a61395 | 1576 | unsigned char mask = (~0U) << (unsigned char) (BITS_PER_UNIT - len); |
f663d9ad KT |
1577 | mask >>= BITS_PER_UNIT - (start + len); |
1578 | ||
1579 | ptr[0] &= ~mask; | |
1580 | ||
1581 | return; | |
1582 | } | |
1583 | /* Clear most significant bits in a byte and proceed with the next byte. */ | |
1584 | else if (start != 0) | |
1585 | { | |
1586 | clear_bit_region (ptr, start, BITS_PER_UNIT - start); | |
1f069ef5 | 1587 | clear_bit_region (ptr + 1, 0, len - (BITS_PER_UNIT - start)); |
f663d9ad KT |
1588 | } |
1589 | /* Whole bytes need to be cleared. */ | |
1590 | else if (start == 0 && len > BITS_PER_UNIT) | |
1591 | { | |
1592 | unsigned int nbytes = len / BITS_PER_UNIT; | |
a848c710 KT |
1593 | /* We could recurse on each byte but we clear whole bytes, so a simple |
1594 | memset will do. */ | |
46a61395 | 1595 | memset (ptr, '\0', nbytes); |
f663d9ad KT |
1596 | /* Clear the remaining sub-byte region if there is one. */ |
1597 | if (len % BITS_PER_UNIT != 0) | |
1598 | clear_bit_region (ptr + nbytes, 0, len % BITS_PER_UNIT); | |
1599 | } | |
1600 | else | |
1601 | gcc_unreachable (); | |
1602 | } | |
1603 | ||
1604 | /* Write BITLEN bits of EXPR to the byte array PTR at | |
1605 | bit position BITPOS. PTR should contain TOTAL_BYTES elements. | |
1606 | Return true if the operation succeeded. */ | |
1607 | ||
1608 | static bool | |
1609 | encode_tree_to_bitpos (tree expr, unsigned char *ptr, int bitlen, int bitpos, | |
46a61395 | 1610 | unsigned int total_bytes) |
f663d9ad KT |
1611 | { |
1612 | unsigned int first_byte = bitpos / BITS_PER_UNIT; | |
1613 | tree tmp_int = expr; | |
ad1de652 JJ |
1614 | bool sub_byte_op_p = ((bitlen % BITS_PER_UNIT) |
1615 | || (bitpos % BITS_PER_UNIT) | |
f4b31647 | 1616 | || !int_mode_for_size (bitlen, 0).exists ()); |
f663d9ad KT |
1617 | |
1618 | if (!sub_byte_op_p) | |
2f391428 | 1619 | return native_encode_expr (tmp_int, ptr + first_byte, total_bytes) != 0; |
f663d9ad KT |
1620 | |
1621 | /* LITTLE-ENDIAN | |
1622 | We are writing a non byte-sized quantity or at a position that is not | |
1623 | at a byte boundary. | |
1624 | |--------|--------|--------| ptr + first_byte | |
1625 | ^ ^ | |
1626 | xxx xxxxxxxx xxx< bp> | |
1627 | |______EXPR____| | |
1628 | ||
46a61395 | 1629 | First native_encode_expr EXPR into a temporary buffer and shift each |
f663d9ad KT |
1630 | byte in the buffer by 'bp' (carrying the bits over as necessary). |
1631 | |00000000|00xxxxxx|xxxxxxxx| << bp = |000xxxxx|xxxxxxxx|xxx00000| | |
1632 | <------bitlen---->< bp> | |
1633 | Then we clear the destination bits: | |
1634 | |---00000|00000000|000-----| ptr + first_byte | |
1635 | <-------bitlen--->< bp> | |
1636 | ||
1637 | Finally we ORR the bytes of the shifted EXPR into the cleared region: | |
1638 | |---xxxxx||xxxxxxxx||xxx-----| ptr + first_byte. | |
1639 | ||
1640 | BIG-ENDIAN | |
1641 | We are writing a non byte-sized quantity or at a position that is not | |
1642 | at a byte boundary. | |
1643 | ptr + first_byte |--------|--------|--------| | |
1644 | ^ ^ | |
1645 | <bp >xxx xxxxxxxx xxx | |
1646 | |_____EXPR_____| | |
1647 | ||
46a61395 | 1648 | First native_encode_expr EXPR into a temporary buffer and shift each |
f663d9ad KT |
1649 | byte in the buffer to the right by (carrying the bits over as necessary). |
1650 | We shift by as much as needed to align the most significant bit of EXPR | |
1651 | with bitpos: | |
1652 | |00xxxxxx|xxxxxxxx| >> 3 = |00000xxx|xxxxxxxx|xxxxx000| | |
1653 | <---bitlen----> <bp ><-----bitlen-----> | |
1654 | Then we clear the destination bits: | |
1655 | ptr + first_byte |-----000||00000000||00000---| | |
1656 | <bp ><-------bitlen-----> | |
1657 | ||
1658 | Finally we ORR the bytes of the shifted EXPR into the cleared region: | |
1659 | ptr + first_byte |---xxxxx||xxxxxxxx||xxx-----|. | |
1660 | The awkwardness comes from the fact that bitpos is counted from the | |
1661 | most significant bit of a byte. */ | |
1662 | ||
ef1d3b57 RS |
1663 | /* We must be dealing with fixed-size data at this point, since the |
1664 | total size is also fixed. */ | |
1665 | fixed_size_mode mode = as_a <fixed_size_mode> (TYPE_MODE (TREE_TYPE (expr))); | |
f663d9ad | 1666 | /* Allocate an extra byte so that we have space to shift into. */ |
ef1d3b57 | 1667 | unsigned int byte_size = GET_MODE_SIZE (mode) + 1; |
f663d9ad | 1668 | unsigned char *tmpbuf = XALLOCAVEC (unsigned char, byte_size); |
46a61395 | 1669 | memset (tmpbuf, '\0', byte_size); |
f663d9ad KT |
1670 | /* The store detection code should only have allowed constants that are |
1671 | accepted by native_encode_expr. */ | |
2f391428 | 1672 | if (native_encode_expr (expr, tmpbuf, byte_size - 1) == 0) |
f663d9ad KT |
1673 | gcc_unreachable (); |
1674 | ||
1675 | /* The native_encode_expr machinery uses TYPE_MODE to determine how many | |
1676 | bytes to write. This means it can write more than | |
1677 | ROUND_UP (bitlen, BITS_PER_UNIT) / BITS_PER_UNIT bytes (for example | |
1678 | write 8 bytes for a bitlen of 40). Skip the bytes that are not within | |
1679 | bitlen and zero out the bits that are not relevant as well (that may | |
1680 | contain a sign bit due to sign-extension). */ | |
1681 | unsigned int padding | |
1682 | = byte_size - ROUND_UP (bitlen, BITS_PER_UNIT) / BITS_PER_UNIT - 1; | |
ad1de652 JJ |
1683 | /* On big-endian the padding is at the 'front' so just skip the initial |
1684 | bytes. */ | |
1685 | if (BYTES_BIG_ENDIAN) | |
1686 | tmpbuf += padding; | |
1687 | ||
1688 | byte_size -= padding; | |
1689 | ||
1690 | if (bitlen % BITS_PER_UNIT != 0) | |
f663d9ad | 1691 | { |
4b2c06f4 | 1692 | if (BYTES_BIG_ENDIAN) |
ad1de652 JJ |
1693 | clear_bit_region_be (tmpbuf, BITS_PER_UNIT - 1, |
1694 | BITS_PER_UNIT - (bitlen % BITS_PER_UNIT)); | |
1695 | else | |
1696 | clear_bit_region (tmpbuf, bitlen, | |
1697 | byte_size * BITS_PER_UNIT - bitlen); | |
f663d9ad | 1698 | } |
ad1de652 JJ |
1699 | /* Left shifting relies on the last byte being clear if bitlen is |
1700 | a multiple of BITS_PER_UNIT, which might not be clear if | |
1701 | there are padding bytes. */ | |
1702 | else if (!BYTES_BIG_ENDIAN) | |
1703 | tmpbuf[byte_size - 1] = '\0'; | |
f663d9ad KT |
1704 | |
1705 | /* Clear the bit region in PTR where the bits from TMPBUF will be | |
46a61395 | 1706 | inserted into. */ |
f663d9ad KT |
1707 | if (BYTES_BIG_ENDIAN) |
1708 | clear_bit_region_be (ptr + first_byte, | |
1709 | BITS_PER_UNIT - 1 - (bitpos % BITS_PER_UNIT), bitlen); | |
1710 | else | |
1711 | clear_bit_region (ptr + first_byte, bitpos % BITS_PER_UNIT, bitlen); | |
1712 | ||
1713 | int shift_amnt; | |
1714 | int bitlen_mod = bitlen % BITS_PER_UNIT; | |
1715 | int bitpos_mod = bitpos % BITS_PER_UNIT; | |
1716 | ||
1717 | bool skip_byte = false; | |
1718 | if (BYTES_BIG_ENDIAN) | |
1719 | { | |
1720 | /* BITPOS and BITLEN are exactly aligned and no shifting | |
1721 | is necessary. */ | |
1722 | if (bitpos_mod + bitlen_mod == BITS_PER_UNIT | |
1723 | || (bitpos_mod == 0 && bitlen_mod == 0)) | |
1724 | shift_amnt = 0; | |
1725 | /* |. . . . . . . .| | |
1726 | <bp > <blen >. | |
1727 | We always shift right for BYTES_BIG_ENDIAN so shift the beginning | |
1728 | of the value until it aligns with 'bp' in the next byte over. */ | |
1729 | else if (bitpos_mod + bitlen_mod < BITS_PER_UNIT) | |
1730 | { | |
1731 | shift_amnt = bitlen_mod + bitpos_mod; | |
1732 | skip_byte = bitlen_mod != 0; | |
1733 | } | |
1734 | /* |. . . . . . . .| | |
1735 | <----bp---> | |
1736 | <---blen---->. | |
1737 | Shift the value right within the same byte so it aligns with 'bp'. */ | |
1738 | else | |
1739 | shift_amnt = bitlen_mod + bitpos_mod - BITS_PER_UNIT; | |
1740 | } | |
1741 | else | |
1742 | shift_amnt = bitpos % BITS_PER_UNIT; | |
1743 | ||
1744 | /* Create the shifted version of EXPR. */ | |
1745 | if (!BYTES_BIG_ENDIAN) | |
46a61395 JJ |
1746 | { |
1747 | shift_bytes_in_array (tmpbuf, byte_size, shift_amnt); | |
1748 | if (shift_amnt == 0) | |
1749 | byte_size--; | |
1750 | } | |
f663d9ad KT |
1751 | else |
1752 | { | |
1753 | gcc_assert (BYTES_BIG_ENDIAN); | |
1754 | shift_bytes_in_array_right (tmpbuf, byte_size, shift_amnt); | |
1755 | /* If shifting right forced us to move into the next byte skip the now | |
1756 | empty byte. */ | |
1757 | if (skip_byte) | |
1758 | { | |
1759 | tmpbuf++; | |
1760 | byte_size--; | |
1761 | } | |
1762 | } | |
1763 | ||
1764 | /* Insert the bits from TMPBUF. */ | |
1765 | for (unsigned int i = 0; i < byte_size; i++) | |
1766 | ptr[first_byte + i] |= tmpbuf[i]; | |
1767 | ||
1768 | return true; | |
1769 | } | |
1770 | ||
1771 | /* Sorting function for store_immediate_info objects. | |
1772 | Sorts them by bitposition. */ | |
1773 | ||
1774 | static int | |
1775 | sort_by_bitpos (const void *x, const void *y) | |
1776 | { | |
1777 | store_immediate_info *const *tmp = (store_immediate_info * const *) x; | |
1778 | store_immediate_info *const *tmp2 = (store_immediate_info * const *) y; | |
1779 | ||
109cca3b | 1780 | if ((*tmp)->bitpos < (*tmp2)->bitpos) |
f663d9ad KT |
1781 | return -1; |
1782 | else if ((*tmp)->bitpos > (*tmp2)->bitpos) | |
1783 | return 1; | |
109cca3b | 1784 | else |
0f0027d1 KT |
1785 | /* If they are the same let's use the order which is guaranteed to |
1786 | be different. */ | |
1787 | return (*tmp)->order - (*tmp2)->order; | |
f663d9ad KT |
1788 | } |
1789 | ||
1790 | /* Sorting function for store_immediate_info objects. | |
1791 | Sorts them by the order field. */ | |
1792 | ||
1793 | static int | |
1794 | sort_by_order (const void *x, const void *y) | |
1795 | { | |
1796 | store_immediate_info *const *tmp = (store_immediate_info * const *) x; | |
1797 | store_immediate_info *const *tmp2 = (store_immediate_info * const *) y; | |
1798 | ||
1799 | if ((*tmp)->order < (*tmp2)->order) | |
1800 | return -1; | |
1801 | else if ((*tmp)->order > (*tmp2)->order) | |
1802 | return 1; | |
1803 | ||
1804 | gcc_unreachable (); | |
1805 | } | |
1806 | ||
1807 | /* Initialize a merged_store_group object from a store_immediate_info | |
1808 | object. */ | |
1809 | ||
1810 | merged_store_group::merged_store_group (store_immediate_info *info) | |
1811 | { | |
1812 | start = info->bitpos; | |
1813 | width = info->bitsize; | |
a62b3dc5 JJ |
1814 | bitregion_start = info->bitregion_start; |
1815 | bitregion_end = info->bitregion_end; | |
f663d9ad KT |
1816 | /* VAL has memory allocated for it in apply_stores once the group |
1817 | width has been finalized. */ | |
1818 | val = NULL; | |
a62b3dc5 | 1819 | mask = NULL; |
c94c3532 | 1820 | bit_insertion = false; |
a62b3dc5 JJ |
1821 | unsigned HOST_WIDE_INT align_bitpos = 0; |
1822 | get_object_alignment_1 (gimple_assign_lhs (info->stmt), | |
1823 | &align, &align_bitpos); | |
1824 | align_base = start - align_bitpos; | |
245f6de1 JJ |
1825 | for (int i = 0; i < 2; ++i) |
1826 | { | |
1827 | store_operand_info &op = info->ops[i]; | |
1828 | if (op.base_addr == NULL_TREE) | |
1829 | { | |
1830 | load_align[i] = 0; | |
1831 | load_align_base[i] = 0; | |
1832 | } | |
1833 | else | |
1834 | { | |
1835 | get_object_alignment_1 (op.val, &load_align[i], &align_bitpos); | |
1836 | load_align_base[i] = op.bitpos - align_bitpos; | |
1837 | } | |
1838 | } | |
f663d9ad KT |
1839 | stores.create (1); |
1840 | stores.safe_push (info); | |
1841 | last_stmt = info->stmt; | |
1842 | last_order = info->order; | |
1843 | first_stmt = last_stmt; | |
1844 | first_order = last_order; | |
1845 | buf_size = 0; | |
1846 | } | |
1847 | ||
1848 | merged_store_group::~merged_store_group () | |
1849 | { | |
1850 | if (val) | |
1851 | XDELETEVEC (val); | |
1852 | } | |
1853 | ||
a62b3dc5 JJ |
1854 | /* Helper method for merge_into and merge_overlapping to do |
1855 | the common part. */ | |
f663d9ad | 1856 | void |
a62b3dc5 | 1857 | merged_store_group::do_merge (store_immediate_info *info) |
f663d9ad | 1858 | { |
a62b3dc5 JJ |
1859 | bitregion_start = MIN (bitregion_start, info->bitregion_start); |
1860 | bitregion_end = MAX (bitregion_end, info->bitregion_end); | |
1861 | ||
1862 | unsigned int this_align; | |
1863 | unsigned HOST_WIDE_INT align_bitpos = 0; | |
1864 | get_object_alignment_1 (gimple_assign_lhs (info->stmt), | |
1865 | &this_align, &align_bitpos); | |
1866 | if (this_align > align) | |
1867 | { | |
1868 | align = this_align; | |
1869 | align_base = info->bitpos - align_bitpos; | |
1870 | } | |
245f6de1 JJ |
1871 | for (int i = 0; i < 2; ++i) |
1872 | { | |
1873 | store_operand_info &op = info->ops[i]; | |
1874 | if (!op.base_addr) | |
1875 | continue; | |
1876 | ||
1877 | get_object_alignment_1 (op.val, &this_align, &align_bitpos); | |
1878 | if (this_align > load_align[i]) | |
1879 | { | |
1880 | load_align[i] = this_align; | |
1881 | load_align_base[i] = op.bitpos - align_bitpos; | |
1882 | } | |
1883 | } | |
f663d9ad | 1884 | |
f663d9ad KT |
1885 | gimple *stmt = info->stmt; |
1886 | stores.safe_push (info); | |
1887 | if (info->order > last_order) | |
1888 | { | |
1889 | last_order = info->order; | |
1890 | last_stmt = stmt; | |
1891 | } | |
1892 | else if (info->order < first_order) | |
1893 | { | |
1894 | first_order = info->order; | |
1895 | first_stmt = stmt; | |
1896 | } | |
1897 | } | |
1898 | ||
a62b3dc5 JJ |
1899 | /* Merge a store recorded by INFO into this merged store. |
1900 | The store is not overlapping with the existing recorded | |
1901 | stores. */ | |
1902 | ||
1903 | void | |
1904 | merged_store_group::merge_into (store_immediate_info *info) | |
1905 | { | |
a62b3dc5 JJ |
1906 | /* Make sure we're inserting in the position we think we're inserting. */ |
1907 | gcc_assert (info->bitpos >= start + width | |
1908 | && info->bitregion_start <= bitregion_end); | |
1909 | ||
c5679c37 | 1910 | width = info->bitpos + info->bitsize - start; |
a62b3dc5 JJ |
1911 | do_merge (info); |
1912 | } | |
1913 | ||
f663d9ad KT |
1914 | /* Merge a store described by INFO into this merged store. |
1915 | INFO overlaps in some way with the current store (i.e. it's not contiguous | |
1916 | which is handled by merged_store_group::merge_into). */ | |
1917 | ||
1918 | void | |
1919 | merged_store_group::merge_overlapping (store_immediate_info *info) | |
1920 | { | |
f663d9ad | 1921 | /* If the store extends the size of the group, extend the width. */ |
a62b3dc5 | 1922 | if (info->bitpos + info->bitsize > start + width) |
c5679c37 | 1923 | width = info->bitpos + info->bitsize - start; |
f663d9ad | 1924 | |
a62b3dc5 | 1925 | do_merge (info); |
f663d9ad KT |
1926 | } |
1927 | ||
1928 | /* Go through all the recorded stores in this group in program order and | |
1929 | apply their values to the VAL byte array to create the final merged | |
1930 | value. Return true if the operation succeeded. */ | |
1931 | ||
1932 | bool | |
1933 | merged_store_group::apply_stores () | |
1934 | { | |
a62b3dc5 JJ |
1935 | /* Make sure we have more than one store in the group, otherwise we cannot |
1936 | merge anything. */ | |
1937 | if (bitregion_start % BITS_PER_UNIT != 0 | |
1938 | || bitregion_end % BITS_PER_UNIT != 0 | |
f663d9ad KT |
1939 | || stores.length () == 1) |
1940 | return false; | |
1941 | ||
1942 | stores.qsort (sort_by_order); | |
a62b3dc5 | 1943 | store_immediate_info *info; |
f663d9ad | 1944 | unsigned int i; |
c94c3532 EB |
1945 | /* Create a power-of-2-sized buffer for native_encode_expr. */ |
1946 | buf_size = 1 << ceil_log2 ((bitregion_end - bitregion_start) / BITS_PER_UNIT); | |
a62b3dc5 JJ |
1947 | val = XNEWVEC (unsigned char, 2 * buf_size); |
1948 | mask = val + buf_size; | |
1949 | memset (val, 0, buf_size); | |
1950 | memset (mask, ~0U, buf_size); | |
f663d9ad KT |
1951 | |
1952 | FOR_EACH_VEC_ELT (stores, i, info) | |
1953 | { | |
a62b3dc5 | 1954 | unsigned int pos_in_buffer = info->bitpos - bitregion_start; |
c94c3532 | 1955 | tree cst; |
245f6de1 JJ |
1956 | if (info->ops[0].val && info->ops[0].base_addr == NULL_TREE) |
1957 | cst = info->ops[0].val; | |
1958 | else if (info->ops[1].val && info->ops[1].base_addr == NULL_TREE) | |
1959 | cst = info->ops[1].val; | |
c94c3532 EB |
1960 | else |
1961 | cst = NULL_TREE; | |
245f6de1 JJ |
1962 | bool ret = true; |
1963 | if (cst) | |
c94c3532 EB |
1964 | { |
1965 | if (info->rhs_code == BIT_INSERT_EXPR) | |
1966 | bit_insertion = true; | |
1967 | else | |
1968 | ret = encode_tree_to_bitpos (cst, val, info->bitsize, | |
1969 | pos_in_buffer, buf_size); | |
1970 | } | |
1971 | unsigned char *m = mask + (pos_in_buffer / BITS_PER_UNIT); | |
1972 | if (BYTES_BIG_ENDIAN) | |
1973 | clear_bit_region_be (m, (BITS_PER_UNIT - 1 | |
1974 | - (pos_in_buffer % BITS_PER_UNIT)), | |
1975 | info->bitsize); | |
1976 | else | |
1977 | clear_bit_region (m, pos_in_buffer % BITS_PER_UNIT, info->bitsize); | |
245f6de1 | 1978 | if (cst && dump_file && (dump_flags & TDF_DETAILS)) |
f663d9ad KT |
1979 | { |
1980 | if (ret) | |
1981 | { | |
c94c3532 | 1982 | fputs ("After writing ", dump_file); |
245f6de1 | 1983 | print_generic_expr (dump_file, cst, 0); |
f663d9ad | 1984 | fprintf (dump_file, " of size " HOST_WIDE_INT_PRINT_DEC |
c94c3532 EB |
1985 | " at position %d\n", info->bitsize, pos_in_buffer); |
1986 | fputs (" the merged value contains ", dump_file); | |
f663d9ad | 1987 | dump_char_array (dump_file, val, buf_size); |
c94c3532 EB |
1988 | fputs (" the merged mask contains ", dump_file); |
1989 | dump_char_array (dump_file, mask, buf_size); | |
1990 | if (bit_insertion) | |
1991 | fputs (" bit insertion is required\n", dump_file); | |
f663d9ad KT |
1992 | } |
1993 | else | |
1994 | fprintf (dump_file, "Failed to merge stores\n"); | |
4b84d9b8 | 1995 | } |
f663d9ad KT |
1996 | if (!ret) |
1997 | return false; | |
1998 | } | |
4b84d9b8 | 1999 | stores.qsort (sort_by_bitpos); |
f663d9ad KT |
2000 | return true; |
2001 | } | |
2002 | ||
2003 | /* Structure describing the store chain. */ | |
2004 | ||
2005 | struct imm_store_chain_info | |
2006 | { | |
50b6d676 AO |
2007 | /* Doubly-linked list that imposes an order on chain processing. |
2008 | PNXP (prev's next pointer) points to the head of a list, or to | |
2009 | the next field in the previous chain in the list. | |
2010 | See pass_store_merging::m_stores_head for more rationale. */ | |
2011 | imm_store_chain_info *next, **pnxp; | |
b5926e23 | 2012 | tree base_addr; |
a62b3dc5 | 2013 | auto_vec<store_immediate_info *> m_store_info; |
f663d9ad KT |
2014 | auto_vec<merged_store_group *> m_merged_store_groups; |
2015 | ||
50b6d676 AO |
2016 | imm_store_chain_info (imm_store_chain_info *&inspt, tree b_a) |
2017 | : next (inspt), pnxp (&inspt), base_addr (b_a) | |
2018 | { | |
2019 | inspt = this; | |
2020 | if (next) | |
2021 | { | |
2022 | gcc_checking_assert (pnxp == next->pnxp); | |
2023 | next->pnxp = &next; | |
2024 | } | |
2025 | } | |
2026 | ~imm_store_chain_info () | |
2027 | { | |
2028 | *pnxp = next; | |
2029 | if (next) | |
2030 | { | |
2031 | gcc_checking_assert (&next == next->pnxp); | |
2032 | next->pnxp = pnxp; | |
2033 | } | |
2034 | } | |
b5926e23 | 2035 | bool terminate_and_process_chain (); |
4b84d9b8 | 2036 | bool try_coalesce_bswap (merged_store_group *, unsigned int, unsigned int); |
f663d9ad | 2037 | bool coalesce_immediate_stores (); |
b5926e23 RB |
2038 | bool output_merged_store (merged_store_group *); |
2039 | bool output_merged_stores (); | |
f663d9ad KT |
2040 | }; |
2041 | ||
2042 | const pass_data pass_data_tree_store_merging = { | |
2043 | GIMPLE_PASS, /* type */ | |
2044 | "store-merging", /* name */ | |
2045 | OPTGROUP_NONE, /* optinfo_flags */ | |
2046 | TV_GIMPLE_STORE_MERGING, /* tv_id */ | |
2047 | PROP_ssa, /* properties_required */ | |
2048 | 0, /* properties_provided */ | |
2049 | 0, /* properties_destroyed */ | |
2050 | 0, /* todo_flags_start */ | |
2051 | TODO_update_ssa, /* todo_flags_finish */ | |
2052 | }; | |
2053 | ||
2054 | class pass_store_merging : public gimple_opt_pass | |
2055 | { | |
2056 | public: | |
2057 | pass_store_merging (gcc::context *ctxt) | |
faec5f24 | 2058 | : gimple_opt_pass (pass_data_tree_store_merging, ctxt), m_stores_head () |
f663d9ad KT |
2059 | { |
2060 | } | |
2061 | ||
c94c3532 EB |
2062 | /* Pass not supported for PDP-endian, nor for insane hosts or |
2063 | target character sizes where native_{encode,interpret}_expr | |
a62b3dc5 | 2064 | doesn't work properly. */ |
f663d9ad KT |
2065 | virtual bool |
2066 | gate (function *) | |
2067 | { | |
a62b3dc5 | 2068 | return flag_store_merging |
c94c3532 | 2069 | && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN |
a62b3dc5 JJ |
2070 | && CHAR_BIT == 8 |
2071 | && BITS_PER_UNIT == 8; | |
f663d9ad KT |
2072 | } |
2073 | ||
2074 | virtual unsigned int execute (function *); | |
2075 | ||
2076 | private: | |
2077 | hash_map<tree_operand_hash, struct imm_store_chain_info *> m_stores; | |
2078 | ||
50b6d676 AO |
2079 | /* Form a doubly-linked stack of the elements of m_stores, so that |
2080 | we can iterate over them in a predictable way. Using this order | |
2081 | avoids extraneous differences in the compiler output just because | |
2082 | of tree pointer variations (e.g. different chains end up in | |
2083 | different positions of m_stores, so they are handled in different | |
2084 | orders, so they allocate or release SSA names in different | |
2085 | orders, and when they get reused, subsequent passes end up | |
2086 | getting different SSA names, which may ultimately change | |
2087 | decisions when going out of SSA). */ | |
2088 | imm_store_chain_info *m_stores_head; | |
2089 | ||
245f6de1 | 2090 | void process_store (gimple *); |
f663d9ad | 2091 | bool terminate_and_process_all_chains (); |
383ac8dc | 2092 | bool terminate_all_aliasing_chains (imm_store_chain_info **, gimple *); |
b5926e23 | 2093 | bool terminate_and_release_chain (imm_store_chain_info *); |
f663d9ad KT |
2094 | }; // class pass_store_merging |
2095 | ||
2096 | /* Terminate and process all recorded chains. Return true if any changes | |
2097 | were made. */ | |
2098 | ||
2099 | bool | |
2100 | pass_store_merging::terminate_and_process_all_chains () | |
2101 | { | |
f663d9ad | 2102 | bool ret = false; |
50b6d676 AO |
2103 | while (m_stores_head) |
2104 | ret |= terminate_and_release_chain (m_stores_head); | |
2105 | gcc_assert (m_stores.elements () == 0); | |
2106 | gcc_assert (m_stores_head == NULL); | |
f663d9ad KT |
2107 | |
2108 | return ret; | |
2109 | } | |
2110 | ||
383ac8dc JJ |
2111 | /* Terminate all chains that are affected by the statement STMT. |
2112 | CHAIN_INFO is the chain we should ignore from the checks if | |
2113 | non-NULL. */ | |
f663d9ad KT |
2114 | |
2115 | bool | |
20770eb8 | 2116 | pass_store_merging::terminate_all_aliasing_chains (imm_store_chain_info |
b5926e23 | 2117 | **chain_info, |
f663d9ad KT |
2118 | gimple *stmt) |
2119 | { | |
2120 | bool ret = false; | |
2121 | ||
2122 | /* If the statement doesn't touch memory it can't alias. */ | |
2123 | if (!gimple_vuse (stmt)) | |
2124 | return false; | |
2125 | ||
9e875fd8 | 2126 | tree store_lhs = gimple_store_p (stmt) ? gimple_get_lhs (stmt) : NULL_TREE; |
383ac8dc | 2127 | for (imm_store_chain_info *next = m_stores_head, *cur = next; cur; cur = next) |
f663d9ad | 2128 | { |
383ac8dc JJ |
2129 | next = cur->next; |
2130 | ||
2131 | /* We already checked all the stores in chain_info and terminated the | |
2132 | chain if necessary. Skip it here. */ | |
2133 | if (chain_info && *chain_info == cur) | |
2134 | continue; | |
2135 | ||
245f6de1 JJ |
2136 | store_immediate_info *info; |
2137 | unsigned int i; | |
383ac8dc | 2138 | FOR_EACH_VEC_ELT (cur->m_store_info, i, info) |
f663d9ad | 2139 | { |
9e875fd8 JJ |
2140 | tree lhs = gimple_assign_lhs (info->stmt); |
2141 | if (ref_maybe_used_by_stmt_p (stmt, lhs) | |
2142 | || stmt_may_clobber_ref_p (stmt, lhs) | |
2143 | || (store_lhs && refs_output_dependent_p (store_lhs, lhs))) | |
f663d9ad | 2144 | { |
245f6de1 | 2145 | if (dump_file && (dump_flags & TDF_DETAILS)) |
f663d9ad | 2146 | { |
245f6de1 JJ |
2147 | fprintf (dump_file, "stmt causes chain termination:\n"); |
2148 | print_gimple_stmt (dump_file, stmt, 0); | |
f663d9ad | 2149 | } |
383ac8dc | 2150 | terminate_and_release_chain (cur); |
245f6de1 JJ |
2151 | ret = true; |
2152 | break; | |
f663d9ad KT |
2153 | } |
2154 | } | |
2155 | } | |
2156 | ||
f663d9ad KT |
2157 | return ret; |
2158 | } | |
2159 | ||
2160 | /* Helper function. Terminate the recorded chain storing to base object | |
2161 | BASE. Return true if the merging and output was successful. The m_stores | |
2162 | entry is removed after the processing in any case. */ | |
2163 | ||
2164 | bool | |
b5926e23 | 2165 | pass_store_merging::terminate_and_release_chain (imm_store_chain_info *chain_info) |
f663d9ad | 2166 | { |
b5926e23 RB |
2167 | bool ret = chain_info->terminate_and_process_chain (); |
2168 | m_stores.remove (chain_info->base_addr); | |
2169 | delete chain_info; | |
f663d9ad KT |
2170 | return ret; |
2171 | } | |
2172 | ||
245f6de1 JJ |
2173 | /* Return true if stmts in between FIRST (inclusive) and LAST (exclusive) |
2174 | may clobber REF. FIRST and LAST must be in the same basic block and | |
4b84d9b8 JJ |
2175 | have non-NULL vdef. We want to be able to sink load of REF across |
2176 | stores between FIRST and LAST, up to right before LAST. */ | |
245f6de1 JJ |
2177 | |
2178 | bool | |
2179 | stmts_may_clobber_ref_p (gimple *first, gimple *last, tree ref) | |
2180 | { | |
2181 | ao_ref r; | |
2182 | ao_ref_init (&r, ref); | |
2183 | unsigned int count = 0; | |
2184 | tree vop = gimple_vdef (last); | |
2185 | gimple *stmt; | |
2186 | ||
2187 | gcc_checking_assert (gimple_bb (first) == gimple_bb (last)); | |
2188 | do | |
2189 | { | |
2190 | stmt = SSA_NAME_DEF_STMT (vop); | |
2191 | if (stmt_may_clobber_ref_p_1 (stmt, &r)) | |
2192 | return true; | |
4b84d9b8 JJ |
2193 | if (gimple_store_p (stmt) |
2194 | && refs_anti_dependent_p (ref, gimple_get_lhs (stmt))) | |
2195 | return true; | |
245f6de1 JJ |
2196 | /* Avoid quadratic compile time by bounding the number of checks |
2197 | we perform. */ | |
2198 | if (++count > MAX_STORE_ALIAS_CHECKS) | |
2199 | return true; | |
2200 | vop = gimple_vuse (stmt); | |
2201 | } | |
2202 | while (stmt != first); | |
2203 | return false; | |
2204 | } | |
2205 | ||
2206 | /* Return true if INFO->ops[IDX] is mergeable with the | |
2207 | corresponding loads already in MERGED_STORE group. | |
2208 | BASE_ADDR is the base address of the whole store group. */ | |
2209 | ||
2210 | bool | |
2211 | compatible_load_p (merged_store_group *merged_store, | |
2212 | store_immediate_info *info, | |
2213 | tree base_addr, int idx) | |
2214 | { | |
2215 | store_immediate_info *infof = merged_store->stores[0]; | |
2216 | if (!info->ops[idx].base_addr | |
8a91d545 RS |
2217 | || maybe_ne (info->ops[idx].bitpos - infof->ops[idx].bitpos, |
2218 | info->bitpos - infof->bitpos) | |
245f6de1 JJ |
2219 | || !operand_equal_p (info->ops[idx].base_addr, |
2220 | infof->ops[idx].base_addr, 0)) | |
2221 | return false; | |
2222 | ||
2223 | store_immediate_info *infol = merged_store->stores.last (); | |
2224 | tree load_vuse = gimple_vuse (info->ops[idx].stmt); | |
2225 | /* In this case all vuses should be the same, e.g. | |
2226 | _1 = s.a; _2 = s.b; _3 = _1 | 1; t.a = _3; _4 = _2 | 2; t.b = _4; | |
2227 | or | |
2228 | _1 = s.a; _2 = s.b; t.a = _1; t.b = _2; | |
2229 | and we can emit the coalesced load next to any of those loads. */ | |
2230 | if (gimple_vuse (infof->ops[idx].stmt) == load_vuse | |
2231 | && gimple_vuse (infol->ops[idx].stmt) == load_vuse) | |
2232 | return true; | |
2233 | ||
2234 | /* Otherwise, at least for now require that the load has the same | |
2235 | vuse as the store. See following examples. */ | |
2236 | if (gimple_vuse (info->stmt) != load_vuse) | |
2237 | return false; | |
2238 | ||
2239 | if (gimple_vuse (infof->stmt) != gimple_vuse (infof->ops[idx].stmt) | |
2240 | || (infof != infol | |
2241 | && gimple_vuse (infol->stmt) != gimple_vuse (infol->ops[idx].stmt))) | |
2242 | return false; | |
2243 | ||
2244 | /* If the load is from the same location as the store, already | |
2245 | the construction of the immediate chain info guarantees no intervening | |
2246 | stores, so no further checks are needed. Example: | |
2247 | _1 = s.a; _2 = _1 & -7; s.a = _2; _3 = s.b; _4 = _3 & -7; s.b = _4; */ | |
8a91d545 | 2248 | if (known_eq (info->ops[idx].bitpos, info->bitpos) |
245f6de1 JJ |
2249 | && operand_equal_p (info->ops[idx].base_addr, base_addr, 0)) |
2250 | return true; | |
2251 | ||
2252 | /* Otherwise, we need to punt if any of the loads can be clobbered by any | |
2253 | of the stores in the group, or any other stores in between those. | |
2254 | Previous calls to compatible_load_p ensured that for all the | |
2255 | merged_store->stores IDX loads, no stmts starting with | |
2256 | merged_store->first_stmt and ending right before merged_store->last_stmt | |
2257 | clobbers those loads. */ | |
2258 | gimple *first = merged_store->first_stmt; | |
2259 | gimple *last = merged_store->last_stmt; | |
2260 | unsigned int i; | |
2261 | store_immediate_info *infoc; | |
2262 | /* The stores are sorted by increasing store bitpos, so if info->stmt store | |
2263 | comes before the so far first load, we'll be changing | |
2264 | merged_store->first_stmt. In that case we need to give up if | |
2265 | any of the earlier processed loads clobber with the stmts in the new | |
2266 | range. */ | |
2267 | if (info->order < merged_store->first_order) | |
2268 | { | |
2269 | FOR_EACH_VEC_ELT (merged_store->stores, i, infoc) | |
2270 | if (stmts_may_clobber_ref_p (info->stmt, first, infoc->ops[idx].val)) | |
2271 | return false; | |
2272 | first = info->stmt; | |
2273 | } | |
2274 | /* Similarly, we could change merged_store->last_stmt, so ensure | |
2275 | in that case no stmts in the new range clobber any of the earlier | |
2276 | processed loads. */ | |
2277 | else if (info->order > merged_store->last_order) | |
2278 | { | |
2279 | FOR_EACH_VEC_ELT (merged_store->stores, i, infoc) | |
2280 | if (stmts_may_clobber_ref_p (last, info->stmt, infoc->ops[idx].val)) | |
2281 | return false; | |
2282 | last = info->stmt; | |
2283 | } | |
2284 | /* And finally, we'd be adding a new load to the set, ensure it isn't | |
2285 | clobbered in the new range. */ | |
2286 | if (stmts_may_clobber_ref_p (first, last, info->ops[idx].val)) | |
2287 | return false; | |
2288 | ||
2289 | /* Otherwise, we are looking for: | |
2290 | _1 = s.a; _2 = _1 ^ 15; t.a = _2; _3 = s.b; _4 = _3 ^ 15; t.b = _4; | |
2291 | or | |
2292 | _1 = s.a; t.a = _1; _2 = s.b; t.b = _2; */ | |
2293 | return true; | |
2294 | } | |
2295 | ||
4b84d9b8 JJ |
2296 | /* Add all refs loaded to compute VAL to REFS vector. */ |
2297 | ||
2298 | void | |
2299 | gather_bswap_load_refs (vec<tree> *refs, tree val) | |
2300 | { | |
2301 | if (TREE_CODE (val) != SSA_NAME) | |
2302 | return; | |
2303 | ||
2304 | gimple *stmt = SSA_NAME_DEF_STMT (val); | |
2305 | if (!is_gimple_assign (stmt)) | |
2306 | return; | |
2307 | ||
2308 | if (gimple_assign_load_p (stmt)) | |
2309 | { | |
2310 | refs->safe_push (gimple_assign_rhs1 (stmt)); | |
2311 | return; | |
2312 | } | |
2313 | ||
2314 | switch (gimple_assign_rhs_class (stmt)) | |
2315 | { | |
2316 | case GIMPLE_BINARY_RHS: | |
2317 | gather_bswap_load_refs (refs, gimple_assign_rhs2 (stmt)); | |
2318 | /* FALLTHRU */ | |
2319 | case GIMPLE_UNARY_RHS: | |
2320 | gather_bswap_load_refs (refs, gimple_assign_rhs1 (stmt)); | |
2321 | break; | |
2322 | default: | |
2323 | gcc_unreachable (); | |
2324 | } | |
2325 | } | |
2326 | ||
c5679c37 JJ |
2327 | /* Check if there are any stores in M_STORE_INFO after index I |
2328 | (where M_STORE_INFO must be sorted by sort_by_bitpos) that overlap | |
2329 | a potential group ending with END that have their order | |
2330 | smaller than LAST_ORDER. RHS_CODE is the kind of store in the | |
2331 | group. Return true if there are no such stores. | |
2332 | Consider: | |
2333 | MEM[(long long int *)p_28] = 0; | |
2334 | MEM[(long long int *)p_28 + 8B] = 0; | |
2335 | MEM[(long long int *)p_28 + 16B] = 0; | |
2336 | MEM[(long long int *)p_28 + 24B] = 0; | |
2337 | _129 = (int) _130; | |
2338 | MEM[(int *)p_28 + 8B] = _129; | |
2339 | MEM[(int *)p_28].a = -1; | |
2340 | We already have | |
2341 | MEM[(long long int *)p_28] = 0; | |
2342 | MEM[(int *)p_28].a = -1; | |
2343 | stmts in the current group and need to consider if it is safe to | |
2344 | add MEM[(long long int *)p_28 + 8B] = 0; store into the same group. | |
2345 | There is an overlap between that store and the MEM[(int *)p_28 + 8B] = _129; | |
2346 | store though, so if we add the MEM[(long long int *)p_28 + 8B] = 0; | |
2347 | into the group and merging of those 3 stores is successful, merged | |
2348 | stmts will be emitted at the latest store from that group, i.e. | |
2349 | LAST_ORDER, which is the MEM[(int *)p_28].a = -1; store. | |
2350 | The MEM[(int *)p_28 + 8B] = _129; store that originally follows | |
2351 | the MEM[(long long int *)p_28 + 8B] = 0; would now be before it, | |
2352 | so we need to refuse merging MEM[(long long int *)p_28 + 8B] = 0; | |
2353 | into the group. That way it will be its own store group and will | |
2354 | not be touched. If RHS_CODE is INTEGER_CST and there are overlapping | |
2355 | INTEGER_CST stores, those are mergeable using merge_overlapping, | |
2356 | so don't return false for those. */ | |
2357 | ||
2358 | static bool | |
2359 | check_no_overlap (vec<store_immediate_info *> m_store_info, unsigned int i, | |
2360 | enum tree_code rhs_code, unsigned int last_order, | |
2361 | unsigned HOST_WIDE_INT end) | |
2362 | { | |
2363 | unsigned int len = m_store_info.length (); | |
2364 | for (++i; i < len; ++i) | |
2365 | { | |
2366 | store_immediate_info *info = m_store_info[i]; | |
2367 | if (info->bitpos >= end) | |
2368 | break; | |
2369 | if (info->order < last_order | |
2370 | && (rhs_code != INTEGER_CST || info->rhs_code != INTEGER_CST)) | |
2371 | return false; | |
2372 | } | |
2373 | return true; | |
2374 | } | |
2375 | ||
4b84d9b8 JJ |
2376 | /* Return true if m_store_info[first] and at least one following store |
2377 | form a group which store try_size bitsize value which is byte swapped | |
2378 | from a memory load or some value, or identity from some value. | |
2379 | This uses the bswap pass APIs. */ | |
2380 | ||
2381 | bool | |
2382 | imm_store_chain_info::try_coalesce_bswap (merged_store_group *merged_store, | |
2383 | unsigned int first, | |
2384 | unsigned int try_size) | |
2385 | { | |
2386 | unsigned int len = m_store_info.length (), last = first; | |
2387 | unsigned HOST_WIDE_INT width = m_store_info[first]->bitsize; | |
2388 | if (width >= try_size) | |
2389 | return false; | |
2390 | for (unsigned int i = first + 1; i < len; ++i) | |
2391 | { | |
2392 | if (m_store_info[i]->bitpos != m_store_info[first]->bitpos + width | |
2393 | || m_store_info[i]->ins_stmt == NULL) | |
2394 | return false; | |
2395 | width += m_store_info[i]->bitsize; | |
2396 | if (width >= try_size) | |
2397 | { | |
2398 | last = i; | |
2399 | break; | |
2400 | } | |
2401 | } | |
2402 | if (width != try_size) | |
2403 | return false; | |
2404 | ||
2405 | bool allow_unaligned | |
2406 | = !STRICT_ALIGNMENT && PARAM_VALUE (PARAM_STORE_MERGING_ALLOW_UNALIGNED); | |
2407 | /* Punt if the combined store would not be aligned and we need alignment. */ | |
2408 | if (!allow_unaligned) | |
2409 | { | |
2410 | unsigned int align = merged_store->align; | |
2411 | unsigned HOST_WIDE_INT align_base = merged_store->align_base; | |
2412 | for (unsigned int i = first + 1; i <= last; ++i) | |
2413 | { | |
2414 | unsigned int this_align; | |
2415 | unsigned HOST_WIDE_INT align_bitpos = 0; | |
2416 | get_object_alignment_1 (gimple_assign_lhs (m_store_info[i]->stmt), | |
2417 | &this_align, &align_bitpos); | |
2418 | if (this_align > align) | |
2419 | { | |
2420 | align = this_align; | |
2421 | align_base = m_store_info[i]->bitpos - align_bitpos; | |
2422 | } | |
2423 | } | |
2424 | unsigned HOST_WIDE_INT align_bitpos | |
2425 | = (m_store_info[first]->bitpos - align_base) & (align - 1); | |
2426 | if (align_bitpos) | |
2427 | align = least_bit_hwi (align_bitpos); | |
2428 | if (align < try_size) | |
2429 | return false; | |
2430 | } | |
2431 | ||
2432 | tree type; | |
2433 | switch (try_size) | |
2434 | { | |
2435 | case 16: type = uint16_type_node; break; | |
2436 | case 32: type = uint32_type_node; break; | |
2437 | case 64: type = uint64_type_node; break; | |
2438 | default: gcc_unreachable (); | |
2439 | } | |
2440 | struct symbolic_number n; | |
2441 | gimple *ins_stmt = NULL; | |
2442 | int vuse_store = -1; | |
2443 | unsigned int first_order = merged_store->first_order; | |
2444 | unsigned int last_order = merged_store->last_order; | |
2445 | gimple *first_stmt = merged_store->first_stmt; | |
2446 | gimple *last_stmt = merged_store->last_stmt; | |
c5679c37 | 2447 | unsigned HOST_WIDE_INT end = merged_store->start + merged_store->width; |
4b84d9b8 JJ |
2448 | store_immediate_info *infof = m_store_info[first]; |
2449 | ||
2450 | for (unsigned int i = first; i <= last; ++i) | |
2451 | { | |
2452 | store_immediate_info *info = m_store_info[i]; | |
2453 | struct symbolic_number this_n = info->n; | |
2454 | this_n.type = type; | |
2455 | if (!this_n.base_addr) | |
2456 | this_n.range = try_size / BITS_PER_UNIT; | |
30fa8e9c JJ |
2457 | else |
2458 | /* Update vuse in case it has changed by output_merged_stores. */ | |
2459 | this_n.vuse = gimple_vuse (info->ins_stmt); | |
4b84d9b8 JJ |
2460 | unsigned int bitpos = info->bitpos - infof->bitpos; |
2461 | if (!do_shift_rotate (LSHIFT_EXPR, &this_n, | |
2462 | BYTES_BIG_ENDIAN | |
2463 | ? try_size - info->bitsize - bitpos | |
2464 | : bitpos)) | |
2465 | return false; | |
aa11164a | 2466 | if (this_n.base_addr && vuse_store) |
4b84d9b8 JJ |
2467 | { |
2468 | unsigned int j; | |
2469 | for (j = first; j <= last; ++j) | |
2470 | if (this_n.vuse == gimple_vuse (m_store_info[j]->stmt)) | |
2471 | break; | |
2472 | if (j > last) | |
2473 | { | |
2474 | if (vuse_store == 1) | |
2475 | return false; | |
2476 | vuse_store = 0; | |
2477 | } | |
2478 | } | |
2479 | if (i == first) | |
2480 | { | |
2481 | n = this_n; | |
2482 | ins_stmt = info->ins_stmt; | |
2483 | } | |
2484 | else | |
2485 | { | |
c5679c37 | 2486 | if (n.base_addr && n.vuse != this_n.vuse) |
4b84d9b8 | 2487 | { |
c5679c37 JJ |
2488 | if (vuse_store == 0) |
2489 | return false; | |
2490 | vuse_store = 1; | |
4b84d9b8 | 2491 | } |
c5679c37 JJ |
2492 | if (info->order > last_order) |
2493 | { | |
2494 | last_order = info->order; | |
2495 | last_stmt = info->stmt; | |
2496 | } | |
2497 | else if (info->order < first_order) | |
2498 | { | |
2499 | first_order = info->order; | |
2500 | first_stmt = info->stmt; | |
2501 | } | |
2502 | end = MAX (end, info->bitpos + info->bitsize); | |
4b84d9b8 JJ |
2503 | |
2504 | ins_stmt = perform_symbolic_merge (ins_stmt, &n, info->ins_stmt, | |
2505 | &this_n, &n); | |
2506 | if (ins_stmt == NULL) | |
2507 | return false; | |
2508 | } | |
2509 | } | |
2510 | ||
2511 | uint64_t cmpxchg, cmpnop; | |
2512 | find_bswap_or_nop_finalize (&n, &cmpxchg, &cmpnop); | |
2513 | ||
2514 | /* A complete byte swap should make the symbolic number to start with | |
2515 | the largest digit in the highest order byte. Unchanged symbolic | |
2516 | number indicates a read with same endianness as target architecture. */ | |
2517 | if (n.n != cmpnop && n.n != cmpxchg) | |
2518 | return false; | |
2519 | ||
2520 | if (n.base_addr == NULL_TREE && !is_gimple_val (n.src)) | |
2521 | return false; | |
2522 | ||
c5679c37 JJ |
2523 | if (!check_no_overlap (m_store_info, last, LROTATE_EXPR, last_order, end)) |
2524 | return false; | |
2525 | ||
4b84d9b8 JJ |
2526 | /* Don't handle memory copy this way if normal non-bswap processing |
2527 | would handle it too. */ | |
2528 | if (n.n == cmpnop && (unsigned) n.n_ops == last - first + 1) | |
2529 | { | |
2530 | unsigned int i; | |
2531 | for (i = first; i <= last; ++i) | |
2532 | if (m_store_info[i]->rhs_code != MEM_REF) | |
2533 | break; | |
2534 | if (i == last + 1) | |
2535 | return false; | |
2536 | } | |
2537 | ||
2538 | if (n.n == cmpxchg) | |
2539 | switch (try_size) | |
2540 | { | |
2541 | case 16: | |
2542 | /* Will emit LROTATE_EXPR. */ | |
2543 | break; | |
2544 | case 32: | |
2545 | if (builtin_decl_explicit_p (BUILT_IN_BSWAP32) | |
2546 | && optab_handler (bswap_optab, SImode) != CODE_FOR_nothing) | |
2547 | break; | |
2548 | return false; | |
2549 | case 64: | |
2550 | if (builtin_decl_explicit_p (BUILT_IN_BSWAP64) | |
2551 | && optab_handler (bswap_optab, DImode) != CODE_FOR_nothing) | |
2552 | break; | |
2553 | return false; | |
2554 | default: | |
2555 | gcc_unreachable (); | |
2556 | } | |
2557 | ||
2558 | if (!allow_unaligned && n.base_addr) | |
2559 | { | |
2560 | unsigned int align = get_object_alignment (n.src); | |
2561 | if (align < try_size) | |
2562 | return false; | |
2563 | } | |
2564 | ||
2565 | /* If each load has vuse of the corresponding store, need to verify | |
2566 | the loads can be sunk right before the last store. */ | |
2567 | if (vuse_store == 1) | |
2568 | { | |
2569 | auto_vec<tree, 64> refs; | |
2570 | for (unsigned int i = first; i <= last; ++i) | |
2571 | gather_bswap_load_refs (&refs, | |
2572 | gimple_assign_rhs1 (m_store_info[i]->stmt)); | |
2573 | ||
2574 | unsigned int i; | |
2575 | tree ref; | |
2576 | FOR_EACH_VEC_ELT (refs, i, ref) | |
2577 | if (stmts_may_clobber_ref_p (first_stmt, last_stmt, ref)) | |
2578 | return false; | |
2579 | n.vuse = NULL_TREE; | |
2580 | } | |
2581 | ||
2582 | infof->n = n; | |
2583 | infof->ins_stmt = ins_stmt; | |
2584 | for (unsigned int i = first; i <= last; ++i) | |
2585 | { | |
2586 | m_store_info[i]->rhs_code = n.n == cmpxchg ? LROTATE_EXPR : NOP_EXPR; | |
2587 | m_store_info[i]->ops[0].base_addr = NULL_TREE; | |
2588 | m_store_info[i]->ops[1].base_addr = NULL_TREE; | |
2589 | if (i != first) | |
2590 | merged_store->merge_into (m_store_info[i]); | |
2591 | } | |
2592 | ||
2593 | return true; | |
2594 | } | |
2595 | ||
f663d9ad KT |
2596 | /* Go through the candidate stores recorded in m_store_info and merge them |
2597 | into merged_store_group objects recorded into m_merged_store_groups | |
2598 | representing the widened stores. Return true if coalescing was successful | |
2599 | and the number of widened stores is fewer than the original number | |
2600 | of stores. */ | |
2601 | ||
2602 | bool | |
2603 | imm_store_chain_info::coalesce_immediate_stores () | |
2604 | { | |
2605 | /* Anything less can't be processed. */ | |
2606 | if (m_store_info.length () < 2) | |
2607 | return false; | |
2608 | ||
2609 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
c94c3532 | 2610 | fprintf (dump_file, "Attempting to coalesce %u stores in chain\n", |
f663d9ad KT |
2611 | m_store_info.length ()); |
2612 | ||
2613 | store_immediate_info *info; | |
4b84d9b8 | 2614 | unsigned int i, ignore = 0; |
f663d9ad KT |
2615 | |
2616 | /* Order the stores by the bitposition they write to. */ | |
2617 | m_store_info.qsort (sort_by_bitpos); | |
2618 | ||
2619 | info = m_store_info[0]; | |
2620 | merged_store_group *merged_store = new merged_store_group (info); | |
c94c3532 EB |
2621 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2622 | fputs ("New store group\n", dump_file); | |
f663d9ad KT |
2623 | |
2624 | FOR_EACH_VEC_ELT (m_store_info, i, info) | |
2625 | { | |
4b84d9b8 | 2626 | if (i <= ignore) |
c94c3532 | 2627 | goto done; |
f663d9ad | 2628 | |
4b84d9b8 JJ |
2629 | /* First try to handle group of stores like: |
2630 | p[0] = data >> 24; | |
2631 | p[1] = data >> 16; | |
2632 | p[2] = data >> 8; | |
2633 | p[3] = data; | |
2634 | using the bswap framework. */ | |
2635 | if (info->bitpos == merged_store->start + merged_store->width | |
2636 | && merged_store->stores.length () == 1 | |
2637 | && merged_store->stores[0]->ins_stmt != NULL | |
2638 | && info->ins_stmt != NULL) | |
2639 | { | |
2640 | unsigned int try_size; | |
2641 | for (try_size = 64; try_size >= 16; try_size >>= 1) | |
2642 | if (try_coalesce_bswap (merged_store, i - 1, try_size)) | |
2643 | break; | |
2644 | ||
2645 | if (try_size >= 16) | |
2646 | { | |
2647 | ignore = i + merged_store->stores.length () - 1; | |
2648 | m_merged_store_groups.safe_push (merged_store); | |
2649 | if (ignore < m_store_info.length ()) | |
2650 | merged_store = new merged_store_group (m_store_info[ignore]); | |
2651 | else | |
2652 | merged_store = NULL; | |
c94c3532 | 2653 | goto done; |
4b84d9b8 JJ |
2654 | } |
2655 | } | |
2656 | ||
f663d9ad KT |
2657 | /* |---store 1---| |
2658 | |---store 2---| | |
4b84d9b8 JJ |
2659 | Overlapping stores. */ |
2660 | if (IN_RANGE (info->bitpos, merged_store->start, | |
f663d9ad KT |
2661 | merged_store->start + merged_store->width - 1)) |
2662 | { | |
245f6de1 JJ |
2663 | /* Only allow overlapping stores of constants. */ |
2664 | if (info->rhs_code == INTEGER_CST | |
2665 | && merged_store->stores[0]->rhs_code == INTEGER_CST) | |
2666 | { | |
2667 | merged_store->merge_overlapping (info); | |
c94c3532 | 2668 | goto done; |
245f6de1 | 2669 | } |
f663d9ad | 2670 | } |
245f6de1 JJ |
2671 | /* |---store 1---||---store 2---| |
2672 | This store is consecutive to the previous one. | |
2673 | Merge it into the current store group. There can be gaps in between | |
2674 | the stores, but there can't be gaps in between bitregions. */ | |
c94c3532 EB |
2675 | else if (info->bitregion_start <= merged_store->bitregion_end |
2676 | && info->rhs_code != LROTATE_EXPR | |
2677 | && (info->rhs_code == merged_store->stores[0]->rhs_code | |
2678 | || (info->rhs_code == INTEGER_CST | |
2679 | && merged_store->stores[0]->rhs_code == BIT_INSERT_EXPR) | |
2680 | || (info->rhs_code == BIT_INSERT_EXPR | |
2681 | && merged_store->stores[0]->rhs_code == INTEGER_CST))) | |
f663d9ad | 2682 | { |
245f6de1 JJ |
2683 | store_immediate_info *infof = merged_store->stores[0]; |
2684 | ||
2685 | /* All the rhs_code ops that take 2 operands are commutative, | |
2686 | swap the operands if it could make the operands compatible. */ | |
2687 | if (infof->ops[0].base_addr | |
2688 | && infof->ops[1].base_addr | |
2689 | && info->ops[0].base_addr | |
2690 | && info->ops[1].base_addr | |
8a91d545 RS |
2691 | && known_eq (info->ops[1].bitpos - infof->ops[0].bitpos, |
2692 | info->bitpos - infof->bitpos) | |
245f6de1 JJ |
2693 | && operand_equal_p (info->ops[1].base_addr, |
2694 | infof->ops[0].base_addr, 0)) | |
127ef369 JJ |
2695 | { |
2696 | std::swap (info->ops[0], info->ops[1]); | |
2697 | info->ops_swapped_p = true; | |
2698 | } | |
5bfd2f9b JJ |
2699 | if ((infof->ops[0].base_addr |
2700 | ? compatible_load_p (merged_store, info, base_addr, 0) | |
2701 | : !info->ops[0].base_addr) | |
2702 | && (infof->ops[1].base_addr | |
2703 | ? compatible_load_p (merged_store, info, base_addr, 1) | |
c5679c37 JJ |
2704 | : !info->ops[1].base_addr) |
2705 | && check_no_overlap (m_store_info, i, info->rhs_code, | |
2706 | MAX (merged_store->last_order, | |
2707 | info->order), | |
2708 | MAX (merged_store->start | |
2709 | + merged_store->width, | |
2710 | info->bitpos + info->bitsize))) | |
245f6de1 JJ |
2711 | { |
2712 | merged_store->merge_into (info); | |
c94c3532 | 2713 | goto done; |
245f6de1 JJ |
2714 | } |
2715 | } | |
f663d9ad | 2716 | |
245f6de1 JJ |
2717 | /* |---store 1---| <gap> |---store 2---|. |
2718 | Gap between stores or the rhs not compatible. Start a new group. */ | |
f663d9ad | 2719 | |
245f6de1 JJ |
2720 | /* Try to apply all the stores recorded for the group to determine |
2721 | the bitpattern they write and discard it if that fails. | |
2722 | This will also reject single-store groups. */ | |
c94c3532 | 2723 | if (merged_store->apply_stores ()) |
245f6de1 | 2724 | m_merged_store_groups.safe_push (merged_store); |
c94c3532 EB |
2725 | else |
2726 | delete merged_store; | |
f663d9ad | 2727 | |
245f6de1 | 2728 | merged_store = new merged_store_group (info); |
c94c3532 EB |
2729 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2730 | fputs ("New store group\n", dump_file); | |
2731 | ||
2732 | done: | |
2733 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2734 | { | |
2735 | fprintf (dump_file, "Store %u:\nbitsize:" HOST_WIDE_INT_PRINT_DEC | |
2736 | " bitpos:" HOST_WIDE_INT_PRINT_DEC " val:", | |
2737 | i, info->bitsize, info->bitpos); | |
2738 | print_generic_expr (dump_file, gimple_assign_rhs1 (info->stmt)); | |
2739 | fputc ('\n', dump_file); | |
2740 | } | |
f663d9ad KT |
2741 | } |
2742 | ||
a62b3dc5 | 2743 | /* Record or discard the last store group. */ |
4b84d9b8 JJ |
2744 | if (merged_store) |
2745 | { | |
c94c3532 | 2746 | if (merged_store->apply_stores ()) |
4b84d9b8 | 2747 | m_merged_store_groups.safe_push (merged_store); |
c94c3532 EB |
2748 | else |
2749 | delete merged_store; | |
4b84d9b8 | 2750 | } |
f663d9ad KT |
2751 | |
2752 | gcc_assert (m_merged_store_groups.length () <= m_store_info.length ()); | |
c94c3532 | 2753 | |
f663d9ad KT |
2754 | bool success |
2755 | = !m_merged_store_groups.is_empty () | |
2756 | && m_merged_store_groups.length () < m_store_info.length (); | |
2757 | ||
2758 | if (success && dump_file) | |
c94c3532 | 2759 | fprintf (dump_file, "Coalescing successful!\nMerged into %u stores\n", |
a62b3dc5 | 2760 | m_merged_store_groups.length ()); |
f663d9ad KT |
2761 | |
2762 | return success; | |
2763 | } | |
2764 | ||
245f6de1 JJ |
2765 | /* Return the type to use for the merged stores or loads described by STMTS. |
2766 | This is needed to get the alias sets right. If IS_LOAD, look for rhs, | |
2767 | otherwise lhs. Additionally set *CLIQUEP and *BASEP to MR_DEPENDENCE_* | |
2768 | of the MEM_REFs if any. */ | |
f663d9ad KT |
2769 | |
2770 | static tree | |
245f6de1 JJ |
2771 | get_alias_type_for_stmts (vec<gimple *> &stmts, bool is_load, |
2772 | unsigned short *cliquep, unsigned short *basep) | |
f663d9ad KT |
2773 | { |
2774 | gimple *stmt; | |
2775 | unsigned int i; | |
245f6de1 JJ |
2776 | tree type = NULL_TREE; |
2777 | tree ret = NULL_TREE; | |
2778 | *cliquep = 0; | |
2779 | *basep = 0; | |
f663d9ad KT |
2780 | |
2781 | FOR_EACH_VEC_ELT (stmts, i, stmt) | |
2782 | { | |
245f6de1 JJ |
2783 | tree ref = is_load ? gimple_assign_rhs1 (stmt) |
2784 | : gimple_assign_lhs (stmt); | |
2785 | tree type1 = reference_alias_ptr_type (ref); | |
2786 | tree base = get_base_address (ref); | |
f663d9ad | 2787 | |
245f6de1 JJ |
2788 | if (i == 0) |
2789 | { | |
2790 | if (TREE_CODE (base) == MEM_REF) | |
2791 | { | |
2792 | *cliquep = MR_DEPENDENCE_CLIQUE (base); | |
2793 | *basep = MR_DEPENDENCE_BASE (base); | |
2794 | } | |
2795 | ret = type = type1; | |
2796 | continue; | |
2797 | } | |
f663d9ad | 2798 | if (!alias_ptr_types_compatible_p (type, type1)) |
245f6de1 JJ |
2799 | ret = ptr_type_node; |
2800 | if (TREE_CODE (base) != MEM_REF | |
2801 | || *cliquep != MR_DEPENDENCE_CLIQUE (base) | |
2802 | || *basep != MR_DEPENDENCE_BASE (base)) | |
2803 | { | |
2804 | *cliquep = 0; | |
2805 | *basep = 0; | |
2806 | } | |
f663d9ad | 2807 | } |
245f6de1 | 2808 | return ret; |
f663d9ad KT |
2809 | } |
2810 | ||
2811 | /* Return the location_t information we can find among the statements | |
2812 | in STMTS. */ | |
2813 | ||
2814 | static location_t | |
245f6de1 | 2815 | get_location_for_stmts (vec<gimple *> &stmts) |
f663d9ad KT |
2816 | { |
2817 | gimple *stmt; | |
2818 | unsigned int i; | |
2819 | ||
2820 | FOR_EACH_VEC_ELT (stmts, i, stmt) | |
2821 | if (gimple_has_location (stmt)) | |
2822 | return gimple_location (stmt); | |
2823 | ||
2824 | return UNKNOWN_LOCATION; | |
2825 | } | |
2826 | ||
2827 | /* Used to decribe a store resulting from splitting a wide store in smaller | |
2828 | regularly-sized stores in split_group. */ | |
2829 | ||
2830 | struct split_store | |
2831 | { | |
2832 | unsigned HOST_WIDE_INT bytepos; | |
2833 | unsigned HOST_WIDE_INT size; | |
2834 | unsigned HOST_WIDE_INT align; | |
245f6de1 | 2835 | auto_vec<store_immediate_info *> orig_stores; |
a62b3dc5 JJ |
2836 | /* True if there is a single orig stmt covering the whole split store. */ |
2837 | bool orig; | |
f663d9ad KT |
2838 | split_store (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT, |
2839 | unsigned HOST_WIDE_INT); | |
2840 | }; | |
2841 | ||
2842 | /* Simple constructor. */ | |
2843 | ||
2844 | split_store::split_store (unsigned HOST_WIDE_INT bp, | |
2845 | unsigned HOST_WIDE_INT sz, | |
2846 | unsigned HOST_WIDE_INT al) | |
a62b3dc5 | 2847 | : bytepos (bp), size (sz), align (al), orig (false) |
f663d9ad | 2848 | { |
245f6de1 | 2849 | orig_stores.create (0); |
f663d9ad KT |
2850 | } |
2851 | ||
245f6de1 JJ |
2852 | /* Record all stores in GROUP that write to the region starting at BITPOS and |
2853 | is of size BITSIZE. Record infos for such statements in STORES if | |
2854 | non-NULL. The stores in GROUP must be sorted by bitposition. Return INFO | |
2855 | if there is exactly one original store in the range. */ | |
f663d9ad | 2856 | |
a62b3dc5 | 2857 | static store_immediate_info * |
245f6de1 JJ |
2858 | find_constituent_stores (struct merged_store_group *group, |
2859 | vec<store_immediate_info *> *stores, | |
2860 | unsigned int *first, | |
2861 | unsigned HOST_WIDE_INT bitpos, | |
2862 | unsigned HOST_WIDE_INT bitsize) | |
f663d9ad | 2863 | { |
a62b3dc5 | 2864 | store_immediate_info *info, *ret = NULL; |
f663d9ad | 2865 | unsigned int i; |
a62b3dc5 JJ |
2866 | bool second = false; |
2867 | bool update_first = true; | |
f663d9ad | 2868 | unsigned HOST_WIDE_INT end = bitpos + bitsize; |
a62b3dc5 | 2869 | for (i = *first; group->stores.iterate (i, &info); ++i) |
f663d9ad KT |
2870 | { |
2871 | unsigned HOST_WIDE_INT stmt_start = info->bitpos; | |
2872 | unsigned HOST_WIDE_INT stmt_end = stmt_start + info->bitsize; | |
a62b3dc5 JJ |
2873 | if (stmt_end <= bitpos) |
2874 | { | |
2875 | /* BITPOS passed to this function never decreases from within the | |
2876 | same split_group call, so optimize and don't scan info records | |
2877 | which are known to end before or at BITPOS next time. | |
2878 | Only do it if all stores before this one also pass this. */ | |
2879 | if (update_first) | |
2880 | *first = i + 1; | |
2881 | continue; | |
2882 | } | |
2883 | else | |
2884 | update_first = false; | |
2885 | ||
f663d9ad | 2886 | /* The stores in GROUP are ordered by bitposition so if we're past |
a62b3dc5 JJ |
2887 | the region for this group return early. */ |
2888 | if (stmt_start >= end) | |
2889 | return ret; | |
2890 | ||
245f6de1 | 2891 | if (stores) |
a62b3dc5 | 2892 | { |
245f6de1 | 2893 | stores->safe_push (info); |
a62b3dc5 JJ |
2894 | if (ret) |
2895 | { | |
2896 | ret = NULL; | |
2897 | second = true; | |
2898 | } | |
2899 | } | |
2900 | else if (ret) | |
2901 | return NULL; | |
2902 | if (!second) | |
2903 | ret = info; | |
f663d9ad | 2904 | } |
a62b3dc5 | 2905 | return ret; |
f663d9ad KT |
2906 | } |
2907 | ||
d7a9512e JJ |
2908 | /* Return how many SSA_NAMEs used to compute value to store in the INFO |
2909 | store have multiple uses. If any SSA_NAME has multiple uses, also | |
2910 | count statements needed to compute it. */ | |
2911 | ||
2912 | static unsigned | |
2913 | count_multiple_uses (store_immediate_info *info) | |
2914 | { | |
2915 | gimple *stmt = info->stmt; | |
2916 | unsigned ret = 0; | |
2917 | switch (info->rhs_code) | |
2918 | { | |
2919 | case INTEGER_CST: | |
2920 | return 0; | |
2921 | case BIT_AND_EXPR: | |
2922 | case BIT_IOR_EXPR: | |
2923 | case BIT_XOR_EXPR: | |
d60edaba JJ |
2924 | if (info->bit_not_p) |
2925 | { | |
2926 | if (!has_single_use (gimple_assign_rhs1 (stmt))) | |
2927 | ret = 1; /* Fall through below to return | |
2928 | the BIT_NOT_EXPR stmt and then | |
2929 | BIT_{AND,IOR,XOR}_EXPR and anything it | |
2930 | uses. */ | |
2931 | else | |
2932 | /* stmt is after this the BIT_NOT_EXPR. */ | |
2933 | stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)); | |
2934 | } | |
d7a9512e JJ |
2935 | if (!has_single_use (gimple_assign_rhs1 (stmt))) |
2936 | { | |
2937 | ret += 1 + info->ops[0].bit_not_p; | |
2938 | if (info->ops[1].base_addr) | |
2939 | ret += 1 + info->ops[1].bit_not_p; | |
2940 | return ret + 1; | |
2941 | } | |
2942 | stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)); | |
2943 | /* stmt is now the BIT_*_EXPR. */ | |
2944 | if (!has_single_use (gimple_assign_rhs1 (stmt))) | |
127ef369 JJ |
2945 | ret += 1 + info->ops[info->ops_swapped_p].bit_not_p; |
2946 | else if (info->ops[info->ops_swapped_p].bit_not_p) | |
d7a9512e JJ |
2947 | { |
2948 | gimple *stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)); | |
2949 | if (!has_single_use (gimple_assign_rhs1 (stmt2))) | |
2950 | ++ret; | |
2951 | } | |
2952 | if (info->ops[1].base_addr == NULL_TREE) | |
127ef369 JJ |
2953 | { |
2954 | gcc_checking_assert (!info->ops_swapped_p); | |
2955 | return ret; | |
2956 | } | |
d7a9512e | 2957 | if (!has_single_use (gimple_assign_rhs2 (stmt))) |
127ef369 JJ |
2958 | ret += 1 + info->ops[1 - info->ops_swapped_p].bit_not_p; |
2959 | else if (info->ops[1 - info->ops_swapped_p].bit_not_p) | |
d7a9512e JJ |
2960 | { |
2961 | gimple *stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt)); | |
2962 | if (!has_single_use (gimple_assign_rhs1 (stmt2))) | |
2963 | ++ret; | |
2964 | } | |
2965 | return ret; | |
2966 | case MEM_REF: | |
2967 | if (!has_single_use (gimple_assign_rhs1 (stmt))) | |
2968 | return 1 + info->ops[0].bit_not_p; | |
2969 | else if (info->ops[0].bit_not_p) | |
2970 | { | |
2971 | stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)); | |
2972 | if (!has_single_use (gimple_assign_rhs1 (stmt))) | |
2973 | return 1; | |
2974 | } | |
2975 | return 0; | |
c94c3532 EB |
2976 | case BIT_INSERT_EXPR: |
2977 | return has_single_use (gimple_assign_rhs1 (stmt)) ? 0 : 1; | |
d7a9512e JJ |
2978 | default: |
2979 | gcc_unreachable (); | |
2980 | } | |
2981 | } | |
2982 | ||
f663d9ad | 2983 | /* Split a merged store described by GROUP by populating the SPLIT_STORES |
a62b3dc5 JJ |
2984 | vector (if non-NULL) with split_store structs describing the byte offset |
2985 | (from the base), the bit size and alignment of each store as well as the | |
2986 | original statements involved in each such split group. | |
f663d9ad KT |
2987 | This is to separate the splitting strategy from the statement |
2988 | building/emission/linking done in output_merged_store. | |
a62b3dc5 | 2989 | Return number of new stores. |
245f6de1 JJ |
2990 | If ALLOW_UNALIGNED_STORE is false, then all stores must be aligned. |
2991 | If ALLOW_UNALIGNED_LOAD is false, then all loads must be aligned. | |
a62b3dc5 JJ |
2992 | If SPLIT_STORES is NULL, it is just a dry run to count number of |
2993 | new stores. */ | |
f663d9ad | 2994 | |
a62b3dc5 | 2995 | static unsigned int |
245f6de1 JJ |
2996 | split_group (merged_store_group *group, bool allow_unaligned_store, |
2997 | bool allow_unaligned_load, | |
d7a9512e JJ |
2998 | vec<struct split_store *> *split_stores, |
2999 | unsigned *total_orig, | |
3000 | unsigned *total_new) | |
f663d9ad | 3001 | { |
a62b3dc5 JJ |
3002 | unsigned HOST_WIDE_INT pos = group->bitregion_start; |
3003 | unsigned HOST_WIDE_INT size = group->bitregion_end - pos; | |
f663d9ad | 3004 | unsigned HOST_WIDE_INT bytepos = pos / BITS_PER_UNIT; |
a62b3dc5 JJ |
3005 | unsigned HOST_WIDE_INT group_align = group->align; |
3006 | unsigned HOST_WIDE_INT align_base = group->align_base; | |
245f6de1 | 3007 | unsigned HOST_WIDE_INT group_load_align = group_align; |
d7a9512e | 3008 | bool any_orig = false; |
f663d9ad | 3009 | |
f663d9ad KT |
3010 | gcc_assert ((size % BITS_PER_UNIT == 0) && (pos % BITS_PER_UNIT == 0)); |
3011 | ||
4b84d9b8 JJ |
3012 | if (group->stores[0]->rhs_code == LROTATE_EXPR |
3013 | || group->stores[0]->rhs_code == NOP_EXPR) | |
3014 | { | |
3015 | /* For bswap framework using sets of stores, all the checking | |
3016 | has been done earlier in try_coalesce_bswap and needs to be | |
3017 | emitted as a single store. */ | |
3018 | if (total_orig) | |
3019 | { | |
3020 | /* Avoid the old/new stmt count heuristics. It should be | |
3021 | always beneficial. */ | |
3022 | total_new[0] = 1; | |
3023 | total_orig[0] = 2; | |
3024 | } | |
3025 | ||
3026 | if (split_stores) | |
3027 | { | |
3028 | unsigned HOST_WIDE_INT align_bitpos | |
3029 | = (group->start - align_base) & (group_align - 1); | |
3030 | unsigned HOST_WIDE_INT align = group_align; | |
3031 | if (align_bitpos) | |
3032 | align = least_bit_hwi (align_bitpos); | |
3033 | bytepos = group->start / BITS_PER_UNIT; | |
3034 | struct split_store *store | |
3035 | = new split_store (bytepos, group->width, align); | |
3036 | unsigned int first = 0; | |
3037 | find_constituent_stores (group, &store->orig_stores, | |
3038 | &first, group->start, group->width); | |
3039 | split_stores->safe_push (store); | |
3040 | } | |
3041 | ||
3042 | return 1; | |
3043 | } | |
3044 | ||
a62b3dc5 | 3045 | unsigned int ret = 0, first = 0; |
f663d9ad | 3046 | unsigned HOST_WIDE_INT try_pos = bytepos; |
f663d9ad | 3047 | |
d7a9512e JJ |
3048 | if (total_orig) |
3049 | { | |
3050 | unsigned int i; | |
3051 | store_immediate_info *info = group->stores[0]; | |
3052 | ||
3053 | total_new[0] = 0; | |
3054 | total_orig[0] = 1; /* The orig store. */ | |
3055 | info = group->stores[0]; | |
3056 | if (info->ops[0].base_addr) | |
a6fbd154 | 3057 | total_orig[0]++; |
d7a9512e | 3058 | if (info->ops[1].base_addr) |
a6fbd154 | 3059 | total_orig[0]++; |
d7a9512e JJ |
3060 | switch (info->rhs_code) |
3061 | { | |
3062 | case BIT_AND_EXPR: | |
3063 | case BIT_IOR_EXPR: | |
3064 | case BIT_XOR_EXPR: | |
3065 | total_orig[0]++; /* The orig BIT_*_EXPR stmt. */ | |
3066 | break; | |
3067 | default: | |
3068 | break; | |
3069 | } | |
3070 | total_orig[0] *= group->stores.length (); | |
3071 | ||
3072 | FOR_EACH_VEC_ELT (group->stores, i, info) | |
a6fbd154 JJ |
3073 | { |
3074 | total_new[0] += count_multiple_uses (info); | |
3075 | total_orig[0] += (info->bit_not_p | |
3076 | + info->ops[0].bit_not_p | |
3077 | + info->ops[1].bit_not_p); | |
3078 | } | |
d7a9512e JJ |
3079 | } |
3080 | ||
245f6de1 JJ |
3081 | if (!allow_unaligned_load) |
3082 | for (int i = 0; i < 2; ++i) | |
3083 | if (group->load_align[i]) | |
3084 | group_load_align = MIN (group_load_align, group->load_align[i]); | |
3085 | ||
f663d9ad KT |
3086 | while (size > 0) |
3087 | { | |
245f6de1 | 3088 | if ((allow_unaligned_store || group_align <= BITS_PER_UNIT) |
a62b3dc5 JJ |
3089 | && group->mask[try_pos - bytepos] == (unsigned char) ~0U) |
3090 | { | |
3091 | /* Skip padding bytes. */ | |
3092 | ++try_pos; | |
3093 | size -= BITS_PER_UNIT; | |
3094 | continue; | |
3095 | } | |
3096 | ||
f663d9ad | 3097 | unsigned HOST_WIDE_INT try_bitpos = try_pos * BITS_PER_UNIT; |
a62b3dc5 JJ |
3098 | unsigned int try_size = MAX_STORE_BITSIZE, nonmasked; |
3099 | unsigned HOST_WIDE_INT align_bitpos | |
3100 | = (try_bitpos - align_base) & (group_align - 1); | |
3101 | unsigned HOST_WIDE_INT align = group_align; | |
3102 | if (align_bitpos) | |
3103 | align = least_bit_hwi (align_bitpos); | |
245f6de1 | 3104 | if (!allow_unaligned_store) |
a62b3dc5 | 3105 | try_size = MIN (try_size, align); |
245f6de1 JJ |
3106 | if (!allow_unaligned_load) |
3107 | { | |
3108 | /* If we can't do or don't want to do unaligned stores | |
3109 | as well as loads, we need to take the loads into account | |
3110 | as well. */ | |
3111 | unsigned HOST_WIDE_INT load_align = group_load_align; | |
3112 | align_bitpos = (try_bitpos - align_base) & (load_align - 1); | |
3113 | if (align_bitpos) | |
3114 | load_align = least_bit_hwi (align_bitpos); | |
3115 | for (int i = 0; i < 2; ++i) | |
3116 | if (group->load_align[i]) | |
3117 | { | |
8a91d545 RS |
3118 | align_bitpos |
3119 | = known_alignment (try_bitpos | |
3120 | - group->stores[0]->bitpos | |
3121 | + group->stores[0]->ops[i].bitpos | |
3122 | - group->load_align_base[i]); | |
3123 | if (align_bitpos & (group_load_align - 1)) | |
245f6de1 JJ |
3124 | { |
3125 | unsigned HOST_WIDE_INT a = least_bit_hwi (align_bitpos); | |
3126 | load_align = MIN (load_align, a); | |
3127 | } | |
3128 | } | |
3129 | try_size = MIN (try_size, load_align); | |
3130 | } | |
a62b3dc5 | 3131 | store_immediate_info *info |
245f6de1 | 3132 | = find_constituent_stores (group, NULL, &first, try_bitpos, try_size); |
a62b3dc5 JJ |
3133 | if (info) |
3134 | { | |
3135 | /* If there is just one original statement for the range, see if | |
3136 | we can just reuse the original store which could be even larger | |
3137 | than try_size. */ | |
3138 | unsigned HOST_WIDE_INT stmt_end | |
3139 | = ROUND_UP (info->bitpos + info->bitsize, BITS_PER_UNIT); | |
245f6de1 JJ |
3140 | info = find_constituent_stores (group, NULL, &first, try_bitpos, |
3141 | stmt_end - try_bitpos); | |
a62b3dc5 JJ |
3142 | if (info && info->bitpos >= try_bitpos) |
3143 | { | |
3144 | try_size = stmt_end - try_bitpos; | |
3145 | goto found; | |
3146 | } | |
3147 | } | |
f663d9ad | 3148 | |
a62b3dc5 JJ |
3149 | /* Approximate store bitsize for the case when there are no padding |
3150 | bits. */ | |
3151 | while (try_size > size) | |
3152 | try_size /= 2; | |
3153 | /* Now look for whole padding bytes at the end of that bitsize. */ | |
3154 | for (nonmasked = try_size / BITS_PER_UNIT; nonmasked > 0; --nonmasked) | |
3155 | if (group->mask[try_pos - bytepos + nonmasked - 1] | |
3156 | != (unsigned char) ~0U) | |
3157 | break; | |
3158 | if (nonmasked == 0) | |
3159 | { | |
3160 | /* If entire try_size range is padding, skip it. */ | |
3161 | try_pos += try_size / BITS_PER_UNIT; | |
3162 | size -= try_size; | |
3163 | continue; | |
3164 | } | |
3165 | /* Otherwise try to decrease try_size if second half, last 3 quarters | |
3166 | etc. are padding. */ | |
3167 | nonmasked *= BITS_PER_UNIT; | |
3168 | while (nonmasked <= try_size / 2) | |
3169 | try_size /= 2; | |
245f6de1 | 3170 | if (!allow_unaligned_store && group_align > BITS_PER_UNIT) |
a62b3dc5 JJ |
3171 | { |
3172 | /* Now look for whole padding bytes at the start of that bitsize. */ | |
3173 | unsigned int try_bytesize = try_size / BITS_PER_UNIT, masked; | |
3174 | for (masked = 0; masked < try_bytesize; ++masked) | |
3175 | if (group->mask[try_pos - bytepos + masked] != (unsigned char) ~0U) | |
3176 | break; | |
3177 | masked *= BITS_PER_UNIT; | |
3178 | gcc_assert (masked < try_size); | |
3179 | if (masked >= try_size / 2) | |
3180 | { | |
3181 | while (masked >= try_size / 2) | |
3182 | { | |
3183 | try_size /= 2; | |
3184 | try_pos += try_size / BITS_PER_UNIT; | |
3185 | size -= try_size; | |
3186 | masked -= try_size; | |
3187 | } | |
3188 | /* Need to recompute the alignment, so just retry at the new | |
3189 | position. */ | |
3190 | continue; | |
3191 | } | |
3192 | } | |
3193 | ||
3194 | found: | |
3195 | ++ret; | |
f663d9ad | 3196 | |
a62b3dc5 JJ |
3197 | if (split_stores) |
3198 | { | |
3199 | struct split_store *store | |
3200 | = new split_store (try_pos, try_size, align); | |
245f6de1 JJ |
3201 | info = find_constituent_stores (group, &store->orig_stores, |
3202 | &first, try_bitpos, try_size); | |
a62b3dc5 JJ |
3203 | if (info |
3204 | && info->bitpos >= try_bitpos | |
3205 | && info->bitpos + info->bitsize <= try_bitpos + try_size) | |
d7a9512e JJ |
3206 | { |
3207 | store->orig = true; | |
3208 | any_orig = true; | |
3209 | } | |
a62b3dc5 JJ |
3210 | split_stores->safe_push (store); |
3211 | } | |
3212 | ||
3213 | try_pos += try_size / BITS_PER_UNIT; | |
f663d9ad | 3214 | size -= try_size; |
f663d9ad | 3215 | } |
a62b3dc5 | 3216 | |
d7a9512e JJ |
3217 | if (total_orig) |
3218 | { | |
a6fbd154 JJ |
3219 | unsigned int i; |
3220 | struct split_store *store; | |
d7a9512e JJ |
3221 | /* If we are reusing some original stores and any of the |
3222 | original SSA_NAMEs had multiple uses, we need to subtract | |
3223 | those now before we add the new ones. */ | |
3224 | if (total_new[0] && any_orig) | |
3225 | { | |
d7a9512e JJ |
3226 | FOR_EACH_VEC_ELT (*split_stores, i, store) |
3227 | if (store->orig) | |
3228 | total_new[0] -= count_multiple_uses (store->orig_stores[0]); | |
3229 | } | |
3230 | total_new[0] += ret; /* The new store. */ | |
3231 | store_immediate_info *info = group->stores[0]; | |
3232 | if (info->ops[0].base_addr) | |
a6fbd154 | 3233 | total_new[0] += ret; |
d7a9512e | 3234 | if (info->ops[1].base_addr) |
a6fbd154 | 3235 | total_new[0] += ret; |
d7a9512e JJ |
3236 | switch (info->rhs_code) |
3237 | { | |
3238 | case BIT_AND_EXPR: | |
3239 | case BIT_IOR_EXPR: | |
3240 | case BIT_XOR_EXPR: | |
3241 | total_new[0] += ret; /* The new BIT_*_EXPR stmt. */ | |
3242 | break; | |
3243 | default: | |
3244 | break; | |
3245 | } | |
a6fbd154 JJ |
3246 | FOR_EACH_VEC_ELT (*split_stores, i, store) |
3247 | { | |
3248 | unsigned int j; | |
3249 | bool bit_not_p[3] = { false, false, false }; | |
3250 | /* If all orig_stores have certain bit_not_p set, then | |
3251 | we'd use a BIT_NOT_EXPR stmt and need to account for it. | |
3252 | If some orig_stores have certain bit_not_p set, then | |
3253 | we'd use a BIT_XOR_EXPR with a mask and need to account for | |
3254 | it. */ | |
3255 | FOR_EACH_VEC_ELT (store->orig_stores, j, info) | |
3256 | { | |
3257 | if (info->ops[0].bit_not_p) | |
3258 | bit_not_p[0] = true; | |
3259 | if (info->ops[1].bit_not_p) | |
3260 | bit_not_p[1] = true; | |
3261 | if (info->bit_not_p) | |
3262 | bit_not_p[2] = true; | |
3263 | } | |
3264 | total_new[0] += bit_not_p[0] + bit_not_p[1] + bit_not_p[2]; | |
3265 | } | |
3266 | ||
d7a9512e JJ |
3267 | } |
3268 | ||
a62b3dc5 | 3269 | return ret; |
f663d9ad KT |
3270 | } |
3271 | ||
a6fbd154 JJ |
3272 | /* Return the operation through which the operand IDX (if < 2) or |
3273 | result (IDX == 2) should be inverted. If NOP_EXPR, no inversion | |
3274 | is done, if BIT_NOT_EXPR, all bits are inverted, if BIT_XOR_EXPR, | |
3275 | the bits should be xored with mask. */ | |
3276 | ||
3277 | static enum tree_code | |
3278 | invert_op (split_store *split_store, int idx, tree int_type, tree &mask) | |
3279 | { | |
3280 | unsigned int i; | |
3281 | store_immediate_info *info; | |
3282 | unsigned int cnt = 0; | |
e215422f | 3283 | bool any_paddings = false; |
a6fbd154 JJ |
3284 | FOR_EACH_VEC_ELT (split_store->orig_stores, i, info) |
3285 | { | |
3286 | bool bit_not_p = idx < 2 ? info->ops[idx].bit_not_p : info->bit_not_p; | |
3287 | if (bit_not_p) | |
e215422f JJ |
3288 | { |
3289 | ++cnt; | |
3290 | tree lhs = gimple_assign_lhs (info->stmt); | |
3291 | if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)) | |
3292 | && TYPE_PRECISION (TREE_TYPE (lhs)) < info->bitsize) | |
3293 | any_paddings = true; | |
3294 | } | |
a6fbd154 JJ |
3295 | } |
3296 | mask = NULL_TREE; | |
3297 | if (cnt == 0) | |
3298 | return NOP_EXPR; | |
e215422f | 3299 | if (cnt == split_store->orig_stores.length () && !any_paddings) |
a6fbd154 JJ |
3300 | return BIT_NOT_EXPR; |
3301 | ||
3302 | unsigned HOST_WIDE_INT try_bitpos = split_store->bytepos * BITS_PER_UNIT; | |
3303 | unsigned buf_size = split_store->size / BITS_PER_UNIT; | |
3304 | unsigned char *buf | |
3305 | = XALLOCAVEC (unsigned char, buf_size); | |
3306 | memset (buf, ~0U, buf_size); | |
3307 | FOR_EACH_VEC_ELT (split_store->orig_stores, i, info) | |
3308 | { | |
3309 | bool bit_not_p = idx < 2 ? info->ops[idx].bit_not_p : info->bit_not_p; | |
3310 | if (!bit_not_p) | |
3311 | continue; | |
3312 | /* Clear regions with bit_not_p and invert afterwards, rather than | |
3313 | clear regions with !bit_not_p, so that gaps in between stores aren't | |
3314 | set in the mask. */ | |
3315 | unsigned HOST_WIDE_INT bitsize = info->bitsize; | |
e215422f | 3316 | unsigned HOST_WIDE_INT prec = bitsize; |
a6fbd154 | 3317 | unsigned int pos_in_buffer = 0; |
e215422f JJ |
3318 | if (any_paddings) |
3319 | { | |
3320 | tree lhs = gimple_assign_lhs (info->stmt); | |
3321 | if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)) | |
3322 | && TYPE_PRECISION (TREE_TYPE (lhs)) < bitsize) | |
3323 | prec = TYPE_PRECISION (TREE_TYPE (lhs)); | |
3324 | } | |
a6fbd154 JJ |
3325 | if (info->bitpos < try_bitpos) |
3326 | { | |
3327 | gcc_assert (info->bitpos + bitsize > try_bitpos); | |
e215422f JJ |
3328 | if (!BYTES_BIG_ENDIAN) |
3329 | { | |
3330 | if (prec <= try_bitpos - info->bitpos) | |
3331 | continue; | |
3332 | prec -= try_bitpos - info->bitpos; | |
3333 | } | |
3334 | bitsize -= try_bitpos - info->bitpos; | |
3335 | if (BYTES_BIG_ENDIAN && prec > bitsize) | |
3336 | prec = bitsize; | |
a6fbd154 JJ |
3337 | } |
3338 | else | |
3339 | pos_in_buffer = info->bitpos - try_bitpos; | |
e215422f JJ |
3340 | if (prec < bitsize) |
3341 | { | |
3342 | /* If this is a bool inversion, invert just the least significant | |
3343 | prec bits rather than all bits of it. */ | |
3344 | if (BYTES_BIG_ENDIAN) | |
3345 | { | |
3346 | pos_in_buffer += bitsize - prec; | |
3347 | if (pos_in_buffer >= split_store->size) | |
3348 | continue; | |
3349 | } | |
3350 | bitsize = prec; | |
3351 | } | |
a6fbd154 JJ |
3352 | if (pos_in_buffer + bitsize > split_store->size) |
3353 | bitsize = split_store->size - pos_in_buffer; | |
3354 | unsigned char *p = buf + (pos_in_buffer / BITS_PER_UNIT); | |
3355 | if (BYTES_BIG_ENDIAN) | |
3356 | clear_bit_region_be (p, (BITS_PER_UNIT - 1 | |
3357 | - (pos_in_buffer % BITS_PER_UNIT)), bitsize); | |
3358 | else | |
3359 | clear_bit_region (p, pos_in_buffer % BITS_PER_UNIT, bitsize); | |
3360 | } | |
3361 | for (unsigned int i = 0; i < buf_size; ++i) | |
3362 | buf[i] = ~buf[i]; | |
3363 | mask = native_interpret_expr (int_type, buf, buf_size); | |
3364 | return BIT_XOR_EXPR; | |
3365 | } | |
3366 | ||
f663d9ad KT |
3367 | /* Given a merged store group GROUP output the widened version of it. |
3368 | The store chain is against the base object BASE. | |
3369 | Try store sizes of at most MAX_STORE_BITSIZE bits wide and don't output | |
3370 | unaligned stores for STRICT_ALIGNMENT targets or if it's too expensive. | |
3371 | Make sure that the number of statements output is less than the number of | |
3372 | original statements. If a better sequence is possible emit it and | |
3373 | return true. */ | |
3374 | ||
3375 | bool | |
b5926e23 | 3376 | imm_store_chain_info::output_merged_store (merged_store_group *group) |
f663d9ad | 3377 | { |
dd172744 RB |
3378 | split_store *split_store; |
3379 | unsigned int i; | |
a62b3dc5 JJ |
3380 | unsigned HOST_WIDE_INT start_byte_pos |
3381 | = group->bitregion_start / BITS_PER_UNIT; | |
f663d9ad KT |
3382 | |
3383 | unsigned int orig_num_stmts = group->stores.length (); | |
3384 | if (orig_num_stmts < 2) | |
3385 | return false; | |
3386 | ||
a62b3dc5 | 3387 | auto_vec<struct split_store *, 32> split_stores; |
245f6de1 | 3388 | bool allow_unaligned_store |
a62b3dc5 | 3389 | = !STRICT_ALIGNMENT && PARAM_VALUE (PARAM_STORE_MERGING_ALLOW_UNALIGNED); |
245f6de1 JJ |
3390 | bool allow_unaligned_load = allow_unaligned_store; |
3391 | if (allow_unaligned_store) | |
a62b3dc5 JJ |
3392 | { |
3393 | /* If unaligned stores are allowed, see how many stores we'd emit | |
3394 | for unaligned and how many stores we'd emit for aligned stores. | |
3395 | Only use unaligned stores if it allows fewer stores than aligned. */ | |
245f6de1 | 3396 | unsigned aligned_cnt |
d7a9512e | 3397 | = split_group (group, false, allow_unaligned_load, NULL, NULL, NULL); |
245f6de1 | 3398 | unsigned unaligned_cnt |
d7a9512e | 3399 | = split_group (group, true, allow_unaligned_load, NULL, NULL, NULL); |
a62b3dc5 | 3400 | if (aligned_cnt <= unaligned_cnt) |
245f6de1 | 3401 | allow_unaligned_store = false; |
a62b3dc5 | 3402 | } |
d7a9512e | 3403 | unsigned total_orig, total_new; |
245f6de1 | 3404 | split_group (group, allow_unaligned_store, allow_unaligned_load, |
d7a9512e | 3405 | &split_stores, &total_orig, &total_new); |
a62b3dc5 JJ |
3406 | |
3407 | if (split_stores.length () >= orig_num_stmts) | |
3408 | { | |
3409 | /* We didn't manage to reduce the number of statements. Bail out. */ | |
3410 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
d7a9512e JJ |
3411 | fprintf (dump_file, "Exceeded original number of stmts (%u)." |
3412 | " Not profitable to emit new sequence.\n", | |
3413 | orig_num_stmts); | |
dd172744 RB |
3414 | FOR_EACH_VEC_ELT (split_stores, i, split_store) |
3415 | delete split_store; | |
a62b3dc5 JJ |
3416 | return false; |
3417 | } | |
d7a9512e JJ |
3418 | if (total_orig <= total_new) |
3419 | { | |
3420 | /* If number of estimated new statements is above estimated original | |
3421 | statements, bail out too. */ | |
3422 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3423 | fprintf (dump_file, "Estimated number of original stmts (%u)" | |
3424 | " not larger than estimated number of new" | |
3425 | " stmts (%u).\n", | |
3426 | total_orig, total_new); | |
dd172744 RB |
3427 | FOR_EACH_VEC_ELT (split_stores, i, split_store) |
3428 | delete split_store; | |
4b84d9b8 | 3429 | return false; |
d7a9512e | 3430 | } |
f663d9ad KT |
3431 | |
3432 | gimple_stmt_iterator last_gsi = gsi_for_stmt (group->last_stmt); | |
3433 | gimple_seq seq = NULL; | |
f663d9ad KT |
3434 | tree last_vdef, new_vuse; |
3435 | last_vdef = gimple_vdef (group->last_stmt); | |
3436 | new_vuse = gimple_vuse (group->last_stmt); | |
4b84d9b8 JJ |
3437 | tree bswap_res = NULL_TREE; |
3438 | ||
3439 | if (group->stores[0]->rhs_code == LROTATE_EXPR | |
3440 | || group->stores[0]->rhs_code == NOP_EXPR) | |
3441 | { | |
3442 | tree fndecl = NULL_TREE, bswap_type = NULL_TREE, load_type; | |
3443 | gimple *ins_stmt = group->stores[0]->ins_stmt; | |
3444 | struct symbolic_number *n = &group->stores[0]->n; | |
3445 | bool bswap = group->stores[0]->rhs_code == LROTATE_EXPR; | |
3446 | ||
3447 | switch (n->range) | |
3448 | { | |
3449 | case 16: | |
3450 | load_type = bswap_type = uint16_type_node; | |
3451 | break; | |
3452 | case 32: | |
3453 | load_type = uint32_type_node; | |
3454 | if (bswap) | |
3455 | { | |
3456 | fndecl = builtin_decl_explicit (BUILT_IN_BSWAP32); | |
3457 | bswap_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); | |
3458 | } | |
3459 | break; | |
3460 | case 64: | |
3461 | load_type = uint64_type_node; | |
3462 | if (bswap) | |
3463 | { | |
3464 | fndecl = builtin_decl_explicit (BUILT_IN_BSWAP64); | |
3465 | bswap_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); | |
3466 | } | |
3467 | break; | |
3468 | default: | |
3469 | gcc_unreachable (); | |
3470 | } | |
3471 | ||
3472 | /* If the loads have each vuse of the corresponding store, | |
3473 | we've checked the aliasing already in try_coalesce_bswap and | |
3474 | we want to sink the need load into seq. So need to use new_vuse | |
3475 | on the load. */ | |
30fa8e9c | 3476 | if (n->base_addr) |
4b84d9b8 | 3477 | { |
30fa8e9c JJ |
3478 | if (n->vuse == NULL) |
3479 | { | |
3480 | n->vuse = new_vuse; | |
3481 | ins_stmt = NULL; | |
3482 | } | |
3483 | else | |
3484 | /* Update vuse in case it has changed by output_merged_stores. */ | |
3485 | n->vuse = gimple_vuse (ins_stmt); | |
4b84d9b8 JJ |
3486 | } |
3487 | bswap_res = bswap_replace (gsi_start (seq), ins_stmt, fndecl, | |
3488 | bswap_type, load_type, n, bswap); | |
3489 | gcc_assert (bswap_res); | |
3490 | } | |
f663d9ad KT |
3491 | |
3492 | gimple *stmt = NULL; | |
245f6de1 | 3493 | auto_vec<gimple *, 32> orig_stmts; |
4b84d9b8 JJ |
3494 | gimple_seq this_seq; |
3495 | tree addr = force_gimple_operand_1 (unshare_expr (base_addr), &this_seq, | |
aa55dc0c | 3496 | is_gimple_mem_ref_addr, NULL_TREE); |
4b84d9b8 | 3497 | gimple_seq_add_seq_without_update (&seq, this_seq); |
245f6de1 JJ |
3498 | |
3499 | tree load_addr[2] = { NULL_TREE, NULL_TREE }; | |
3500 | gimple_seq load_seq[2] = { NULL, NULL }; | |
3501 | gimple_stmt_iterator load_gsi[2] = { gsi_none (), gsi_none () }; | |
3502 | for (int j = 0; j < 2; ++j) | |
3503 | { | |
3504 | store_operand_info &op = group->stores[0]->ops[j]; | |
3505 | if (op.base_addr == NULL_TREE) | |
3506 | continue; | |
3507 | ||
3508 | store_immediate_info *infol = group->stores.last (); | |
3509 | if (gimple_vuse (op.stmt) == gimple_vuse (infol->ops[j].stmt)) | |
3510 | { | |
97031af7 JJ |
3511 | /* We can't pick the location randomly; while we've verified |
3512 | all the loads have the same vuse, they can be still in different | |
3513 | basic blocks and we need to pick the one from the last bb: | |
3514 | int x = q[0]; | |
3515 | if (x == N) return; | |
3516 | int y = q[1]; | |
3517 | p[0] = x; | |
3518 | p[1] = y; | |
3519 | otherwise if we put the wider load at the q[0] load, we might | |
3520 | segfault if q[1] is not mapped. */ | |
3521 | basic_block bb = gimple_bb (op.stmt); | |
3522 | gimple *ostmt = op.stmt; | |
3523 | store_immediate_info *info; | |
3524 | FOR_EACH_VEC_ELT (group->stores, i, info) | |
3525 | { | |
3526 | gimple *tstmt = info->ops[j].stmt; | |
3527 | basic_block tbb = gimple_bb (tstmt); | |
3528 | if (dominated_by_p (CDI_DOMINATORS, tbb, bb)) | |
3529 | { | |
3530 | ostmt = tstmt; | |
3531 | bb = tbb; | |
3532 | } | |
3533 | } | |
3534 | load_gsi[j] = gsi_for_stmt (ostmt); | |
245f6de1 JJ |
3535 | load_addr[j] |
3536 | = force_gimple_operand_1 (unshare_expr (op.base_addr), | |
3537 | &load_seq[j], is_gimple_mem_ref_addr, | |
3538 | NULL_TREE); | |
3539 | } | |
3540 | else if (operand_equal_p (base_addr, op.base_addr, 0)) | |
3541 | load_addr[j] = addr; | |
3542 | else | |
3e2927a1 | 3543 | { |
3e2927a1 JJ |
3544 | load_addr[j] |
3545 | = force_gimple_operand_1 (unshare_expr (op.base_addr), | |
3546 | &this_seq, is_gimple_mem_ref_addr, | |
3547 | NULL_TREE); | |
3548 | gimple_seq_add_seq_without_update (&seq, this_seq); | |
3549 | } | |
245f6de1 JJ |
3550 | } |
3551 | ||
f663d9ad KT |
3552 | FOR_EACH_VEC_ELT (split_stores, i, split_store) |
3553 | { | |
3554 | unsigned HOST_WIDE_INT try_size = split_store->size; | |
3555 | unsigned HOST_WIDE_INT try_pos = split_store->bytepos; | |
c94c3532 | 3556 | unsigned HOST_WIDE_INT try_bitpos = try_pos * BITS_PER_UNIT; |
f663d9ad | 3557 | unsigned HOST_WIDE_INT align = split_store->align; |
a62b3dc5 JJ |
3558 | tree dest, src; |
3559 | location_t loc; | |
3560 | if (split_store->orig) | |
3561 | { | |
3562 | /* If there is just a single constituent store which covers | |
3563 | the whole area, just reuse the lhs and rhs. */ | |
245f6de1 JJ |
3564 | gimple *orig_stmt = split_store->orig_stores[0]->stmt; |
3565 | dest = gimple_assign_lhs (orig_stmt); | |
3566 | src = gimple_assign_rhs1 (orig_stmt); | |
3567 | loc = gimple_location (orig_stmt); | |
a62b3dc5 JJ |
3568 | } |
3569 | else | |
3570 | { | |
245f6de1 JJ |
3571 | store_immediate_info *info; |
3572 | unsigned short clique, base; | |
3573 | unsigned int k; | |
3574 | FOR_EACH_VEC_ELT (split_store->orig_stores, k, info) | |
3575 | orig_stmts.safe_push (info->stmt); | |
a62b3dc5 | 3576 | tree offset_type |
245f6de1 JJ |
3577 | = get_alias_type_for_stmts (orig_stmts, false, &clique, &base); |
3578 | loc = get_location_for_stmts (orig_stmts); | |
3579 | orig_stmts.truncate (0); | |
a62b3dc5 JJ |
3580 | |
3581 | tree int_type = build_nonstandard_integer_type (try_size, UNSIGNED); | |
3582 | int_type = build_aligned_type (int_type, align); | |
3583 | dest = fold_build2 (MEM_REF, int_type, addr, | |
3584 | build_int_cst (offset_type, try_pos)); | |
245f6de1 JJ |
3585 | if (TREE_CODE (dest) == MEM_REF) |
3586 | { | |
3587 | MR_DEPENDENCE_CLIQUE (dest) = clique; | |
3588 | MR_DEPENDENCE_BASE (dest) = base; | |
3589 | } | |
3590 | ||
c94c3532 EB |
3591 | tree mask; |
3592 | if (bswap_res) | |
3593 | mask = integer_zero_node; | |
3594 | else | |
4b84d9b8 JJ |
3595 | mask = native_interpret_expr (int_type, |
3596 | group->mask + try_pos | |
3597 | - start_byte_pos, | |
3598 | group->buf_size); | |
245f6de1 JJ |
3599 | |
3600 | tree ops[2]; | |
3601 | for (int j = 0; | |
3602 | j < 1 + (split_store->orig_stores[0]->ops[1].val != NULL_TREE); | |
3603 | ++j) | |
3604 | { | |
3605 | store_operand_info &op = split_store->orig_stores[0]->ops[j]; | |
4b84d9b8 JJ |
3606 | if (bswap_res) |
3607 | ops[j] = bswap_res; | |
3608 | else if (op.base_addr) | |
245f6de1 JJ |
3609 | { |
3610 | FOR_EACH_VEC_ELT (split_store->orig_stores, k, info) | |
3611 | orig_stmts.safe_push (info->ops[j].stmt); | |
3612 | ||
3613 | offset_type = get_alias_type_for_stmts (orig_stmts, true, | |
3614 | &clique, &base); | |
3615 | location_t load_loc = get_location_for_stmts (orig_stmts); | |
3616 | orig_stmts.truncate (0); | |
3617 | ||
3618 | unsigned HOST_WIDE_INT load_align = group->load_align[j]; | |
3619 | unsigned HOST_WIDE_INT align_bitpos | |
c94c3532 | 3620 | = known_alignment (try_bitpos |
8a91d545 RS |
3621 | - split_store->orig_stores[0]->bitpos |
3622 | + op.bitpos); | |
3623 | if (align_bitpos & (load_align - 1)) | |
245f6de1 JJ |
3624 | load_align = least_bit_hwi (align_bitpos); |
3625 | ||
3626 | tree load_int_type | |
3627 | = build_nonstandard_integer_type (try_size, UNSIGNED); | |
3628 | load_int_type | |
3629 | = build_aligned_type (load_int_type, load_align); | |
3630 | ||
8a91d545 | 3631 | poly_uint64 load_pos |
c94c3532 | 3632 | = exact_div (try_bitpos |
8a91d545 RS |
3633 | - split_store->orig_stores[0]->bitpos |
3634 | + op.bitpos, | |
3635 | BITS_PER_UNIT); | |
245f6de1 JJ |
3636 | ops[j] = fold_build2 (MEM_REF, load_int_type, load_addr[j], |
3637 | build_int_cst (offset_type, load_pos)); | |
3638 | if (TREE_CODE (ops[j]) == MEM_REF) | |
3639 | { | |
3640 | MR_DEPENDENCE_CLIQUE (ops[j]) = clique; | |
3641 | MR_DEPENDENCE_BASE (ops[j]) = base; | |
3642 | } | |
3643 | if (!integer_zerop (mask)) | |
3644 | /* The load might load some bits (that will be masked off | |
3645 | later on) uninitialized, avoid -W*uninitialized | |
3646 | warnings in that case. */ | |
3647 | TREE_NO_WARNING (ops[j]) = 1; | |
3648 | ||
3649 | stmt = gimple_build_assign (make_ssa_name (int_type), | |
3650 | ops[j]); | |
3651 | gimple_set_location (stmt, load_loc); | |
3652 | if (gsi_bb (load_gsi[j])) | |
3653 | { | |
3654 | gimple_set_vuse (stmt, gimple_vuse (op.stmt)); | |
3655 | gimple_seq_add_stmt_without_update (&load_seq[j], stmt); | |
3656 | } | |
3657 | else | |
3658 | { | |
3659 | gimple_set_vuse (stmt, new_vuse); | |
3660 | gimple_seq_add_stmt_without_update (&seq, stmt); | |
3661 | } | |
3662 | ops[j] = gimple_assign_lhs (stmt); | |
a6fbd154 JJ |
3663 | tree xor_mask; |
3664 | enum tree_code inv_op | |
3665 | = invert_op (split_store, j, int_type, xor_mask); | |
3666 | if (inv_op != NOP_EXPR) | |
383ac8dc JJ |
3667 | { |
3668 | stmt = gimple_build_assign (make_ssa_name (int_type), | |
a6fbd154 | 3669 | inv_op, ops[j], xor_mask); |
383ac8dc JJ |
3670 | gimple_set_location (stmt, load_loc); |
3671 | ops[j] = gimple_assign_lhs (stmt); | |
3672 | ||
3673 | if (gsi_bb (load_gsi[j])) | |
3674 | gimple_seq_add_stmt_without_update (&load_seq[j], | |
3675 | stmt); | |
3676 | else | |
3677 | gimple_seq_add_stmt_without_update (&seq, stmt); | |
3678 | } | |
245f6de1 JJ |
3679 | } |
3680 | else | |
3681 | ops[j] = native_interpret_expr (int_type, | |
3682 | group->val + try_pos | |
3683 | - start_byte_pos, | |
3684 | group->buf_size); | |
3685 | } | |
3686 | ||
3687 | switch (split_store->orig_stores[0]->rhs_code) | |
3688 | { | |
3689 | case BIT_AND_EXPR: | |
3690 | case BIT_IOR_EXPR: | |
3691 | case BIT_XOR_EXPR: | |
3692 | FOR_EACH_VEC_ELT (split_store->orig_stores, k, info) | |
3693 | { | |
3694 | tree rhs1 = gimple_assign_rhs1 (info->stmt); | |
3695 | orig_stmts.safe_push (SSA_NAME_DEF_STMT (rhs1)); | |
3696 | } | |
3697 | location_t bit_loc; | |
3698 | bit_loc = get_location_for_stmts (orig_stmts); | |
3699 | orig_stmts.truncate (0); | |
3700 | ||
3701 | stmt | |
3702 | = gimple_build_assign (make_ssa_name (int_type), | |
3703 | split_store->orig_stores[0]->rhs_code, | |
3704 | ops[0], ops[1]); | |
3705 | gimple_set_location (stmt, bit_loc); | |
3706 | /* If there is just one load and there is a separate | |
3707 | load_seq[0], emit the bitwise op right after it. */ | |
3708 | if (load_addr[1] == NULL_TREE && gsi_bb (load_gsi[0])) | |
3709 | gimple_seq_add_stmt_without_update (&load_seq[0], stmt); | |
3710 | /* Otherwise, if at least one load is in seq, we need to | |
3711 | emit the bitwise op right before the store. If there | |
3712 | are two loads and are emitted somewhere else, it would | |
3713 | be better to emit the bitwise op as early as possible; | |
3714 | we don't track where that would be possible right now | |
3715 | though. */ | |
3716 | else | |
3717 | gimple_seq_add_stmt_without_update (&seq, stmt); | |
3718 | src = gimple_assign_lhs (stmt); | |
a6fbd154 JJ |
3719 | tree xor_mask; |
3720 | enum tree_code inv_op; | |
3721 | inv_op = invert_op (split_store, 2, int_type, xor_mask); | |
3722 | if (inv_op != NOP_EXPR) | |
d60edaba JJ |
3723 | { |
3724 | stmt = gimple_build_assign (make_ssa_name (int_type), | |
a6fbd154 | 3725 | inv_op, src, xor_mask); |
d60edaba JJ |
3726 | gimple_set_location (stmt, bit_loc); |
3727 | if (load_addr[1] == NULL_TREE && gsi_bb (load_gsi[0])) | |
3728 | gimple_seq_add_stmt_without_update (&load_seq[0], stmt); | |
3729 | else | |
3730 | gimple_seq_add_stmt_without_update (&seq, stmt); | |
3731 | src = gimple_assign_lhs (stmt); | |
3732 | } | |
245f6de1 | 3733 | break; |
4b84d9b8 JJ |
3734 | case LROTATE_EXPR: |
3735 | case NOP_EXPR: | |
3736 | src = ops[0]; | |
3737 | if (!is_gimple_val (src)) | |
3738 | { | |
3739 | stmt = gimple_build_assign (make_ssa_name (TREE_TYPE (src)), | |
3740 | src); | |
3741 | gimple_seq_add_stmt_without_update (&seq, stmt); | |
3742 | src = gimple_assign_lhs (stmt); | |
3743 | } | |
3744 | if (!useless_type_conversion_p (int_type, TREE_TYPE (src))) | |
3745 | { | |
3746 | stmt = gimple_build_assign (make_ssa_name (int_type), | |
3747 | NOP_EXPR, src); | |
3748 | gimple_seq_add_stmt_without_update (&seq, stmt); | |
3749 | src = gimple_assign_lhs (stmt); | |
3750 | } | |
be52ac73 JJ |
3751 | inv_op = invert_op (split_store, 2, int_type, xor_mask); |
3752 | if (inv_op != NOP_EXPR) | |
3753 | { | |
3754 | stmt = gimple_build_assign (make_ssa_name (int_type), | |
3755 | inv_op, src, xor_mask); | |
3756 | gimple_set_location (stmt, loc); | |
3757 | gimple_seq_add_stmt_without_update (&seq, stmt); | |
3758 | src = gimple_assign_lhs (stmt); | |
3759 | } | |
4b84d9b8 | 3760 | break; |
245f6de1 JJ |
3761 | default: |
3762 | src = ops[0]; | |
3763 | break; | |
3764 | } | |
3765 | ||
c94c3532 EB |
3766 | /* If bit insertion is required, we use the source as an accumulator |
3767 | into which the successive bit-field values are manually inserted. | |
3768 | FIXME: perhaps use BIT_INSERT_EXPR instead in some cases? */ | |
3769 | if (group->bit_insertion) | |
3770 | FOR_EACH_VEC_ELT (split_store->orig_stores, k, info) | |
3771 | if (info->rhs_code == BIT_INSERT_EXPR | |
3772 | && info->bitpos < try_bitpos + try_size | |
3773 | && info->bitpos + info->bitsize > try_bitpos) | |
3774 | { | |
3775 | /* Mask, truncate, convert to final type, shift and ior into | |
3776 | the accumulator. Note that every step can be a no-op. */ | |
3777 | const HOST_WIDE_INT start_gap = info->bitpos - try_bitpos; | |
3778 | const HOST_WIDE_INT end_gap | |
3779 | = (try_bitpos + try_size) - (info->bitpos + info->bitsize); | |
3780 | tree tem = info->ops[0].val; | |
3781 | if ((BYTES_BIG_ENDIAN ? start_gap : end_gap) > 0) | |
3782 | { | |
3783 | const unsigned HOST_WIDE_INT imask | |
3784 | = (HOST_WIDE_INT_1U << info->bitsize) - 1; | |
3785 | tem = gimple_build (&seq, loc, | |
3786 | BIT_AND_EXPR, TREE_TYPE (tem), tem, | |
3787 | build_int_cst (TREE_TYPE (tem), | |
3788 | imask)); | |
3789 | } | |
3790 | const HOST_WIDE_INT shift | |
3791 | = (BYTES_BIG_ENDIAN ? end_gap : start_gap); | |
3792 | if (shift < 0) | |
3793 | tem = gimple_build (&seq, loc, | |
3794 | RSHIFT_EXPR, TREE_TYPE (tem), tem, | |
3795 | build_int_cst (NULL_TREE, -shift)); | |
3796 | tem = gimple_convert (&seq, loc, int_type, tem); | |
3797 | if (shift > 0) | |
3798 | tem = gimple_build (&seq, loc, | |
3799 | LSHIFT_EXPR, int_type, tem, | |
3800 | build_int_cst (NULL_TREE, shift)); | |
3801 | src = gimple_build (&seq, loc, | |
3802 | BIT_IOR_EXPR, int_type, tem, src); | |
3803 | } | |
3804 | ||
a62b3dc5 JJ |
3805 | if (!integer_zerop (mask)) |
3806 | { | |
3807 | tree tem = make_ssa_name (int_type); | |
3808 | tree load_src = unshare_expr (dest); | |
3809 | /* The load might load some or all bits uninitialized, | |
3810 | avoid -W*uninitialized warnings in that case. | |
3811 | As optimization, it would be nice if all the bits are | |
3812 | provably uninitialized (no stores at all yet or previous | |
3813 | store a CLOBBER) we'd optimize away the load and replace | |
3814 | it e.g. with 0. */ | |
3815 | TREE_NO_WARNING (load_src) = 1; | |
3816 | stmt = gimple_build_assign (tem, load_src); | |
3817 | gimple_set_location (stmt, loc); | |
3818 | gimple_set_vuse (stmt, new_vuse); | |
3819 | gimple_seq_add_stmt_without_update (&seq, stmt); | |
3820 | ||
3821 | /* FIXME: If there is a single chunk of zero bits in mask, | |
3822 | perhaps use BIT_INSERT_EXPR instead? */ | |
3823 | stmt = gimple_build_assign (make_ssa_name (int_type), | |
3824 | BIT_AND_EXPR, tem, mask); | |
3825 | gimple_set_location (stmt, loc); | |
3826 | gimple_seq_add_stmt_without_update (&seq, stmt); | |
3827 | tem = gimple_assign_lhs (stmt); | |
3828 | ||
245f6de1 JJ |
3829 | if (TREE_CODE (src) == INTEGER_CST) |
3830 | src = wide_int_to_tree (int_type, | |
3831 | wi::bit_and_not (wi::to_wide (src), | |
3832 | wi::to_wide (mask))); | |
3833 | else | |
3834 | { | |
3835 | tree nmask | |
3836 | = wide_int_to_tree (int_type, | |
3837 | wi::bit_not (wi::to_wide (mask))); | |
3838 | stmt = gimple_build_assign (make_ssa_name (int_type), | |
3839 | BIT_AND_EXPR, src, nmask); | |
3840 | gimple_set_location (stmt, loc); | |
3841 | gimple_seq_add_stmt_without_update (&seq, stmt); | |
3842 | src = gimple_assign_lhs (stmt); | |
3843 | } | |
a62b3dc5 JJ |
3844 | stmt = gimple_build_assign (make_ssa_name (int_type), |
3845 | BIT_IOR_EXPR, tem, src); | |
3846 | gimple_set_location (stmt, loc); | |
3847 | gimple_seq_add_stmt_without_update (&seq, stmt); | |
3848 | src = gimple_assign_lhs (stmt); | |
3849 | } | |
3850 | } | |
f663d9ad KT |
3851 | |
3852 | stmt = gimple_build_assign (dest, src); | |
3853 | gimple_set_location (stmt, loc); | |
3854 | gimple_set_vuse (stmt, new_vuse); | |
3855 | gimple_seq_add_stmt_without_update (&seq, stmt); | |
3856 | ||
f663d9ad KT |
3857 | tree new_vdef; |
3858 | if (i < split_stores.length () - 1) | |
a62b3dc5 | 3859 | new_vdef = make_ssa_name (gimple_vop (cfun), stmt); |
f663d9ad KT |
3860 | else |
3861 | new_vdef = last_vdef; | |
3862 | ||
3863 | gimple_set_vdef (stmt, new_vdef); | |
3864 | SSA_NAME_DEF_STMT (new_vdef) = stmt; | |
3865 | new_vuse = new_vdef; | |
3866 | } | |
3867 | ||
3868 | FOR_EACH_VEC_ELT (split_stores, i, split_store) | |
3869 | delete split_store; | |
3870 | ||
f663d9ad KT |
3871 | gcc_assert (seq); |
3872 | if (dump_file) | |
3873 | { | |
3874 | fprintf (dump_file, | |
c94c3532 | 3875 | "New sequence of %u stores to replace old one of %u stores\n", |
a62b3dc5 | 3876 | split_stores.length (), orig_num_stmts); |
f663d9ad KT |
3877 | if (dump_flags & TDF_DETAILS) |
3878 | print_gimple_seq (dump_file, seq, 0, TDF_VOPS | TDF_MEMSYMS); | |
3879 | } | |
3880 | gsi_insert_seq_after (&last_gsi, seq, GSI_SAME_STMT); | |
245f6de1 JJ |
3881 | for (int j = 0; j < 2; ++j) |
3882 | if (load_seq[j]) | |
3883 | gsi_insert_seq_after (&load_gsi[j], load_seq[j], GSI_SAME_STMT); | |
f663d9ad KT |
3884 | |
3885 | return true; | |
3886 | } | |
3887 | ||
3888 | /* Process the merged_store_group objects created in the coalescing phase. | |
3889 | The stores are all against the base object BASE. | |
3890 | Try to output the widened stores and delete the original statements if | |
3891 | successful. Return true iff any changes were made. */ | |
3892 | ||
3893 | bool | |
b5926e23 | 3894 | imm_store_chain_info::output_merged_stores () |
f663d9ad KT |
3895 | { |
3896 | unsigned int i; | |
3897 | merged_store_group *merged_store; | |
3898 | bool ret = false; | |
3899 | FOR_EACH_VEC_ELT (m_merged_store_groups, i, merged_store) | |
3900 | { | |
b5926e23 | 3901 | if (output_merged_store (merged_store)) |
f663d9ad KT |
3902 | { |
3903 | unsigned int j; | |
3904 | store_immediate_info *store; | |
3905 | FOR_EACH_VEC_ELT (merged_store->stores, j, store) | |
3906 | { | |
3907 | gimple *stmt = store->stmt; | |
3908 | gimple_stmt_iterator gsi = gsi_for_stmt (stmt); | |
3909 | gsi_remove (&gsi, true); | |
3910 | if (stmt != merged_store->last_stmt) | |
3911 | { | |
3912 | unlink_stmt_vdef (stmt); | |
3913 | release_defs (stmt); | |
3914 | } | |
3915 | } | |
3916 | ret = true; | |
3917 | } | |
3918 | } | |
3919 | if (ret && dump_file) | |
3920 | fprintf (dump_file, "Merging successful!\n"); | |
3921 | ||
3922 | return ret; | |
3923 | } | |
3924 | ||
3925 | /* Coalesce the store_immediate_info objects recorded against the base object | |
3926 | BASE in the first phase and output them. | |
3927 | Delete the allocated structures. | |
3928 | Return true if any changes were made. */ | |
3929 | ||
3930 | bool | |
b5926e23 | 3931 | imm_store_chain_info::terminate_and_process_chain () |
f663d9ad KT |
3932 | { |
3933 | /* Process store chain. */ | |
3934 | bool ret = false; | |
3935 | if (m_store_info.length () > 1) | |
3936 | { | |
3937 | ret = coalesce_immediate_stores (); | |
3938 | if (ret) | |
b5926e23 | 3939 | ret = output_merged_stores (); |
f663d9ad KT |
3940 | } |
3941 | ||
3942 | /* Delete all the entries we allocated ourselves. */ | |
3943 | store_immediate_info *info; | |
3944 | unsigned int i; | |
3945 | FOR_EACH_VEC_ELT (m_store_info, i, info) | |
3946 | delete info; | |
3947 | ||
3948 | merged_store_group *merged_info; | |
3949 | FOR_EACH_VEC_ELT (m_merged_store_groups, i, merged_info) | |
3950 | delete merged_info; | |
3951 | ||
3952 | return ret; | |
3953 | } | |
3954 | ||
3955 | /* Return true iff LHS is a destination potentially interesting for | |
3956 | store merging. In practice these are the codes that get_inner_reference | |
3957 | can process. */ | |
3958 | ||
3959 | static bool | |
3960 | lhs_valid_for_store_merging_p (tree lhs) | |
3961 | { | |
3962 | tree_code code = TREE_CODE (lhs); | |
3963 | ||
3964 | if (code == ARRAY_REF || code == ARRAY_RANGE_REF || code == MEM_REF | |
3965 | || code == COMPONENT_REF || code == BIT_FIELD_REF) | |
3966 | return true; | |
3967 | ||
3968 | return false; | |
3969 | } | |
3970 | ||
3971 | /* Return true if the tree RHS is a constant we want to consider | |
3972 | during store merging. In practice accept all codes that | |
3973 | native_encode_expr accepts. */ | |
3974 | ||
3975 | static bool | |
3976 | rhs_valid_for_store_merging_p (tree rhs) | |
3977 | { | |
cf098191 RS |
3978 | unsigned HOST_WIDE_INT size; |
3979 | return (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (rhs))).is_constant (&size) | |
3980 | && native_encode_expr (rhs, NULL, size) != 0); | |
f663d9ad KT |
3981 | } |
3982 | ||
245f6de1 JJ |
3983 | /* If MEM is a memory reference usable for store merging (either as |
3984 | store destination or for loads), return the non-NULL base_addr | |
3985 | and set *PBITSIZE, *PBITPOS, *PBITREGION_START and *PBITREGION_END. | |
3986 | Otherwise return NULL, *PBITPOS should be still valid even for that | |
3987 | case. */ | |
3988 | ||
3989 | static tree | |
8a91d545 RS |
3990 | mem_valid_for_store_merging (tree mem, poly_uint64 *pbitsize, |
3991 | poly_uint64 *pbitpos, | |
3992 | poly_uint64 *pbitregion_start, | |
3993 | poly_uint64 *pbitregion_end) | |
245f6de1 | 3994 | { |
8a91d545 RS |
3995 | poly_int64 bitsize, bitpos; |
3996 | poly_uint64 bitregion_start = 0, bitregion_end = 0; | |
245f6de1 JJ |
3997 | machine_mode mode; |
3998 | int unsignedp = 0, reversep = 0, volatilep = 0; | |
3999 | tree offset; | |
4000 | tree base_addr = get_inner_reference (mem, &bitsize, &bitpos, &offset, &mode, | |
4001 | &unsignedp, &reversep, &volatilep); | |
4002 | *pbitsize = bitsize; | |
8a91d545 | 4003 | if (known_eq (bitsize, 0)) |
245f6de1 JJ |
4004 | return NULL_TREE; |
4005 | ||
4006 | if (TREE_CODE (mem) == COMPONENT_REF | |
4007 | && DECL_BIT_FIELD_TYPE (TREE_OPERAND (mem, 1))) | |
4008 | { | |
4009 | get_bit_range (&bitregion_start, &bitregion_end, mem, &bitpos, &offset); | |
8a91d545 RS |
4010 | if (maybe_ne (bitregion_end, 0U)) |
4011 | bitregion_end += 1; | |
245f6de1 JJ |
4012 | } |
4013 | ||
4014 | if (reversep) | |
4015 | return NULL_TREE; | |
4016 | ||
4017 | /* We do not want to rewrite TARGET_MEM_REFs. */ | |
4018 | if (TREE_CODE (base_addr) == TARGET_MEM_REF) | |
4019 | return NULL_TREE; | |
4020 | /* In some cases get_inner_reference may return a | |
4021 | MEM_REF [ptr + byteoffset]. For the purposes of this pass | |
4022 | canonicalize the base_addr to MEM_REF [ptr] and take | |
4023 | byteoffset into account in the bitpos. This occurs in | |
4024 | PR 23684 and this way we can catch more chains. */ | |
4025 | else if (TREE_CODE (base_addr) == MEM_REF) | |
4026 | { | |
8a91d545 RS |
4027 | poly_offset_int byte_off = mem_ref_offset (base_addr); |
4028 | poly_offset_int bit_off = byte_off << LOG2_BITS_PER_UNIT; | |
245f6de1 | 4029 | bit_off += bitpos; |
8a91d545 | 4030 | if (known_ge (bit_off, 0) && bit_off.to_shwi (&bitpos)) |
245f6de1 | 4031 | { |
8a91d545 | 4032 | if (maybe_ne (bitregion_end, 0U)) |
245f6de1 JJ |
4033 | { |
4034 | bit_off = byte_off << LOG2_BITS_PER_UNIT; | |
4035 | bit_off += bitregion_start; | |
8a91d545 | 4036 | if (bit_off.to_uhwi (&bitregion_start)) |
245f6de1 | 4037 | { |
245f6de1 JJ |
4038 | bit_off = byte_off << LOG2_BITS_PER_UNIT; |
4039 | bit_off += bitregion_end; | |
8a91d545 | 4040 | if (!bit_off.to_uhwi (&bitregion_end)) |
245f6de1 JJ |
4041 | bitregion_end = 0; |
4042 | } | |
4043 | else | |
4044 | bitregion_end = 0; | |
4045 | } | |
4046 | } | |
4047 | else | |
4048 | return NULL_TREE; | |
4049 | base_addr = TREE_OPERAND (base_addr, 0); | |
4050 | } | |
4051 | /* get_inner_reference returns the base object, get at its | |
4052 | address now. */ | |
4053 | else | |
4054 | { | |
8a91d545 | 4055 | if (maybe_lt (bitpos, 0)) |
245f6de1 JJ |
4056 | return NULL_TREE; |
4057 | base_addr = build_fold_addr_expr (base_addr); | |
4058 | } | |
4059 | ||
8a91d545 | 4060 | if (known_eq (bitregion_end, 0U)) |
245f6de1 | 4061 | { |
8a91d545 | 4062 | bitregion_start = round_down_to_byte_boundary (bitpos); |
f87b4b2f JJ |
4063 | bitregion_end = bitpos; |
4064 | bitregion_end = round_up_to_byte_boundary (bitregion_end + bitsize); | |
245f6de1 JJ |
4065 | } |
4066 | ||
4067 | if (offset != NULL_TREE) | |
4068 | { | |
4069 | /* If the access is variable offset then a base decl has to be | |
4070 | address-taken to be able to emit pointer-based stores to it. | |
4071 | ??? We might be able to get away with re-using the original | |
4072 | base up to the first variable part and then wrapping that inside | |
4073 | a BIT_FIELD_REF. */ | |
4074 | tree base = get_base_address (base_addr); | |
4075 | if (! base | |
4076 | || (DECL_P (base) && ! TREE_ADDRESSABLE (base))) | |
4077 | return NULL_TREE; | |
4078 | ||
4079 | base_addr = build2 (POINTER_PLUS_EXPR, TREE_TYPE (base_addr), | |
4080 | base_addr, offset); | |
4081 | } | |
4082 | ||
4083 | *pbitsize = bitsize; | |
4084 | *pbitpos = bitpos; | |
4085 | *pbitregion_start = bitregion_start; | |
4086 | *pbitregion_end = bitregion_end; | |
4087 | return base_addr; | |
4088 | } | |
4089 | ||
4090 | /* Return true if STMT is a load that can be used for store merging. | |
4091 | In that case fill in *OP. BITSIZE, BITPOS, BITREGION_START and | |
4092 | BITREGION_END are properties of the corresponding store. */ | |
4093 | ||
4094 | static bool | |
4095 | handled_load (gimple *stmt, store_operand_info *op, | |
8a91d545 RS |
4096 | poly_uint64 bitsize, poly_uint64 bitpos, |
4097 | poly_uint64 bitregion_start, poly_uint64 bitregion_end) | |
245f6de1 | 4098 | { |
383ac8dc | 4099 | if (!is_gimple_assign (stmt)) |
245f6de1 | 4100 | return false; |
383ac8dc JJ |
4101 | if (gimple_assign_rhs_code (stmt) == BIT_NOT_EXPR) |
4102 | { | |
4103 | tree rhs1 = gimple_assign_rhs1 (stmt); | |
4104 | if (TREE_CODE (rhs1) == SSA_NAME | |
383ac8dc JJ |
4105 | && handled_load (SSA_NAME_DEF_STMT (rhs1), op, bitsize, bitpos, |
4106 | bitregion_start, bitregion_end)) | |
4107 | { | |
d60edaba JJ |
4108 | /* Don't allow _1 = load; _2 = ~1; _3 = ~_2; which should have |
4109 | been optimized earlier, but if allowed here, would confuse the | |
4110 | multiple uses counting. */ | |
4111 | if (op->bit_not_p) | |
4112 | return false; | |
383ac8dc JJ |
4113 | op->bit_not_p = !op->bit_not_p; |
4114 | return true; | |
4115 | } | |
4116 | return false; | |
4117 | } | |
4118 | if (gimple_vuse (stmt) | |
4119 | && gimple_assign_load_p (stmt) | |
245f6de1 JJ |
4120 | && !stmt_can_throw_internal (stmt) |
4121 | && !gimple_has_volatile_ops (stmt)) | |
4122 | { | |
4123 | tree mem = gimple_assign_rhs1 (stmt); | |
4124 | op->base_addr | |
4125 | = mem_valid_for_store_merging (mem, &op->bitsize, &op->bitpos, | |
4126 | &op->bitregion_start, | |
4127 | &op->bitregion_end); | |
4128 | if (op->base_addr != NULL_TREE | |
8a91d545 RS |
4129 | && known_eq (op->bitsize, bitsize) |
4130 | && multiple_p (op->bitpos - bitpos, BITS_PER_UNIT) | |
4131 | && known_ge (op->bitpos - op->bitregion_start, | |
4132 | bitpos - bitregion_start) | |
4133 | && known_ge (op->bitregion_end - op->bitpos, | |
4134 | bitregion_end - bitpos)) | |
245f6de1 JJ |
4135 | { |
4136 | op->stmt = stmt; | |
4137 | op->val = mem; | |
383ac8dc | 4138 | op->bit_not_p = false; |
245f6de1 JJ |
4139 | return true; |
4140 | } | |
4141 | } | |
4142 | return false; | |
4143 | } | |
4144 | ||
4145 | /* Record the store STMT for store merging optimization if it can be | |
4146 | optimized. */ | |
4147 | ||
4148 | void | |
4149 | pass_store_merging::process_store (gimple *stmt) | |
4150 | { | |
4151 | tree lhs = gimple_assign_lhs (stmt); | |
4152 | tree rhs = gimple_assign_rhs1 (stmt); | |
8a91d545 RS |
4153 | poly_uint64 bitsize, bitpos; |
4154 | poly_uint64 bitregion_start, bitregion_end; | |
245f6de1 JJ |
4155 | tree base_addr |
4156 | = mem_valid_for_store_merging (lhs, &bitsize, &bitpos, | |
4157 | &bitregion_start, &bitregion_end); | |
8a91d545 | 4158 | if (known_eq (bitsize, 0U)) |
245f6de1 JJ |
4159 | return; |
4160 | ||
4161 | bool invalid = (base_addr == NULL_TREE | |
8a91d545 RS |
4162 | || (maybe_gt (bitsize, |
4163 | (unsigned int) MAX_BITSIZE_MODE_ANY_INT) | |
4164 | && (TREE_CODE (rhs) != INTEGER_CST))); | |
245f6de1 | 4165 | enum tree_code rhs_code = ERROR_MARK; |
d60edaba | 4166 | bool bit_not_p = false; |
4b84d9b8 JJ |
4167 | struct symbolic_number n; |
4168 | gimple *ins_stmt = NULL; | |
245f6de1 JJ |
4169 | store_operand_info ops[2]; |
4170 | if (invalid) | |
4171 | ; | |
4172 | else if (rhs_valid_for_store_merging_p (rhs)) | |
4173 | { | |
4174 | rhs_code = INTEGER_CST; | |
4175 | ops[0].val = rhs; | |
4176 | } | |
d7a9512e | 4177 | else if (TREE_CODE (rhs) != SSA_NAME) |
245f6de1 JJ |
4178 | invalid = true; |
4179 | else | |
4180 | { | |
4181 | gimple *def_stmt = SSA_NAME_DEF_STMT (rhs), *def_stmt1, *def_stmt2; | |
4182 | if (!is_gimple_assign (def_stmt)) | |
4183 | invalid = true; | |
4184 | else if (handled_load (def_stmt, &ops[0], bitsize, bitpos, | |
4185 | bitregion_start, bitregion_end)) | |
4186 | rhs_code = MEM_REF; | |
d60edaba JJ |
4187 | else if (gimple_assign_rhs_code (def_stmt) == BIT_NOT_EXPR) |
4188 | { | |
4189 | tree rhs1 = gimple_assign_rhs1 (def_stmt); | |
4190 | if (TREE_CODE (rhs1) == SSA_NAME | |
4191 | && is_gimple_assign (SSA_NAME_DEF_STMT (rhs1))) | |
4192 | { | |
4193 | bit_not_p = true; | |
4194 | def_stmt = SSA_NAME_DEF_STMT (rhs1); | |
4195 | } | |
4196 | } | |
c94c3532 | 4197 | |
d60edaba | 4198 | if (rhs_code == ERROR_MARK && !invalid) |
245f6de1 JJ |
4199 | switch ((rhs_code = gimple_assign_rhs_code (def_stmt))) |
4200 | { | |
4201 | case BIT_AND_EXPR: | |
4202 | case BIT_IOR_EXPR: | |
4203 | case BIT_XOR_EXPR: | |
4204 | tree rhs1, rhs2; | |
4205 | rhs1 = gimple_assign_rhs1 (def_stmt); | |
4206 | rhs2 = gimple_assign_rhs2 (def_stmt); | |
4207 | invalid = true; | |
d7a9512e | 4208 | if (TREE_CODE (rhs1) != SSA_NAME) |
245f6de1 JJ |
4209 | break; |
4210 | def_stmt1 = SSA_NAME_DEF_STMT (rhs1); | |
4211 | if (!is_gimple_assign (def_stmt1) | |
4212 | || !handled_load (def_stmt1, &ops[0], bitsize, bitpos, | |
4213 | bitregion_start, bitregion_end)) | |
4214 | break; | |
4215 | if (rhs_valid_for_store_merging_p (rhs2)) | |
4216 | ops[1].val = rhs2; | |
d7a9512e | 4217 | else if (TREE_CODE (rhs2) != SSA_NAME) |
245f6de1 JJ |
4218 | break; |
4219 | else | |
4220 | { | |
4221 | def_stmt2 = SSA_NAME_DEF_STMT (rhs2); | |
4222 | if (!is_gimple_assign (def_stmt2)) | |
4223 | break; | |
4224 | else if (!handled_load (def_stmt2, &ops[1], bitsize, bitpos, | |
4225 | bitregion_start, bitregion_end)) | |
4226 | break; | |
4227 | } | |
4228 | invalid = false; | |
4229 | break; | |
4230 | default: | |
4231 | invalid = true; | |
4232 | break; | |
4233 | } | |
c94c3532 | 4234 | |
8a91d545 RS |
4235 | unsigned HOST_WIDE_INT const_bitsize; |
4236 | if (bitsize.is_constant (&const_bitsize) | |
c94c3532 | 4237 | && (const_bitsize % BITS_PER_UNIT) == 0 |
8a91d545 | 4238 | && const_bitsize <= 64 |
c94c3532 | 4239 | && multiple_p (bitpos, BITS_PER_UNIT)) |
4b84d9b8 JJ |
4240 | { |
4241 | ins_stmt = find_bswap_or_nop_1 (def_stmt, &n, 12); | |
4242 | if (ins_stmt) | |
4243 | { | |
4244 | uint64_t nn = n.n; | |
4245 | for (unsigned HOST_WIDE_INT i = 0; | |
8a91d545 RS |
4246 | i < const_bitsize; |
4247 | i += BITS_PER_UNIT, nn >>= BITS_PER_MARKER) | |
4b84d9b8 JJ |
4248 | if ((nn & MARKER_MASK) == 0 |
4249 | || (nn & MARKER_MASK) == MARKER_BYTE_UNKNOWN) | |
4250 | { | |
4251 | ins_stmt = NULL; | |
4252 | break; | |
4253 | } | |
4254 | if (ins_stmt) | |
4255 | { | |
4256 | if (invalid) | |
4257 | { | |
4258 | rhs_code = LROTATE_EXPR; | |
4259 | ops[0].base_addr = NULL_TREE; | |
4260 | ops[1].base_addr = NULL_TREE; | |
4261 | } | |
4262 | invalid = false; | |
4263 | } | |
4264 | } | |
4265 | } | |
c94c3532 EB |
4266 | |
4267 | if (invalid | |
4268 | && bitsize.is_constant (&const_bitsize) | |
4269 | && ((const_bitsize % BITS_PER_UNIT) != 0 | |
4270 | || !multiple_p (bitpos, BITS_PER_UNIT)) | |
4271 | && const_bitsize <= 64) | |
4272 | { | |
4273 | /* Bypass a truncating conversion to the bit-field type. */ | |
4274 | if (is_gimple_assign (def_stmt) && CONVERT_EXPR_CODE_P (rhs_code)) | |
4275 | { | |
4276 | tree rhs1 = gimple_assign_rhs1 (def_stmt); | |
4277 | if (TREE_CODE (rhs1) == SSA_NAME | |
4278 | && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)) | |
4279 | && const_bitsize <= TYPE_PRECISION (TREE_TYPE (rhs1))) | |
4280 | rhs = rhs1; | |
4281 | } | |
4282 | rhs_code = BIT_INSERT_EXPR; | |
4283 | ops[0].val = rhs; | |
4284 | ops[0].base_addr = NULL_TREE; | |
4285 | ops[1].base_addr = NULL_TREE; | |
4286 | invalid = false; | |
4287 | } | |
245f6de1 JJ |
4288 | } |
4289 | ||
8a91d545 RS |
4290 | unsigned HOST_WIDE_INT const_bitsize, const_bitpos; |
4291 | unsigned HOST_WIDE_INT const_bitregion_start, const_bitregion_end; | |
4292 | if (invalid | |
4293 | || !bitsize.is_constant (&const_bitsize) | |
4294 | || !bitpos.is_constant (&const_bitpos) | |
4295 | || !bitregion_start.is_constant (&const_bitregion_start) | |
4296 | || !bitregion_end.is_constant (&const_bitregion_end)) | |
245f6de1 | 4297 | { |
383ac8dc | 4298 | terminate_all_aliasing_chains (NULL, stmt); |
245f6de1 JJ |
4299 | return; |
4300 | } | |
4301 | ||
4b84d9b8 JJ |
4302 | if (!ins_stmt) |
4303 | memset (&n, 0, sizeof (n)); | |
4304 | ||
383ac8dc JJ |
4305 | struct imm_store_chain_info **chain_info = NULL; |
4306 | if (base_addr) | |
4307 | chain_info = m_stores.get (base_addr); | |
4308 | ||
245f6de1 JJ |
4309 | store_immediate_info *info; |
4310 | if (chain_info) | |
4311 | { | |
4312 | unsigned int ord = (*chain_info)->m_store_info.length (); | |
8a91d545 RS |
4313 | info = new store_immediate_info (const_bitsize, const_bitpos, |
4314 | const_bitregion_start, | |
4315 | const_bitregion_end, | |
4316 | stmt, ord, rhs_code, n, ins_stmt, | |
d60edaba | 4317 | bit_not_p, ops[0], ops[1]); |
245f6de1 JJ |
4318 | if (dump_file && (dump_flags & TDF_DETAILS)) |
4319 | { | |
4320 | fprintf (dump_file, "Recording immediate store from stmt:\n"); | |
4321 | print_gimple_stmt (dump_file, stmt, 0); | |
4322 | } | |
4323 | (*chain_info)->m_store_info.safe_push (info); | |
383ac8dc | 4324 | terminate_all_aliasing_chains (chain_info, stmt); |
245f6de1 JJ |
4325 | /* If we reach the limit of stores to merge in a chain terminate and |
4326 | process the chain now. */ | |
4327 | if ((*chain_info)->m_store_info.length () | |
4328 | == (unsigned int) PARAM_VALUE (PARAM_MAX_STORES_TO_MERGE)) | |
4329 | { | |
4330 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4331 | fprintf (dump_file, | |
4332 | "Reached maximum number of statements to merge:\n"); | |
4333 | terminate_and_release_chain (*chain_info); | |
4334 | } | |
4335 | return; | |
4336 | } | |
4337 | ||
4338 | /* Store aliases any existing chain? */ | |
383ac8dc | 4339 | terminate_all_aliasing_chains (NULL, stmt); |
245f6de1 JJ |
4340 | /* Start a new chain. */ |
4341 | struct imm_store_chain_info *new_chain | |
4342 | = new imm_store_chain_info (m_stores_head, base_addr); | |
8a91d545 RS |
4343 | info = new store_immediate_info (const_bitsize, const_bitpos, |
4344 | const_bitregion_start, | |
4345 | const_bitregion_end, | |
4346 | stmt, 0, rhs_code, n, ins_stmt, | |
d60edaba | 4347 | bit_not_p, ops[0], ops[1]); |
245f6de1 JJ |
4348 | new_chain->m_store_info.safe_push (info); |
4349 | m_stores.put (base_addr, new_chain); | |
4350 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4351 | { | |
4352 | fprintf (dump_file, "Starting new chain with statement:\n"); | |
4353 | print_gimple_stmt (dump_file, stmt, 0); | |
4354 | fprintf (dump_file, "The base object is:\n"); | |
4355 | print_generic_expr (dump_file, base_addr); | |
4356 | fprintf (dump_file, "\n"); | |
4357 | } | |
4358 | } | |
4359 | ||
f663d9ad | 4360 | /* Entry point for the pass. Go over each basic block recording chains of |
245f6de1 JJ |
4361 | immediate stores. Upon encountering a terminating statement (as defined |
4362 | by stmt_terminates_chain_p) process the recorded stores and emit the widened | |
4363 | variants. */ | |
f663d9ad KT |
4364 | |
4365 | unsigned int | |
4366 | pass_store_merging::execute (function *fun) | |
4367 | { | |
4368 | basic_block bb; | |
4369 | hash_set<gimple *> orig_stmts; | |
4370 | ||
4b84d9b8 JJ |
4371 | calculate_dominance_info (CDI_DOMINATORS); |
4372 | ||
f663d9ad KT |
4373 | FOR_EACH_BB_FN (bb, fun) |
4374 | { | |
4375 | gimple_stmt_iterator gsi; | |
4376 | unsigned HOST_WIDE_INT num_statements = 0; | |
4377 | /* Record the original statements so that we can keep track of | |
4378 | statements emitted in this pass and not re-process new | |
4379 | statements. */ | |
4380 | for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
4381 | { | |
4382 | if (is_gimple_debug (gsi_stmt (gsi))) | |
4383 | continue; | |
4384 | ||
2f391428 | 4385 | if (++num_statements >= 2) |
f663d9ad KT |
4386 | break; |
4387 | } | |
4388 | ||
4389 | if (num_statements < 2) | |
4390 | continue; | |
4391 | ||
4392 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4393 | fprintf (dump_file, "Processing basic block <%d>:\n", bb->index); | |
4394 | ||
4395 | for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
4396 | { | |
4397 | gimple *stmt = gsi_stmt (gsi); | |
4398 | ||
50b6d676 AO |
4399 | if (is_gimple_debug (stmt)) |
4400 | continue; | |
4401 | ||
f663d9ad KT |
4402 | if (gimple_has_volatile_ops (stmt)) |
4403 | { | |
4404 | /* Terminate all chains. */ | |
4405 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4406 | fprintf (dump_file, "Volatile access terminates " | |
4407 | "all chains\n"); | |
4408 | terminate_and_process_all_chains (); | |
4409 | continue; | |
4410 | } | |
4411 | ||
f663d9ad KT |
4412 | if (gimple_assign_single_p (stmt) && gimple_vdef (stmt) |
4413 | && !stmt_can_throw_internal (stmt) | |
4414 | && lhs_valid_for_store_merging_p (gimple_assign_lhs (stmt))) | |
245f6de1 JJ |
4415 | process_store (stmt); |
4416 | else | |
4417 | terminate_all_aliasing_chains (NULL, stmt); | |
f663d9ad KT |
4418 | } |
4419 | terminate_and_process_all_chains (); | |
4420 | } | |
4421 | return 0; | |
4422 | } | |
4423 | ||
4424 | } // anon namespace | |
4425 | ||
4426 | /* Construct and return a store merging pass object. */ | |
4427 | ||
4428 | gimple_opt_pass * | |
4429 | make_pass_store_merging (gcc::context *ctxt) | |
4430 | { | |
4431 | return new pass_store_merging (ctxt); | |
4432 | } | |
c22d8787 KT |
4433 | |
4434 | #if CHECKING_P | |
4435 | ||
4436 | namespace selftest { | |
4437 | ||
4438 | /* Selftests for store merging helpers. */ | |
4439 | ||
4440 | /* Assert that all elements of the byte arrays X and Y, both of length N | |
4441 | are equal. */ | |
4442 | ||
4443 | static void | |
4444 | verify_array_eq (unsigned char *x, unsigned char *y, unsigned int n) | |
4445 | { | |
4446 | for (unsigned int i = 0; i < n; i++) | |
4447 | { | |
4448 | if (x[i] != y[i]) | |
4449 | { | |
4450 | fprintf (stderr, "Arrays do not match. X:\n"); | |
4451 | dump_char_array (stderr, x, n); | |
4452 | fprintf (stderr, "Y:\n"); | |
4453 | dump_char_array (stderr, y, n); | |
4454 | } | |
4455 | ASSERT_EQ (x[i], y[i]); | |
4456 | } | |
4457 | } | |
4458 | ||
4459 | /* Test shift_bytes_in_array and that it carries bits across between | |
4460 | bytes correctly. */ | |
4461 | ||
4462 | static void | |
4463 | verify_shift_bytes_in_array (void) | |
4464 | { | |
4465 | /* byte 1 | byte 0 | |
4466 | 00011111 | 11100000. */ | |
4467 | unsigned char orig[2] = { 0xe0, 0x1f }; | |
4468 | unsigned char in[2]; | |
4469 | memcpy (in, orig, sizeof orig); | |
4470 | ||
4471 | unsigned char expected[2] = { 0x80, 0x7f }; | |
4472 | shift_bytes_in_array (in, sizeof (in), 2); | |
4473 | verify_array_eq (in, expected, sizeof (in)); | |
4474 | ||
4475 | memcpy (in, orig, sizeof orig); | |
4476 | memcpy (expected, orig, sizeof orig); | |
4477 | /* Check that shifting by zero doesn't change anything. */ | |
4478 | shift_bytes_in_array (in, sizeof (in), 0); | |
4479 | verify_array_eq (in, expected, sizeof (in)); | |
4480 | ||
4481 | } | |
4482 | ||
4483 | /* Test shift_bytes_in_array_right and that it carries bits across between | |
4484 | bytes correctly. */ | |
4485 | ||
4486 | static void | |
4487 | verify_shift_bytes_in_array_right (void) | |
4488 | { | |
4489 | /* byte 1 | byte 0 | |
4490 | 00011111 | 11100000. */ | |
4491 | unsigned char orig[2] = { 0x1f, 0xe0}; | |
4492 | unsigned char in[2]; | |
4493 | memcpy (in, orig, sizeof orig); | |
4494 | unsigned char expected[2] = { 0x07, 0xf8}; | |
4495 | shift_bytes_in_array_right (in, sizeof (in), 2); | |
4496 | verify_array_eq (in, expected, sizeof (in)); | |
4497 | ||
4498 | memcpy (in, orig, sizeof orig); | |
4499 | memcpy (expected, orig, sizeof orig); | |
4500 | /* Check that shifting by zero doesn't change anything. */ | |
4501 | shift_bytes_in_array_right (in, sizeof (in), 0); | |
4502 | verify_array_eq (in, expected, sizeof (in)); | |
4503 | } | |
4504 | ||
4505 | /* Test clear_bit_region that it clears exactly the bits asked and | |
4506 | nothing more. */ | |
4507 | ||
4508 | static void | |
4509 | verify_clear_bit_region (void) | |
4510 | { | |
4511 | /* Start with all bits set and test clearing various patterns in them. */ | |
4512 | unsigned char orig[3] = { 0xff, 0xff, 0xff}; | |
4513 | unsigned char in[3]; | |
4514 | unsigned char expected[3]; | |
4515 | memcpy (in, orig, sizeof in); | |
4516 | ||
4517 | /* Check zeroing out all the bits. */ | |
4518 | clear_bit_region (in, 0, 3 * BITS_PER_UNIT); | |
4519 | expected[0] = expected[1] = expected[2] = 0; | |
4520 | verify_array_eq (in, expected, sizeof in); | |
4521 | ||
4522 | memcpy (in, orig, sizeof in); | |
4523 | /* Leave the first and last bits intact. */ | |
4524 | clear_bit_region (in, 1, 3 * BITS_PER_UNIT - 2); | |
4525 | expected[0] = 0x1; | |
4526 | expected[1] = 0; | |
4527 | expected[2] = 0x80; | |
4528 | verify_array_eq (in, expected, sizeof in); | |
4529 | } | |
4530 | ||
4531 | /* Test verify_clear_bit_region_be that it clears exactly the bits asked and | |
4532 | nothing more. */ | |
4533 | ||
4534 | static void | |
4535 | verify_clear_bit_region_be (void) | |
4536 | { | |
4537 | /* Start with all bits set and test clearing various patterns in them. */ | |
4538 | unsigned char orig[3] = { 0xff, 0xff, 0xff}; | |
4539 | unsigned char in[3]; | |
4540 | unsigned char expected[3]; | |
4541 | memcpy (in, orig, sizeof in); | |
4542 | ||
4543 | /* Check zeroing out all the bits. */ | |
4544 | clear_bit_region_be (in, BITS_PER_UNIT - 1, 3 * BITS_PER_UNIT); | |
4545 | expected[0] = expected[1] = expected[2] = 0; | |
4546 | verify_array_eq (in, expected, sizeof in); | |
4547 | ||
4548 | memcpy (in, orig, sizeof in); | |
4549 | /* Leave the first and last bits intact. */ | |
4550 | clear_bit_region_be (in, BITS_PER_UNIT - 2, 3 * BITS_PER_UNIT - 2); | |
4551 | expected[0] = 0x80; | |
4552 | expected[1] = 0; | |
4553 | expected[2] = 0x1; | |
4554 | verify_array_eq (in, expected, sizeof in); | |
4555 | } | |
4556 | ||
4557 | ||
4558 | /* Run all of the selftests within this file. */ | |
4559 | ||
4560 | void | |
4561 | store_merging_c_tests (void) | |
4562 | { | |
4563 | verify_shift_bytes_in_array (); | |
4564 | verify_shift_bytes_in_array_right (); | |
4565 | verify_clear_bit_region (); | |
4566 | verify_clear_bit_region_be (); | |
4567 | } | |
4568 | ||
4569 | } // namespace selftest | |
4570 | #endif /* CHECKING_P. */ |