| 1 | /* GIMPLE store merging and byte swapping passes. |
| 2 | Copyright (C) 2009-2025 Free Software Foundation, Inc. |
| 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 | |
| 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 | |
| 25 | For example, if we have a sequence peforming four byte stores to |
| 26 | consecutive memory locations: |
| 27 | [p ] := imm1; |
| 28 | [p + 1B] := imm2; |
| 29 | [p + 2B] := imm3; |
| 30 | [p + 3B] := imm4; |
| 31 | we can transform this into a single 4-byte store if the target supports it: |
| 32 | [p] := imm1:imm2:imm3:imm4 concatenated according to endianness. |
| 33 | |
| 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 | |
| 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 | |
| 56 | The algorithm is applied to each basic block in three phases: |
| 57 | |
| 58 | 1) Scan through the basic block and record assignments to destinations |
| 59 | that can be expressed as a store to memory of a certain size at a certain |
| 60 | bit offset from base expressions we can handle. For bit-fields we also |
| 61 | record the surrounding bit region, i.e. bits that could be stored in |
| 62 | a read-modify-write operation when storing the bit-field. Record store |
| 63 | chains to different bases in a hash_map (m_stores) and make sure to |
| 64 | terminate such chains when appropriate (for example when the stored |
| 65 | values get used subsequently). |
| 66 | These stores can be a result of structure element initializers, array stores |
| 67 | etc. A store_immediate_info object is recorded for every such store. |
| 68 | Record as many such assignments to a single base as possible until a |
| 69 | statement that interferes with the store sequence is encountered. |
| 70 | Each store has up to 2 operands, which can be a either constant, a memory |
| 71 | load or an SSA name, from which the value to be stored can be computed. |
| 72 | At most one of the operands can be a constant. The operands are recorded |
| 73 | in store_operand_info struct. |
| 74 | |
| 75 | 2) Analyze the chains of stores recorded in phase 1) (i.e. the vector of |
| 76 | store_immediate_info objects) and coalesce contiguous stores into |
| 77 | merged_store_group objects. For bit-field stores, we don't need to |
| 78 | require the stores to be contiguous, just their surrounding bit regions |
| 79 | have to be contiguous. If the expression being stored is different |
| 80 | between adjacent stores, such as one store storing a constant and |
| 81 | following storing a value loaded from memory, or if the loaded memory |
| 82 | objects are not adjacent, a new merged_store_group is created as well. |
| 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. |
| 101 | We try to emit as wide stores as we can while respecting STRICT_ALIGNMENT |
| 102 | or TARGET_SLOW_UNALIGNED_ACCESS settings. |
| 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 "print-tree.h" |
| 155 | #include "tree-hash-traits.h" |
| 156 | #include "gimple-iterator.h" |
| 157 | #include "gimplify.h" |
| 158 | #include "gimple-fold.h" |
| 159 | #include "stor-layout.h" |
| 160 | #include "timevar.h" |
| 161 | #include "cfganal.h" |
| 162 | #include "cfgcleanup.h" |
| 163 | #include "tree-cfg.h" |
| 164 | #include "except.h" |
| 165 | #include "tree-eh.h" |
| 166 | #include "target.h" |
| 167 | #include "gimplify-me.h" |
| 168 | #include "rtl.h" |
| 169 | #include "expr.h" /* For get_bit_range. */ |
| 170 | #include "optabs-tree.h" |
| 171 | #include "dbgcnt.h" |
| 172 | #include "selftest.h" |
| 173 | |
| 174 | /* The maximum size (in bits) of the stores this pass should generate. */ |
| 175 | #define MAX_STORE_BITSIZE (BITS_PER_WORD) |
| 176 | #define MAX_STORE_BYTES (MAX_STORE_BITSIZE / BITS_PER_UNIT) |
| 177 | |
| 178 | /* Limit to bound the number of aliasing checks for loads with the same |
| 179 | vuse as the corresponding store. */ |
| 180 | #define MAX_STORE_ALIAS_CHECKS 64 |
| 181 | |
| 182 | namespace { |
| 183 | |
| 184 | struct bswap_stat |
| 185 | { |
| 186 | /* Number of hand-written 16-bit nop / bswaps found. */ |
| 187 | int found_16bit; |
| 188 | |
| 189 | /* Number of hand-written 32-bit nop / bswaps found. */ |
| 190 | int found_32bit; |
| 191 | |
| 192 | /* Number of hand-written 64-bit nop / bswaps found. */ |
| 193 | int found_64bit; |
| 194 | } nop_stats, bswap_stats; |
| 195 | |
| 196 | /* A symbolic number structure is used to detect byte permutation and selection |
| 197 | patterns of a source. To achieve that, its field N contains an artificial |
| 198 | number consisting of BITS_PER_MARKER sized markers tracking where does each |
| 199 | byte come from in the source: |
| 200 | |
| 201 | 0 - target byte has the value 0 |
| 202 | FF - target byte has an unknown value (eg. due to sign extension) |
| 203 | 1..size - marker value is the byte index in the source (0 for lsb). |
| 204 | |
| 205 | To detect permutations on memory sources (arrays and structures), a symbolic |
| 206 | number is also associated: |
| 207 | - a base address BASE_ADDR and an OFFSET giving the address of the source; |
| 208 | - a range which gives the difference between the highest and lowest accessed |
| 209 | memory location to make such a symbolic number; |
| 210 | - the address SRC of the source element of lowest address as a convenience |
| 211 | to easily get BASE_ADDR + offset + lowest bytepos; |
| 212 | - number of expressions N_OPS bitwise ored together to represent |
| 213 | approximate cost of the computation. |
| 214 | |
| 215 | Note 1: the range is different from size as size reflects the size of the |
| 216 | type of the current expression. For instance, for an array char a[], |
| 217 | (short) a[0] | (short) a[3] would have a size of 2 but a range of 4 while |
| 218 | (short) a[0] | ((short) a[0] << 1) would still have a size of 2 but this |
| 219 | time a range of 1. |
| 220 | |
| 221 | Note 2: for non-memory sources, range holds the same value as size. |
| 222 | |
| 223 | Note 3: SRC points to the SSA_NAME in case of non-memory source. */ |
| 224 | |
| 225 | struct symbolic_number { |
| 226 | uint64_t n; |
| 227 | tree type; |
| 228 | tree base_addr; |
| 229 | tree offset; |
| 230 | poly_int64 bytepos; |
| 231 | tree src; |
| 232 | tree alias_set; |
| 233 | tree vuse; |
| 234 | unsigned HOST_WIDE_INT range; |
| 235 | int n_ops; |
| 236 | }; |
| 237 | |
| 238 | #define BITS_PER_MARKER 8 |
| 239 | #define MARKER_MASK ((1 << BITS_PER_MARKER) - 1) |
| 240 | #define MARKER_BYTE_UNKNOWN MARKER_MASK |
| 241 | #define HEAD_MARKER(n, size) \ |
| 242 | ((n) & ((uint64_t) MARKER_MASK << (((size) - 1) * BITS_PER_MARKER))) |
| 243 | |
| 244 | /* The number which the find_bswap_or_nop_1 result should match in |
| 245 | order to have a nop. The number is masked according to the size of |
| 246 | the symbolic number before using it. */ |
| 247 | #define CMPNOP (sizeof (int64_t) < 8 ? 0 : \ |
| 248 | (uint64_t)0x08070605 << 32 | 0x04030201) |
| 249 | |
| 250 | /* The number which the find_bswap_or_nop_1 result should match in |
| 251 | order to have a byte swap. The number is masked according to the |
| 252 | size of the symbolic number before using it. */ |
| 253 | #define CMPXCHG (sizeof (int64_t) < 8 ? 0 : \ |
| 254 | (uint64_t)0x01020304 << 32 | 0x05060708) |
| 255 | |
| 256 | /* Perform a SHIFT or ROTATE operation by COUNT bits on symbolic |
| 257 | number N. Return false if the requested operation is not permitted |
| 258 | on a symbolic number. */ |
| 259 | |
| 260 | inline bool |
| 261 | do_shift_rotate (enum tree_code code, |
| 262 | struct symbolic_number *n, |
| 263 | int count) |
| 264 | { |
| 265 | int i, size = TYPE_PRECISION (n->type) / BITS_PER_UNIT; |
| 266 | uint64_t head_marker; |
| 267 | |
| 268 | if (count < 0 |
| 269 | || count >= TYPE_PRECISION (n->type) |
| 270 | || count % BITS_PER_UNIT != 0) |
| 271 | return false; |
| 272 | count = (count / BITS_PER_UNIT) * BITS_PER_MARKER; |
| 273 | |
| 274 | /* Zero out the extra bits of N in order to avoid them being shifted |
| 275 | into the significant bits. */ |
| 276 | if (size < 64 / BITS_PER_MARKER) |
| 277 | n->n &= ((uint64_t) 1 << (size * BITS_PER_MARKER)) - 1; |
| 278 | |
| 279 | switch (code) |
| 280 | { |
| 281 | case LSHIFT_EXPR: |
| 282 | n->n <<= count; |
| 283 | break; |
| 284 | case RSHIFT_EXPR: |
| 285 | head_marker = HEAD_MARKER (n->n, size); |
| 286 | n->n >>= count; |
| 287 | /* Arithmetic shift of signed type: result is dependent on the value. */ |
| 288 | if (!TYPE_UNSIGNED (n->type) && head_marker) |
| 289 | for (i = 0; i < count / BITS_PER_MARKER; i++) |
| 290 | n->n |= (uint64_t) MARKER_BYTE_UNKNOWN |
| 291 | << ((size - 1 - i) * BITS_PER_MARKER); |
| 292 | break; |
| 293 | case LROTATE_EXPR: |
| 294 | n->n = (n->n << count) | (n->n >> ((size * BITS_PER_MARKER) - count)); |
| 295 | break; |
| 296 | case RROTATE_EXPR: |
| 297 | n->n = (n->n >> count) | (n->n << ((size * BITS_PER_MARKER) - count)); |
| 298 | break; |
| 299 | default: |
| 300 | return false; |
| 301 | } |
| 302 | /* Zero unused bits for size. */ |
| 303 | if (size < 64 / BITS_PER_MARKER) |
| 304 | n->n &= ((uint64_t) 1 << (size * BITS_PER_MARKER)) - 1; |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | /* Perform sanity checking for the symbolic number N and the gimple |
| 309 | statement STMT. */ |
| 310 | |
| 311 | inline bool |
| 312 | verify_symbolic_number_p (struct symbolic_number *n, gimple *stmt) |
| 313 | { |
| 314 | tree lhs_type; |
| 315 | |
| 316 | lhs_type = TREE_TYPE (gimple_get_lhs (stmt)); |
| 317 | |
| 318 | if (TREE_CODE (lhs_type) != INTEGER_TYPE |
| 319 | && TREE_CODE (lhs_type) != ENUMERAL_TYPE) |
| 320 | return false; |
| 321 | |
| 322 | if (TYPE_PRECISION (lhs_type) != TYPE_PRECISION (n->type)) |
| 323 | return false; |
| 324 | |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | /* Initialize the symbolic number N for the bswap pass from the base element |
| 329 | SRC manipulated by the bitwise OR expression. */ |
| 330 | |
| 331 | bool |
| 332 | init_symbolic_number (struct symbolic_number *n, tree src) |
| 333 | { |
| 334 | int size; |
| 335 | |
| 336 | if (!INTEGRAL_TYPE_P (TREE_TYPE (src)) && !POINTER_TYPE_P (TREE_TYPE (src))) |
| 337 | return false; |
| 338 | |
| 339 | n->base_addr = n->offset = n->alias_set = n->vuse = NULL_TREE; |
| 340 | n->src = src; |
| 341 | |
| 342 | /* Set up the symbolic number N by setting each byte to a value between 1 and |
| 343 | the byte size of rhs1. The highest order byte is set to n->size and the |
| 344 | lowest order byte to 1. */ |
| 345 | n->type = TREE_TYPE (src); |
| 346 | size = TYPE_PRECISION (n->type); |
| 347 | if (size % BITS_PER_UNIT != 0) |
| 348 | return false; |
| 349 | size /= BITS_PER_UNIT; |
| 350 | if (size > 64 / BITS_PER_MARKER) |
| 351 | return false; |
| 352 | n->range = size; |
| 353 | n->n = CMPNOP; |
| 354 | n->n_ops = 1; |
| 355 | |
| 356 | if (size < 64 / BITS_PER_MARKER) |
| 357 | n->n &= ((uint64_t) 1 << (size * BITS_PER_MARKER)) - 1; |
| 358 | |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | /* Check if STMT might be a byte swap or a nop from a memory source and returns |
| 363 | the answer. If so, REF is that memory source and the base of the memory area |
| 364 | accessed and the offset of the access from that base are recorded in N. */ |
| 365 | |
| 366 | static bool |
| 367 | find_bswap_or_nop_load (gimple *stmt, tree ref, struct symbolic_number *n) |
| 368 | { |
| 369 | /* Leaf node is an array or component ref. Memorize its base and |
| 370 | offset from base to compare to other such leaf node. */ |
| 371 | poly_int64 bitsize, bitpos, bytepos; |
| 372 | machine_mode mode; |
| 373 | int unsignedp, reversep, volatilep; |
| 374 | tree offset, base_addr; |
| 375 | |
| 376 | /* Not prepared to handle PDP endian. */ |
| 377 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) |
| 378 | return false; |
| 379 | |
| 380 | if (!gimple_assign_load_p (stmt) || gimple_has_volatile_ops (stmt)) |
| 381 | return false; |
| 382 | |
| 383 | base_addr = get_inner_reference (ref, &bitsize, &bitpos, &offset, &mode, |
| 384 | &unsignedp, &reversep, &volatilep); |
| 385 | |
| 386 | if (TREE_CODE (base_addr) == TARGET_MEM_REF) |
| 387 | /* Do not rewrite TARGET_MEM_REF. */ |
| 388 | return false; |
| 389 | else if (TREE_CODE (base_addr) == MEM_REF) |
| 390 | { |
| 391 | poly_offset_int bit_offset = 0; |
| 392 | tree off = TREE_OPERAND (base_addr, 1); |
| 393 | |
| 394 | if (!integer_zerop (off)) |
| 395 | { |
| 396 | poly_offset_int boff = mem_ref_offset (base_addr); |
| 397 | boff <<= LOG2_BITS_PER_UNIT; |
| 398 | bit_offset += boff; |
| 399 | } |
| 400 | |
| 401 | base_addr = TREE_OPERAND (base_addr, 0); |
| 402 | |
| 403 | /* Avoid returning a negative bitpos as this may wreak havoc later. */ |
| 404 | if (maybe_lt (bit_offset, 0)) |
| 405 | { |
| 406 | tree byte_offset = wide_int_to_tree |
| 407 | (sizetype, bits_to_bytes_round_down (bit_offset)); |
| 408 | bit_offset = num_trailing_bits (bit_offset); |
| 409 | if (offset) |
| 410 | offset = size_binop (PLUS_EXPR, offset, byte_offset); |
| 411 | else |
| 412 | offset = byte_offset; |
| 413 | } |
| 414 | |
| 415 | bitpos += bit_offset.force_shwi (); |
| 416 | } |
| 417 | else |
| 418 | base_addr = build_fold_addr_expr (base_addr); |
| 419 | |
| 420 | if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)) |
| 421 | return false; |
| 422 | if (!multiple_p (bitsize, BITS_PER_UNIT)) |
| 423 | return false; |
| 424 | if (reversep) |
| 425 | return false; |
| 426 | |
| 427 | if (!init_symbolic_number (n, ref)) |
| 428 | return false; |
| 429 | n->base_addr = base_addr; |
| 430 | n->offset = offset; |
| 431 | n->bytepos = bytepos; |
| 432 | n->alias_set = reference_alias_ptr_type (ref); |
| 433 | n->vuse = gimple_vuse (stmt); |
| 434 | return true; |
| 435 | } |
| 436 | |
| 437 | /* Compute the symbolic number N representing the result of a bitwise OR, |
| 438 | bitwise XOR or plus on 2 symbolic number N1 and N2 whose source statements |
| 439 | are respectively SOURCE_STMT1 and SOURCE_STMT2. CODE is the operation. */ |
| 440 | |
| 441 | gimple * |
| 442 | perform_symbolic_merge (gimple *source_stmt1, struct symbolic_number *n1, |
| 443 | gimple *source_stmt2, struct symbolic_number *n2, |
| 444 | struct symbolic_number *n, enum tree_code code) |
| 445 | { |
| 446 | int i, size; |
| 447 | uint64_t mask; |
| 448 | gimple *source_stmt; |
| 449 | struct symbolic_number *n_start; |
| 450 | |
| 451 | tree rhs1 = gimple_assign_rhs1 (source_stmt1); |
| 452 | if (TREE_CODE (rhs1) == BIT_FIELD_REF |
| 453 | && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME) |
| 454 | rhs1 = TREE_OPERAND (rhs1, 0); |
| 455 | tree rhs2 = gimple_assign_rhs1 (source_stmt2); |
| 456 | if (TREE_CODE (rhs2) == BIT_FIELD_REF |
| 457 | && TREE_CODE (TREE_OPERAND (rhs2, 0)) == SSA_NAME) |
| 458 | rhs2 = TREE_OPERAND (rhs2, 0); |
| 459 | |
| 460 | /* Sources are different, cancel bswap if they are not memory location with |
| 461 | the same base (array, structure, ...). */ |
| 462 | if (rhs1 != rhs2) |
| 463 | { |
| 464 | uint64_t inc; |
| 465 | HOST_WIDE_INT start1, start2, start_sub, end_sub, end1, end2, end; |
| 466 | struct symbolic_number *toinc_n_ptr, *n_end; |
| 467 | basic_block bb1, bb2; |
| 468 | |
| 469 | if (!n1->base_addr || !n2->base_addr |
| 470 | || !operand_equal_p (n1->base_addr, n2->base_addr, 0)) |
| 471 | return NULL; |
| 472 | |
| 473 | if (!n1->offset != !n2->offset |
| 474 | || (n1->offset && !operand_equal_p (n1->offset, n2->offset, 0))) |
| 475 | return NULL; |
| 476 | |
| 477 | start1 = 0; |
| 478 | if (!(n2->bytepos - n1->bytepos).is_constant (&start2)) |
| 479 | return NULL; |
| 480 | |
| 481 | if (start1 < start2) |
| 482 | { |
| 483 | n_start = n1; |
| 484 | start_sub = start2 - start1; |
| 485 | } |
| 486 | else |
| 487 | { |
| 488 | n_start = n2; |
| 489 | start_sub = start1 - start2; |
| 490 | } |
| 491 | |
| 492 | bb1 = gimple_bb (source_stmt1); |
| 493 | bb2 = gimple_bb (source_stmt2); |
| 494 | if (dominated_by_p (CDI_DOMINATORS, bb1, bb2)) |
| 495 | source_stmt = source_stmt1; |
| 496 | else |
| 497 | source_stmt = source_stmt2; |
| 498 | |
| 499 | /* Find the highest address at which a load is performed and |
| 500 | compute related info. */ |
| 501 | end1 = start1 + (n1->range - 1); |
| 502 | end2 = start2 + (n2->range - 1); |
| 503 | if (end1 < end2) |
| 504 | { |
| 505 | end = end2; |
| 506 | end_sub = end2 - end1; |
| 507 | } |
| 508 | else |
| 509 | { |
| 510 | end = end1; |
| 511 | end_sub = end1 - end2; |
| 512 | } |
| 513 | n_end = (end2 > end1) ? n2 : n1; |
| 514 | |
| 515 | /* Find symbolic number whose lsb is the most significant. */ |
| 516 | if (BYTES_BIG_ENDIAN) |
| 517 | toinc_n_ptr = (n_end == n1) ? n2 : n1; |
| 518 | else |
| 519 | toinc_n_ptr = (n_start == n1) ? n2 : n1; |
| 520 | |
| 521 | n->range = end - MIN (start1, start2) + 1; |
| 522 | |
| 523 | /* Check that the range of memory covered can be represented by |
| 524 | a symbolic number. */ |
| 525 | if (n->range > 64 / BITS_PER_MARKER) |
| 526 | return NULL; |
| 527 | |
| 528 | /* Reinterpret byte marks in symbolic number holding the value of |
| 529 | bigger weight according to target endianness. */ |
| 530 | inc = BYTES_BIG_ENDIAN ? end_sub : start_sub; |
| 531 | size = TYPE_PRECISION (n1->type) / BITS_PER_UNIT; |
| 532 | for (i = 0; i < size; i++, inc <<= BITS_PER_MARKER) |
| 533 | { |
| 534 | unsigned marker |
| 535 | = (toinc_n_ptr->n >> (i * BITS_PER_MARKER)) & MARKER_MASK; |
| 536 | if (marker && marker != MARKER_BYTE_UNKNOWN) |
| 537 | toinc_n_ptr->n += inc; |
| 538 | } |
| 539 | } |
| 540 | else |
| 541 | { |
| 542 | n->range = n1->range; |
| 543 | n_start = n1; |
| 544 | source_stmt = source_stmt1; |
| 545 | } |
| 546 | |
| 547 | if (!n1->alias_set |
| 548 | || alias_ptr_types_compatible_p (n1->alias_set, n2->alias_set)) |
| 549 | n->alias_set = n1->alias_set; |
| 550 | else |
| 551 | n->alias_set = ptr_type_node; |
| 552 | n->vuse = n_start->vuse; |
| 553 | n->base_addr = n_start->base_addr; |
| 554 | n->offset = n_start->offset; |
| 555 | n->src = n_start->src; |
| 556 | n->bytepos = n_start->bytepos; |
| 557 | n->type = n_start->type; |
| 558 | size = TYPE_PRECISION (n->type) / BITS_PER_UNIT; |
| 559 | uint64_t res_n = n1->n | n2->n; |
| 560 | |
| 561 | for (i = 0, mask = MARKER_MASK; i < size; i++, mask <<= BITS_PER_MARKER) |
| 562 | { |
| 563 | uint64_t masked1, masked2; |
| 564 | |
| 565 | masked1 = n1->n & mask; |
| 566 | masked2 = n2->n & mask; |
| 567 | /* If at least one byte is 0, all of 0 | x == 0 ^ x == 0 + x == x. */ |
| 568 | if (masked1 && masked2) |
| 569 | { |
| 570 | /* + can carry into upper bits, just punt. */ |
| 571 | if (code == PLUS_EXPR) |
| 572 | return NULL; |
| 573 | /* x | x is still x. */ |
| 574 | if (code == BIT_IOR_EXPR && masked1 == masked2) |
| 575 | continue; |
| 576 | if (code == BIT_XOR_EXPR) |
| 577 | { |
| 578 | /* x ^ x is 0, but MARKER_BYTE_UNKNOWN stands for |
| 579 | unknown values and unknown ^ unknown is unknown. */ |
| 580 | if (masked1 == masked2 |
| 581 | && masked1 != ((uint64_t) MARKER_BYTE_UNKNOWN |
| 582 | << i * BITS_PER_MARKER)) |
| 583 | { |
| 584 | res_n &= ~mask; |
| 585 | continue; |
| 586 | } |
| 587 | } |
| 588 | /* Otherwise set the byte to unknown, it might still be |
| 589 | later masked off. */ |
| 590 | res_n |= mask; |
| 591 | } |
| 592 | } |
| 593 | n->n = res_n; |
| 594 | n->n_ops = n1->n_ops + n2->n_ops; |
| 595 | |
| 596 | return source_stmt; |
| 597 | } |
| 598 | |
| 599 | /* find_bswap_or_nop_1 invokes itself recursively with N and tries to perform |
| 600 | the operation given by the rhs of STMT on the result. If the operation |
| 601 | could successfully be executed the function returns a gimple stmt whose |
| 602 | rhs's first tree is the expression of the source operand and NULL |
| 603 | otherwise. */ |
| 604 | |
| 605 | gimple * |
| 606 | find_bswap_or_nop_1 (gimple *stmt, struct symbolic_number *n, int limit) |
| 607 | { |
| 608 | enum tree_code code; |
| 609 | tree rhs1, rhs2 = NULL; |
| 610 | gimple *rhs1_stmt, *rhs2_stmt, *source_stmt1; |
| 611 | enum gimple_rhs_class rhs_class; |
| 612 | |
| 613 | if (!limit |
| 614 | || !is_gimple_assign (stmt) |
| 615 | || stmt_can_throw_internal (cfun, stmt)) |
| 616 | return NULL; |
| 617 | |
| 618 | rhs1 = gimple_assign_rhs1 (stmt); |
| 619 | |
| 620 | if (find_bswap_or_nop_load (stmt, rhs1, n)) |
| 621 | return stmt; |
| 622 | |
| 623 | /* Handle BIT_FIELD_REF. */ |
| 624 | if (TREE_CODE (rhs1) == BIT_FIELD_REF |
| 625 | && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME) |
| 626 | { |
| 627 | if (!tree_fits_uhwi_p (TREE_OPERAND (rhs1, 1)) |
| 628 | || !tree_fits_uhwi_p (TREE_OPERAND (rhs1, 2))) |
| 629 | return NULL; |
| 630 | |
| 631 | unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (TREE_OPERAND (rhs1, 1)); |
| 632 | unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (TREE_OPERAND (rhs1, 2)); |
| 633 | if (bitpos % BITS_PER_UNIT == 0 |
| 634 | && bitsize % BITS_PER_UNIT == 0 |
| 635 | && init_symbolic_number (n, TREE_OPERAND (rhs1, 0))) |
| 636 | { |
| 637 | /* Handle big-endian bit numbering in BIT_FIELD_REF. */ |
| 638 | if (BYTES_BIG_ENDIAN) |
| 639 | bitpos = TYPE_PRECISION (n->type) - bitpos - bitsize; |
| 640 | |
| 641 | /* Shift. */ |
| 642 | if (!do_shift_rotate (RSHIFT_EXPR, n, bitpos)) |
| 643 | return NULL; |
| 644 | |
| 645 | /* Mask. */ |
| 646 | uint64_t mask = 0; |
| 647 | uint64_t tmp = (1 << BITS_PER_UNIT) - 1; |
| 648 | for (unsigned i = 0; i < bitsize / BITS_PER_UNIT; |
| 649 | i++, tmp <<= BITS_PER_UNIT) |
| 650 | mask |= (uint64_t) MARKER_MASK << (i * BITS_PER_MARKER); |
| 651 | n->n &= mask; |
| 652 | |
| 653 | /* Convert. */ |
| 654 | n->type = TREE_TYPE (rhs1); |
| 655 | if (!verify_symbolic_number_p (n, stmt)) |
| 656 | return NULL; |
| 657 | |
| 658 | if (!n->base_addr) |
| 659 | n->range = TYPE_PRECISION (n->type) / BITS_PER_UNIT; |
| 660 | |
| 661 | return stmt; |
| 662 | } |
| 663 | |
| 664 | return NULL; |
| 665 | } |
| 666 | |
| 667 | if (TREE_CODE (rhs1) != SSA_NAME) |
| 668 | return NULL; |
| 669 | |
| 670 | code = gimple_assign_rhs_code (stmt); |
| 671 | rhs_class = gimple_assign_rhs_class (stmt); |
| 672 | rhs1_stmt = SSA_NAME_DEF_STMT (rhs1); |
| 673 | |
| 674 | if (rhs_class == GIMPLE_BINARY_RHS) |
| 675 | rhs2 = gimple_assign_rhs2 (stmt); |
| 676 | |
| 677 | /* Handle unary rhs and binary rhs with integer constants as second |
| 678 | operand. */ |
| 679 | |
| 680 | if (rhs_class == GIMPLE_UNARY_RHS |
| 681 | || (rhs_class == GIMPLE_BINARY_RHS |
| 682 | && TREE_CODE (rhs2) == INTEGER_CST)) |
| 683 | { |
| 684 | if (code != BIT_AND_EXPR |
| 685 | && code != LSHIFT_EXPR |
| 686 | && code != RSHIFT_EXPR |
| 687 | && code != LROTATE_EXPR |
| 688 | && code != RROTATE_EXPR |
| 689 | && !CONVERT_EXPR_CODE_P (code)) |
| 690 | return NULL; |
| 691 | |
| 692 | source_stmt1 = find_bswap_or_nop_1 (rhs1_stmt, n, limit - 1); |
| 693 | |
| 694 | /* If find_bswap_or_nop_1 returned NULL, STMT is a leaf node and |
| 695 | we have to initialize the symbolic number. */ |
| 696 | if (!source_stmt1) |
| 697 | { |
| 698 | if (gimple_assign_load_p (stmt) |
| 699 | || !init_symbolic_number (n, rhs1)) |
| 700 | return NULL; |
| 701 | source_stmt1 = stmt; |
| 702 | } |
| 703 | |
| 704 | switch (code) |
| 705 | { |
| 706 | case BIT_AND_EXPR: |
| 707 | { |
| 708 | int i, size = TYPE_PRECISION (n->type) / BITS_PER_UNIT; |
| 709 | uint64_t val = int_cst_value (rhs2), mask = 0; |
| 710 | uint64_t tmp = (1 << BITS_PER_UNIT) - 1; |
| 711 | |
| 712 | /* Only constants masking full bytes are allowed. */ |
| 713 | for (i = 0; i < size; i++, tmp <<= BITS_PER_UNIT) |
| 714 | if ((val & tmp) != 0 && (val & tmp) != tmp) |
| 715 | return NULL; |
| 716 | else if (val & tmp) |
| 717 | mask |= (uint64_t) MARKER_MASK << (i * BITS_PER_MARKER); |
| 718 | |
| 719 | n->n &= mask; |
| 720 | } |
| 721 | break; |
| 722 | case LSHIFT_EXPR: |
| 723 | case RSHIFT_EXPR: |
| 724 | case LROTATE_EXPR: |
| 725 | case RROTATE_EXPR: |
| 726 | if (!do_shift_rotate (code, n, (int) TREE_INT_CST_LOW (rhs2))) |
| 727 | return NULL; |
| 728 | break; |
| 729 | CASE_CONVERT: |
| 730 | { |
| 731 | int i, type_size, old_type_size; |
| 732 | tree type; |
| 733 | |
| 734 | type = TREE_TYPE (gimple_assign_lhs (stmt)); |
| 735 | type_size = TYPE_PRECISION (type); |
| 736 | if (type_size % BITS_PER_UNIT != 0) |
| 737 | return NULL; |
| 738 | type_size /= BITS_PER_UNIT; |
| 739 | if (type_size > 64 / BITS_PER_MARKER) |
| 740 | return NULL; |
| 741 | |
| 742 | /* Sign extension: result is dependent on the value. */ |
| 743 | old_type_size = TYPE_PRECISION (n->type) / BITS_PER_UNIT; |
| 744 | if (!TYPE_UNSIGNED (n->type) && type_size > old_type_size |
| 745 | && HEAD_MARKER (n->n, old_type_size)) |
| 746 | for (i = 0; i < type_size - old_type_size; i++) |
| 747 | n->n |= (uint64_t) MARKER_BYTE_UNKNOWN |
| 748 | << ((type_size - 1 - i) * BITS_PER_MARKER); |
| 749 | |
| 750 | if (type_size < 64 / BITS_PER_MARKER) |
| 751 | { |
| 752 | /* If STMT casts to a smaller type mask out the bits not |
| 753 | belonging to the target type. */ |
| 754 | n->n &= ((uint64_t) 1 << (type_size * BITS_PER_MARKER)) - 1; |
| 755 | } |
| 756 | n->type = type; |
| 757 | if (!n->base_addr) |
| 758 | n->range = type_size; |
| 759 | } |
| 760 | break; |
| 761 | default: |
| 762 | return NULL; |
| 763 | }; |
| 764 | return verify_symbolic_number_p (n, stmt) ? source_stmt1 : NULL; |
| 765 | } |
| 766 | |
| 767 | /* Handle binary rhs. */ |
| 768 | |
| 769 | if (rhs_class == GIMPLE_BINARY_RHS) |
| 770 | { |
| 771 | struct symbolic_number n1, n2; |
| 772 | gimple *source_stmt, *source_stmt2; |
| 773 | |
| 774 | if (!rhs2 || TREE_CODE (rhs2) != SSA_NAME) |
| 775 | return NULL; |
| 776 | |
| 777 | rhs2_stmt = SSA_NAME_DEF_STMT (rhs2); |
| 778 | |
| 779 | switch (code) |
| 780 | { |
| 781 | case BIT_IOR_EXPR: |
| 782 | case BIT_XOR_EXPR: |
| 783 | case PLUS_EXPR: |
| 784 | source_stmt1 = find_bswap_or_nop_1 (rhs1_stmt, &n1, limit - 1); |
| 785 | |
| 786 | if (!source_stmt1) |
| 787 | return NULL; |
| 788 | |
| 789 | source_stmt2 = find_bswap_or_nop_1 (rhs2_stmt, &n2, limit - 1); |
| 790 | |
| 791 | if (!source_stmt2) |
| 792 | return NULL; |
| 793 | |
| 794 | if (TYPE_PRECISION (n1.type) != TYPE_PRECISION (n2.type)) |
| 795 | return NULL; |
| 796 | |
| 797 | if (n1.vuse != n2.vuse) |
| 798 | return NULL; |
| 799 | |
| 800 | source_stmt |
| 801 | = perform_symbolic_merge (source_stmt1, &n1, source_stmt2, &n2, n, |
| 802 | code); |
| 803 | |
| 804 | if (!source_stmt) |
| 805 | return NULL; |
| 806 | |
| 807 | if (!verify_symbolic_number_p (n, stmt)) |
| 808 | return NULL; |
| 809 | |
| 810 | break; |
| 811 | default: |
| 812 | return NULL; |
| 813 | } |
| 814 | return source_stmt; |
| 815 | } |
| 816 | return NULL; |
| 817 | } |
| 818 | |
| 819 | /* Helper for find_bswap_or_nop and try_coalesce_bswap to compute |
| 820 | *CMPXCHG, *CMPNOP and adjust *N. */ |
| 821 | |
| 822 | void |
| 823 | find_bswap_or_nop_finalize (struct symbolic_number *n, uint64_t *cmpxchg, |
| 824 | uint64_t *cmpnop, bool *cast64_to_32) |
| 825 | { |
| 826 | unsigned rsize; |
| 827 | uint64_t tmpn, mask; |
| 828 | |
| 829 | /* The number which the find_bswap_or_nop_1 result should match in order |
| 830 | to have a full byte swap. The number is shifted to the right |
| 831 | according to the size of the symbolic number before using it. */ |
| 832 | *cmpxchg = CMPXCHG; |
| 833 | *cmpnop = CMPNOP; |
| 834 | *cast64_to_32 = false; |
| 835 | |
| 836 | /* Find real size of result (highest non-zero byte). */ |
| 837 | if (n->base_addr) |
| 838 | for (tmpn = n->n, rsize = 0; tmpn; tmpn >>= BITS_PER_MARKER, rsize++); |
| 839 | else |
| 840 | rsize = n->range; |
| 841 | |
| 842 | /* Zero out the bits corresponding to untouched bytes in original gimple |
| 843 | expression. */ |
| 844 | if (n->range < (int) sizeof (int64_t)) |
| 845 | { |
| 846 | mask = ((uint64_t) 1 << (n->range * BITS_PER_MARKER)) - 1; |
| 847 | if (n->base_addr == NULL |
| 848 | && n->range == 4 |
| 849 | && int_size_in_bytes (TREE_TYPE (n->src)) == 8) |
| 850 | { |
| 851 | /* If all bytes in n->n are either 0 or in [5..8] range, this |
| 852 | might be a candidate for (unsigned) __builtin_bswap64 (src). |
| 853 | It is not worth it for (unsigned short) __builtin_bswap64 (src) |
| 854 | or (unsigned short) __builtin_bswap32 (src). */ |
| 855 | *cast64_to_32 = true; |
| 856 | for (tmpn = n->n; tmpn; tmpn >>= BITS_PER_MARKER) |
| 857 | if ((tmpn & MARKER_MASK) |
| 858 | && ((tmpn & MARKER_MASK) <= 4 || (tmpn & MARKER_MASK) > 8)) |
| 859 | { |
| 860 | *cast64_to_32 = false; |
| 861 | break; |
| 862 | } |
| 863 | } |
| 864 | if (*cast64_to_32) |
| 865 | *cmpxchg &= mask; |
| 866 | else |
| 867 | *cmpxchg >>= (64 / BITS_PER_MARKER - n->range) * BITS_PER_MARKER; |
| 868 | *cmpnop &= mask; |
| 869 | } |
| 870 | |
| 871 | /* Zero out the bits corresponding to unused bytes in the result of the |
| 872 | gimple expression. */ |
| 873 | if (rsize < n->range) |
| 874 | { |
| 875 | if (BYTES_BIG_ENDIAN) |
| 876 | { |
| 877 | mask = ((uint64_t) 1 << (rsize * BITS_PER_MARKER)) - 1; |
| 878 | *cmpxchg &= mask; |
| 879 | if (n->range - rsize == sizeof (int64_t)) |
| 880 | *cmpnop = 0; |
| 881 | else |
| 882 | *cmpnop >>= (n->range - rsize) * BITS_PER_MARKER; |
| 883 | } |
| 884 | else |
| 885 | { |
| 886 | mask = ((uint64_t) 1 << (rsize * BITS_PER_MARKER)) - 1; |
| 887 | if (n->range - rsize == sizeof (int64_t)) |
| 888 | *cmpxchg = 0; |
| 889 | else |
| 890 | *cmpxchg >>= (n->range - rsize) * BITS_PER_MARKER; |
| 891 | *cmpnop &= mask; |
| 892 | } |
| 893 | n->range = rsize; |
| 894 | } |
| 895 | |
| 896 | if (*cast64_to_32) |
| 897 | n->range = 8; |
| 898 | n->range *= BITS_PER_UNIT; |
| 899 | } |
| 900 | |
| 901 | /* Helper function for find_bswap_or_nop, |
| 902 | Return true if N is a swap or nop with MASK. */ |
| 903 | static bool |
| 904 | is_bswap_or_nop_p (uint64_t n, uint64_t cmpxchg, |
| 905 | uint64_t cmpnop, uint64_t* mask, |
| 906 | bool* bswap) |
| 907 | { |
| 908 | *mask = ~(uint64_t) 0; |
| 909 | if (n == cmpnop) |
| 910 | *bswap = false; |
| 911 | else if (n == cmpxchg) |
| 912 | *bswap = true; |
| 913 | else |
| 914 | { |
| 915 | int set = 0; |
| 916 | for (uint64_t msk = MARKER_MASK; msk; msk <<= BITS_PER_MARKER) |
| 917 | if ((n & msk) == 0) |
| 918 | *mask &= ~msk; |
| 919 | else if ((n & msk) == (cmpxchg & msk)) |
| 920 | set++; |
| 921 | else |
| 922 | return false; |
| 923 | |
| 924 | if (set < 2) |
| 925 | return false; |
| 926 | *bswap = true; |
| 927 | } |
| 928 | return true; |
| 929 | } |
| 930 | |
| 931 | |
| 932 | /* Check if STMT completes a bswap implementation or a read in a given |
| 933 | endianness consisting of ORs, SHIFTs and ANDs and sets *BSWAP |
| 934 | accordingly. It also sets N to represent the kind of operations |
| 935 | performed: size of the resulting expression and whether it works on |
| 936 | a memory source, and if so alias-set and vuse. At last, the |
| 937 | function returns a stmt whose rhs's first tree is the source |
| 938 | expression. */ |
| 939 | |
| 940 | gimple * |
| 941 | find_bswap_or_nop (gimple *stmt, struct symbolic_number *n, bool *bswap, |
| 942 | bool *cast64_to_32, uint64_t *mask, uint64_t* l_rotate) |
| 943 | { |
| 944 | tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (gimple_get_lhs (stmt))); |
| 945 | if (!tree_fits_uhwi_p (type_size)) |
| 946 | return NULL; |
| 947 | |
| 948 | /* The last parameter determines the depth search limit. It usually |
| 949 | correlates directly to the number n of bytes to be touched. We |
| 950 | increase that number by 2 * (log2(n) + 1) here in order to also |
| 951 | cover signed -> unsigned conversions of the src operand as can be seen |
| 952 | in libgcc, and for initial shift/and operation of the src operand. */ |
| 953 | int limit = tree_to_uhwi (type_size); |
| 954 | limit += 2 * (1 + (int) ceil_log2 ((unsigned HOST_WIDE_INT) limit)); |
| 955 | gimple *ins_stmt = find_bswap_or_nop_1 (stmt, n, limit); |
| 956 | |
| 957 | if (!ins_stmt) |
| 958 | { |
| 959 | if (gimple_assign_rhs_code (stmt) != CONSTRUCTOR |
| 960 | || BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) |
| 961 | return NULL; |
| 962 | unsigned HOST_WIDE_INT sz = tree_to_uhwi (type_size) * BITS_PER_UNIT; |
| 963 | if (sz != 16 && sz != 32 && sz != 64) |
| 964 | return NULL; |
| 965 | tree rhs = gimple_assign_rhs1 (stmt); |
| 966 | if (CONSTRUCTOR_NELTS (rhs) == 0) |
| 967 | return NULL; |
| 968 | tree eltype = TREE_TYPE (TREE_TYPE (rhs)); |
| 969 | unsigned HOST_WIDE_INT eltsz |
| 970 | = int_size_in_bytes (eltype) * BITS_PER_UNIT; |
| 971 | if (TYPE_PRECISION (eltype) != eltsz) |
| 972 | return NULL; |
| 973 | constructor_elt *elt; |
| 974 | unsigned int i; |
| 975 | tree type = build_nonstandard_integer_type (sz, 1); |
| 976 | FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (rhs), i, elt) |
| 977 | { |
| 978 | if (TREE_CODE (elt->value) != SSA_NAME |
| 979 | || !INTEGRAL_TYPE_P (TREE_TYPE (elt->value))) |
| 980 | return NULL; |
| 981 | struct symbolic_number n1; |
| 982 | gimple *source_stmt |
| 983 | = find_bswap_or_nop_1 (SSA_NAME_DEF_STMT (elt->value), &n1, |
| 984 | limit - 1); |
| 985 | |
| 986 | if (!source_stmt) |
| 987 | return NULL; |
| 988 | |
| 989 | n1.type = type; |
| 990 | if (!n1.base_addr) |
| 991 | n1.range = sz / BITS_PER_UNIT; |
| 992 | |
| 993 | if (i == 0) |
| 994 | { |
| 995 | ins_stmt = source_stmt; |
| 996 | *n = n1; |
| 997 | } |
| 998 | else |
| 999 | { |
| 1000 | if (n->vuse != n1.vuse) |
| 1001 | return NULL; |
| 1002 | |
| 1003 | struct symbolic_number n0 = *n; |
| 1004 | |
| 1005 | if (!BYTES_BIG_ENDIAN) |
| 1006 | { |
| 1007 | if (!do_shift_rotate (LSHIFT_EXPR, &n1, i * eltsz)) |
| 1008 | return NULL; |
| 1009 | } |
| 1010 | else if (!do_shift_rotate (LSHIFT_EXPR, &n0, eltsz)) |
| 1011 | return NULL; |
| 1012 | ins_stmt |
| 1013 | = perform_symbolic_merge (ins_stmt, &n0, source_stmt, &n1, n, |
| 1014 | BIT_IOR_EXPR); |
| 1015 | |
| 1016 | if (!ins_stmt) |
| 1017 | return NULL; |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | uint64_t cmpxchg, cmpnop; |
| 1023 | uint64_t orig_range = n->range * BITS_PER_UNIT; |
| 1024 | find_bswap_or_nop_finalize (n, &cmpxchg, &cmpnop, cast64_to_32); |
| 1025 | |
| 1026 | /* A complete byte swap should make the symbolic number to start with |
| 1027 | the largest digit in the highest order byte. Unchanged symbolic |
| 1028 | number indicates a read with same endianness as target architecture. */ |
| 1029 | *l_rotate = 0; |
| 1030 | uint64_t tmp_n = n->n; |
| 1031 | if (!is_bswap_or_nop_p (tmp_n, cmpxchg, cmpnop, mask, bswap)) |
| 1032 | { |
| 1033 | /* Try bswap + lrotate. */ |
| 1034 | /* TODO, handle cast64_to_32 and big/litte_endian memory |
| 1035 | source when rsize < range. */ |
| 1036 | if (n->range == orig_range |
| 1037 | /* There're case like 0x300000200 for uint32->uint64 cast, |
| 1038 | Don't hanlde this. */ |
| 1039 | && n->range == TYPE_PRECISION (n->type) |
| 1040 | && ((orig_range == 32 |
| 1041 | && optab_handler (rotl_optab, SImode) != CODE_FOR_nothing) |
| 1042 | || (orig_range == 64 |
| 1043 | && optab_handler (rotl_optab, DImode) != CODE_FOR_nothing)) |
| 1044 | && (tmp_n & MARKER_MASK) < orig_range / BITS_PER_UNIT) |
| 1045 | { |
| 1046 | uint64_t range = (orig_range / BITS_PER_UNIT) * BITS_PER_MARKER; |
| 1047 | uint64_t count = (tmp_n & MARKER_MASK) * BITS_PER_MARKER; |
| 1048 | /* .i.e. hanlde 0x203040506070800 when lower byte is zero. */ |
| 1049 | if (!count) |
| 1050 | { |
| 1051 | for (uint64_t i = 1; i != range / BITS_PER_MARKER; i++) |
| 1052 | { |
| 1053 | count = (tmp_n >> i * BITS_PER_MARKER) & MARKER_MASK; |
| 1054 | if (count) |
| 1055 | { |
| 1056 | /* Count should be meaningful not 0xff. */ |
| 1057 | if (count <= range / BITS_PER_MARKER) |
| 1058 | { |
| 1059 | count = (count + i) * BITS_PER_MARKER % range; |
| 1060 | if (!count) |
| 1061 | return NULL; |
| 1062 | break; |
| 1063 | } |
| 1064 | else |
| 1065 | return NULL; |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | tmp_n = tmp_n >> count | tmp_n << (range - count); |
| 1070 | if (orig_range == 32) |
| 1071 | tmp_n &= (1ULL << 32) - 1; |
| 1072 | if (!is_bswap_or_nop_p (tmp_n, cmpxchg, cmpnop, mask, bswap)) |
| 1073 | return NULL; |
| 1074 | *l_rotate = count / BITS_PER_MARKER * BITS_PER_UNIT; |
| 1075 | gcc_assert (*bswap); |
| 1076 | } |
| 1077 | else |
| 1078 | return NULL; |
| 1079 | } |
| 1080 | |
| 1081 | /* Useless bit manipulation performed by code. */ |
| 1082 | if (!n->base_addr && n->n == cmpnop && n->n_ops == 1) |
| 1083 | return NULL; |
| 1084 | |
| 1085 | return ins_stmt; |
| 1086 | } |
| 1087 | |
| 1088 | const pass_data pass_data_optimize_bswap = |
| 1089 | { |
| 1090 | GIMPLE_PASS, /* type */ |
| 1091 | "bswap", /* name */ |
| 1092 | OPTGROUP_NONE, /* optinfo_flags */ |
| 1093 | TV_NONE, /* tv_id */ |
| 1094 | PROP_ssa, /* properties_required */ |
| 1095 | 0, /* properties_provided */ |
| 1096 | 0, /* properties_destroyed */ |
| 1097 | 0, /* todo_flags_start */ |
| 1098 | 0, /* todo_flags_finish */ |
| 1099 | }; |
| 1100 | |
| 1101 | class pass_optimize_bswap : public gimple_opt_pass |
| 1102 | { |
| 1103 | public: |
| 1104 | pass_optimize_bswap (gcc::context *ctxt) |
| 1105 | : gimple_opt_pass (pass_data_optimize_bswap, ctxt) |
| 1106 | {} |
| 1107 | |
| 1108 | /* opt_pass methods: */ |
| 1109 | bool gate (function *) final override |
| 1110 | { |
| 1111 | return flag_expensive_optimizations && optimize && BITS_PER_UNIT == 8; |
| 1112 | } |
| 1113 | |
| 1114 | unsigned int execute (function *) final override; |
| 1115 | |
| 1116 | }; // class pass_optimize_bswap |
| 1117 | |
| 1118 | /* Helper function for bswap_replace. Build VIEW_CONVERT_EXPR from |
| 1119 | VAL to TYPE. If VAL has different type size, emit a NOP_EXPR cast |
| 1120 | first. */ |
| 1121 | |
| 1122 | static tree |
| 1123 | bswap_view_convert (gimple_stmt_iterator *gsi, tree type, tree val, |
| 1124 | bool before) |
| 1125 | { |
| 1126 | gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (val)) |
| 1127 | || POINTER_TYPE_P (TREE_TYPE (val))); |
| 1128 | if (TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (val))) |
| 1129 | { |
| 1130 | HOST_WIDE_INT prec = TREE_INT_CST_LOW (TYPE_SIZE (type)); |
| 1131 | if (POINTER_TYPE_P (TREE_TYPE (val))) |
| 1132 | { |
| 1133 | gimple *g |
| 1134 | = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
| 1135 | NOP_EXPR, val); |
| 1136 | if (before) |
| 1137 | gsi_insert_before (gsi, g, GSI_SAME_STMT); |
| 1138 | else |
| 1139 | gsi_insert_after (gsi, g, GSI_NEW_STMT); |
| 1140 | val = gimple_assign_lhs (g); |
| 1141 | } |
| 1142 | tree itype = build_nonstandard_integer_type (prec, 1); |
| 1143 | gimple *g = gimple_build_assign (make_ssa_name (itype), NOP_EXPR, val); |
| 1144 | if (before) |
| 1145 | gsi_insert_before (gsi, g, GSI_SAME_STMT); |
| 1146 | else |
| 1147 | gsi_insert_after (gsi, g, GSI_NEW_STMT); |
| 1148 | val = gimple_assign_lhs (g); |
| 1149 | } |
| 1150 | return build1 (VIEW_CONVERT_EXPR, type, val); |
| 1151 | } |
| 1152 | |
| 1153 | /* Perform the bswap optimization: replace the expression computed in the rhs |
| 1154 | of gsi_stmt (GSI) (or if NULL add instead of replace) by an equivalent |
| 1155 | bswap, load or load + bswap expression. |
| 1156 | Which of these alternatives replace the rhs is given by N->base_addr (non |
| 1157 | null if a load is needed) and BSWAP. The type, VUSE and set-alias of the |
| 1158 | load to perform are also given in N while the builtin bswap invoke is given |
| 1159 | in FNDEL. Finally, if a load is involved, INS_STMT refers to one of the |
| 1160 | load statements involved to construct the rhs in gsi_stmt (GSI) and |
| 1161 | N->range gives the size of the rhs expression for maintaining some |
| 1162 | statistics. |
| 1163 | |
| 1164 | Note that if the replacement involve a load and if gsi_stmt (GSI) is |
| 1165 | non-NULL, that stmt is moved just after INS_STMT to do the load with the |
| 1166 | same VUSE which can lead to gsi_stmt (GSI) changing of basic block. */ |
| 1167 | |
| 1168 | tree |
| 1169 | bswap_replace (gimple_stmt_iterator gsi, gimple *ins_stmt, tree fndecl, |
| 1170 | tree bswap_type, tree load_type, struct symbolic_number *n, |
| 1171 | bool bswap, uint64_t mask, uint64_t l_rotate) |
| 1172 | { |
| 1173 | tree src, tmp, tgt = NULL_TREE; |
| 1174 | gimple *bswap_stmt, *mask_stmt = NULL, *rotl_stmt = NULL; |
| 1175 | tree_code conv_code = NOP_EXPR; |
| 1176 | |
| 1177 | gimple *cur_stmt = gsi_stmt (gsi); |
| 1178 | src = n->src; |
| 1179 | if (cur_stmt) |
| 1180 | { |
| 1181 | tgt = gimple_assign_lhs (cur_stmt); |
| 1182 | if (gimple_assign_rhs_code (cur_stmt) == CONSTRUCTOR |
| 1183 | && tgt |
| 1184 | && VECTOR_TYPE_P (TREE_TYPE (tgt))) |
| 1185 | conv_code = VIEW_CONVERT_EXPR; |
| 1186 | } |
| 1187 | |
| 1188 | /* Need to load the value from memory first. */ |
| 1189 | if (n->base_addr) |
| 1190 | { |
| 1191 | gimple_stmt_iterator gsi_ins = gsi; |
| 1192 | if (ins_stmt) |
| 1193 | gsi_ins = gsi_for_stmt (ins_stmt); |
| 1194 | tree addr_expr, addr_tmp, val_expr, val_tmp; |
| 1195 | tree load_offset_ptr, aligned_load_type; |
| 1196 | gimple *load_stmt; |
| 1197 | unsigned align = get_object_alignment (src); |
| 1198 | poly_int64 load_offset = 0; |
| 1199 | |
| 1200 | if (cur_stmt) |
| 1201 | { |
| 1202 | basic_block ins_bb = gimple_bb (ins_stmt); |
| 1203 | basic_block cur_bb = gimple_bb (cur_stmt); |
| 1204 | if (!dominated_by_p (CDI_DOMINATORS, cur_bb, ins_bb)) |
| 1205 | return NULL_TREE; |
| 1206 | |
| 1207 | /* Move cur_stmt just before one of the load of the original |
| 1208 | to ensure it has the same VUSE. See PR61517 for what could |
| 1209 | go wrong. */ |
| 1210 | if (gimple_bb (cur_stmt) != gimple_bb (ins_stmt)) |
| 1211 | reset_flow_sensitive_info (gimple_assign_lhs (cur_stmt)); |
| 1212 | gsi_move_before (&gsi, &gsi_ins); |
| 1213 | gsi = gsi_for_stmt (cur_stmt); |
| 1214 | } |
| 1215 | else |
| 1216 | gsi = gsi_ins; |
| 1217 | |
| 1218 | /* Compute address to load from and cast according to the size |
| 1219 | of the load. */ |
| 1220 | addr_expr = build_fold_addr_expr (src); |
| 1221 | if (is_gimple_mem_ref_addr (addr_expr)) |
| 1222 | addr_tmp = unshare_expr (addr_expr); |
| 1223 | else |
| 1224 | { |
| 1225 | addr_tmp = unshare_expr (n->base_addr); |
| 1226 | if (!is_gimple_mem_ref_addr (addr_tmp)) |
| 1227 | addr_tmp = force_gimple_operand_gsi_1 (&gsi, addr_tmp, |
| 1228 | is_gimple_mem_ref_addr, |
| 1229 | NULL_TREE, true, |
| 1230 | GSI_SAME_STMT); |
| 1231 | load_offset = n->bytepos; |
| 1232 | if (n->offset) |
| 1233 | { |
| 1234 | tree off |
| 1235 | = force_gimple_operand_gsi (&gsi, unshare_expr (n->offset), |
| 1236 | true, NULL_TREE, true, |
| 1237 | GSI_SAME_STMT); |
| 1238 | gimple *stmt |
| 1239 | = gimple_build_assign (make_ssa_name (TREE_TYPE (addr_tmp)), |
| 1240 | POINTER_PLUS_EXPR, addr_tmp, off); |
| 1241 | gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); |
| 1242 | addr_tmp = gimple_assign_lhs (stmt); |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | /* Perform the load. */ |
| 1247 | aligned_load_type = load_type; |
| 1248 | if (align < TYPE_ALIGN (load_type)) |
| 1249 | aligned_load_type = build_aligned_type (load_type, align); |
| 1250 | load_offset_ptr = build_int_cst (n->alias_set, load_offset); |
| 1251 | val_expr = fold_build2 (MEM_REF, aligned_load_type, addr_tmp, |
| 1252 | load_offset_ptr); |
| 1253 | |
| 1254 | if (!bswap) |
| 1255 | { |
| 1256 | if (n->range == 16) |
| 1257 | nop_stats.found_16bit++; |
| 1258 | else if (n->range == 32) |
| 1259 | nop_stats.found_32bit++; |
| 1260 | else |
| 1261 | { |
| 1262 | gcc_assert (n->range == 64); |
| 1263 | nop_stats.found_64bit++; |
| 1264 | } |
| 1265 | |
| 1266 | /* Convert the result of load if necessary. */ |
| 1267 | if (tgt && !useless_type_conversion_p (TREE_TYPE (tgt), load_type)) |
| 1268 | { |
| 1269 | val_tmp = make_temp_ssa_name (aligned_load_type, NULL, |
| 1270 | "load_dst"); |
| 1271 | load_stmt = gimple_build_assign (val_tmp, val_expr); |
| 1272 | gimple_set_vuse (load_stmt, n->vuse); |
| 1273 | gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT); |
| 1274 | if (conv_code == VIEW_CONVERT_EXPR) |
| 1275 | val_tmp = bswap_view_convert (&gsi, TREE_TYPE (tgt), val_tmp, |
| 1276 | true); |
| 1277 | gimple_assign_set_rhs_with_ops (&gsi, conv_code, val_tmp); |
| 1278 | update_stmt (cur_stmt); |
| 1279 | } |
| 1280 | else if (cur_stmt) |
| 1281 | { |
| 1282 | gimple_assign_set_rhs_with_ops (&gsi, MEM_REF, val_expr); |
| 1283 | gimple_set_vuse (cur_stmt, n->vuse); |
| 1284 | update_stmt (cur_stmt); |
| 1285 | } |
| 1286 | else |
| 1287 | { |
| 1288 | tgt = make_ssa_name (load_type); |
| 1289 | cur_stmt = gimple_build_assign (tgt, MEM_REF, val_expr); |
| 1290 | gimple_set_vuse (cur_stmt, n->vuse); |
| 1291 | gsi_insert_before (&gsi, cur_stmt, GSI_SAME_STMT); |
| 1292 | } |
| 1293 | |
| 1294 | if (dump_file) |
| 1295 | { |
| 1296 | fprintf (dump_file, |
| 1297 | "%d bit load in target endianness found at: ", |
| 1298 | (int) n->range); |
| 1299 | print_gimple_stmt (dump_file, cur_stmt, 0); |
| 1300 | } |
| 1301 | return tgt; |
| 1302 | } |
| 1303 | else |
| 1304 | { |
| 1305 | val_tmp = make_temp_ssa_name (aligned_load_type, NULL, "load_dst"); |
| 1306 | load_stmt = gimple_build_assign (val_tmp, val_expr); |
| 1307 | gimple_set_vuse (load_stmt, n->vuse); |
| 1308 | gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT); |
| 1309 | } |
| 1310 | src = val_tmp; |
| 1311 | } |
| 1312 | else if (!bswap) |
| 1313 | { |
| 1314 | gimple *g = NULL; |
| 1315 | if (tgt && !useless_type_conversion_p (TREE_TYPE (tgt), TREE_TYPE (src))) |
| 1316 | { |
| 1317 | if (!is_gimple_val (src)) |
| 1318 | return NULL_TREE; |
| 1319 | if (conv_code == VIEW_CONVERT_EXPR) |
| 1320 | src = bswap_view_convert (&gsi, TREE_TYPE (tgt), src, true); |
| 1321 | g = gimple_build_assign (tgt, conv_code, src); |
| 1322 | } |
| 1323 | else if (cur_stmt) |
| 1324 | g = gimple_build_assign (tgt, src); |
| 1325 | else |
| 1326 | tgt = src; |
| 1327 | if (n->range == 16) |
| 1328 | nop_stats.found_16bit++; |
| 1329 | else if (n->range == 32) |
| 1330 | nop_stats.found_32bit++; |
| 1331 | else |
| 1332 | { |
| 1333 | gcc_assert (n->range == 64); |
| 1334 | nop_stats.found_64bit++; |
| 1335 | } |
| 1336 | if (dump_file) |
| 1337 | { |
| 1338 | fprintf (dump_file, |
| 1339 | "%d bit reshuffle in target endianness found at: ", |
| 1340 | (int) n->range); |
| 1341 | if (cur_stmt) |
| 1342 | print_gimple_stmt (dump_file, cur_stmt, 0); |
| 1343 | else |
| 1344 | { |
| 1345 | print_generic_expr (dump_file, tgt, TDF_NONE); |
| 1346 | fprintf (dump_file, "\n"); |
| 1347 | } |
| 1348 | } |
| 1349 | if (cur_stmt) |
| 1350 | gsi_replace (&gsi, g, true); |
| 1351 | return tgt; |
| 1352 | } |
| 1353 | else if (TREE_CODE (src) == BIT_FIELD_REF) |
| 1354 | src = TREE_OPERAND (src, 0); |
| 1355 | |
| 1356 | if (n->range == 16) |
| 1357 | bswap_stats.found_16bit++; |
| 1358 | else if (n->range == 32) |
| 1359 | bswap_stats.found_32bit++; |
| 1360 | else |
| 1361 | { |
| 1362 | gcc_assert (n->range == 64); |
| 1363 | bswap_stats.found_64bit++; |
| 1364 | } |
| 1365 | |
| 1366 | tmp = src; |
| 1367 | |
| 1368 | /* Convert the src expression if necessary. */ |
| 1369 | if (!useless_type_conversion_p (TREE_TYPE (tmp), bswap_type)) |
| 1370 | { |
| 1371 | gimple *convert_stmt; |
| 1372 | |
| 1373 | tmp = make_temp_ssa_name (bswap_type, NULL, "bswapsrc"); |
| 1374 | convert_stmt = gimple_build_assign (tmp, NOP_EXPR, src); |
| 1375 | gsi_insert_before (&gsi, convert_stmt, GSI_SAME_STMT); |
| 1376 | } |
| 1377 | |
| 1378 | /* Canonical form for 16 bit bswap is a rotate expression. Only 16bit values |
| 1379 | are considered as rotation of 2N bit values by N bits is generally not |
| 1380 | equivalent to a bswap. Consider for instance 0x01020304 r>> 16 which |
| 1381 | gives 0x03040102 while a bswap for that value is 0x04030201. */ |
| 1382 | if (bswap && n->range == 16) |
| 1383 | { |
| 1384 | tree count = build_int_cst (NULL, BITS_PER_UNIT); |
| 1385 | src = fold_build2 (LROTATE_EXPR, bswap_type, tmp, count); |
| 1386 | bswap_stmt = gimple_build_assign (NULL, src); |
| 1387 | } |
| 1388 | else |
| 1389 | bswap_stmt = gimple_build_call (fndecl, 1, tmp); |
| 1390 | |
| 1391 | if (tgt == NULL_TREE) |
| 1392 | tgt = make_ssa_name (bswap_type); |
| 1393 | tmp = tgt; |
| 1394 | |
| 1395 | if (mask != ~(uint64_t) 0) |
| 1396 | { |
| 1397 | tree m = build_int_cst (bswap_type, mask); |
| 1398 | tmp = make_temp_ssa_name (bswap_type, NULL, "bswapdst"); |
| 1399 | gimple_set_lhs (bswap_stmt, tmp); |
| 1400 | mask_stmt = gimple_build_assign (tgt, BIT_AND_EXPR, tmp, m); |
| 1401 | tmp = tgt; |
| 1402 | } |
| 1403 | |
| 1404 | if (l_rotate) |
| 1405 | { |
| 1406 | tree m = build_int_cst (bswap_type, l_rotate); |
| 1407 | tmp = make_temp_ssa_name (bswap_type, NULL, |
| 1408 | mask_stmt ? "bswapmaskdst" : "bswapdst"); |
| 1409 | gimple_set_lhs (mask_stmt ? mask_stmt : bswap_stmt, tmp); |
| 1410 | rotl_stmt = gimple_build_assign (tgt, LROTATE_EXPR, tmp, m); |
| 1411 | tmp = tgt; |
| 1412 | } |
| 1413 | |
| 1414 | /* Convert the result if necessary. */ |
| 1415 | if (!useless_type_conversion_p (TREE_TYPE (tgt), bswap_type)) |
| 1416 | { |
| 1417 | tmp = make_temp_ssa_name (bswap_type, NULL, "bswapdst"); |
| 1418 | tree atmp = tmp; |
| 1419 | gimple_stmt_iterator gsi2 = gsi; |
| 1420 | if (conv_code == VIEW_CONVERT_EXPR) |
| 1421 | atmp = bswap_view_convert (&gsi2, TREE_TYPE (tgt), tmp, false); |
| 1422 | gimple *convert_stmt = gimple_build_assign (tgt, conv_code, atmp); |
| 1423 | gsi_insert_after (&gsi2, convert_stmt, GSI_SAME_STMT); |
| 1424 | } |
| 1425 | |
| 1426 | gimple_set_lhs (rotl_stmt ? rotl_stmt |
| 1427 | : mask_stmt ? mask_stmt : bswap_stmt, tmp); |
| 1428 | |
| 1429 | if (dump_file) |
| 1430 | { |
| 1431 | fprintf (dump_file, "%d bit bswap implementation found at: ", |
| 1432 | (int) n->range); |
| 1433 | if (cur_stmt) |
| 1434 | print_gimple_stmt (dump_file, cur_stmt, 0); |
| 1435 | else |
| 1436 | { |
| 1437 | print_generic_expr (dump_file, tgt, TDF_NONE); |
| 1438 | fprintf (dump_file, "\n"); |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | if (cur_stmt) |
| 1443 | { |
| 1444 | if (rotl_stmt) |
| 1445 | gsi_insert_after (&gsi, rotl_stmt, GSI_SAME_STMT); |
| 1446 | if (mask_stmt) |
| 1447 | gsi_insert_after (&gsi, mask_stmt, GSI_SAME_STMT); |
| 1448 | gsi_insert_after (&gsi, bswap_stmt, GSI_SAME_STMT); |
| 1449 | gsi_remove (&gsi, true); |
| 1450 | } |
| 1451 | else |
| 1452 | { |
| 1453 | gsi_insert_before (&gsi, bswap_stmt, GSI_SAME_STMT); |
| 1454 | if (mask_stmt) |
| 1455 | gsi_insert_before (&gsi, mask_stmt, GSI_SAME_STMT); |
| 1456 | if (rotl_stmt) |
| 1457 | gsi_insert_after (&gsi, rotl_stmt, GSI_SAME_STMT); |
| 1458 | } |
| 1459 | return tgt; |
| 1460 | } |
| 1461 | |
| 1462 | /* Try to optimize an assignment CUR_STMT with CONSTRUCTOR on the rhs |
| 1463 | using bswap optimizations. CDI_DOMINATORS need to be |
| 1464 | computed on entry. Return true if it has been optimized and |
| 1465 | TODO_update_ssa is needed. */ |
| 1466 | |
| 1467 | static bool |
| 1468 | maybe_optimize_vector_constructor (gimple *cur_stmt) |
| 1469 | { |
| 1470 | tree fndecl = NULL_TREE, bswap_type = NULL_TREE, load_type; |
| 1471 | struct symbolic_number n; |
| 1472 | bool bswap; |
| 1473 | |
| 1474 | gcc_assert (is_gimple_assign (cur_stmt) |
| 1475 | && gimple_assign_rhs_code (cur_stmt) == CONSTRUCTOR); |
| 1476 | |
| 1477 | tree rhs = gimple_assign_rhs1 (cur_stmt); |
| 1478 | if (!VECTOR_TYPE_P (TREE_TYPE (rhs)) |
| 1479 | || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (rhs))) |
| 1480 | || gimple_assign_lhs (cur_stmt) == NULL_TREE) |
| 1481 | return false; |
| 1482 | |
| 1483 | HOST_WIDE_INT sz = int_size_in_bytes (TREE_TYPE (rhs)) * BITS_PER_UNIT; |
| 1484 | switch (sz) |
| 1485 | { |
| 1486 | case 16: |
| 1487 | load_type = bswap_type = uint16_type_node; |
| 1488 | break; |
| 1489 | case 32: |
| 1490 | if (builtin_decl_explicit_p (BUILT_IN_BSWAP32) |
| 1491 | && optab_handler (bswap_optab, SImode) != CODE_FOR_nothing) |
| 1492 | { |
| 1493 | load_type = uint32_type_node; |
| 1494 | fndecl = builtin_decl_explicit (BUILT_IN_BSWAP32); |
| 1495 | bswap_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); |
| 1496 | } |
| 1497 | else |
| 1498 | return false; |
| 1499 | break; |
| 1500 | case 64: |
| 1501 | if (builtin_decl_explicit_p (BUILT_IN_BSWAP64) |
| 1502 | && (optab_handler (bswap_optab, DImode) != CODE_FOR_nothing |
| 1503 | || (word_mode == SImode |
| 1504 | && builtin_decl_explicit_p (BUILT_IN_BSWAP32) |
| 1505 | && optab_handler (bswap_optab, SImode) != CODE_FOR_nothing))) |
| 1506 | { |
| 1507 | load_type = uint64_type_node; |
| 1508 | fndecl = builtin_decl_explicit (BUILT_IN_BSWAP64); |
| 1509 | bswap_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); |
| 1510 | } |
| 1511 | else |
| 1512 | return false; |
| 1513 | break; |
| 1514 | default: |
| 1515 | return false; |
| 1516 | } |
| 1517 | |
| 1518 | bool cast64_to_32; |
| 1519 | uint64_t mask, l_rotate; |
| 1520 | gimple *ins_stmt = find_bswap_or_nop (cur_stmt, &n, &bswap, |
| 1521 | &cast64_to_32, &mask, &l_rotate); |
| 1522 | if (!ins_stmt |
| 1523 | || n.range != (unsigned HOST_WIDE_INT) sz |
| 1524 | || cast64_to_32 |
| 1525 | || mask != ~(uint64_t) 0) |
| 1526 | return false; |
| 1527 | |
| 1528 | if (bswap && !fndecl && n.range != 16) |
| 1529 | return false; |
| 1530 | |
| 1531 | memset (&nop_stats, 0, sizeof (nop_stats)); |
| 1532 | memset (&bswap_stats, 0, sizeof (bswap_stats)); |
| 1533 | return bswap_replace (gsi_for_stmt (cur_stmt), ins_stmt, fndecl, |
| 1534 | bswap_type, load_type, &n, bswap, mask, |
| 1535 | l_rotate) != NULL_TREE; |
| 1536 | } |
| 1537 | |
| 1538 | /* Find manual byte swap implementations as well as load in a given |
| 1539 | endianness. Byte swaps are turned into a bswap builtin invokation |
| 1540 | while endian loads are converted to bswap builtin invokation or |
| 1541 | simple load according to the target endianness. */ |
| 1542 | |
| 1543 | unsigned int |
| 1544 | pass_optimize_bswap::execute (function *fun) |
| 1545 | { |
| 1546 | basic_block bb; |
| 1547 | bool bswap32_p, bswap64_p; |
| 1548 | bool changed = false; |
| 1549 | tree bswap32_type = NULL_TREE, bswap64_type = NULL_TREE; |
| 1550 | |
| 1551 | bswap32_p = (builtin_decl_explicit_p (BUILT_IN_BSWAP32) |
| 1552 | && optab_handler (bswap_optab, SImode) != CODE_FOR_nothing); |
| 1553 | bswap64_p = (builtin_decl_explicit_p (BUILT_IN_BSWAP64) |
| 1554 | && (optab_handler (bswap_optab, DImode) != CODE_FOR_nothing |
| 1555 | || (bswap32_p && word_mode == SImode))); |
| 1556 | |
| 1557 | /* Determine the argument type of the builtins. The code later on |
| 1558 | assumes that the return and argument type are the same. */ |
| 1559 | if (bswap32_p) |
| 1560 | { |
| 1561 | tree fndecl = builtin_decl_explicit (BUILT_IN_BSWAP32); |
| 1562 | bswap32_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); |
| 1563 | } |
| 1564 | |
| 1565 | if (bswap64_p) |
| 1566 | { |
| 1567 | tree fndecl = builtin_decl_explicit (BUILT_IN_BSWAP64); |
| 1568 | bswap64_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); |
| 1569 | } |
| 1570 | |
| 1571 | memset (&nop_stats, 0, sizeof (nop_stats)); |
| 1572 | memset (&bswap_stats, 0, sizeof (bswap_stats)); |
| 1573 | calculate_dominance_info (CDI_DOMINATORS); |
| 1574 | |
| 1575 | FOR_EACH_BB_FN (bb, fun) |
| 1576 | { |
| 1577 | gimple_stmt_iterator gsi; |
| 1578 | |
| 1579 | /* We do a reverse scan for bswap patterns to make sure we get the |
| 1580 | widest match. As bswap pattern matching doesn't handle previously |
| 1581 | inserted smaller bswap replacements as sub-patterns, the wider |
| 1582 | variant wouldn't be detected. */ |
| 1583 | for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);) |
| 1584 | { |
| 1585 | gimple *ins_stmt, *cur_stmt = gsi_stmt (gsi); |
| 1586 | tree fndecl = NULL_TREE, bswap_type = NULL_TREE, load_type; |
| 1587 | enum tree_code code; |
| 1588 | struct symbolic_number n; |
| 1589 | bool bswap, cast64_to_32; |
| 1590 | uint64_t mask, l_rotate; |
| 1591 | |
| 1592 | /* This gsi_prev (&gsi) is not part of the for loop because cur_stmt |
| 1593 | might be moved to a different basic block by bswap_replace and gsi |
| 1594 | must not points to it if that's the case. Moving the gsi_prev |
| 1595 | there make sure that gsi points to the statement previous to |
| 1596 | cur_stmt while still making sure that all statements are |
| 1597 | considered in this basic block. */ |
| 1598 | gsi_prev (&gsi); |
| 1599 | |
| 1600 | if (!is_gimple_assign (cur_stmt)) |
| 1601 | continue; |
| 1602 | |
| 1603 | code = gimple_assign_rhs_code (cur_stmt); |
| 1604 | switch (code) |
| 1605 | { |
| 1606 | case LROTATE_EXPR: |
| 1607 | case RROTATE_EXPR: |
| 1608 | if (!tree_fits_uhwi_p (gimple_assign_rhs2 (cur_stmt)) |
| 1609 | || tree_to_uhwi (gimple_assign_rhs2 (cur_stmt)) |
| 1610 | % BITS_PER_UNIT) |
| 1611 | continue; |
| 1612 | /* Fall through. */ |
| 1613 | case BIT_IOR_EXPR: |
| 1614 | case BIT_XOR_EXPR: |
| 1615 | case PLUS_EXPR: |
| 1616 | break; |
| 1617 | case CONSTRUCTOR: |
| 1618 | { |
| 1619 | tree rhs = gimple_assign_rhs1 (cur_stmt); |
| 1620 | if (VECTOR_TYPE_P (TREE_TYPE (rhs)) |
| 1621 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (rhs)))) |
| 1622 | break; |
| 1623 | } |
| 1624 | continue; |
| 1625 | default: |
| 1626 | continue; |
| 1627 | } |
| 1628 | |
| 1629 | ins_stmt = find_bswap_or_nop (cur_stmt, &n, &bswap, |
| 1630 | &cast64_to_32, &mask, &l_rotate); |
| 1631 | |
| 1632 | if (!ins_stmt) |
| 1633 | continue; |
| 1634 | |
| 1635 | switch (n.range) |
| 1636 | { |
| 1637 | case 16: |
| 1638 | /* Already in canonical form, nothing to do. */ |
| 1639 | if (code == LROTATE_EXPR || code == RROTATE_EXPR) |
| 1640 | continue; |
| 1641 | load_type = bswap_type = uint16_type_node; |
| 1642 | break; |
| 1643 | case 32: |
| 1644 | load_type = uint32_type_node; |
| 1645 | if (bswap32_p) |
| 1646 | { |
| 1647 | fndecl = builtin_decl_explicit (BUILT_IN_BSWAP32); |
| 1648 | bswap_type = bswap32_type; |
| 1649 | } |
| 1650 | break; |
| 1651 | case 64: |
| 1652 | load_type = uint64_type_node; |
| 1653 | if (bswap64_p) |
| 1654 | { |
| 1655 | fndecl = builtin_decl_explicit (BUILT_IN_BSWAP64); |
| 1656 | bswap_type = bswap64_type; |
| 1657 | } |
| 1658 | break; |
| 1659 | default: |
| 1660 | continue; |
| 1661 | } |
| 1662 | |
| 1663 | if (bswap && !fndecl && n.range != 16) |
| 1664 | continue; |
| 1665 | |
| 1666 | if (bswap_replace (gsi_for_stmt (cur_stmt), ins_stmt, fndecl, |
| 1667 | bswap_type, load_type, &n, bswap, mask, |
| 1668 | l_rotate)) |
| 1669 | changed = true; |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | statistics_counter_event (fun, "16-bit nop implementations found", |
| 1674 | nop_stats.found_16bit); |
| 1675 | statistics_counter_event (fun, "32-bit nop implementations found", |
| 1676 | nop_stats.found_32bit); |
| 1677 | statistics_counter_event (fun, "64-bit nop implementations found", |
| 1678 | nop_stats.found_64bit); |
| 1679 | statistics_counter_event (fun, "16-bit bswap implementations found", |
| 1680 | bswap_stats.found_16bit); |
| 1681 | statistics_counter_event (fun, "32-bit bswap implementations found", |
| 1682 | bswap_stats.found_32bit); |
| 1683 | statistics_counter_event (fun, "64-bit bswap implementations found", |
| 1684 | bswap_stats.found_64bit); |
| 1685 | |
| 1686 | return (changed ? TODO_update_ssa : 0); |
| 1687 | } |
| 1688 | |
| 1689 | } // anon namespace |
| 1690 | |
| 1691 | gimple_opt_pass * |
| 1692 | make_pass_optimize_bswap (gcc::context *ctxt) |
| 1693 | { |
| 1694 | return new pass_optimize_bswap (ctxt); |
| 1695 | } |
| 1696 | |
| 1697 | namespace { |
| 1698 | |
| 1699 | /* Struct recording one operand for the store, which is either a constant, |
| 1700 | then VAL represents the constant and all the other fields are zero, or |
| 1701 | a memory load, then VAL represents the reference, BASE_ADDR is non-NULL |
| 1702 | and the other fields also reflect the memory load, or an SSA name, then |
| 1703 | VAL represents the SSA name and all the other fields are zero. */ |
| 1704 | |
| 1705 | class store_operand_info |
| 1706 | { |
| 1707 | public: |
| 1708 | tree val; |
| 1709 | tree base_addr; |
| 1710 | poly_uint64 bitsize; |
| 1711 | poly_uint64 bitpos; |
| 1712 | poly_uint64 bitregion_start; |
| 1713 | poly_uint64 bitregion_end; |
| 1714 | gimple *stmt; |
| 1715 | bool bit_not_p; |
| 1716 | store_operand_info (); |
| 1717 | }; |
| 1718 | |
| 1719 | store_operand_info::store_operand_info () |
| 1720 | : val (NULL_TREE), base_addr (NULL_TREE), bitsize (0), bitpos (0), |
| 1721 | bitregion_start (0), bitregion_end (0), stmt (NULL), bit_not_p (false) |
| 1722 | { |
| 1723 | } |
| 1724 | |
| 1725 | /* Struct recording the information about a single store of an immediate |
| 1726 | to memory. These are created in the first phase and coalesced into |
| 1727 | merged_store_group objects in the second phase. */ |
| 1728 | |
| 1729 | class store_immediate_info |
| 1730 | { |
| 1731 | public: |
| 1732 | unsigned HOST_WIDE_INT bitsize; |
| 1733 | unsigned HOST_WIDE_INT bitpos; |
| 1734 | unsigned HOST_WIDE_INT bitregion_start; |
| 1735 | /* This is one past the last bit of the bit region. */ |
| 1736 | unsigned HOST_WIDE_INT bitregion_end; |
| 1737 | gimple *stmt; |
| 1738 | unsigned int order; |
| 1739 | /* INTEGER_CST for constant store, STRING_CST for string store, |
| 1740 | MEM_REF for memory copy, BIT_*_EXPR for logical bitwise operation, |
| 1741 | BIT_INSERT_EXPR for bit insertion. |
| 1742 | LROTATE_EXPR if it can be only bswap optimized and |
| 1743 | ops are not really meaningful. |
| 1744 | NOP_EXPR if bswap optimization detected identity, ops |
| 1745 | are not meaningful. */ |
| 1746 | enum tree_code rhs_code; |
| 1747 | /* Two fields for bswap optimization purposes. */ |
| 1748 | struct symbolic_number n; |
| 1749 | gimple *ins_stmt; |
| 1750 | /* True if BIT_{AND,IOR,XOR}_EXPR result is inverted before storing. */ |
| 1751 | bool bit_not_p; |
| 1752 | /* True if ops have been swapped and thus ops[1] represents |
| 1753 | rhs1 of BIT_{AND,IOR,XOR}_EXPR and ops[0] represents rhs2. */ |
| 1754 | bool ops_swapped_p; |
| 1755 | /* The index number of the landing pad, or 0 if there is none. */ |
| 1756 | int lp_nr; |
| 1757 | /* Operands. For BIT_*_EXPR rhs_code both operands are used, otherwise |
| 1758 | just the first one. */ |
| 1759 | store_operand_info ops[2]; |
| 1760 | store_immediate_info (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT, |
| 1761 | unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT, |
| 1762 | gimple *, unsigned int, enum tree_code, |
| 1763 | struct symbolic_number &, gimple *, bool, int, |
| 1764 | const store_operand_info &, |
| 1765 | const store_operand_info &); |
| 1766 | }; |
| 1767 | |
| 1768 | store_immediate_info::store_immediate_info (unsigned HOST_WIDE_INT bs, |
| 1769 | unsigned HOST_WIDE_INT bp, |
| 1770 | unsigned HOST_WIDE_INT brs, |
| 1771 | unsigned HOST_WIDE_INT bre, |
| 1772 | gimple *st, |
| 1773 | unsigned int ord, |
| 1774 | enum tree_code rhscode, |
| 1775 | struct symbolic_number &nr, |
| 1776 | gimple *ins_stmtp, |
| 1777 | bool bitnotp, |
| 1778 | int nr2, |
| 1779 | const store_operand_info &op0r, |
| 1780 | const store_operand_info &op1r) |
| 1781 | : bitsize (bs), bitpos (bp), bitregion_start (brs), bitregion_end (bre), |
| 1782 | stmt (st), order (ord), rhs_code (rhscode), n (nr), |
| 1783 | ins_stmt (ins_stmtp), bit_not_p (bitnotp), ops_swapped_p (false), |
| 1784 | lp_nr (nr2), ops { op0r, op1r } |
| 1785 | { |
| 1786 | } |
| 1787 | |
| 1788 | /* Struct representing a group of stores to contiguous memory locations. |
| 1789 | These are produced by the second phase (coalescing) and consumed in the |
| 1790 | third phase that outputs the widened stores. */ |
| 1791 | |
| 1792 | class merged_store_group |
| 1793 | { |
| 1794 | public: |
| 1795 | unsigned HOST_WIDE_INT start; |
| 1796 | unsigned HOST_WIDE_INT width; |
| 1797 | unsigned HOST_WIDE_INT bitregion_start; |
| 1798 | unsigned HOST_WIDE_INT bitregion_end; |
| 1799 | /* The size of the allocated memory for val and mask. */ |
| 1800 | unsigned HOST_WIDE_INT buf_size; |
| 1801 | unsigned HOST_WIDE_INT align_base; |
| 1802 | poly_uint64 load_align_base[2]; |
| 1803 | |
| 1804 | unsigned int align; |
| 1805 | unsigned int load_align[2]; |
| 1806 | unsigned int first_order; |
| 1807 | unsigned int last_order; |
| 1808 | bool bit_insertion; |
| 1809 | bool string_concatenation; |
| 1810 | bool only_constants; |
| 1811 | bool consecutive; |
| 1812 | unsigned int first_nonmergeable_order; |
| 1813 | int lp_nr; |
| 1814 | |
| 1815 | auto_vec<store_immediate_info *> stores; |
| 1816 | /* We record the first and last original statements in the sequence because |
| 1817 | we'll need their vuse/vdef and replacement position. It's easier to keep |
| 1818 | track of them separately as 'stores' is reordered by apply_stores. */ |
| 1819 | gimple *last_stmt; |
| 1820 | gimple *first_stmt; |
| 1821 | unsigned char *val; |
| 1822 | unsigned char *mask; |
| 1823 | |
| 1824 | merged_store_group (store_immediate_info *); |
| 1825 | ~merged_store_group (); |
| 1826 | bool can_be_merged_into (store_immediate_info *); |
| 1827 | void merge_into (store_immediate_info *); |
| 1828 | void merge_overlapping (store_immediate_info *); |
| 1829 | bool apply_stores (); |
| 1830 | private: |
| 1831 | void do_merge (store_immediate_info *); |
| 1832 | }; |
| 1833 | |
| 1834 | /* Debug helper. Dump LEN elements of byte array PTR to FD in hex. */ |
| 1835 | |
| 1836 | static void |
| 1837 | dump_char_array (FILE *fd, unsigned char *ptr, unsigned int len) |
| 1838 | { |
| 1839 | if (!fd) |
| 1840 | return; |
| 1841 | |
| 1842 | for (unsigned int i = 0; i < len; i++) |
| 1843 | fprintf (fd, "%02x ", ptr[i]); |
| 1844 | fprintf (fd, "\n"); |
| 1845 | } |
| 1846 | |
| 1847 | /* Clear out LEN bits starting from bit START in the byte array |
| 1848 | PTR. This clears the bits to the *right* from START. |
| 1849 | START must be within [0, BITS_PER_UNIT) and counts starting from |
| 1850 | the least significant bit. */ |
| 1851 | |
| 1852 | static void |
| 1853 | clear_bit_region_be (unsigned char *ptr, unsigned int start, |
| 1854 | unsigned int len) |
| 1855 | { |
| 1856 | if (len == 0) |
| 1857 | return; |
| 1858 | /* Clear len bits to the right of start. */ |
| 1859 | else if (len <= start + 1) |
| 1860 | { |
| 1861 | unsigned char mask = (~(~0U << len)); |
| 1862 | mask = mask << (start + 1U - len); |
| 1863 | ptr[0] &= ~mask; |
| 1864 | } |
| 1865 | else if (start != BITS_PER_UNIT - 1) |
| 1866 | { |
| 1867 | clear_bit_region_be (ptr, start, (start % BITS_PER_UNIT) + 1); |
| 1868 | clear_bit_region_be (ptr + 1, BITS_PER_UNIT - 1, |
| 1869 | len - (start % BITS_PER_UNIT) - 1); |
| 1870 | } |
| 1871 | else if (start == BITS_PER_UNIT - 1 |
| 1872 | && len > BITS_PER_UNIT) |
| 1873 | { |
| 1874 | unsigned int nbytes = len / BITS_PER_UNIT; |
| 1875 | memset (ptr, 0, nbytes); |
| 1876 | if (len % BITS_PER_UNIT != 0) |
| 1877 | clear_bit_region_be (ptr + nbytes, BITS_PER_UNIT - 1, |
| 1878 | len % BITS_PER_UNIT); |
| 1879 | } |
| 1880 | else |
| 1881 | gcc_unreachable (); |
| 1882 | } |
| 1883 | |
| 1884 | /* In the byte array PTR clear the bit region starting at bit |
| 1885 | START and is LEN bits wide. |
| 1886 | For regions spanning multiple bytes do this recursively until we reach |
| 1887 | zero LEN or a region contained within a single byte. */ |
| 1888 | |
| 1889 | static void |
| 1890 | clear_bit_region (unsigned char *ptr, unsigned int start, |
| 1891 | unsigned int len) |
| 1892 | { |
| 1893 | /* Degenerate base case. */ |
| 1894 | if (len == 0) |
| 1895 | return; |
| 1896 | else if (start >= BITS_PER_UNIT) |
| 1897 | clear_bit_region (ptr + 1, start - BITS_PER_UNIT, len); |
| 1898 | /* Second base case. */ |
| 1899 | else if ((start + len) <= BITS_PER_UNIT) |
| 1900 | { |
| 1901 | unsigned char mask = (~0U) << (unsigned char) (BITS_PER_UNIT - len); |
| 1902 | mask >>= BITS_PER_UNIT - (start + len); |
| 1903 | |
| 1904 | ptr[0] &= ~mask; |
| 1905 | |
| 1906 | return; |
| 1907 | } |
| 1908 | /* Clear most significant bits in a byte and proceed with the next byte. */ |
| 1909 | else if (start != 0) |
| 1910 | { |
| 1911 | clear_bit_region (ptr, start, BITS_PER_UNIT - start); |
| 1912 | clear_bit_region (ptr + 1, 0, len - (BITS_PER_UNIT - start)); |
| 1913 | } |
| 1914 | /* Whole bytes need to be cleared. */ |
| 1915 | else if (start == 0 && len > BITS_PER_UNIT) |
| 1916 | { |
| 1917 | unsigned int nbytes = len / BITS_PER_UNIT; |
| 1918 | /* We could recurse on each byte but we clear whole bytes, so a simple |
| 1919 | memset will do. */ |
| 1920 | memset (ptr, '\0', nbytes); |
| 1921 | /* Clear the remaining sub-byte region if there is one. */ |
| 1922 | if (len % BITS_PER_UNIT != 0) |
| 1923 | clear_bit_region (ptr + nbytes, 0, len % BITS_PER_UNIT); |
| 1924 | } |
| 1925 | else |
| 1926 | gcc_unreachable (); |
| 1927 | } |
| 1928 | |
| 1929 | /* Write BITLEN bits of EXPR to the byte array PTR at |
| 1930 | bit position BITPOS. PTR should contain TOTAL_BYTES elements. |
| 1931 | Return true if the operation succeeded. */ |
| 1932 | |
| 1933 | static bool |
| 1934 | encode_tree_to_bitpos (tree expr, unsigned char *ptr, int bitlen, int bitpos, |
| 1935 | unsigned int total_bytes) |
| 1936 | { |
| 1937 | unsigned int first_byte = bitpos / BITS_PER_UNIT; |
| 1938 | bool empty_ctor_p |
| 1939 | = (TREE_CODE (expr) == CONSTRUCTOR |
| 1940 | && CONSTRUCTOR_NELTS (expr) == 0 |
| 1941 | && TYPE_SIZE_UNIT (TREE_TYPE (expr)) |
| 1942 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr)))); |
| 1943 | bool sub_byte_op_p = ((bitlen % BITS_PER_UNIT) |
| 1944 | || (bitpos % BITS_PER_UNIT) |
| 1945 | || (!int_mode_for_size (bitlen, 0).exists () |
| 1946 | && !empty_ctor_p)); |
| 1947 | |
| 1948 | if (!sub_byte_op_p) |
| 1949 | { |
| 1950 | if (first_byte >= total_bytes) |
| 1951 | return false; |
| 1952 | total_bytes -= first_byte; |
| 1953 | if (empty_ctor_p) |
| 1954 | { |
| 1955 | unsigned HOST_WIDE_INT rhs_bytes |
| 1956 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (expr))); |
| 1957 | if (rhs_bytes > total_bytes) |
| 1958 | return false; |
| 1959 | memset (ptr + first_byte, '\0', rhs_bytes); |
| 1960 | return true; |
| 1961 | } |
| 1962 | return native_encode_expr (expr, ptr + first_byte, total_bytes) != 0; |
| 1963 | } |
| 1964 | |
| 1965 | /* LITTLE-ENDIAN |
| 1966 | We are writing a non byte-sized quantity or at a position that is not |
| 1967 | at a byte boundary. |
| 1968 | |--------|--------|--------| ptr + first_byte |
| 1969 | ^ ^ |
| 1970 | xxx xxxxxxxx xxx< bp> |
| 1971 | |______EXPR____| |
| 1972 | |
| 1973 | First native_encode_expr EXPR into a temporary buffer and shift each |
| 1974 | byte in the buffer by 'bp' (carrying the bits over as necessary). |
| 1975 | |00000000|00xxxxxx|xxxxxxxx| << bp = |000xxxxx|xxxxxxxx|xxx00000| |
| 1976 | <------bitlen---->< bp> |
| 1977 | Then we clear the destination bits: |
| 1978 | |---00000|00000000|000-----| ptr + first_byte |
| 1979 | <-------bitlen--->< bp> |
| 1980 | |
| 1981 | Finally we ORR the bytes of the shifted EXPR into the cleared region: |
| 1982 | |---xxxxx||xxxxxxxx||xxx-----| ptr + first_byte. |
| 1983 | |
| 1984 | BIG-ENDIAN |
| 1985 | We are writing a non byte-sized quantity or at a position that is not |
| 1986 | at a byte boundary. |
| 1987 | ptr + first_byte |--------|--------|--------| |
| 1988 | ^ ^ |
| 1989 | <bp >xxx xxxxxxxx xxx |
| 1990 | |_____EXPR_____| |
| 1991 | |
| 1992 | First native_encode_expr EXPR into a temporary buffer and shift each |
| 1993 | byte in the buffer to the right by (carrying the bits over as necessary). |
| 1994 | We shift by as much as needed to align the most significant bit of EXPR |
| 1995 | with bitpos: |
| 1996 | |00xxxxxx|xxxxxxxx| >> 3 = |00000xxx|xxxxxxxx|xxxxx000| |
| 1997 | <---bitlen----> <bp ><-----bitlen-----> |
| 1998 | Then we clear the destination bits: |
| 1999 | ptr + first_byte |-----000||00000000||00000---| |
| 2000 | <bp ><-------bitlen-----> |
| 2001 | |
| 2002 | Finally we ORR the bytes of the shifted EXPR into the cleared region: |
| 2003 | ptr + first_byte |---xxxxx||xxxxxxxx||xxx-----|. |
| 2004 | The awkwardness comes from the fact that bitpos is counted from the |
| 2005 | most significant bit of a byte. */ |
| 2006 | |
| 2007 | /* We must be dealing with fixed-size data at this point, since the |
| 2008 | total size is also fixed. */ |
| 2009 | unsigned int byte_size; |
| 2010 | if (empty_ctor_p) |
| 2011 | { |
| 2012 | unsigned HOST_WIDE_INT rhs_bytes |
| 2013 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (expr))); |
| 2014 | if (rhs_bytes > total_bytes) |
| 2015 | return false; |
| 2016 | byte_size = rhs_bytes; |
| 2017 | } |
| 2018 | else |
| 2019 | { |
| 2020 | fixed_size_mode mode |
| 2021 | = as_a <fixed_size_mode> (TYPE_MODE (TREE_TYPE (expr))); |
| 2022 | byte_size |
| 2023 | = mode == BLKmode |
| 2024 | ? tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (expr))) |
| 2025 | : GET_MODE_SIZE (mode); |
| 2026 | } |
| 2027 | /* Allocate an extra byte so that we have space to shift into. */ |
| 2028 | byte_size++; |
| 2029 | unsigned char *tmpbuf = XALLOCAVEC (unsigned char, byte_size); |
| 2030 | memset (tmpbuf, '\0', byte_size); |
| 2031 | /* The store detection code should only have allowed constants that are |
| 2032 | accepted by native_encode_expr or empty ctors. */ |
| 2033 | if (!empty_ctor_p |
| 2034 | && native_encode_expr (expr, tmpbuf, byte_size - 1) == 0) |
| 2035 | gcc_unreachable (); |
| 2036 | |
| 2037 | /* The native_encode_expr machinery uses TYPE_MODE to determine how many |
| 2038 | bytes to write. This means it can write more than |
| 2039 | ROUND_UP (bitlen, BITS_PER_UNIT) / BITS_PER_UNIT bytes (for example |
| 2040 | write 8 bytes for a bitlen of 40). Skip the bytes that are not within |
| 2041 | bitlen and zero out the bits that are not relevant as well (that may |
| 2042 | contain a sign bit due to sign-extension). */ |
| 2043 | unsigned int padding |
| 2044 | = byte_size - ROUND_UP (bitlen, BITS_PER_UNIT) / BITS_PER_UNIT - 1; |
| 2045 | /* On big-endian the padding is at the 'front' so just skip the initial |
| 2046 | bytes. */ |
| 2047 | if (BYTES_BIG_ENDIAN) |
| 2048 | tmpbuf += padding; |
| 2049 | |
| 2050 | byte_size -= padding; |
| 2051 | |
| 2052 | if (bitlen % BITS_PER_UNIT != 0) |
| 2053 | { |
| 2054 | if (BYTES_BIG_ENDIAN) |
| 2055 | clear_bit_region_be (tmpbuf, BITS_PER_UNIT - 1, |
| 2056 | BITS_PER_UNIT - (bitlen % BITS_PER_UNIT)); |
| 2057 | else |
| 2058 | clear_bit_region (tmpbuf, bitlen, |
| 2059 | byte_size * BITS_PER_UNIT - bitlen); |
| 2060 | } |
| 2061 | /* Left shifting relies on the last byte being clear if bitlen is |
| 2062 | a multiple of BITS_PER_UNIT, which might not be clear if |
| 2063 | there are padding bytes. */ |
| 2064 | else if (!BYTES_BIG_ENDIAN) |
| 2065 | tmpbuf[byte_size - 1] = '\0'; |
| 2066 | |
| 2067 | /* Clear the bit region in PTR where the bits from TMPBUF will be |
| 2068 | inserted into. */ |
| 2069 | if (BYTES_BIG_ENDIAN) |
| 2070 | clear_bit_region_be (ptr + first_byte, |
| 2071 | BITS_PER_UNIT - 1 - (bitpos % BITS_PER_UNIT), bitlen); |
| 2072 | else |
| 2073 | clear_bit_region (ptr + first_byte, bitpos % BITS_PER_UNIT, bitlen); |
| 2074 | |
| 2075 | int shift_amnt; |
| 2076 | int bitlen_mod = bitlen % BITS_PER_UNIT; |
| 2077 | int bitpos_mod = bitpos % BITS_PER_UNIT; |
| 2078 | |
| 2079 | bool skip_byte = false; |
| 2080 | if (BYTES_BIG_ENDIAN) |
| 2081 | { |
| 2082 | /* BITPOS and BITLEN are exactly aligned and no shifting |
| 2083 | is necessary. */ |
| 2084 | if (bitpos_mod + bitlen_mod == BITS_PER_UNIT |
| 2085 | || (bitpos_mod == 0 && bitlen_mod == 0)) |
| 2086 | shift_amnt = 0; |
| 2087 | /* |. . . . . . . .| |
| 2088 | <bp > <blen >. |
| 2089 | We always shift right for BYTES_BIG_ENDIAN so shift the beginning |
| 2090 | of the value until it aligns with 'bp' in the next byte over. */ |
| 2091 | else if (bitpos_mod + bitlen_mod < BITS_PER_UNIT) |
| 2092 | { |
| 2093 | shift_amnt = bitlen_mod + bitpos_mod; |
| 2094 | skip_byte = bitlen_mod != 0; |
| 2095 | } |
| 2096 | /* |. . . . . . . .| |
| 2097 | <----bp---> |
| 2098 | <---blen---->. |
| 2099 | Shift the value right within the same byte so it aligns with 'bp'. */ |
| 2100 | else |
| 2101 | shift_amnt = bitlen_mod + bitpos_mod - BITS_PER_UNIT; |
| 2102 | } |
| 2103 | else |
| 2104 | shift_amnt = bitpos % BITS_PER_UNIT; |
| 2105 | |
| 2106 | /* Create the shifted version of EXPR. */ |
| 2107 | if (!BYTES_BIG_ENDIAN) |
| 2108 | { |
| 2109 | shift_bytes_in_array_left (tmpbuf, byte_size, shift_amnt); |
| 2110 | if (shift_amnt == 0) |
| 2111 | byte_size--; |
| 2112 | } |
| 2113 | else |
| 2114 | { |
| 2115 | gcc_assert (BYTES_BIG_ENDIAN); |
| 2116 | shift_bytes_in_array_right (tmpbuf, byte_size, shift_amnt); |
| 2117 | /* If shifting right forced us to move into the next byte skip the now |
| 2118 | empty byte. */ |
| 2119 | if (skip_byte) |
| 2120 | { |
| 2121 | tmpbuf++; |
| 2122 | byte_size--; |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | /* Insert the bits from TMPBUF. */ |
| 2127 | for (unsigned int i = 0; i < byte_size; i++) |
| 2128 | ptr[first_byte + i] |= tmpbuf[i]; |
| 2129 | |
| 2130 | return true; |
| 2131 | } |
| 2132 | |
| 2133 | /* Sorting function for store_immediate_info objects. |
| 2134 | Sorts them by bitposition. */ |
| 2135 | |
| 2136 | static int |
| 2137 | sort_by_bitpos (const void *x, const void *y) |
| 2138 | { |
| 2139 | store_immediate_info *const *tmp = (store_immediate_info * const *) x; |
| 2140 | store_immediate_info *const *tmp2 = (store_immediate_info * const *) y; |
| 2141 | |
| 2142 | if ((*tmp)->bitpos < (*tmp2)->bitpos) |
| 2143 | return -1; |
| 2144 | else if ((*tmp)->bitpos > (*tmp2)->bitpos) |
| 2145 | return 1; |
| 2146 | else |
| 2147 | /* If they are the same let's use the order which is guaranteed to |
| 2148 | be different. */ |
| 2149 | return (*tmp)->order - (*tmp2)->order; |
| 2150 | } |
| 2151 | |
| 2152 | /* Sorting function for store_immediate_info objects. |
| 2153 | Sorts them by the order field. */ |
| 2154 | |
| 2155 | static int |
| 2156 | sort_by_order (const void *x, const void *y) |
| 2157 | { |
| 2158 | store_immediate_info *const *tmp = (store_immediate_info * const *) x; |
| 2159 | store_immediate_info *const *tmp2 = (store_immediate_info * const *) y; |
| 2160 | |
| 2161 | if ((*tmp)->order < (*tmp2)->order) |
| 2162 | return -1; |
| 2163 | else if ((*tmp)->order > (*tmp2)->order) |
| 2164 | return 1; |
| 2165 | |
| 2166 | gcc_unreachable (); |
| 2167 | } |
| 2168 | |
| 2169 | /* Initialize a merged_store_group object from a store_immediate_info |
| 2170 | object. */ |
| 2171 | |
| 2172 | merged_store_group::merged_store_group (store_immediate_info *info) |
| 2173 | { |
| 2174 | start = info->bitpos; |
| 2175 | width = info->bitsize; |
| 2176 | bitregion_start = info->bitregion_start; |
| 2177 | bitregion_end = info->bitregion_end; |
| 2178 | /* VAL has memory allocated for it in apply_stores once the group |
| 2179 | width has been finalized. */ |
| 2180 | val = NULL; |
| 2181 | mask = NULL; |
| 2182 | bit_insertion = info->rhs_code == BIT_INSERT_EXPR; |
| 2183 | string_concatenation = info->rhs_code == STRING_CST; |
| 2184 | only_constants = info->rhs_code == INTEGER_CST; |
| 2185 | consecutive = true; |
| 2186 | first_nonmergeable_order = ~0U; |
| 2187 | lp_nr = info->lp_nr; |
| 2188 | unsigned HOST_WIDE_INT align_bitpos = 0; |
| 2189 | get_object_alignment_1 (gimple_assign_lhs (info->stmt), |
| 2190 | &align, &align_bitpos); |
| 2191 | align_base = start - align_bitpos; |
| 2192 | for (int i = 0; i < 2; ++i) |
| 2193 | { |
| 2194 | store_operand_info &op = info->ops[i]; |
| 2195 | if (op.base_addr == NULL_TREE) |
| 2196 | { |
| 2197 | load_align[i] = 0; |
| 2198 | load_align_base[i] = 0; |
| 2199 | } |
| 2200 | else |
| 2201 | { |
| 2202 | get_object_alignment_1 (op.val, &load_align[i], &align_bitpos); |
| 2203 | load_align_base[i] = op.bitpos - align_bitpos; |
| 2204 | } |
| 2205 | } |
| 2206 | stores.create (1); |
| 2207 | stores.safe_push (info); |
| 2208 | last_stmt = info->stmt; |
| 2209 | last_order = info->order; |
| 2210 | first_stmt = last_stmt; |
| 2211 | first_order = last_order; |
| 2212 | buf_size = 0; |
| 2213 | } |
| 2214 | |
| 2215 | merged_store_group::~merged_store_group () |
| 2216 | { |
| 2217 | if (val) |
| 2218 | XDELETEVEC (val); |
| 2219 | } |
| 2220 | |
| 2221 | /* Return true if the store described by INFO can be merged into the group. */ |
| 2222 | |
| 2223 | bool |
| 2224 | merged_store_group::can_be_merged_into (store_immediate_info *info) |
| 2225 | { |
| 2226 | /* Do not merge bswap patterns. */ |
| 2227 | if (info->rhs_code == LROTATE_EXPR) |
| 2228 | return false; |
| 2229 | |
| 2230 | if (info->lp_nr != lp_nr) |
| 2231 | return false; |
| 2232 | |
| 2233 | /* The canonical case. */ |
| 2234 | if (info->rhs_code == stores[0]->rhs_code) |
| 2235 | return true; |
| 2236 | |
| 2237 | /* BIT_INSERT_EXPR is compatible with INTEGER_CST if no STRING_CST. */ |
| 2238 | if (info->rhs_code == BIT_INSERT_EXPR && stores[0]->rhs_code == INTEGER_CST) |
| 2239 | return !string_concatenation; |
| 2240 | |
| 2241 | if (stores[0]->rhs_code == BIT_INSERT_EXPR && info->rhs_code == INTEGER_CST) |
| 2242 | return !string_concatenation; |
| 2243 | |
| 2244 | /* We can turn MEM_REF into BIT_INSERT_EXPR for bit-field stores, but do it |
| 2245 | only for small regions since this can generate a lot of instructions. */ |
| 2246 | if (info->rhs_code == MEM_REF |
| 2247 | && (stores[0]->rhs_code == INTEGER_CST |
| 2248 | || stores[0]->rhs_code == BIT_INSERT_EXPR) |
| 2249 | && info->bitregion_start == stores[0]->bitregion_start |
| 2250 | && info->bitregion_end == stores[0]->bitregion_end |
| 2251 | && info->bitregion_end - info->bitregion_start <= MAX_FIXED_MODE_SIZE) |
| 2252 | return !string_concatenation; |
| 2253 | |
| 2254 | if (stores[0]->rhs_code == MEM_REF |
| 2255 | && (info->rhs_code == INTEGER_CST |
| 2256 | || info->rhs_code == BIT_INSERT_EXPR) |
| 2257 | && info->bitregion_start == stores[0]->bitregion_start |
| 2258 | && info->bitregion_end == stores[0]->bitregion_end |
| 2259 | && info->bitregion_end - info->bitregion_start <= MAX_FIXED_MODE_SIZE) |
| 2260 | return !string_concatenation; |
| 2261 | |
| 2262 | /* STRING_CST is compatible with INTEGER_CST if no BIT_INSERT_EXPR. */ |
| 2263 | if (info->rhs_code == STRING_CST |
| 2264 | && stores[0]->rhs_code == INTEGER_CST |
| 2265 | && stores[0]->bitsize == CHAR_BIT) |
| 2266 | return !bit_insertion; |
| 2267 | |
| 2268 | if (stores[0]->rhs_code == STRING_CST |
| 2269 | && info->rhs_code == INTEGER_CST |
| 2270 | && info->bitsize == CHAR_BIT) |
| 2271 | return !bit_insertion; |
| 2272 | |
| 2273 | return false; |
| 2274 | } |
| 2275 | |
| 2276 | /* Helper method for merge_into and merge_overlapping to do |
| 2277 | the common part. */ |
| 2278 | |
| 2279 | void |
| 2280 | merged_store_group::do_merge (store_immediate_info *info) |
| 2281 | { |
| 2282 | bitregion_start = MIN (bitregion_start, info->bitregion_start); |
| 2283 | bitregion_end = MAX (bitregion_end, info->bitregion_end); |
| 2284 | |
| 2285 | unsigned int this_align; |
| 2286 | unsigned HOST_WIDE_INT align_bitpos = 0; |
| 2287 | get_object_alignment_1 (gimple_assign_lhs (info->stmt), |
| 2288 | &this_align, &align_bitpos); |
| 2289 | if (this_align > align) |
| 2290 | { |
| 2291 | align = this_align; |
| 2292 | align_base = info->bitpos - align_bitpos; |
| 2293 | } |
| 2294 | for (int i = 0; i < 2; ++i) |
| 2295 | { |
| 2296 | store_operand_info &op = info->ops[i]; |
| 2297 | if (!op.base_addr) |
| 2298 | continue; |
| 2299 | |
| 2300 | get_object_alignment_1 (op.val, &this_align, &align_bitpos); |
| 2301 | if (this_align > load_align[i]) |
| 2302 | { |
| 2303 | load_align[i] = this_align; |
| 2304 | load_align_base[i] = op.bitpos - align_bitpos; |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | gimple *stmt = info->stmt; |
| 2309 | stores.safe_push (info); |
| 2310 | if (info->order > last_order) |
| 2311 | { |
| 2312 | last_order = info->order; |
| 2313 | last_stmt = stmt; |
| 2314 | } |
| 2315 | else if (info->order < first_order) |
| 2316 | { |
| 2317 | first_order = info->order; |
| 2318 | first_stmt = stmt; |
| 2319 | } |
| 2320 | |
| 2321 | if (info->bitpos != start + width) |
| 2322 | consecutive = false; |
| 2323 | |
| 2324 | /* We need to use extraction if there is any bit-field. */ |
| 2325 | if (info->rhs_code == BIT_INSERT_EXPR) |
| 2326 | { |
| 2327 | bit_insertion = true; |
| 2328 | gcc_assert (!string_concatenation); |
| 2329 | } |
| 2330 | |
| 2331 | /* We want to use concatenation if there is any string. */ |
| 2332 | if (info->rhs_code == STRING_CST) |
| 2333 | { |
| 2334 | string_concatenation = true; |
| 2335 | gcc_assert (!bit_insertion); |
| 2336 | } |
| 2337 | |
| 2338 | /* But we cannot use it if we don't have consecutive stores. */ |
| 2339 | if (!consecutive) |
| 2340 | string_concatenation = false; |
| 2341 | |
| 2342 | if (info->rhs_code != INTEGER_CST) |
| 2343 | only_constants = false; |
| 2344 | } |
| 2345 | |
| 2346 | /* Merge a store recorded by INFO into this merged store. |
| 2347 | The store is not overlapping with the existing recorded |
| 2348 | stores. */ |
| 2349 | |
| 2350 | void |
| 2351 | merged_store_group::merge_into (store_immediate_info *info) |
| 2352 | { |
| 2353 | do_merge (info); |
| 2354 | |
| 2355 | /* Make sure we're inserting in the position we think we're inserting. */ |
| 2356 | gcc_assert (info->bitpos >= start + width |
| 2357 | && info->bitregion_start <= bitregion_end); |
| 2358 | |
| 2359 | width = info->bitpos + info->bitsize - start; |
| 2360 | } |
| 2361 | |
| 2362 | /* Merge a store described by INFO into this merged store. |
| 2363 | INFO overlaps in some way with the current store (i.e. it's not contiguous |
| 2364 | which is handled by merged_store_group::merge_into). */ |
| 2365 | |
| 2366 | void |
| 2367 | merged_store_group::merge_overlapping (store_immediate_info *info) |
| 2368 | { |
| 2369 | do_merge (info); |
| 2370 | |
| 2371 | /* If the store extends the size of the group, extend the width. */ |
| 2372 | if (info->bitpos + info->bitsize > start + width) |
| 2373 | width = info->bitpos + info->bitsize - start; |
| 2374 | } |
| 2375 | |
| 2376 | /* Go through all the recorded stores in this group in program order and |
| 2377 | apply their values to the VAL byte array to create the final merged |
| 2378 | value. Return true if the operation succeeded. */ |
| 2379 | |
| 2380 | bool |
| 2381 | merged_store_group::apply_stores () |
| 2382 | { |
| 2383 | store_immediate_info *info; |
| 2384 | unsigned int i; |
| 2385 | |
| 2386 | /* Make sure we have more than one store in the group, otherwise we cannot |
| 2387 | merge anything. */ |
| 2388 | if (bitregion_start % BITS_PER_UNIT != 0 |
| 2389 | || bitregion_end % BITS_PER_UNIT != 0 |
| 2390 | || stores.length () == 1) |
| 2391 | return false; |
| 2392 | |
| 2393 | buf_size = (bitregion_end - bitregion_start) / BITS_PER_UNIT; |
| 2394 | |
| 2395 | /* Really do string concatenation for large strings only. */ |
| 2396 | if (buf_size <= MOVE_MAX) |
| 2397 | string_concatenation = false; |
| 2398 | |
| 2399 | /* String concatenation only works for byte aligned start and end. */ |
| 2400 | if (start % BITS_PER_UNIT != 0 || width % BITS_PER_UNIT != 0) |
| 2401 | string_concatenation = false; |
| 2402 | |
| 2403 | /* Create a power-of-2-sized buffer for native_encode_expr. */ |
| 2404 | if (!string_concatenation) |
| 2405 | buf_size = 1 << ceil_log2 (buf_size); |
| 2406 | |
| 2407 | val = XNEWVEC (unsigned char, 2 * buf_size); |
| 2408 | mask = val + buf_size; |
| 2409 | memset (val, 0, buf_size); |
| 2410 | memset (mask, ~0U, buf_size); |
| 2411 | |
| 2412 | stores.qsort (sort_by_order); |
| 2413 | |
| 2414 | FOR_EACH_VEC_ELT (stores, i, info) |
| 2415 | { |
| 2416 | unsigned int pos_in_buffer = info->bitpos - bitregion_start; |
| 2417 | tree cst; |
| 2418 | if (info->ops[0].val && info->ops[0].base_addr == NULL_TREE) |
| 2419 | cst = info->ops[0].val; |
| 2420 | else if (info->ops[1].val && info->ops[1].base_addr == NULL_TREE) |
| 2421 | cst = info->ops[1].val; |
| 2422 | else |
| 2423 | cst = NULL_TREE; |
| 2424 | bool ret = true; |
| 2425 | if (cst && info->rhs_code != BIT_INSERT_EXPR) |
| 2426 | ret = encode_tree_to_bitpos (cst, val, info->bitsize, pos_in_buffer, |
| 2427 | buf_size); |
| 2428 | unsigned char *m = mask + (pos_in_buffer / BITS_PER_UNIT); |
| 2429 | if (BYTES_BIG_ENDIAN) |
| 2430 | clear_bit_region_be (m, (BITS_PER_UNIT - 1 |
| 2431 | - (pos_in_buffer % BITS_PER_UNIT)), |
| 2432 | info->bitsize); |
| 2433 | else |
| 2434 | clear_bit_region (m, pos_in_buffer % BITS_PER_UNIT, info->bitsize); |
| 2435 | if (cst && dump_file && (dump_flags & TDF_DETAILS)) |
| 2436 | { |
| 2437 | if (ret) |
| 2438 | { |
| 2439 | fputs ("After writing ", dump_file); |
| 2440 | print_generic_expr (dump_file, cst, TDF_NONE); |
| 2441 | fprintf (dump_file, " of size " HOST_WIDE_INT_PRINT_DEC |
| 2442 | " at position %d\n", info->bitsize, pos_in_buffer); |
| 2443 | fputs (" the merged value contains ", dump_file); |
| 2444 | dump_char_array (dump_file, val, buf_size); |
| 2445 | fputs (" the merged mask contains ", dump_file); |
| 2446 | dump_char_array (dump_file, mask, buf_size); |
| 2447 | if (bit_insertion) |
| 2448 | fputs (" bit insertion is required\n", dump_file); |
| 2449 | if (string_concatenation) |
| 2450 | fputs (" string concatenation is required\n", dump_file); |
| 2451 | } |
| 2452 | else |
| 2453 | fprintf (dump_file, "Failed to merge stores\n"); |
| 2454 | } |
| 2455 | if (!ret) |
| 2456 | return false; |
| 2457 | } |
| 2458 | stores.qsort (sort_by_bitpos); |
| 2459 | return true; |
| 2460 | } |
| 2461 | |
| 2462 | /* Structure describing the store chain. */ |
| 2463 | |
| 2464 | class imm_store_chain_info |
| 2465 | { |
| 2466 | public: |
| 2467 | /* Doubly-linked list that imposes an order on chain processing. |
| 2468 | PNXP (prev's next pointer) points to the head of a list, or to |
| 2469 | the next field in the previous chain in the list. |
| 2470 | See pass_store_merging::m_stores_head for more rationale. */ |
| 2471 | imm_store_chain_info *next, **pnxp; |
| 2472 | tree base_addr; |
| 2473 | auto_vec<store_immediate_info *> m_store_info; |
| 2474 | auto_vec<merged_store_group *> m_merged_store_groups; |
| 2475 | |
| 2476 | imm_store_chain_info (imm_store_chain_info *&inspt, tree b_a) |
| 2477 | : next (inspt), pnxp (&inspt), base_addr (b_a) |
| 2478 | { |
| 2479 | inspt = this; |
| 2480 | if (next) |
| 2481 | { |
| 2482 | gcc_checking_assert (pnxp == next->pnxp); |
| 2483 | next->pnxp = &next; |
| 2484 | } |
| 2485 | } |
| 2486 | ~imm_store_chain_info () |
| 2487 | { |
| 2488 | *pnxp = next; |
| 2489 | if (next) |
| 2490 | { |
| 2491 | gcc_checking_assert (&next == next->pnxp); |
| 2492 | next->pnxp = pnxp; |
| 2493 | } |
| 2494 | } |
| 2495 | bool terminate_and_process_chain (); |
| 2496 | bool try_coalesce_bswap (merged_store_group *, unsigned int, unsigned int, |
| 2497 | unsigned int); |
| 2498 | bool coalesce_immediate_stores (); |
| 2499 | bool output_merged_store (merged_store_group *); |
| 2500 | bool output_merged_stores (); |
| 2501 | }; |
| 2502 | |
| 2503 | const pass_data pass_data_tree_store_merging = { |
| 2504 | GIMPLE_PASS, /* type */ |
| 2505 | "store-merging", /* name */ |
| 2506 | OPTGROUP_NONE, /* optinfo_flags */ |
| 2507 | TV_GIMPLE_STORE_MERGING, /* tv_id */ |
| 2508 | PROP_ssa, /* properties_required */ |
| 2509 | 0, /* properties_provided */ |
| 2510 | 0, /* properties_destroyed */ |
| 2511 | 0, /* todo_flags_start */ |
| 2512 | TODO_update_ssa, /* todo_flags_finish */ |
| 2513 | }; |
| 2514 | |
| 2515 | class pass_store_merging : public gimple_opt_pass |
| 2516 | { |
| 2517 | public: |
| 2518 | pass_store_merging (gcc::context *ctxt) |
| 2519 | : gimple_opt_pass (pass_data_tree_store_merging, ctxt), m_stores_head (), |
| 2520 | m_n_chains (0), m_n_stores (0) |
| 2521 | { |
| 2522 | } |
| 2523 | |
| 2524 | /* Pass not supported for PDP-endian, nor for insane hosts or |
| 2525 | target character sizes where native_{encode,interpret}_expr |
| 2526 | doesn't work properly. */ |
| 2527 | bool |
| 2528 | gate (function *) final override |
| 2529 | { |
| 2530 | return flag_store_merging |
| 2531 | && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN |
| 2532 | && CHAR_BIT == 8 |
| 2533 | && BITS_PER_UNIT == 8; |
| 2534 | } |
| 2535 | |
| 2536 | unsigned int execute (function *) final override; |
| 2537 | |
| 2538 | private: |
| 2539 | hash_map<tree_operand_hash, class imm_store_chain_info *> m_stores; |
| 2540 | |
| 2541 | /* Form a doubly-linked stack of the elements of m_stores, so that |
| 2542 | we can iterate over them in a predictable way. Using this order |
| 2543 | avoids extraneous differences in the compiler output just because |
| 2544 | of tree pointer variations (e.g. different chains end up in |
| 2545 | different positions of m_stores, so they are handled in different |
| 2546 | orders, so they allocate or release SSA names in different |
| 2547 | orders, and when they get reused, subsequent passes end up |
| 2548 | getting different SSA names, which may ultimately change |
| 2549 | decisions when going out of SSA). */ |
| 2550 | imm_store_chain_info *m_stores_head; |
| 2551 | |
| 2552 | /* The number of store chains currently tracked. */ |
| 2553 | unsigned m_n_chains; |
| 2554 | /* The number of stores currently tracked. */ |
| 2555 | unsigned m_n_stores; |
| 2556 | |
| 2557 | bool process_store (gimple *); |
| 2558 | bool terminate_and_process_chain (imm_store_chain_info *); |
| 2559 | bool terminate_all_aliasing_chains (imm_store_chain_info **, gimple *); |
| 2560 | bool terminate_and_process_all_chains (); |
| 2561 | }; // class pass_store_merging |
| 2562 | |
| 2563 | /* Terminate and process all recorded chains. Return true if any changes |
| 2564 | were made. */ |
| 2565 | |
| 2566 | bool |
| 2567 | pass_store_merging::terminate_and_process_all_chains () |
| 2568 | { |
| 2569 | bool ret = false; |
| 2570 | while (m_stores_head) |
| 2571 | ret |= terminate_and_process_chain (m_stores_head); |
| 2572 | gcc_assert (m_stores.is_empty ()); |
| 2573 | return ret; |
| 2574 | } |
| 2575 | |
| 2576 | /* Terminate all chains that are affected by the statement STMT. |
| 2577 | CHAIN_INFO is the chain we should ignore from the checks if |
| 2578 | non-NULL. Return true if any changes were made. */ |
| 2579 | |
| 2580 | bool |
| 2581 | pass_store_merging::terminate_all_aliasing_chains (imm_store_chain_info |
| 2582 | **chain_info, |
| 2583 | gimple *stmt) |
| 2584 | { |
| 2585 | bool ret = false; |
| 2586 | |
| 2587 | /* If the statement doesn't touch memory it can't alias. */ |
| 2588 | if (!gimple_vuse (stmt)) |
| 2589 | return false; |
| 2590 | |
| 2591 | tree store_lhs = gimple_store_p (stmt) ? gimple_get_lhs (stmt) : NULL_TREE; |
| 2592 | ao_ref store_lhs_ref; |
| 2593 | ao_ref_init (&store_lhs_ref, store_lhs); |
| 2594 | for (imm_store_chain_info *next = m_stores_head, *cur = next; cur; cur = next) |
| 2595 | { |
| 2596 | next = cur->next; |
| 2597 | |
| 2598 | /* We already checked all the stores in chain_info and terminated the |
| 2599 | chain if necessary. Skip it here. */ |
| 2600 | if (chain_info && *chain_info == cur) |
| 2601 | continue; |
| 2602 | |
| 2603 | store_immediate_info *info; |
| 2604 | unsigned int i; |
| 2605 | FOR_EACH_VEC_ELT (cur->m_store_info, i, info) |
| 2606 | { |
| 2607 | tree lhs = gimple_assign_lhs (info->stmt); |
| 2608 | ao_ref lhs_ref; |
| 2609 | ao_ref_init (&lhs_ref, lhs); |
| 2610 | if (ref_maybe_used_by_stmt_p (stmt, &lhs_ref) |
| 2611 | || stmt_may_clobber_ref_p_1 (stmt, &lhs_ref) |
| 2612 | || (store_lhs && refs_may_alias_p_1 (&store_lhs_ref, |
| 2613 | &lhs_ref, false))) |
| 2614 | { |
| 2615 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 2616 | { |
| 2617 | fprintf (dump_file, "stmt causes chain termination:\n"); |
| 2618 | print_gimple_stmt (dump_file, stmt, 0); |
| 2619 | } |
| 2620 | ret |= terminate_and_process_chain (cur); |
| 2621 | break; |
| 2622 | } |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | return ret; |
| 2627 | } |
| 2628 | |
| 2629 | /* Helper function. Terminate the recorded chain storing to base object |
| 2630 | BASE. Return true if the merging and output was successful. The m_stores |
| 2631 | entry is removed after the processing in any case. */ |
| 2632 | |
| 2633 | bool |
| 2634 | pass_store_merging::terminate_and_process_chain (imm_store_chain_info *chain_info) |
| 2635 | { |
| 2636 | m_n_stores -= chain_info->m_store_info.length (); |
| 2637 | m_n_chains--; |
| 2638 | bool ret = chain_info->terminate_and_process_chain (); |
| 2639 | m_stores.remove (chain_info->base_addr); |
| 2640 | delete chain_info; |
| 2641 | return ret; |
| 2642 | } |
| 2643 | |
| 2644 | /* Return true if stmts in between FIRST (inclusive) and LAST (exclusive) |
| 2645 | may clobber REF. FIRST and LAST must have non-NULL vdef. We want to |
| 2646 | be able to sink load of REF across stores between FIRST and LAST, up |
| 2647 | to right before LAST. */ |
| 2648 | |
| 2649 | bool |
| 2650 | stmts_may_clobber_ref_p (gimple *first, gimple *last, tree ref) |
| 2651 | { |
| 2652 | ao_ref r; |
| 2653 | ao_ref_init (&r, ref); |
| 2654 | unsigned int count = 0; |
| 2655 | tree vop = gimple_vdef (last); |
| 2656 | gimple *stmt; |
| 2657 | |
| 2658 | /* Return true conservatively if the basic blocks are different. */ |
| 2659 | if (gimple_bb (first) != gimple_bb (last)) |
| 2660 | return true; |
| 2661 | |
| 2662 | do |
| 2663 | { |
| 2664 | stmt = SSA_NAME_DEF_STMT (vop); |
| 2665 | if (stmt_may_clobber_ref_p_1 (stmt, &r)) |
| 2666 | return true; |
| 2667 | if (gimple_store_p (stmt) |
| 2668 | && refs_anti_dependent_p (ref, gimple_get_lhs (stmt))) |
| 2669 | return true; |
| 2670 | /* Avoid quadratic compile time by bounding the number of checks |
| 2671 | we perform. */ |
| 2672 | if (++count > MAX_STORE_ALIAS_CHECKS) |
| 2673 | return true; |
| 2674 | vop = gimple_vuse (stmt); |
| 2675 | } |
| 2676 | while (stmt != first); |
| 2677 | |
| 2678 | return false; |
| 2679 | } |
| 2680 | |
| 2681 | /* Return true if INFO->ops[IDX] is mergeable with the |
| 2682 | corresponding loads already in MERGED_STORE group. |
| 2683 | BASE_ADDR is the base address of the whole store group. */ |
| 2684 | |
| 2685 | bool |
| 2686 | compatible_load_p (merged_store_group *merged_store, |
| 2687 | store_immediate_info *info, |
| 2688 | tree base_addr, int idx) |
| 2689 | { |
| 2690 | store_immediate_info *infof = merged_store->stores[0]; |
| 2691 | if (!info->ops[idx].base_addr |
| 2692 | || maybe_ne (info->ops[idx].bitpos - infof->ops[idx].bitpos, |
| 2693 | info->bitpos - infof->bitpos) |
| 2694 | || !operand_equal_p (info->ops[idx].base_addr, |
| 2695 | infof->ops[idx].base_addr, 0)) |
| 2696 | return false; |
| 2697 | |
| 2698 | store_immediate_info *infol = merged_store->stores.last (); |
| 2699 | tree load_vuse = gimple_vuse (info->ops[idx].stmt); |
| 2700 | /* In this case all vuses should be the same, e.g. |
| 2701 | _1 = s.a; _2 = s.b; _3 = _1 | 1; t.a = _3; _4 = _2 | 2; t.b = _4; |
| 2702 | or |
| 2703 | _1 = s.a; _2 = s.b; t.a = _1; t.b = _2; |
| 2704 | and we can emit the coalesced load next to any of those loads. */ |
| 2705 | if (gimple_vuse (infof->ops[idx].stmt) == load_vuse |
| 2706 | && gimple_vuse (infol->ops[idx].stmt) == load_vuse) |
| 2707 | return true; |
| 2708 | |
| 2709 | /* Otherwise, at least for now require that the load has the same |
| 2710 | vuse as the store. See following examples. */ |
| 2711 | if (gimple_vuse (info->stmt) != load_vuse) |
| 2712 | return false; |
| 2713 | |
| 2714 | if (gimple_vuse (infof->stmt) != gimple_vuse (infof->ops[idx].stmt) |
| 2715 | || (infof != infol |
| 2716 | && gimple_vuse (infol->stmt) != gimple_vuse (infol->ops[idx].stmt))) |
| 2717 | return false; |
| 2718 | |
| 2719 | /* If the load is from the same location as the store, already |
| 2720 | the construction of the immediate chain info guarantees no intervening |
| 2721 | stores, so no further checks are needed. Example: |
| 2722 | _1 = s.a; _2 = _1 & -7; s.a = _2; _3 = s.b; _4 = _3 & -7; s.b = _4; */ |
| 2723 | if (known_eq (info->ops[idx].bitpos, info->bitpos) |
| 2724 | && operand_equal_p (info->ops[idx].base_addr, base_addr, 0)) |
| 2725 | return true; |
| 2726 | |
| 2727 | /* Otherwise, we need to punt if any of the loads can be clobbered by any |
| 2728 | of the stores in the group, or any other stores in between those. |
| 2729 | Previous calls to compatible_load_p ensured that for all the |
| 2730 | merged_store->stores IDX loads, no stmts starting with |
| 2731 | merged_store->first_stmt and ending right before merged_store->last_stmt |
| 2732 | clobbers those loads. */ |
| 2733 | gimple *first = merged_store->first_stmt; |
| 2734 | gimple *last = merged_store->last_stmt; |
| 2735 | /* The stores are sorted by increasing store bitpos, so if info->stmt store |
| 2736 | comes before the so far first load, we'll be changing |
| 2737 | merged_store->first_stmt. In that case we need to give up if |
| 2738 | any of the earlier processed loads clobber with the stmts in the new |
| 2739 | range. */ |
| 2740 | if (info->order < merged_store->first_order) |
| 2741 | { |
| 2742 | for (store_immediate_info *infoc : merged_store->stores) |
| 2743 | if (stmts_may_clobber_ref_p (info->stmt, first, infoc->ops[idx].val)) |
| 2744 | return false; |
| 2745 | first = info->stmt; |
| 2746 | } |
| 2747 | /* Similarly, we could change merged_store->last_stmt, so ensure |
| 2748 | in that case no stmts in the new range clobber any of the earlier |
| 2749 | processed loads. */ |
| 2750 | else if (info->order > merged_store->last_order) |
| 2751 | { |
| 2752 | for (store_immediate_info *infoc : merged_store->stores) |
| 2753 | if (stmts_may_clobber_ref_p (last, info->stmt, infoc->ops[idx].val)) |
| 2754 | return false; |
| 2755 | last = info->stmt; |
| 2756 | } |
| 2757 | /* And finally, we'd be adding a new load to the set, ensure it isn't |
| 2758 | clobbered in the new range. */ |
| 2759 | if (stmts_may_clobber_ref_p (first, last, info->ops[idx].val)) |
| 2760 | return false; |
| 2761 | |
| 2762 | /* Otherwise, we are looking for: |
| 2763 | _1 = s.a; _2 = _1 ^ 15; t.a = _2; _3 = s.b; _4 = _3 ^ 15; t.b = _4; |
| 2764 | or |
| 2765 | _1 = s.a; t.a = _1; _2 = s.b; t.b = _2; */ |
| 2766 | return true; |
| 2767 | } |
| 2768 | |
| 2769 | /* Add all refs loaded to compute VAL to REFS vector. */ |
| 2770 | |
| 2771 | void |
| 2772 | gather_bswap_load_refs (vec<tree> *refs, tree val) |
| 2773 | { |
| 2774 | if (TREE_CODE (val) != SSA_NAME) |
| 2775 | return; |
| 2776 | |
| 2777 | gimple *stmt = SSA_NAME_DEF_STMT (val); |
| 2778 | if (!is_gimple_assign (stmt)) |
| 2779 | return; |
| 2780 | |
| 2781 | if (gimple_assign_load_p (stmt)) |
| 2782 | { |
| 2783 | refs->safe_push (gimple_assign_rhs1 (stmt)); |
| 2784 | return; |
| 2785 | } |
| 2786 | |
| 2787 | switch (gimple_assign_rhs_class (stmt)) |
| 2788 | { |
| 2789 | case GIMPLE_BINARY_RHS: |
| 2790 | gather_bswap_load_refs (refs, gimple_assign_rhs2 (stmt)); |
| 2791 | /* FALLTHRU */ |
| 2792 | case GIMPLE_UNARY_RHS: |
| 2793 | gather_bswap_load_refs (refs, gimple_assign_rhs1 (stmt)); |
| 2794 | break; |
| 2795 | default: |
| 2796 | gcc_unreachable (); |
| 2797 | } |
| 2798 | } |
| 2799 | |
| 2800 | /* Check if there are any stores in M_STORE_INFO after index I |
| 2801 | (where M_STORE_INFO must be sorted by sort_by_bitpos) that overlap |
| 2802 | a potential group ending with END that have their order |
| 2803 | smaller than LAST_ORDER. ALL_INTEGER_CST_P is true if |
| 2804 | all the stores already merged and the one under consideration |
| 2805 | have rhs_code of INTEGER_CST. Return true if there are no such stores. |
| 2806 | Consider: |
| 2807 | MEM[(long long int *)p_28] = 0; |
| 2808 | MEM[(long long int *)p_28 + 8B] = 0; |
| 2809 | MEM[(long long int *)p_28 + 16B] = 0; |
| 2810 | MEM[(long long int *)p_28 + 24B] = 0; |
| 2811 | _129 = (int) _130; |
| 2812 | MEM[(int *)p_28 + 8B] = _129; |
| 2813 | MEM[(int *)p_28].a = -1; |
| 2814 | We already have |
| 2815 | MEM[(long long int *)p_28] = 0; |
| 2816 | MEM[(int *)p_28].a = -1; |
| 2817 | stmts in the current group and need to consider if it is safe to |
| 2818 | add MEM[(long long int *)p_28 + 8B] = 0; store into the same group. |
| 2819 | There is an overlap between that store and the MEM[(int *)p_28 + 8B] = _129; |
| 2820 | store though, so if we add the MEM[(long long int *)p_28 + 8B] = 0; |
| 2821 | into the group and merging of those 3 stores is successful, merged |
| 2822 | stmts will be emitted at the latest store from that group, i.e. |
| 2823 | LAST_ORDER, which is the MEM[(int *)p_28].a = -1; store. |
| 2824 | The MEM[(int *)p_28 + 8B] = _129; store that originally follows |
| 2825 | the MEM[(long long int *)p_28 + 8B] = 0; would now be before it, |
| 2826 | so we need to refuse merging MEM[(long long int *)p_28 + 8B] = 0; |
| 2827 | into the group. That way it will be its own store group and will |
| 2828 | not be touched. If ALL_INTEGER_CST_P and there are overlapping |
| 2829 | INTEGER_CST stores, those are mergeable using merge_overlapping, |
| 2830 | so don't return false for those. |
| 2831 | |
| 2832 | Similarly, check stores from FIRST_EARLIER (inclusive) to END_EARLIER |
| 2833 | (exclusive), whether they don't overlap the bitrange START to END |
| 2834 | and have order in between FIRST_ORDER and LAST_ORDER. This is to |
| 2835 | prevent merging in cases like: |
| 2836 | MEM <char[12]> [&b + 8B] = {}; |
| 2837 | MEM[(short *) &b] = 5; |
| 2838 | _5 = *x_4(D); |
| 2839 | MEM <long long unsigned int> [&b + 2B] = _5; |
| 2840 | MEM[(char *)&b + 16B] = 88; |
| 2841 | MEM[(int *)&b + 20B] = 1; |
| 2842 | The = {} store comes in sort_by_bitpos before the = 88 store, and can't |
| 2843 | be merged with it, because the = _5 store overlaps these and is in between |
| 2844 | them in sort_by_order ordering. If it was merged, the merged store would |
| 2845 | go after the = _5 store and thus change behavior. */ |
| 2846 | |
| 2847 | static bool |
| 2848 | check_no_overlap (const vec<store_immediate_info *> &m_store_info, |
| 2849 | unsigned int i, |
| 2850 | bool all_integer_cst_p, unsigned int first_order, |
| 2851 | unsigned int last_order, unsigned HOST_WIDE_INT start, |
| 2852 | unsigned HOST_WIDE_INT end, unsigned int first_earlier, |
| 2853 | unsigned end_earlier) |
| 2854 | { |
| 2855 | unsigned int len = m_store_info.length (); |
| 2856 | for (unsigned int j = first_earlier; j < end_earlier; j++) |
| 2857 | { |
| 2858 | store_immediate_info *info = m_store_info[j]; |
| 2859 | if (info->order > first_order |
| 2860 | && info->order < last_order |
| 2861 | && info->bitpos + info->bitsize > start) |
| 2862 | return false; |
| 2863 | } |
| 2864 | for (++i; i < len; ++i) |
| 2865 | { |
| 2866 | store_immediate_info *info = m_store_info[i]; |
| 2867 | if (info->bitpos >= end) |
| 2868 | break; |
| 2869 | if (info->order < last_order |
| 2870 | && (!all_integer_cst_p || info->rhs_code != INTEGER_CST)) |
| 2871 | return false; |
| 2872 | } |
| 2873 | return true; |
| 2874 | } |
| 2875 | |
| 2876 | /* Return true if m_store_info[first] and at least one following store |
| 2877 | form a group which store try_size bitsize value which is byte swapped |
| 2878 | from a memory load or some value, or identity from some value. |
| 2879 | This uses the bswap pass APIs. */ |
| 2880 | |
| 2881 | bool |
| 2882 | imm_store_chain_info::try_coalesce_bswap (merged_store_group *merged_store, |
| 2883 | unsigned int first, |
| 2884 | unsigned int try_size, |
| 2885 | unsigned int first_earlier) |
| 2886 | { |
| 2887 | unsigned int len = m_store_info.length (), last = first; |
| 2888 | unsigned HOST_WIDE_INT width = m_store_info[first]->bitsize; |
| 2889 | if (width >= try_size) |
| 2890 | return false; |
| 2891 | for (unsigned int i = first + 1; i < len; ++i) |
| 2892 | { |
| 2893 | if (m_store_info[i]->bitpos != m_store_info[first]->bitpos + width |
| 2894 | || m_store_info[i]->lp_nr != merged_store->lp_nr |
| 2895 | || m_store_info[i]->ins_stmt == NULL) |
| 2896 | return false; |
| 2897 | width += m_store_info[i]->bitsize; |
| 2898 | if (width >= try_size) |
| 2899 | { |
| 2900 | last = i; |
| 2901 | break; |
| 2902 | } |
| 2903 | } |
| 2904 | if (width != try_size) |
| 2905 | return false; |
| 2906 | |
| 2907 | bool allow_unaligned |
| 2908 | = !STRICT_ALIGNMENT && param_store_merging_allow_unaligned; |
| 2909 | /* Punt if the combined store would not be aligned and we need alignment. */ |
| 2910 | if (!allow_unaligned) |
| 2911 | { |
| 2912 | unsigned int align = merged_store->align; |
| 2913 | unsigned HOST_WIDE_INT align_base = merged_store->align_base; |
| 2914 | for (unsigned int i = first + 1; i <= last; ++i) |
| 2915 | { |
| 2916 | unsigned int this_align; |
| 2917 | unsigned HOST_WIDE_INT align_bitpos = 0; |
| 2918 | get_object_alignment_1 (gimple_assign_lhs (m_store_info[i]->stmt), |
| 2919 | &this_align, &align_bitpos); |
| 2920 | if (this_align > align) |
| 2921 | { |
| 2922 | align = this_align; |
| 2923 | align_base = m_store_info[i]->bitpos - align_bitpos; |
| 2924 | } |
| 2925 | } |
| 2926 | unsigned HOST_WIDE_INT align_bitpos |
| 2927 | = (m_store_info[first]->bitpos - align_base) & (align - 1); |
| 2928 | if (align_bitpos) |
| 2929 | align = least_bit_hwi (align_bitpos); |
| 2930 | if (align < try_size) |
| 2931 | return false; |
| 2932 | } |
| 2933 | |
| 2934 | tree type; |
| 2935 | switch (try_size) |
| 2936 | { |
| 2937 | case 16: type = uint16_type_node; break; |
| 2938 | case 32: type = uint32_type_node; break; |
| 2939 | case 64: type = uint64_type_node; break; |
| 2940 | default: gcc_unreachable (); |
| 2941 | } |
| 2942 | struct symbolic_number n; |
| 2943 | gimple *ins_stmt = NULL; |
| 2944 | int vuse_store = -1; |
| 2945 | unsigned int first_order = merged_store->first_order; |
| 2946 | unsigned int last_order = merged_store->last_order; |
| 2947 | gimple *first_stmt = merged_store->first_stmt; |
| 2948 | gimple *last_stmt = merged_store->last_stmt; |
| 2949 | unsigned HOST_WIDE_INT end = merged_store->start + merged_store->width; |
| 2950 | store_immediate_info *infof = m_store_info[first]; |
| 2951 | |
| 2952 | for (unsigned int i = first; i <= last; ++i) |
| 2953 | { |
| 2954 | store_immediate_info *info = m_store_info[i]; |
| 2955 | struct symbolic_number this_n = info->n; |
| 2956 | this_n.type = type; |
| 2957 | if (!this_n.base_addr) |
| 2958 | this_n.range = try_size / BITS_PER_UNIT; |
| 2959 | else |
| 2960 | /* Update vuse in case it has changed by output_merged_stores. */ |
| 2961 | this_n.vuse = gimple_vuse (info->ins_stmt); |
| 2962 | unsigned int bitpos = info->bitpos - infof->bitpos; |
| 2963 | if (!do_shift_rotate (LSHIFT_EXPR, &this_n, |
| 2964 | BYTES_BIG_ENDIAN |
| 2965 | ? try_size - info->bitsize - bitpos |
| 2966 | : bitpos)) |
| 2967 | return false; |
| 2968 | if (this_n.base_addr && vuse_store) |
| 2969 | { |
| 2970 | unsigned int j; |
| 2971 | for (j = first; j <= last; ++j) |
| 2972 | if (this_n.vuse == gimple_vuse (m_store_info[j]->stmt)) |
| 2973 | break; |
| 2974 | if (j > last) |
| 2975 | { |
| 2976 | if (vuse_store == 1) |
| 2977 | return false; |
| 2978 | vuse_store = 0; |
| 2979 | } |
| 2980 | } |
| 2981 | if (i == first) |
| 2982 | { |
| 2983 | n = this_n; |
| 2984 | ins_stmt = info->ins_stmt; |
| 2985 | } |
| 2986 | else |
| 2987 | { |
| 2988 | if (n.base_addr && n.vuse != this_n.vuse) |
| 2989 | { |
| 2990 | if (vuse_store == 0) |
| 2991 | return false; |
| 2992 | vuse_store = 1; |
| 2993 | } |
| 2994 | if (info->order > last_order) |
| 2995 | { |
| 2996 | last_order = info->order; |
| 2997 | last_stmt = info->stmt; |
| 2998 | } |
| 2999 | else if (info->order < first_order) |
| 3000 | { |
| 3001 | first_order = info->order; |
| 3002 | first_stmt = info->stmt; |
| 3003 | } |
| 3004 | end = MAX (end, info->bitpos + info->bitsize); |
| 3005 | |
| 3006 | ins_stmt = perform_symbolic_merge (ins_stmt, &n, info->ins_stmt, |
| 3007 | &this_n, &n, BIT_IOR_EXPR); |
| 3008 | if (ins_stmt == NULL) |
| 3009 | return false; |
| 3010 | } |
| 3011 | } |
| 3012 | |
| 3013 | uint64_t cmpxchg, cmpnop; |
| 3014 | bool cast64_to_32; |
| 3015 | find_bswap_or_nop_finalize (&n, &cmpxchg, &cmpnop, &cast64_to_32); |
| 3016 | |
| 3017 | /* A complete byte swap should make the symbolic number to start with |
| 3018 | the largest digit in the highest order byte. Unchanged symbolic |
| 3019 | number indicates a read with same endianness as target architecture. */ |
| 3020 | if (n.n != cmpnop && n.n != cmpxchg) |
| 3021 | return false; |
| 3022 | |
| 3023 | /* For now. */ |
| 3024 | if (cast64_to_32) |
| 3025 | return false; |
| 3026 | |
| 3027 | if (n.base_addr == NULL_TREE && !is_gimple_val (n.src)) |
| 3028 | return false; |
| 3029 | |
| 3030 | if (!check_no_overlap (m_store_info, last, false, first_order, last_order, |
| 3031 | merged_store->start, end, first_earlier, first)) |
| 3032 | return false; |
| 3033 | |
| 3034 | /* Don't handle memory copy this way if normal non-bswap processing |
| 3035 | would handle it too. */ |
| 3036 | if (n.n == cmpnop && (unsigned) n.n_ops == last - first + 1) |
| 3037 | { |
| 3038 | unsigned int i; |
| 3039 | for (i = first; i <= last; ++i) |
| 3040 | if (m_store_info[i]->rhs_code != MEM_REF) |
| 3041 | break; |
| 3042 | if (i == last + 1) |
| 3043 | return false; |
| 3044 | } |
| 3045 | |
| 3046 | if (n.n == cmpxchg) |
| 3047 | switch (try_size) |
| 3048 | { |
| 3049 | case 16: |
| 3050 | /* Will emit LROTATE_EXPR. */ |
| 3051 | break; |
| 3052 | case 32: |
| 3053 | if (builtin_decl_explicit_p (BUILT_IN_BSWAP32) |
| 3054 | && optab_handler (bswap_optab, SImode) != CODE_FOR_nothing) |
| 3055 | break; |
| 3056 | return false; |
| 3057 | case 64: |
| 3058 | if (builtin_decl_explicit_p (BUILT_IN_BSWAP64) |
| 3059 | && (optab_handler (bswap_optab, DImode) != CODE_FOR_nothing |
| 3060 | || (word_mode == SImode |
| 3061 | && builtin_decl_explicit_p (BUILT_IN_BSWAP32) |
| 3062 | && optab_handler (bswap_optab, SImode) != CODE_FOR_nothing))) |
| 3063 | break; |
| 3064 | return false; |
| 3065 | default: |
| 3066 | gcc_unreachable (); |
| 3067 | } |
| 3068 | |
| 3069 | if (!allow_unaligned && n.base_addr) |
| 3070 | { |
| 3071 | unsigned int align = get_object_alignment (n.src); |
| 3072 | if (align < try_size) |
| 3073 | return false; |
| 3074 | } |
| 3075 | |
| 3076 | /* If each load has vuse of the corresponding store, need to verify |
| 3077 | the loads can be sunk right before the last store. */ |
| 3078 | if (vuse_store == 1) |
| 3079 | { |
| 3080 | auto_vec<tree, 64> refs; |
| 3081 | for (unsigned int i = first; i <= last; ++i) |
| 3082 | gather_bswap_load_refs (&refs, |
| 3083 | gimple_assign_rhs1 (m_store_info[i]->stmt)); |
| 3084 | |
| 3085 | for (tree ref : refs) |
| 3086 | if (stmts_may_clobber_ref_p (first_stmt, last_stmt, ref)) |
| 3087 | return false; |
| 3088 | n.vuse = NULL_TREE; |
| 3089 | } |
| 3090 | |
| 3091 | infof->n = n; |
| 3092 | infof->ins_stmt = ins_stmt; |
| 3093 | for (unsigned int i = first; i <= last; ++i) |
| 3094 | { |
| 3095 | m_store_info[i]->rhs_code = n.n == cmpxchg ? LROTATE_EXPR : NOP_EXPR; |
| 3096 | m_store_info[i]->ops[0].base_addr = NULL_TREE; |
| 3097 | m_store_info[i]->ops[1].base_addr = NULL_TREE; |
| 3098 | if (i != first) |
| 3099 | merged_store->merge_into (m_store_info[i]); |
| 3100 | } |
| 3101 | |
| 3102 | return true; |
| 3103 | } |
| 3104 | |
| 3105 | /* Go through the candidate stores recorded in m_store_info and merge them |
| 3106 | into merged_store_group objects recorded into m_merged_store_groups |
| 3107 | representing the widened stores. Return true if coalescing was successful |
| 3108 | and the number of widened stores is fewer than the original number |
| 3109 | of stores. */ |
| 3110 | |
| 3111 | bool |
| 3112 | imm_store_chain_info::coalesce_immediate_stores () |
| 3113 | { |
| 3114 | /* Anything less can't be processed. */ |
| 3115 | if (m_store_info.length () < 2) |
| 3116 | return false; |
| 3117 | |
| 3118 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 3119 | fprintf (dump_file, "Attempting to coalesce %u stores in chain\n", |
| 3120 | m_store_info.length ()); |
| 3121 | |
| 3122 | store_immediate_info *info; |
| 3123 | unsigned int i, ignore = 0; |
| 3124 | unsigned int first_earlier = 0; |
| 3125 | unsigned int end_earlier = 0; |
| 3126 | |
| 3127 | /* Order the stores by the bitposition they write to. */ |
| 3128 | m_store_info.qsort (sort_by_bitpos); |
| 3129 | |
| 3130 | info = m_store_info[0]; |
| 3131 | merged_store_group *merged_store = new merged_store_group (info); |
| 3132 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 3133 | fputs ("New store group\n", dump_file); |
| 3134 | |
| 3135 | FOR_EACH_VEC_ELT (m_store_info, i, info) |
| 3136 | { |
| 3137 | unsigned HOST_WIDE_INT new_bitregion_start, new_bitregion_end; |
| 3138 | |
| 3139 | if (i <= ignore) |
| 3140 | goto done; |
| 3141 | |
| 3142 | while (first_earlier < end_earlier |
| 3143 | && (m_store_info[first_earlier]->bitpos |
| 3144 | + m_store_info[first_earlier]->bitsize |
| 3145 | <= merged_store->start)) |
| 3146 | first_earlier++; |
| 3147 | |
| 3148 | /* First try to handle group of stores like: |
| 3149 | p[0] = data >> 24; |
| 3150 | p[1] = data >> 16; |
| 3151 | p[2] = data >> 8; |
| 3152 | p[3] = data; |
| 3153 | using the bswap framework. */ |
| 3154 | if (info->bitpos == merged_store->start + merged_store->width |
| 3155 | && merged_store->stores.length () == 1 |
| 3156 | && merged_store->stores[0]->ins_stmt != NULL |
| 3157 | && info->lp_nr == merged_store->lp_nr |
| 3158 | && info->ins_stmt != NULL) |
| 3159 | { |
| 3160 | unsigned int try_size; |
| 3161 | for (try_size = 64; try_size >= 16; try_size >>= 1) |
| 3162 | if (try_coalesce_bswap (merged_store, i - 1, try_size, |
| 3163 | first_earlier)) |
| 3164 | break; |
| 3165 | |
| 3166 | if (try_size >= 16) |
| 3167 | { |
| 3168 | ignore = i + merged_store->stores.length () - 1; |
| 3169 | m_merged_store_groups.safe_push (merged_store); |
| 3170 | if (ignore < m_store_info.length ()) |
| 3171 | { |
| 3172 | merged_store = new merged_store_group (m_store_info[ignore]); |
| 3173 | end_earlier = ignore; |
| 3174 | } |
| 3175 | else |
| 3176 | merged_store = NULL; |
| 3177 | goto done; |
| 3178 | } |
| 3179 | } |
| 3180 | |
| 3181 | new_bitregion_start |
| 3182 | = MIN (merged_store->bitregion_start, info->bitregion_start); |
| 3183 | new_bitregion_end |
| 3184 | = MAX (merged_store->bitregion_end, info->bitregion_end); |
| 3185 | |
| 3186 | if (info->order >= merged_store->first_nonmergeable_order |
| 3187 | || (((new_bitregion_end - new_bitregion_start + 1) / BITS_PER_UNIT) |
| 3188 | > (unsigned) param_store_merging_max_size)) |
| 3189 | ; |
| 3190 | |
| 3191 | /* |---store 1---| |
| 3192 | |---store 2---| |
| 3193 | Overlapping stores. */ |
| 3194 | else if (IN_RANGE (info->bitpos, merged_store->start, |
| 3195 | merged_store->start + merged_store->width - 1) |
| 3196 | /* |---store 1---||---store 2---| |
| 3197 | Handle also the consecutive INTEGER_CST stores case here, |
| 3198 | as we have here the code to deal with overlaps. */ |
| 3199 | || (info->bitregion_start <= merged_store->bitregion_end |
| 3200 | && info->rhs_code == INTEGER_CST |
| 3201 | && merged_store->only_constants |
| 3202 | && merged_store->can_be_merged_into (info))) |
| 3203 | { |
| 3204 | /* Only allow overlapping stores of constants. */ |
| 3205 | if (info->rhs_code == INTEGER_CST |
| 3206 | && merged_store->only_constants |
| 3207 | && info->lp_nr == merged_store->lp_nr) |
| 3208 | { |
| 3209 | unsigned int first_order |
| 3210 | = MIN (merged_store->first_order, info->order); |
| 3211 | unsigned int last_order |
| 3212 | = MAX (merged_store->last_order, info->order); |
| 3213 | unsigned HOST_WIDE_INT end |
| 3214 | = MAX (merged_store->start + merged_store->width, |
| 3215 | info->bitpos + info->bitsize); |
| 3216 | if (check_no_overlap (m_store_info, i, true, first_order, |
| 3217 | last_order, merged_store->start, end, |
| 3218 | first_earlier, end_earlier)) |
| 3219 | { |
| 3220 | /* check_no_overlap call above made sure there are no |
| 3221 | overlapping stores with non-INTEGER_CST rhs_code |
| 3222 | in between the first and last of the stores we've |
| 3223 | just merged. If there are any INTEGER_CST rhs_code |
| 3224 | stores in between, we need to merge_overlapping them |
| 3225 | even if in the sort_by_bitpos order there are other |
| 3226 | overlapping stores in between. Keep those stores as is. |
| 3227 | Example: |
| 3228 | MEM[(int *)p_28] = 0; |
| 3229 | MEM[(char *)p_28 + 3B] = 1; |
| 3230 | MEM[(char *)p_28 + 1B] = 2; |
| 3231 | MEM[(char *)p_28 + 2B] = MEM[(char *)p_28 + 6B]; |
| 3232 | We can't merge the zero store with the store of two and |
| 3233 | not merge anything else, because the store of one is |
| 3234 | in the original order in between those two, but in |
| 3235 | store_by_bitpos order it comes after the last store that |
| 3236 | we can't merge with them. We can merge the first 3 stores |
| 3237 | and keep the last store as is though. */ |
| 3238 | unsigned int len = m_store_info.length (); |
| 3239 | unsigned int try_order = last_order; |
| 3240 | unsigned int first_nonmergeable_order; |
| 3241 | unsigned int k; |
| 3242 | bool last_iter = false; |
| 3243 | int attempts = 0; |
| 3244 | do |
| 3245 | { |
| 3246 | unsigned int max_order = 0; |
| 3247 | unsigned int min_order = first_order; |
| 3248 | unsigned first_nonmergeable_int_order = ~0U; |
| 3249 | unsigned HOST_WIDE_INT this_end = end; |
| 3250 | unsigned HOST_WIDE_INT this_bitregion_start |
| 3251 | = new_bitregion_start; |
| 3252 | unsigned HOST_WIDE_INT this_bitregion_end |
| 3253 | = new_bitregion_end; |
| 3254 | k = i; |
| 3255 | first_nonmergeable_order = ~0U; |
| 3256 | for (unsigned int j = i + 1; j < len; ++j) |
| 3257 | { |
| 3258 | store_immediate_info *info2 = m_store_info[j]; |
| 3259 | if (info2->bitpos >= this_end) |
| 3260 | break; |
| 3261 | if (info2->order < try_order) |
| 3262 | { |
| 3263 | if (info2->rhs_code != INTEGER_CST |
| 3264 | || info2->lp_nr != merged_store->lp_nr) |
| 3265 | { |
| 3266 | /* Normally check_no_overlap makes sure this |
| 3267 | doesn't happen, but if end grows below, |
| 3268 | then we need to process more stores than |
| 3269 | check_no_overlap verified. Example: |
| 3270 | MEM[(int *)p_5] = 0; |
| 3271 | MEM[(short *)p_5 + 3B] = 1; |
| 3272 | MEM[(char *)p_5 + 4B] = _9; |
| 3273 | MEM[(char *)p_5 + 2B] = 2; */ |
| 3274 | k = 0; |
| 3275 | break; |
| 3276 | } |
| 3277 | if (info2->bitregion_start |
| 3278 | < this_bitregion_start) |
| 3279 | this_bitregion_start = info2->bitregion_start; |
| 3280 | if (info2->bitregion_end |
| 3281 | > this_bitregion_end) |
| 3282 | this_bitregion_end = info2->bitregion_end; |
| 3283 | if (((this_bitregion_end - this_bitregion_start |
| 3284 | + 1) / BITS_PER_UNIT) |
| 3285 | > (unsigned) param_store_merging_max_size) |
| 3286 | { |
| 3287 | k = 0; |
| 3288 | break; |
| 3289 | } |
| 3290 | k = j; |
| 3291 | min_order = MIN (min_order, info2->order); |
| 3292 | this_end = MAX (this_end, |
| 3293 | info2->bitpos + info2->bitsize); |
| 3294 | } |
| 3295 | else if (info2->rhs_code == INTEGER_CST |
| 3296 | && info2->lp_nr == merged_store->lp_nr |
| 3297 | && !last_iter) |
| 3298 | { |
| 3299 | max_order = MAX (max_order, info2->order + 1); |
| 3300 | first_nonmergeable_int_order |
| 3301 | = MIN (first_nonmergeable_int_order, |
| 3302 | info2->order); |
| 3303 | } |
| 3304 | else |
| 3305 | first_nonmergeable_order |
| 3306 | = MIN (first_nonmergeable_order, info2->order); |
| 3307 | } |
| 3308 | if (k > i |
| 3309 | && !check_no_overlap (m_store_info, len - 1, true, |
| 3310 | min_order, try_order, |
| 3311 | merged_store->start, this_end, |
| 3312 | first_earlier, end_earlier)) |
| 3313 | k = 0; |
| 3314 | if (k == 0) |
| 3315 | { |
| 3316 | if (last_order == try_order) |
| 3317 | break; |
| 3318 | /* If this failed, but only because we grew |
| 3319 | try_order, retry with the last working one, |
| 3320 | so that we merge at least something. */ |
| 3321 | try_order = last_order; |
| 3322 | last_iter = true; |
| 3323 | continue; |
| 3324 | } |
| 3325 | last_order = try_order; |
| 3326 | /* Retry with a larger try_order to see if we could |
| 3327 | merge some further INTEGER_CST stores. */ |
| 3328 | if (max_order |
| 3329 | && (first_nonmergeable_int_order |
| 3330 | < first_nonmergeable_order)) |
| 3331 | { |
| 3332 | try_order = MIN (max_order, |
| 3333 | first_nonmergeable_order); |
| 3334 | try_order |
| 3335 | = MIN (try_order, |
| 3336 | merged_store->first_nonmergeable_order); |
| 3337 | if (try_order > last_order && ++attempts < 16) |
| 3338 | continue; |
| 3339 | } |
| 3340 | first_nonmergeable_order |
| 3341 | = MIN (first_nonmergeable_order, |
| 3342 | first_nonmergeable_int_order); |
| 3343 | end = this_end; |
| 3344 | break; |
| 3345 | } |
| 3346 | while (1); |
| 3347 | |
| 3348 | if (k != 0) |
| 3349 | { |
| 3350 | merged_store->merge_overlapping (info); |
| 3351 | |
| 3352 | merged_store->first_nonmergeable_order |
| 3353 | = MIN (merged_store->first_nonmergeable_order, |
| 3354 | first_nonmergeable_order); |
| 3355 | |
| 3356 | for (unsigned int j = i + 1; j <= k; j++) |
| 3357 | { |
| 3358 | store_immediate_info *info2 = m_store_info[j]; |
| 3359 | gcc_assert (info2->bitpos < end); |
| 3360 | if (info2->order < last_order) |
| 3361 | { |
| 3362 | gcc_assert (info2->rhs_code == INTEGER_CST); |
| 3363 | if (info != info2) |
| 3364 | merged_store->merge_overlapping (info2); |
| 3365 | } |
| 3366 | /* Other stores are kept and not merged in any |
| 3367 | way. */ |
| 3368 | } |
| 3369 | ignore = k; |
| 3370 | goto done; |
| 3371 | } |
| 3372 | } |
| 3373 | } |
| 3374 | } |
| 3375 | /* |---store 1---||---store 2---| |
| 3376 | This store is consecutive to the previous one. |
| 3377 | Merge it into the current store group. There can be gaps in between |
| 3378 | the stores, but there can't be gaps in between bitregions. */ |
| 3379 | else if (info->bitregion_start <= merged_store->bitregion_end |
| 3380 | && merged_store->can_be_merged_into (info)) |
| 3381 | { |
| 3382 | store_immediate_info *infof = merged_store->stores[0]; |
| 3383 | |
| 3384 | /* All the rhs_code ops that take 2 operands are commutative, |
| 3385 | swap the operands if it could make the operands compatible. */ |
| 3386 | if (infof->ops[0].base_addr |
| 3387 | && infof->ops[1].base_addr |
| 3388 | && info->ops[0].base_addr |
| 3389 | && info->ops[1].base_addr |
| 3390 | && known_eq (info->ops[1].bitpos - infof->ops[0].bitpos, |
| 3391 | info->bitpos - infof->bitpos) |
| 3392 | && operand_equal_p (info->ops[1].base_addr, |
| 3393 | infof->ops[0].base_addr, 0)) |
| 3394 | { |
| 3395 | std::swap (info->ops[0], info->ops[1]); |
| 3396 | info->ops_swapped_p = true; |
| 3397 | } |
| 3398 | if (check_no_overlap (m_store_info, i, false, |
| 3399 | MIN (merged_store->first_order, info->order), |
| 3400 | MAX (merged_store->last_order, info->order), |
| 3401 | merged_store->start, |
| 3402 | MAX (merged_store->start + merged_store->width, |
| 3403 | info->bitpos + info->bitsize), |
| 3404 | first_earlier, end_earlier)) |
| 3405 | { |
| 3406 | /* Turn MEM_REF into BIT_INSERT_EXPR for bit-field stores. */ |
| 3407 | if (info->rhs_code == MEM_REF && infof->rhs_code != MEM_REF) |
| 3408 | { |
| 3409 | info->rhs_code = BIT_INSERT_EXPR; |
| 3410 | info->ops[0].val = gimple_assign_rhs1 (info->stmt); |
| 3411 | info->ops[0].base_addr = NULL_TREE; |
| 3412 | } |
| 3413 | else if (infof->rhs_code == MEM_REF && info->rhs_code != MEM_REF) |
| 3414 | { |
| 3415 | for (store_immediate_info *infoj : merged_store->stores) |
| 3416 | { |
| 3417 | infoj->rhs_code = BIT_INSERT_EXPR; |
| 3418 | infoj->ops[0].val = gimple_assign_rhs1 (infoj->stmt); |
| 3419 | infoj->ops[0].base_addr = NULL_TREE; |
| 3420 | } |
| 3421 | merged_store->bit_insertion = true; |
| 3422 | } |
| 3423 | if ((infof->ops[0].base_addr |
| 3424 | ? compatible_load_p (merged_store, info, base_addr, 0) |
| 3425 | : !info->ops[0].base_addr) |
| 3426 | && (infof->ops[1].base_addr |
| 3427 | ? compatible_load_p (merged_store, info, base_addr, 1) |
| 3428 | : !info->ops[1].base_addr)) |
| 3429 | { |
| 3430 | merged_store->merge_into (info); |
| 3431 | goto done; |
| 3432 | } |
| 3433 | } |
| 3434 | } |
| 3435 | |
| 3436 | /* |---store 1---| <gap> |---store 2---|. |
| 3437 | Gap between stores or the rhs not compatible. Start a new group. */ |
| 3438 | |
| 3439 | /* Try to apply all the stores recorded for the group to determine |
| 3440 | the bitpattern they write and discard it if that fails. |
| 3441 | This will also reject single-store groups. */ |
| 3442 | if (merged_store->apply_stores ()) |
| 3443 | m_merged_store_groups.safe_push (merged_store); |
| 3444 | else |
| 3445 | delete merged_store; |
| 3446 | |
| 3447 | merged_store = new merged_store_group (info); |
| 3448 | end_earlier = i; |
| 3449 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 3450 | fputs ("New store group\n", dump_file); |
| 3451 | |
| 3452 | done: |
| 3453 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 3454 | { |
| 3455 | fprintf (dump_file, "Store %u:\nbitsize:" HOST_WIDE_INT_PRINT_DEC |
| 3456 | " bitpos:" HOST_WIDE_INT_PRINT_DEC " val:", |
| 3457 | i, info->bitsize, info->bitpos); |
| 3458 | print_generic_expr (dump_file, gimple_assign_rhs1 (info->stmt)); |
| 3459 | fputc ('\n', dump_file); |
| 3460 | } |
| 3461 | } |
| 3462 | |
| 3463 | /* Record or discard the last store group. */ |
| 3464 | if (merged_store) |
| 3465 | { |
| 3466 | if (merged_store->apply_stores ()) |
| 3467 | m_merged_store_groups.safe_push (merged_store); |
| 3468 | else |
| 3469 | delete merged_store; |
| 3470 | } |
| 3471 | |
| 3472 | gcc_assert (m_merged_store_groups.length () <= m_store_info.length ()); |
| 3473 | |
| 3474 | bool success |
| 3475 | = !m_merged_store_groups.is_empty () |
| 3476 | && m_merged_store_groups.length () < m_store_info.length (); |
| 3477 | |
| 3478 | if (success && dump_file) |
| 3479 | fprintf (dump_file, "Coalescing successful!\nMerged into %u stores\n", |
| 3480 | m_merged_store_groups.length ()); |
| 3481 | |
| 3482 | return success; |
| 3483 | } |
| 3484 | |
| 3485 | /* Return the type to use for the merged stores or loads described by STMTS. |
| 3486 | This is needed to get the alias sets right. If IS_LOAD, look for rhs, |
| 3487 | otherwise lhs. Additionally set *CLIQUEP and *BASEP to MR_DEPENDENCE_* |
| 3488 | of the MEM_REFs if any. */ |
| 3489 | |
| 3490 | static tree |
| 3491 | get_alias_type_for_stmts (vec<gimple *> &stmts, bool is_load, |
| 3492 | unsigned short *cliquep, unsigned short *basep) |
| 3493 | { |
| 3494 | gimple *stmt; |
| 3495 | unsigned int i; |
| 3496 | tree type = NULL_TREE; |
| 3497 | tree ret = NULL_TREE; |
| 3498 | *cliquep = 0; |
| 3499 | *basep = 0; |
| 3500 | |
| 3501 | FOR_EACH_VEC_ELT (stmts, i, stmt) |
| 3502 | { |
| 3503 | tree ref = is_load ? gimple_assign_rhs1 (stmt) |
| 3504 | : gimple_assign_lhs (stmt); |
| 3505 | tree type1 = reference_alias_ptr_type (ref); |
| 3506 | tree base = get_base_address (ref); |
| 3507 | |
| 3508 | if (i == 0) |
| 3509 | { |
| 3510 | if (TREE_CODE (base) == MEM_REF) |
| 3511 | { |
| 3512 | *cliquep = MR_DEPENDENCE_CLIQUE (base); |
| 3513 | *basep = MR_DEPENDENCE_BASE (base); |
| 3514 | } |
| 3515 | ret = type = type1; |
| 3516 | continue; |
| 3517 | } |
| 3518 | if (!alias_ptr_types_compatible_p (type, type1)) |
| 3519 | ret = ptr_type_node; |
| 3520 | if (TREE_CODE (base) != MEM_REF |
| 3521 | || *cliquep != MR_DEPENDENCE_CLIQUE (base) |
| 3522 | || *basep != MR_DEPENDENCE_BASE (base)) |
| 3523 | { |
| 3524 | *cliquep = 0; |
| 3525 | *basep = 0; |
| 3526 | } |
| 3527 | } |
| 3528 | return ret; |
| 3529 | } |
| 3530 | |
| 3531 | /* Return the location_t information we can find among the statements |
| 3532 | in STMTS. */ |
| 3533 | |
| 3534 | static location_t |
| 3535 | get_location_for_stmts (vec<gimple *> &stmts) |
| 3536 | { |
| 3537 | for (gimple *stmt : stmts) |
| 3538 | if (gimple_has_location (stmt)) |
| 3539 | return gimple_location (stmt); |
| 3540 | |
| 3541 | return UNKNOWN_LOCATION; |
| 3542 | } |
| 3543 | |
| 3544 | /* Used to decribe a store resulting from splitting a wide store in smaller |
| 3545 | regularly-sized stores in split_group. */ |
| 3546 | |
| 3547 | class split_store |
| 3548 | { |
| 3549 | public: |
| 3550 | unsigned HOST_WIDE_INT bytepos; |
| 3551 | unsigned HOST_WIDE_INT size; |
| 3552 | unsigned HOST_WIDE_INT align; |
| 3553 | auto_vec<store_immediate_info *> orig_stores; |
| 3554 | /* True if there is a single orig stmt covering the whole split store. */ |
| 3555 | bool orig; |
| 3556 | split_store (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT, |
| 3557 | unsigned HOST_WIDE_INT); |
| 3558 | }; |
| 3559 | |
| 3560 | /* Simple constructor. */ |
| 3561 | |
| 3562 | split_store::split_store (unsigned HOST_WIDE_INT bp, |
| 3563 | unsigned HOST_WIDE_INT sz, |
| 3564 | unsigned HOST_WIDE_INT al) |
| 3565 | : bytepos (bp), size (sz), align (al), orig (false) |
| 3566 | { |
| 3567 | orig_stores.create (0); |
| 3568 | } |
| 3569 | |
| 3570 | /* Record all stores in GROUP that write to the region starting at BITPOS and |
| 3571 | is of size BITSIZE. Record infos for such statements in STORES if |
| 3572 | non-NULL. The stores in GROUP must be sorted by bitposition. Return INFO |
| 3573 | if there is exactly one original store in the range (in that case ignore |
| 3574 | clobber stmts, unless there are only clobber stmts). */ |
| 3575 | |
| 3576 | static store_immediate_info * |
| 3577 | find_constituent_stores (class merged_store_group *group, |
| 3578 | vec<store_immediate_info *> *stores, |
| 3579 | unsigned int *first, |
| 3580 | unsigned HOST_WIDE_INT bitpos, |
| 3581 | unsigned HOST_WIDE_INT bitsize) |
| 3582 | { |
| 3583 | store_immediate_info *info, *ret = NULL; |
| 3584 | unsigned int i; |
| 3585 | bool second = false; |
| 3586 | bool update_first = true; |
| 3587 | unsigned HOST_WIDE_INT end = bitpos + bitsize; |
| 3588 | for (i = *first; group->stores.iterate (i, &info); ++i) |
| 3589 | { |
| 3590 | unsigned HOST_WIDE_INT stmt_start = info->bitpos; |
| 3591 | unsigned HOST_WIDE_INT stmt_end = stmt_start + info->bitsize; |
| 3592 | if (stmt_end <= bitpos) |
| 3593 | { |
| 3594 | /* BITPOS passed to this function never decreases from within the |
| 3595 | same split_group call, so optimize and don't scan info records |
| 3596 | which are known to end before or at BITPOS next time. |
| 3597 | Only do it if all stores before this one also pass this. */ |
| 3598 | if (update_first) |
| 3599 | *first = i + 1; |
| 3600 | continue; |
| 3601 | } |
| 3602 | else |
| 3603 | update_first = false; |
| 3604 | |
| 3605 | /* The stores in GROUP are ordered by bitposition so if we're past |
| 3606 | the region for this group return early. */ |
| 3607 | if (stmt_start >= end) |
| 3608 | return ret; |
| 3609 | |
| 3610 | if (gimple_clobber_p (info->stmt)) |
| 3611 | { |
| 3612 | if (stores) |
| 3613 | stores->safe_push (info); |
| 3614 | if (ret == NULL) |
| 3615 | ret = info; |
| 3616 | continue; |
| 3617 | } |
| 3618 | if (stores) |
| 3619 | { |
| 3620 | stores->safe_push (info); |
| 3621 | if (ret && !gimple_clobber_p (ret->stmt)) |
| 3622 | { |
| 3623 | ret = NULL; |
| 3624 | second = true; |
| 3625 | } |
| 3626 | } |
| 3627 | else if (ret && !gimple_clobber_p (ret->stmt)) |
| 3628 | return NULL; |
| 3629 | if (!second) |
| 3630 | ret = info; |
| 3631 | } |
| 3632 | return ret; |
| 3633 | } |
| 3634 | |
| 3635 | /* Return how many SSA_NAMEs used to compute value to store in the INFO |
| 3636 | store have multiple uses. If any SSA_NAME has multiple uses, also |
| 3637 | count statements needed to compute it. */ |
| 3638 | |
| 3639 | static unsigned |
| 3640 | count_multiple_uses (store_immediate_info *info) |
| 3641 | { |
| 3642 | gimple *stmt = info->stmt; |
| 3643 | unsigned ret = 0; |
| 3644 | switch (info->rhs_code) |
| 3645 | { |
| 3646 | case INTEGER_CST: |
| 3647 | case STRING_CST: |
| 3648 | return 0; |
| 3649 | case BIT_AND_EXPR: |
| 3650 | case BIT_IOR_EXPR: |
| 3651 | case BIT_XOR_EXPR: |
| 3652 | if (info->bit_not_p) |
| 3653 | { |
| 3654 | if (!has_single_use (gimple_assign_rhs1 (stmt))) |
| 3655 | ret = 1; /* Fall through below to return |
| 3656 | the BIT_NOT_EXPR stmt and then |
| 3657 | BIT_{AND,IOR,XOR}_EXPR and anything it |
| 3658 | uses. */ |
| 3659 | else |
| 3660 | /* stmt is after this the BIT_NOT_EXPR. */ |
| 3661 | stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)); |
| 3662 | } |
| 3663 | if (!has_single_use (gimple_assign_rhs1 (stmt))) |
| 3664 | { |
| 3665 | ret += 1 + info->ops[0].bit_not_p; |
| 3666 | if (info->ops[1].base_addr) |
| 3667 | ret += 1 + info->ops[1].bit_not_p; |
| 3668 | return ret + 1; |
| 3669 | } |
| 3670 | stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)); |
| 3671 | /* stmt is now the BIT_*_EXPR. */ |
| 3672 | if (!has_single_use (gimple_assign_rhs1 (stmt))) |
| 3673 | ret += 1 + info->ops[info->ops_swapped_p].bit_not_p; |
| 3674 | else if (info->ops[info->ops_swapped_p].bit_not_p) |
| 3675 | { |
| 3676 | gimple *stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)); |
| 3677 | if (!has_single_use (gimple_assign_rhs1 (stmt2))) |
| 3678 | ++ret; |
| 3679 | } |
| 3680 | if (info->ops[1].base_addr == NULL_TREE) |
| 3681 | { |
| 3682 | gcc_checking_assert (!info->ops_swapped_p); |
| 3683 | return ret; |
| 3684 | } |
| 3685 | if (!has_single_use (gimple_assign_rhs2 (stmt))) |
| 3686 | ret += 1 + info->ops[1 - info->ops_swapped_p].bit_not_p; |
| 3687 | else if (info->ops[1 - info->ops_swapped_p].bit_not_p) |
| 3688 | { |
| 3689 | gimple *stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt)); |
| 3690 | if (!has_single_use (gimple_assign_rhs1 (stmt2))) |
| 3691 | ++ret; |
| 3692 | } |
| 3693 | return ret; |
| 3694 | case MEM_REF: |
| 3695 | if (!has_single_use (gimple_assign_rhs1 (stmt))) |
| 3696 | return 1 + info->ops[0].bit_not_p; |
| 3697 | else if (info->ops[0].bit_not_p) |
| 3698 | { |
| 3699 | stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)); |
| 3700 | if (!has_single_use (gimple_assign_rhs1 (stmt))) |
| 3701 | return 1; |
| 3702 | } |
| 3703 | return 0; |
| 3704 | case BIT_INSERT_EXPR: |
| 3705 | return has_single_use (gimple_assign_rhs1 (stmt)) ? 0 : 1; |
| 3706 | default: |
| 3707 | gcc_unreachable (); |
| 3708 | } |
| 3709 | } |
| 3710 | |
| 3711 | /* Split a merged store described by GROUP by populating the SPLIT_STORES |
| 3712 | vector (if non-NULL) with split_store structs describing the byte offset |
| 3713 | (from the base), the bit size and alignment of each store as well as the |
| 3714 | original statements involved in each such split group. |
| 3715 | This is to separate the splitting strategy from the statement |
| 3716 | building/emission/linking done in output_merged_store. |
| 3717 | Return number of new stores. |
| 3718 | If ALLOW_UNALIGNED_STORE is false, then all stores must be aligned. |
| 3719 | If ALLOW_UNALIGNED_LOAD is false, then all loads must be aligned. |
| 3720 | BZERO_FIRST may be true only when the first store covers the whole group |
| 3721 | and clears it; if BZERO_FIRST is true, keep that first store in the set |
| 3722 | unmodified and emit further stores for the overrides only. |
| 3723 | If SPLIT_STORES is NULL, it is just a dry run to count number of |
| 3724 | new stores. */ |
| 3725 | |
| 3726 | static unsigned int |
| 3727 | split_group (merged_store_group *group, bool allow_unaligned_store, |
| 3728 | bool allow_unaligned_load, bool bzero_first, |
| 3729 | vec<split_store *> *split_stores, |
| 3730 | unsigned *total_orig, |
| 3731 | unsigned *total_new) |
| 3732 | { |
| 3733 | unsigned HOST_WIDE_INT pos = group->bitregion_start; |
| 3734 | unsigned HOST_WIDE_INT size = group->bitregion_end - pos; |
| 3735 | unsigned HOST_WIDE_INT bytepos = pos / BITS_PER_UNIT; |
| 3736 | unsigned HOST_WIDE_INT group_align = group->align; |
| 3737 | unsigned HOST_WIDE_INT align_base = group->align_base; |
| 3738 | unsigned HOST_WIDE_INT group_load_align = group_align; |
| 3739 | bool any_orig = false; |
| 3740 | |
| 3741 | gcc_assert ((size % BITS_PER_UNIT == 0) && (pos % BITS_PER_UNIT == 0)); |
| 3742 | |
| 3743 | /* For bswap framework using sets of stores, all the checking has been done |
| 3744 | earlier in try_coalesce_bswap and the result always needs to be emitted |
| 3745 | as a single store. Likewise for string concatenation. */ |
| 3746 | if (group->stores[0]->rhs_code == LROTATE_EXPR |
| 3747 | || group->stores[0]->rhs_code == NOP_EXPR |
| 3748 | || group->string_concatenation) |
| 3749 | { |
| 3750 | gcc_assert (!bzero_first); |
| 3751 | if (total_orig) |
| 3752 | { |
| 3753 | /* Avoid the old/new stmt count heuristics. It should be |
| 3754 | always beneficial. */ |
| 3755 | total_new[0] = 1; |
| 3756 | total_orig[0] = 2; |
| 3757 | } |
| 3758 | |
| 3759 | if (split_stores) |
| 3760 | { |
| 3761 | unsigned HOST_WIDE_INT align_bitpos |
| 3762 | = (group->start - align_base) & (group_align - 1); |
| 3763 | unsigned HOST_WIDE_INT align = group_align; |
| 3764 | if (align_bitpos) |
| 3765 | align = least_bit_hwi (align_bitpos); |
| 3766 | bytepos = group->start / BITS_PER_UNIT; |
| 3767 | split_store *store |
| 3768 | = new split_store (bytepos, group->width, align); |
| 3769 | unsigned int first = 0; |
| 3770 | find_constituent_stores (group, &store->orig_stores, |
| 3771 | &first, group->start, group->width); |
| 3772 | split_stores->safe_push (store); |
| 3773 | } |
| 3774 | |
| 3775 | return 1; |
| 3776 | } |
| 3777 | |
| 3778 | unsigned int ret = 0, first = 0; |
| 3779 | unsigned HOST_WIDE_INT try_pos = bytepos; |
| 3780 | |
| 3781 | if (total_orig) |
| 3782 | { |
| 3783 | unsigned int i; |
| 3784 | store_immediate_info *info = group->stores[0]; |
| 3785 | |
| 3786 | total_new[0] = 0; |
| 3787 | total_orig[0] = 1; /* The orig store. */ |
| 3788 | info = group->stores[0]; |
| 3789 | if (info->ops[0].base_addr) |
| 3790 | total_orig[0]++; |
| 3791 | if (info->ops[1].base_addr) |
| 3792 | total_orig[0]++; |
| 3793 | switch (info->rhs_code) |
| 3794 | { |
| 3795 | case BIT_AND_EXPR: |
| 3796 | case BIT_IOR_EXPR: |
| 3797 | case BIT_XOR_EXPR: |
| 3798 | total_orig[0]++; /* The orig BIT_*_EXPR stmt. */ |
| 3799 | break; |
| 3800 | default: |
| 3801 | break; |
| 3802 | } |
| 3803 | total_orig[0] *= group->stores.length (); |
| 3804 | |
| 3805 | FOR_EACH_VEC_ELT (group->stores, i, info) |
| 3806 | { |
| 3807 | total_new[0] += count_multiple_uses (info); |
| 3808 | total_orig[0] += (info->bit_not_p |
| 3809 | + info->ops[0].bit_not_p |
| 3810 | + info->ops[1].bit_not_p); |
| 3811 | } |
| 3812 | } |
| 3813 | |
| 3814 | if (!allow_unaligned_load) |
| 3815 | for (int i = 0; i < 2; ++i) |
| 3816 | if (group->load_align[i]) |
| 3817 | group_load_align = MIN (group_load_align, group->load_align[i]); |
| 3818 | |
| 3819 | if (bzero_first) |
| 3820 | { |
| 3821 | store_immediate_info *gstore; |
| 3822 | FOR_EACH_VEC_ELT (group->stores, first, gstore) |
| 3823 | if (!gimple_clobber_p (gstore->stmt)) |
| 3824 | break; |
| 3825 | ++first; |
| 3826 | ret = 1; |
| 3827 | if (split_stores) |
| 3828 | { |
| 3829 | split_store *store |
| 3830 | = new split_store (bytepos, gstore->bitsize, align_base); |
| 3831 | store->orig_stores.safe_push (gstore); |
| 3832 | store->orig = true; |
| 3833 | any_orig = true; |
| 3834 | split_stores->safe_push (store); |
| 3835 | } |
| 3836 | } |
| 3837 | |
| 3838 | while (size > 0) |
| 3839 | { |
| 3840 | if ((allow_unaligned_store || group_align <= BITS_PER_UNIT) |
| 3841 | && (group->mask[try_pos - bytepos] == (unsigned char) ~0U |
| 3842 | || (bzero_first && group->val[try_pos - bytepos] == 0))) |
| 3843 | { |
| 3844 | /* Skip padding bytes. */ |
| 3845 | ++try_pos; |
| 3846 | size -= BITS_PER_UNIT; |
| 3847 | continue; |
| 3848 | } |
| 3849 | |
| 3850 | unsigned HOST_WIDE_INT try_bitpos = try_pos * BITS_PER_UNIT; |
| 3851 | unsigned int try_size = MAX_STORE_BITSIZE, nonmasked; |
| 3852 | unsigned HOST_WIDE_INT align_bitpos |
| 3853 | = (try_bitpos - align_base) & (group_align - 1); |
| 3854 | unsigned HOST_WIDE_INT align = group_align; |
| 3855 | bool found_orig = false; |
| 3856 | if (align_bitpos) |
| 3857 | align = least_bit_hwi (align_bitpos); |
| 3858 | if (!allow_unaligned_store) |
| 3859 | try_size = MIN (try_size, align); |
| 3860 | if (!allow_unaligned_load) |
| 3861 | { |
| 3862 | /* If we can't do or don't want to do unaligned stores |
| 3863 | as well as loads, we need to take the loads into account |
| 3864 | as well. */ |
| 3865 | unsigned HOST_WIDE_INT load_align = group_load_align; |
| 3866 | align_bitpos = (try_bitpos - align_base) & (load_align - 1); |
| 3867 | if (align_bitpos) |
| 3868 | load_align = least_bit_hwi (align_bitpos); |
| 3869 | for (int i = 0; i < 2; ++i) |
| 3870 | if (group->load_align[i]) |
| 3871 | { |
| 3872 | align_bitpos |
| 3873 | = known_alignment (try_bitpos |
| 3874 | - group->stores[0]->bitpos |
| 3875 | + group->stores[0]->ops[i].bitpos |
| 3876 | - group->load_align_base[i]); |
| 3877 | if (align_bitpos & (group_load_align - 1)) |
| 3878 | { |
| 3879 | unsigned HOST_WIDE_INT a = least_bit_hwi (align_bitpos); |
| 3880 | load_align = MIN (load_align, a); |
| 3881 | } |
| 3882 | } |
| 3883 | try_size = MIN (try_size, load_align); |
| 3884 | } |
| 3885 | store_immediate_info *info |
| 3886 | = find_constituent_stores (group, NULL, &first, try_bitpos, try_size); |
| 3887 | if (info && !gimple_clobber_p (info->stmt)) |
| 3888 | { |
| 3889 | /* If there is just one original statement for the range, see if |
| 3890 | we can just reuse the original store which could be even larger |
| 3891 | than try_size. */ |
| 3892 | unsigned HOST_WIDE_INT stmt_end |
| 3893 | = ROUND_UP (info->bitpos + info->bitsize, BITS_PER_UNIT); |
| 3894 | info = find_constituent_stores (group, NULL, &first, try_bitpos, |
| 3895 | stmt_end - try_bitpos); |
| 3896 | if (info && info->bitpos >= try_bitpos) |
| 3897 | { |
| 3898 | store_immediate_info *info2 = NULL; |
| 3899 | unsigned int first_copy = first; |
| 3900 | if (info->bitpos > try_bitpos |
| 3901 | && stmt_end - try_bitpos <= try_size) |
| 3902 | { |
| 3903 | info2 = find_constituent_stores (group, NULL, &first_copy, |
| 3904 | try_bitpos, |
| 3905 | info->bitpos - try_bitpos); |
| 3906 | gcc_assert (info2 == NULL || gimple_clobber_p (info2->stmt)); |
| 3907 | } |
| 3908 | if (info2 == NULL && stmt_end - try_bitpos < try_size) |
| 3909 | { |
| 3910 | info2 = find_constituent_stores (group, NULL, &first_copy, |
| 3911 | stmt_end, |
| 3912 | (try_bitpos + try_size) |
| 3913 | - stmt_end); |
| 3914 | gcc_assert (info2 == NULL || gimple_clobber_p (info2->stmt)); |
| 3915 | } |
| 3916 | if (info2 == NULL) |
| 3917 | { |
| 3918 | try_size = stmt_end - try_bitpos; |
| 3919 | found_orig = true; |
| 3920 | goto found; |
| 3921 | } |
| 3922 | } |
| 3923 | } |
| 3924 | |
| 3925 | /* Approximate store bitsize for the case when there are no padding |
| 3926 | bits. */ |
| 3927 | while (try_size > size) |
| 3928 | try_size /= 2; |
| 3929 | /* Now look for whole padding bytes at the end of that bitsize. */ |
| 3930 | for (nonmasked = try_size / BITS_PER_UNIT; nonmasked > 0; --nonmasked) |
| 3931 | if (group->mask[try_pos - bytepos + nonmasked - 1] |
| 3932 | != (unsigned char) ~0U |
| 3933 | && (!bzero_first |
| 3934 | || group->val[try_pos - bytepos + nonmasked - 1] != 0)) |
| 3935 | break; |
| 3936 | if (nonmasked == 0 || (info && gimple_clobber_p (info->stmt))) |
| 3937 | { |
| 3938 | /* If entire try_size range is padding, skip it. */ |
| 3939 | try_pos += try_size / BITS_PER_UNIT; |
| 3940 | size -= try_size; |
| 3941 | continue; |
| 3942 | } |
| 3943 | /* Otherwise try to decrease try_size if second half, last 3 quarters |
| 3944 | etc. are padding. */ |
| 3945 | nonmasked *= BITS_PER_UNIT; |
| 3946 | while (nonmasked <= try_size / 2) |
| 3947 | try_size /= 2; |
| 3948 | if (!allow_unaligned_store && group_align > BITS_PER_UNIT) |
| 3949 | { |
| 3950 | /* Now look for whole padding bytes at the start of that bitsize. */ |
| 3951 | unsigned int try_bytesize = try_size / BITS_PER_UNIT, masked; |
| 3952 | for (masked = 0; masked < try_bytesize; ++masked) |
| 3953 | if (group->mask[try_pos - bytepos + masked] != (unsigned char) ~0U |
| 3954 | && (!bzero_first |
| 3955 | || group->val[try_pos - bytepos + masked] != 0)) |
| 3956 | break; |
| 3957 | masked *= BITS_PER_UNIT; |
| 3958 | gcc_assert (masked < try_size); |
| 3959 | if (masked >= try_size / 2) |
| 3960 | { |
| 3961 | while (masked >= try_size / 2) |
| 3962 | { |
| 3963 | try_size /= 2; |
| 3964 | try_pos += try_size / BITS_PER_UNIT; |
| 3965 | size -= try_size; |
| 3966 | masked -= try_size; |
| 3967 | } |
| 3968 | /* Need to recompute the alignment, so just retry at the new |
| 3969 | position. */ |
| 3970 | continue; |
| 3971 | } |
| 3972 | } |
| 3973 | |
| 3974 | found: |
| 3975 | ++ret; |
| 3976 | |
| 3977 | if (split_stores) |
| 3978 | { |
| 3979 | split_store *store |
| 3980 | = new split_store (try_pos, try_size, align); |
| 3981 | info = find_constituent_stores (group, &store->orig_stores, |
| 3982 | &first, try_bitpos, try_size); |
| 3983 | if (info |
| 3984 | && !gimple_clobber_p (info->stmt) |
| 3985 | && info->bitpos >= try_bitpos |
| 3986 | && info->bitpos + info->bitsize <= try_bitpos + try_size |
| 3987 | && (store->orig_stores.length () == 1 |
| 3988 | || found_orig |
| 3989 | || (info->bitpos == try_bitpos |
| 3990 | && (info->bitpos + info->bitsize |
| 3991 | == try_bitpos + try_size)))) |
| 3992 | { |
| 3993 | store->orig = true; |
| 3994 | any_orig = true; |
| 3995 | } |
| 3996 | split_stores->safe_push (store); |
| 3997 | } |
| 3998 | |
| 3999 | try_pos += try_size / BITS_PER_UNIT; |
| 4000 | size -= try_size; |
| 4001 | } |
| 4002 | |
| 4003 | if (total_orig) |
| 4004 | { |
| 4005 | unsigned int i; |
| 4006 | split_store *store; |
| 4007 | /* If we are reusing some original stores and any of the |
| 4008 | original SSA_NAMEs had multiple uses, we need to subtract |
| 4009 | those now before we add the new ones. */ |
| 4010 | if (total_new[0] && any_orig) |
| 4011 | { |
| 4012 | FOR_EACH_VEC_ELT (*split_stores, i, store) |
| 4013 | if (store->orig) |
| 4014 | total_new[0] -= count_multiple_uses (store->orig_stores[0]); |
| 4015 | } |
| 4016 | total_new[0] += ret; /* The new store. */ |
| 4017 | store_immediate_info *info = group->stores[0]; |
| 4018 | if (info->ops[0].base_addr) |
| 4019 | total_new[0] += ret; |
| 4020 | if (info->ops[1].base_addr) |
| 4021 | total_new[0] += ret; |
| 4022 | switch (info->rhs_code) |
| 4023 | { |
| 4024 | case BIT_AND_EXPR: |
| 4025 | case BIT_IOR_EXPR: |
| 4026 | case BIT_XOR_EXPR: |
| 4027 | total_new[0] += ret; /* The new BIT_*_EXPR stmt. */ |
| 4028 | break; |
| 4029 | default: |
| 4030 | break; |
| 4031 | } |
| 4032 | FOR_EACH_VEC_ELT (*split_stores, i, store) |
| 4033 | { |
| 4034 | unsigned int j; |
| 4035 | bool bit_not_p[3] = { false, false, false }; |
| 4036 | /* If all orig_stores have certain bit_not_p set, then |
| 4037 | we'd use a BIT_NOT_EXPR stmt and need to account for it. |
| 4038 | If some orig_stores have certain bit_not_p set, then |
| 4039 | we'd use a BIT_XOR_EXPR with a mask and need to account for |
| 4040 | it. */ |
| 4041 | FOR_EACH_VEC_ELT (store->orig_stores, j, info) |
| 4042 | { |
| 4043 | if (info->ops[0].bit_not_p) |
| 4044 | bit_not_p[0] = true; |
| 4045 | if (info->ops[1].bit_not_p) |
| 4046 | bit_not_p[1] = true; |
| 4047 | if (info->bit_not_p) |
| 4048 | bit_not_p[2] = true; |
| 4049 | } |
| 4050 | total_new[0] += bit_not_p[0] + bit_not_p[1] + bit_not_p[2]; |
| 4051 | } |
| 4052 | |
| 4053 | } |
| 4054 | |
| 4055 | return ret; |
| 4056 | } |
| 4057 | |
| 4058 | /* Return the operation through which the operand IDX (if < 2) or |
| 4059 | result (IDX == 2) should be inverted. If NOP_EXPR, no inversion |
| 4060 | is done, if BIT_NOT_EXPR, all bits are inverted, if BIT_XOR_EXPR, |
| 4061 | the bits should be xored with mask. */ |
| 4062 | |
| 4063 | static enum tree_code |
| 4064 | invert_op (split_store *split_store, int idx, tree int_type, tree &mask) |
| 4065 | { |
| 4066 | unsigned int i; |
| 4067 | store_immediate_info *info; |
| 4068 | unsigned int cnt = 0; |
| 4069 | bool any_paddings = false; |
| 4070 | FOR_EACH_VEC_ELT (split_store->orig_stores, i, info) |
| 4071 | { |
| 4072 | bool bit_not_p = idx < 2 ? info->ops[idx].bit_not_p : info->bit_not_p; |
| 4073 | if (bit_not_p) |
| 4074 | { |
| 4075 | ++cnt; |
| 4076 | tree lhs = gimple_assign_lhs (info->stmt); |
| 4077 | if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)) |
| 4078 | && TYPE_PRECISION (TREE_TYPE (lhs)) < info->bitsize) |
| 4079 | any_paddings = true; |
| 4080 | } |
| 4081 | } |
| 4082 | mask = NULL_TREE; |
| 4083 | if (cnt == 0) |
| 4084 | return NOP_EXPR; |
| 4085 | if (cnt == split_store->orig_stores.length () && !any_paddings) |
| 4086 | return BIT_NOT_EXPR; |
| 4087 | |
| 4088 | unsigned HOST_WIDE_INT try_bitpos = split_store->bytepos * BITS_PER_UNIT; |
| 4089 | unsigned buf_size = split_store->size / BITS_PER_UNIT; |
| 4090 | unsigned char *buf |
| 4091 | = XALLOCAVEC (unsigned char, buf_size); |
| 4092 | memset (buf, ~0U, buf_size); |
| 4093 | FOR_EACH_VEC_ELT (split_store->orig_stores, i, info) |
| 4094 | { |
| 4095 | bool bit_not_p = idx < 2 ? info->ops[idx].bit_not_p : info->bit_not_p; |
| 4096 | if (!bit_not_p) |
| 4097 | continue; |
| 4098 | /* Clear regions with bit_not_p and invert afterwards, rather than |
| 4099 | clear regions with !bit_not_p, so that gaps in between stores aren't |
| 4100 | set in the mask. */ |
| 4101 | unsigned HOST_WIDE_INT bitsize = info->bitsize; |
| 4102 | unsigned HOST_WIDE_INT prec = bitsize; |
| 4103 | unsigned int pos_in_buffer = 0; |
| 4104 | if (any_paddings) |
| 4105 | { |
| 4106 | tree lhs = gimple_assign_lhs (info->stmt); |
| 4107 | if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)) |
| 4108 | && TYPE_PRECISION (TREE_TYPE (lhs)) < bitsize) |
| 4109 | prec = TYPE_PRECISION (TREE_TYPE (lhs)); |
| 4110 | } |
| 4111 | if (info->bitpos < try_bitpos) |
| 4112 | { |
| 4113 | gcc_assert (info->bitpos + bitsize > try_bitpos); |
| 4114 | if (!BYTES_BIG_ENDIAN) |
| 4115 | { |
| 4116 | if (prec <= try_bitpos - info->bitpos) |
| 4117 | continue; |
| 4118 | prec -= try_bitpos - info->bitpos; |
| 4119 | } |
| 4120 | bitsize -= try_bitpos - info->bitpos; |
| 4121 | if (BYTES_BIG_ENDIAN && prec > bitsize) |
| 4122 | prec = bitsize; |
| 4123 | } |
| 4124 | else |
| 4125 | pos_in_buffer = info->bitpos - try_bitpos; |
| 4126 | if (prec < bitsize) |
| 4127 | { |
| 4128 | /* If this is a bool inversion, invert just the least significant |
| 4129 | prec bits rather than all bits of it. */ |
| 4130 | if (BYTES_BIG_ENDIAN) |
| 4131 | { |
| 4132 | pos_in_buffer += bitsize - prec; |
| 4133 | if (pos_in_buffer >= split_store->size) |
| 4134 | continue; |
| 4135 | } |
| 4136 | bitsize = prec; |
| 4137 | } |
| 4138 | if (pos_in_buffer + bitsize > split_store->size) |
| 4139 | bitsize = split_store->size - pos_in_buffer; |
| 4140 | unsigned char *p = buf + (pos_in_buffer / BITS_PER_UNIT); |
| 4141 | if (BYTES_BIG_ENDIAN) |
| 4142 | clear_bit_region_be (p, (BITS_PER_UNIT - 1 |
| 4143 | - (pos_in_buffer % BITS_PER_UNIT)), bitsize); |
| 4144 | else |
| 4145 | clear_bit_region (p, pos_in_buffer % BITS_PER_UNIT, bitsize); |
| 4146 | } |
| 4147 | for (unsigned int i = 0; i < buf_size; ++i) |
| 4148 | buf[i] = ~buf[i]; |
| 4149 | mask = native_interpret_expr (int_type, buf, buf_size); |
| 4150 | return BIT_XOR_EXPR; |
| 4151 | } |
| 4152 | |
| 4153 | /* Given a merged store group GROUP output the widened version of it. |
| 4154 | The store chain is against the base object BASE. |
| 4155 | Try store sizes of at most MAX_STORE_BITSIZE bits wide and don't output |
| 4156 | unaligned stores for STRICT_ALIGNMENT targets or if it's too expensive. |
| 4157 | Make sure that the number of statements output is less than the number of |
| 4158 | original statements. If a better sequence is possible emit it and |
| 4159 | return true. */ |
| 4160 | |
| 4161 | bool |
| 4162 | imm_store_chain_info::output_merged_store (merged_store_group *group) |
| 4163 | { |
| 4164 | const unsigned HOST_WIDE_INT start_byte_pos |
| 4165 | = group->bitregion_start / BITS_PER_UNIT; |
| 4166 | unsigned int orig_num_stmts = group->stores.length (); |
| 4167 | if (orig_num_stmts < 2) |
| 4168 | return false; |
| 4169 | |
| 4170 | bool allow_unaligned_store |
| 4171 | = !STRICT_ALIGNMENT && param_store_merging_allow_unaligned; |
| 4172 | bool allow_unaligned_load = allow_unaligned_store; |
| 4173 | bool bzero_first = false; |
| 4174 | store_immediate_info *store; |
| 4175 | unsigned int num_clobber_stmts = 0; |
| 4176 | if (group->stores[0]->rhs_code == INTEGER_CST) |
| 4177 | { |
| 4178 | unsigned int i; |
| 4179 | FOR_EACH_VEC_ELT (group->stores, i, store) |
| 4180 | if (gimple_clobber_p (store->stmt)) |
| 4181 | num_clobber_stmts++; |
| 4182 | else if (TREE_CODE (gimple_assign_rhs1 (store->stmt)) == CONSTRUCTOR |
| 4183 | && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (store->stmt)) == 0 |
| 4184 | && group->start == store->bitpos |
| 4185 | && group->width == store->bitsize |
| 4186 | && (group->start % BITS_PER_UNIT) == 0 |
| 4187 | && (group->width % BITS_PER_UNIT) == 0) |
| 4188 | { |
| 4189 | bzero_first = true; |
| 4190 | break; |
| 4191 | } |
| 4192 | else |
| 4193 | break; |
| 4194 | FOR_EACH_VEC_ELT_FROM (group->stores, i, store, i) |
| 4195 | if (gimple_clobber_p (store->stmt)) |
| 4196 | num_clobber_stmts++; |
| 4197 | if (num_clobber_stmts == orig_num_stmts) |
| 4198 | return false; |
| 4199 | orig_num_stmts -= num_clobber_stmts; |
| 4200 | } |
| 4201 | if (allow_unaligned_store || bzero_first) |
| 4202 | { |
| 4203 | /* If unaligned stores are allowed, see how many stores we'd emit |
| 4204 | for unaligned and how many stores we'd emit for aligned stores. |
| 4205 | Only use unaligned stores if it allows fewer stores than aligned. |
| 4206 | Similarly, if there is a whole region clear first, prefer expanding |
| 4207 | it together compared to expanding clear first followed by merged |
| 4208 | further stores. */ |
| 4209 | unsigned cnt[4] = { ~0U, ~0U, ~0U, ~0U }; |
| 4210 | int pass_min = 0; |
| 4211 | for (int pass = 0; pass < 4; ++pass) |
| 4212 | { |
| 4213 | if (!allow_unaligned_store && (pass & 1) != 0) |
| 4214 | continue; |
| 4215 | if (!bzero_first && (pass & 2) != 0) |
| 4216 | continue; |
| 4217 | cnt[pass] = split_group (group, (pass & 1) != 0, |
| 4218 | allow_unaligned_load, (pass & 2) != 0, |
| 4219 | NULL, NULL, NULL); |
| 4220 | if (cnt[pass] < cnt[pass_min]) |
| 4221 | pass_min = pass; |
| 4222 | } |
| 4223 | if ((pass_min & 1) == 0) |
| 4224 | allow_unaligned_store = false; |
| 4225 | if ((pass_min & 2) == 0) |
| 4226 | bzero_first = false; |
| 4227 | } |
| 4228 | |
| 4229 | auto_vec<class split_store *, 32> split_stores; |
| 4230 | split_store *split_store; |
| 4231 | unsigned total_orig, total_new, i; |
| 4232 | split_group (group, allow_unaligned_store, allow_unaligned_load, bzero_first, |
| 4233 | &split_stores, &total_orig, &total_new); |
| 4234 | |
| 4235 | /* Determine if there is a clobber covering the whole group at the start, |
| 4236 | followed by proposed split stores that cover the whole group. In that |
| 4237 | case, prefer the transformation even if |
| 4238 | split_stores.length () == orig_num_stmts. */ |
| 4239 | bool clobber_first = false; |
| 4240 | if (num_clobber_stmts |
| 4241 | && gimple_clobber_p (group->stores[0]->stmt) |
| 4242 | && group->start == group->stores[0]->bitpos |
| 4243 | && group->width == group->stores[0]->bitsize |
| 4244 | && (group->start % BITS_PER_UNIT) == 0 |
| 4245 | && (group->width % BITS_PER_UNIT) == 0) |
| 4246 | { |
| 4247 | clobber_first = true; |
| 4248 | unsigned HOST_WIDE_INT pos = group->start / BITS_PER_UNIT; |
| 4249 | FOR_EACH_VEC_ELT (split_stores, i, split_store) |
| 4250 | if (split_store->bytepos != pos) |
| 4251 | { |
| 4252 | clobber_first = false; |
| 4253 | break; |
| 4254 | } |
| 4255 | else |
| 4256 | pos += split_store->size / BITS_PER_UNIT; |
| 4257 | if (pos != (group->start + group->width) / BITS_PER_UNIT) |
| 4258 | clobber_first = false; |
| 4259 | } |
| 4260 | |
| 4261 | if (split_stores.length () >= orig_num_stmts + clobber_first) |
| 4262 | { |
| 4263 | |
| 4264 | /* We didn't manage to reduce the number of statements. Bail out. */ |
| 4265 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 4266 | fprintf (dump_file, "Exceeded original number of stmts (%u)." |
| 4267 | " Not profitable to emit new sequence.\n", |
| 4268 | orig_num_stmts); |
| 4269 | FOR_EACH_VEC_ELT (split_stores, i, split_store) |
| 4270 | delete split_store; |
| 4271 | return false; |
| 4272 | } |
| 4273 | if (total_orig <= total_new) |
| 4274 | { |
| 4275 | /* If number of estimated new statements is above estimated original |
| 4276 | statements, bail out too. */ |
| 4277 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 4278 | fprintf (dump_file, "Estimated number of original stmts (%u)" |
| 4279 | " not larger than estimated number of new" |
| 4280 | " stmts (%u).\n", |
| 4281 | total_orig, total_new); |
| 4282 | FOR_EACH_VEC_ELT (split_stores, i, split_store) |
| 4283 | delete split_store; |
| 4284 | return false; |
| 4285 | } |
| 4286 | if (group->stores[0]->rhs_code == INTEGER_CST) |
| 4287 | { |
| 4288 | bool all_orig = true; |
| 4289 | FOR_EACH_VEC_ELT (split_stores, i, split_store) |
| 4290 | if (!split_store->orig) |
| 4291 | { |
| 4292 | all_orig = false; |
| 4293 | break; |
| 4294 | } |
| 4295 | if (all_orig) |
| 4296 | { |
| 4297 | unsigned int cnt = split_stores.length (); |
| 4298 | store_immediate_info *store; |
| 4299 | FOR_EACH_VEC_ELT (group->stores, i, store) |
| 4300 | if (gimple_clobber_p (store->stmt)) |
| 4301 | ++cnt; |
| 4302 | /* Punt if we wouldn't make any real changes, i.e. keep all |
| 4303 | orig stmts + all clobbers. */ |
| 4304 | if (cnt == group->stores.length ()) |
| 4305 | { |
| 4306 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 4307 | fprintf (dump_file, "Exceeded original number of stmts (%u)." |
| 4308 | " Not profitable to emit new sequence.\n", |
| 4309 | orig_num_stmts); |
| 4310 | FOR_EACH_VEC_ELT (split_stores, i, split_store) |
| 4311 | delete split_store; |
| 4312 | return false; |
| 4313 | } |
| 4314 | } |
| 4315 | } |
| 4316 | |
| 4317 | gimple_stmt_iterator last_gsi = gsi_for_stmt (group->last_stmt); |
| 4318 | gimple_seq seq = NULL; |
| 4319 | tree last_vdef, new_vuse; |
| 4320 | last_vdef = gimple_vdef (group->last_stmt); |
| 4321 | new_vuse = gimple_vuse (group->last_stmt); |
| 4322 | tree bswap_res = NULL_TREE; |
| 4323 | |
| 4324 | /* Clobbers are not removed. */ |
| 4325 | if (gimple_clobber_p (group->last_stmt)) |
| 4326 | { |
| 4327 | new_vuse = make_ssa_name (gimple_vop (cfun), group->last_stmt); |
| 4328 | gimple_set_vdef (group->last_stmt, new_vuse); |
| 4329 | } |
| 4330 | |
| 4331 | if (group->stores[0]->rhs_code == LROTATE_EXPR |
| 4332 | || group->stores[0]->rhs_code == NOP_EXPR) |
| 4333 | { |
| 4334 | tree fndecl = NULL_TREE, bswap_type = NULL_TREE, load_type; |
| 4335 | gimple *ins_stmt = group->stores[0]->ins_stmt; |
| 4336 | struct symbolic_number *n = &group->stores[0]->n; |
| 4337 | bool bswap = group->stores[0]->rhs_code == LROTATE_EXPR; |
| 4338 | |
| 4339 | switch (n->range) |
| 4340 | { |
| 4341 | case 16: |
| 4342 | load_type = bswap_type = uint16_type_node; |
| 4343 | break; |
| 4344 | case 32: |
| 4345 | load_type = uint32_type_node; |
| 4346 | if (bswap) |
| 4347 | { |
| 4348 | fndecl = builtin_decl_explicit (BUILT_IN_BSWAP32); |
| 4349 | bswap_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); |
| 4350 | } |
| 4351 | break; |
| 4352 | case 64: |
| 4353 | load_type = uint64_type_node; |
| 4354 | if (bswap) |
| 4355 | { |
| 4356 | fndecl = builtin_decl_explicit (BUILT_IN_BSWAP64); |
| 4357 | bswap_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); |
| 4358 | } |
| 4359 | break; |
| 4360 | default: |
| 4361 | gcc_unreachable (); |
| 4362 | } |
| 4363 | |
| 4364 | /* If the loads have each vuse of the corresponding store, |
| 4365 | we've checked the aliasing already in try_coalesce_bswap and |
| 4366 | we want to sink the need load into seq. So need to use new_vuse |
| 4367 | on the load. */ |
| 4368 | if (n->base_addr) |
| 4369 | { |
| 4370 | if (n->vuse == NULL) |
| 4371 | { |
| 4372 | n->vuse = new_vuse; |
| 4373 | ins_stmt = NULL; |
| 4374 | } |
| 4375 | else |
| 4376 | /* Update vuse in case it has changed by output_merged_stores. */ |
| 4377 | n->vuse = gimple_vuse (ins_stmt); |
| 4378 | } |
| 4379 | bswap_res = bswap_replace (gsi_start (seq), ins_stmt, fndecl, |
| 4380 | bswap_type, load_type, n, bswap, |
| 4381 | ~(uint64_t) 0, 0); |
| 4382 | gcc_assert (bswap_res); |
| 4383 | } |
| 4384 | |
| 4385 | gimple *stmt = NULL; |
| 4386 | auto_vec<gimple *, 32> orig_stmts; |
| 4387 | gimple_seq this_seq; |
| 4388 | tree addr = force_gimple_operand_1 (unshare_expr (base_addr), &this_seq, |
| 4389 | is_gimple_mem_ref_addr, NULL_TREE); |
| 4390 | gimple_seq_add_seq_without_update (&seq, this_seq); |
| 4391 | |
| 4392 | tree load_addr[2] = { NULL_TREE, NULL_TREE }; |
| 4393 | gimple_seq load_seq[2] = { NULL, NULL }; |
| 4394 | gimple_stmt_iterator load_gsi[2] = { gsi_none (), gsi_none () }; |
| 4395 | for (int j = 0; j < 2; ++j) |
| 4396 | { |
| 4397 | store_operand_info &op = group->stores[0]->ops[j]; |
| 4398 | if (op.base_addr == NULL_TREE) |
| 4399 | continue; |
| 4400 | |
| 4401 | store_immediate_info *infol = group->stores.last (); |
| 4402 | if (gimple_vuse (op.stmt) == gimple_vuse (infol->ops[j].stmt)) |
| 4403 | { |
| 4404 | /* We can't pick the location randomly; while we've verified |
| 4405 | all the loads have the same vuse, they can be still in different |
| 4406 | basic blocks and we need to pick the one from the last bb: |
| 4407 | int x = q[0]; |
| 4408 | if (x == N) return; |
| 4409 | int y = q[1]; |
| 4410 | p[0] = x; |
| 4411 | p[1] = y; |
| 4412 | otherwise if we put the wider load at the q[0] load, we might |
| 4413 | segfault if q[1] is not mapped. */ |
| 4414 | basic_block bb = gimple_bb (op.stmt); |
| 4415 | gimple *ostmt = op.stmt; |
| 4416 | store_immediate_info *info; |
| 4417 | FOR_EACH_VEC_ELT (group->stores, i, info) |
| 4418 | { |
| 4419 | gimple *tstmt = info->ops[j].stmt; |
| 4420 | basic_block tbb = gimple_bb (tstmt); |
| 4421 | if (dominated_by_p (CDI_DOMINATORS, tbb, bb)) |
| 4422 | { |
| 4423 | ostmt = tstmt; |
| 4424 | bb = tbb; |
| 4425 | } |
| 4426 | } |
| 4427 | load_gsi[j] = gsi_for_stmt (ostmt); |
| 4428 | load_addr[j] |
| 4429 | = force_gimple_operand_1 (unshare_expr (op.base_addr), |
| 4430 | &load_seq[j], is_gimple_mem_ref_addr, |
| 4431 | NULL_TREE); |
| 4432 | } |
| 4433 | else if (operand_equal_p (base_addr, op.base_addr, 0)) |
| 4434 | load_addr[j] = addr; |
| 4435 | else |
| 4436 | { |
| 4437 | load_addr[j] |
| 4438 | = force_gimple_operand_1 (unshare_expr (op.base_addr), |
| 4439 | &this_seq, is_gimple_mem_ref_addr, |
| 4440 | NULL_TREE); |
| 4441 | gimple_seq_add_seq_without_update (&seq, this_seq); |
| 4442 | } |
| 4443 | } |
| 4444 | |
| 4445 | FOR_EACH_VEC_ELT (split_stores, i, split_store) |
| 4446 | { |
| 4447 | const unsigned HOST_WIDE_INT try_size = split_store->size; |
| 4448 | const unsigned HOST_WIDE_INT try_pos = split_store->bytepos; |
| 4449 | const unsigned HOST_WIDE_INT try_bitpos = try_pos * BITS_PER_UNIT; |
| 4450 | const unsigned HOST_WIDE_INT try_align = split_store->align; |
| 4451 | const unsigned HOST_WIDE_INT try_offset = try_pos - start_byte_pos; |
| 4452 | tree dest, src; |
| 4453 | location_t loc; |
| 4454 | |
| 4455 | if (split_store->orig) |
| 4456 | { |
| 4457 | /* If there is just a single non-clobber constituent store |
| 4458 | which covers the whole area, just reuse the lhs and rhs. */ |
| 4459 | gimple *orig_stmt = NULL; |
| 4460 | store_immediate_info *store; |
| 4461 | unsigned int j; |
| 4462 | FOR_EACH_VEC_ELT (split_store->orig_stores, j, store) |
| 4463 | if (!gimple_clobber_p (store->stmt)) |
| 4464 | { |
| 4465 | orig_stmt = store->stmt; |
| 4466 | break; |
| 4467 | } |
| 4468 | dest = gimple_assign_lhs (orig_stmt); |
| 4469 | src = gimple_assign_rhs1 (orig_stmt); |
| 4470 | loc = gimple_location (orig_stmt); |
| 4471 | } |
| 4472 | else |
| 4473 | { |
| 4474 | store_immediate_info *info; |
| 4475 | unsigned short clique, base; |
| 4476 | unsigned int k; |
| 4477 | FOR_EACH_VEC_ELT (split_store->orig_stores, k, info) |
| 4478 | orig_stmts.safe_push (info->stmt); |
| 4479 | tree offset_type |
| 4480 | = get_alias_type_for_stmts (orig_stmts, false, &clique, &base); |
| 4481 | tree dest_type; |
| 4482 | loc = get_location_for_stmts (orig_stmts); |
| 4483 | orig_stmts.truncate (0); |
| 4484 | |
| 4485 | if (group->string_concatenation) |
| 4486 | dest_type |
| 4487 | = build_array_type_nelts (char_type_node, |
| 4488 | try_size / BITS_PER_UNIT); |
| 4489 | else |
| 4490 | { |
| 4491 | dest_type = build_nonstandard_integer_type (try_size, UNSIGNED); |
| 4492 | dest_type = build_aligned_type (dest_type, try_align); |
| 4493 | } |
| 4494 | dest = fold_build2 (MEM_REF, dest_type, addr, |
| 4495 | build_int_cst (offset_type, try_pos)); |
| 4496 | if (TREE_CODE (dest) == MEM_REF) |
| 4497 | { |
| 4498 | MR_DEPENDENCE_CLIQUE (dest) = clique; |
| 4499 | MR_DEPENDENCE_BASE (dest) = base; |
| 4500 | } |
| 4501 | |
| 4502 | tree mask; |
| 4503 | if (bswap_res || group->string_concatenation) |
| 4504 | mask = integer_zero_node; |
| 4505 | else |
| 4506 | mask = native_interpret_expr (dest_type, |
| 4507 | group->mask + try_offset, |
| 4508 | group->buf_size); |
| 4509 | |
| 4510 | tree ops[2]; |
| 4511 | for (int j = 0; |
| 4512 | j < 1 + (split_store->orig_stores[0]->ops[1].val != NULL_TREE); |
| 4513 | ++j) |
| 4514 | { |
| 4515 | store_operand_info &op = split_store->orig_stores[0]->ops[j]; |
| 4516 | if (bswap_res) |
| 4517 | ops[j] = bswap_res; |
| 4518 | else if (group->string_concatenation) |
| 4519 | { |
| 4520 | ops[j] = build_string (try_size / BITS_PER_UNIT, |
| 4521 | (const char *) group->val + try_offset); |
| 4522 | TREE_TYPE (ops[j]) = dest_type; |
| 4523 | } |
| 4524 | else if (op.base_addr) |
| 4525 | { |
| 4526 | FOR_EACH_VEC_ELT (split_store->orig_stores, k, info) |
| 4527 | orig_stmts.safe_push (info->ops[j].stmt); |
| 4528 | |
| 4529 | offset_type = get_alias_type_for_stmts (orig_stmts, true, |
| 4530 | &clique, &base); |
| 4531 | location_t load_loc = get_location_for_stmts (orig_stmts); |
| 4532 | orig_stmts.truncate (0); |
| 4533 | |
| 4534 | unsigned HOST_WIDE_INT load_align = group->load_align[j]; |
| 4535 | unsigned HOST_WIDE_INT align_bitpos |
| 4536 | = known_alignment (try_bitpos |
| 4537 | - split_store->orig_stores[0]->bitpos |
| 4538 | + op.bitpos); |
| 4539 | if (align_bitpos & (load_align - 1)) |
| 4540 | load_align = least_bit_hwi (align_bitpos); |
| 4541 | |
| 4542 | tree load_int_type |
| 4543 | = build_nonstandard_integer_type (try_size, UNSIGNED); |
| 4544 | load_int_type |
| 4545 | = build_aligned_type (load_int_type, load_align); |
| 4546 | |
| 4547 | poly_uint64 load_pos |
| 4548 | = exact_div (try_bitpos |
| 4549 | - split_store->orig_stores[0]->bitpos |
| 4550 | + op.bitpos, |
| 4551 | BITS_PER_UNIT); |
| 4552 | ops[j] = fold_build2 (MEM_REF, load_int_type, load_addr[j], |
| 4553 | build_int_cst (offset_type, load_pos)); |
| 4554 | if (TREE_CODE (ops[j]) == MEM_REF) |
| 4555 | { |
| 4556 | MR_DEPENDENCE_CLIQUE (ops[j]) = clique; |
| 4557 | MR_DEPENDENCE_BASE (ops[j]) = base; |
| 4558 | } |
| 4559 | if (!integer_zerop (mask)) |
| 4560 | { |
| 4561 | /* The load might load some bits (that will be masked |
| 4562 | off later on) uninitialized, avoid -W*uninitialized |
| 4563 | warnings in that case. */ |
| 4564 | suppress_warning (ops[j], OPT_Wuninitialized); |
| 4565 | } |
| 4566 | |
| 4567 | stmt = gimple_build_assign (make_ssa_name (dest_type), ops[j]); |
| 4568 | gimple_set_location (stmt, load_loc); |
| 4569 | if (gsi_bb (load_gsi[j])) |
| 4570 | { |
| 4571 | gimple_set_vuse (stmt, gimple_vuse (op.stmt)); |
| 4572 | gimple_seq_add_stmt_without_update (&load_seq[j], stmt); |
| 4573 | } |
| 4574 | else |
| 4575 | { |
| 4576 | gimple_set_vuse (stmt, new_vuse); |
| 4577 | gimple_seq_add_stmt_without_update (&seq, stmt); |
| 4578 | } |
| 4579 | ops[j] = gimple_assign_lhs (stmt); |
| 4580 | tree xor_mask; |
| 4581 | enum tree_code inv_op |
| 4582 | = invert_op (split_store, j, dest_type, xor_mask); |
| 4583 | if (inv_op != NOP_EXPR) |
| 4584 | { |
| 4585 | stmt = gimple_build_assign (make_ssa_name (dest_type), |
| 4586 | inv_op, ops[j], xor_mask); |
| 4587 | gimple_set_location (stmt, load_loc); |
| 4588 | ops[j] = gimple_assign_lhs (stmt); |
| 4589 | |
| 4590 | if (gsi_bb (load_gsi[j])) |
| 4591 | gimple_seq_add_stmt_without_update (&load_seq[j], |
| 4592 | stmt); |
| 4593 | else |
| 4594 | gimple_seq_add_stmt_without_update (&seq, stmt); |
| 4595 | } |
| 4596 | } |
| 4597 | else |
| 4598 | ops[j] = native_interpret_expr (dest_type, |
| 4599 | group->val + try_offset, |
| 4600 | group->buf_size); |
| 4601 | } |
| 4602 | |
| 4603 | switch (split_store->orig_stores[0]->rhs_code) |
| 4604 | { |
| 4605 | case BIT_AND_EXPR: |
| 4606 | case BIT_IOR_EXPR: |
| 4607 | case BIT_XOR_EXPR: |
| 4608 | FOR_EACH_VEC_ELT (split_store->orig_stores, k, info) |
| 4609 | { |
| 4610 | tree rhs1 = gimple_assign_rhs1 (info->stmt); |
| 4611 | orig_stmts.safe_push (SSA_NAME_DEF_STMT (rhs1)); |
| 4612 | } |
| 4613 | location_t bit_loc; |
| 4614 | bit_loc = get_location_for_stmts (orig_stmts); |
| 4615 | orig_stmts.truncate (0); |
| 4616 | |
| 4617 | stmt |
| 4618 | = gimple_build_assign (make_ssa_name (dest_type), |
| 4619 | split_store->orig_stores[0]->rhs_code, |
| 4620 | ops[0], ops[1]); |
| 4621 | gimple_set_location (stmt, bit_loc); |
| 4622 | /* If there is just one load and there is a separate |
| 4623 | load_seq[0], emit the bitwise op right after it. */ |
| 4624 | if (load_addr[1] == NULL_TREE && gsi_bb (load_gsi[0])) |
| 4625 | gimple_seq_add_stmt_without_update (&load_seq[0], stmt); |
| 4626 | /* Otherwise, if at least one load is in seq, we need to |
| 4627 | emit the bitwise op right before the store. If there |
| 4628 | are two loads and are emitted somewhere else, it would |
| 4629 | be better to emit the bitwise op as early as possible; |
| 4630 | we don't track where that would be possible right now |
| 4631 | though. */ |
| 4632 | else |
| 4633 | gimple_seq_add_stmt_without_update (&seq, stmt); |
| 4634 | src = gimple_assign_lhs (stmt); |
| 4635 | tree xor_mask; |
| 4636 | enum tree_code inv_op; |
| 4637 | inv_op = invert_op (split_store, 2, dest_type, xor_mask); |
| 4638 | if (inv_op != NOP_EXPR) |
| 4639 | { |
| 4640 | stmt = gimple_build_assign (make_ssa_name (dest_type), |
| 4641 | inv_op, src, xor_mask); |
| 4642 | gimple_set_location (stmt, bit_loc); |
| 4643 | if (load_addr[1] == NULL_TREE && gsi_bb (load_gsi[0])) |
| 4644 | gimple_seq_add_stmt_without_update (&load_seq[0], stmt); |
| 4645 | else |
| 4646 | gimple_seq_add_stmt_without_update (&seq, stmt); |
| 4647 | src = gimple_assign_lhs (stmt); |
| 4648 | } |
| 4649 | break; |
| 4650 | case LROTATE_EXPR: |
| 4651 | case NOP_EXPR: |
| 4652 | src = ops[0]; |
| 4653 | if (!is_gimple_val (src)) |
| 4654 | { |
| 4655 | stmt = gimple_build_assign (make_ssa_name (TREE_TYPE (src)), |
| 4656 | src); |
| 4657 | gimple_seq_add_stmt_without_update (&seq, stmt); |
| 4658 | src = gimple_assign_lhs (stmt); |
| 4659 | } |
| 4660 | if (!useless_type_conversion_p (dest_type, TREE_TYPE (src))) |
| 4661 | { |
| 4662 | stmt = gimple_build_assign (make_ssa_name (dest_type), |
| 4663 | NOP_EXPR, src); |
| 4664 | gimple_seq_add_stmt_without_update (&seq, stmt); |
| 4665 | src = gimple_assign_lhs (stmt); |
| 4666 | } |
| 4667 | inv_op = invert_op (split_store, 2, dest_type, xor_mask); |
| 4668 | if (inv_op != NOP_EXPR) |
| 4669 | { |
| 4670 | stmt = gimple_build_assign (make_ssa_name (dest_type), |
| 4671 | inv_op, src, xor_mask); |
| 4672 | gimple_set_location (stmt, loc); |
| 4673 | gimple_seq_add_stmt_without_update (&seq, stmt); |
| 4674 | src = gimple_assign_lhs (stmt); |
| 4675 | } |
| 4676 | break; |
| 4677 | default: |
| 4678 | src = ops[0]; |
| 4679 | break; |
| 4680 | } |
| 4681 | |
| 4682 | /* If bit insertion is required, we use the source as an accumulator |
| 4683 | into which the successive bit-field values are manually inserted. |
| 4684 | FIXME: perhaps use BIT_INSERT_EXPR instead in some cases? */ |
| 4685 | if (group->bit_insertion) |
| 4686 | FOR_EACH_VEC_ELT (split_store->orig_stores, k, info) |
| 4687 | if (info->rhs_code == BIT_INSERT_EXPR |
| 4688 | && info->bitpos < try_bitpos + try_size |
| 4689 | && info->bitpos + info->bitsize > try_bitpos) |
| 4690 | { |
| 4691 | /* Mask, truncate, convert to final type, shift and ior into |
| 4692 | the accumulator. Note that every step can be a no-op. */ |
| 4693 | const HOST_WIDE_INT start_gap = info->bitpos - try_bitpos; |
| 4694 | const HOST_WIDE_INT end_gap |
| 4695 | = (try_bitpos + try_size) - (info->bitpos + info->bitsize); |
| 4696 | tree tem = info->ops[0].val; |
| 4697 | if (!INTEGRAL_TYPE_P (TREE_TYPE (tem))) |
| 4698 | { |
| 4699 | const unsigned HOST_WIDE_INT size |
| 4700 | = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (tem))); |
| 4701 | tree integer_type |
| 4702 | = build_nonstandard_integer_type (size, UNSIGNED); |
| 4703 | tem = gimple_build (&seq, loc, VIEW_CONVERT_EXPR, |
| 4704 | integer_type, tem); |
| 4705 | } |
| 4706 | if (TYPE_PRECISION (TREE_TYPE (tem)) <= info->bitsize) |
| 4707 | { |
| 4708 | tree bitfield_type |
| 4709 | = build_nonstandard_integer_type (info->bitsize, |
| 4710 | UNSIGNED); |
| 4711 | tem = gimple_convert (&seq, loc, bitfield_type, tem); |
| 4712 | } |
| 4713 | else if ((BYTES_BIG_ENDIAN ? start_gap : end_gap) > 0) |
| 4714 | { |
| 4715 | wide_int imask |
| 4716 | = wi::mask (info->bitsize, false, |
| 4717 | TYPE_PRECISION (TREE_TYPE (tem))); |
| 4718 | tem = gimple_build (&seq, loc, |
| 4719 | BIT_AND_EXPR, TREE_TYPE (tem), tem, |
| 4720 | wide_int_to_tree (TREE_TYPE (tem), |
| 4721 | imask)); |
| 4722 | } |
| 4723 | const HOST_WIDE_INT shift |
| 4724 | = (BYTES_BIG_ENDIAN ? end_gap : start_gap); |
| 4725 | if (shift < 0) |
| 4726 | tem = gimple_build (&seq, loc, |
| 4727 | RSHIFT_EXPR, TREE_TYPE (tem), tem, |
| 4728 | build_int_cst (NULL_TREE, -shift)); |
| 4729 | tem = gimple_convert (&seq, loc, dest_type, tem); |
| 4730 | if (shift > 0) |
| 4731 | tem = gimple_build (&seq, loc, |
| 4732 | LSHIFT_EXPR, dest_type, tem, |
| 4733 | build_int_cst (NULL_TREE, shift)); |
| 4734 | src = gimple_build (&seq, loc, |
| 4735 | BIT_IOR_EXPR, dest_type, tem, src); |
| 4736 | } |
| 4737 | |
| 4738 | if (!integer_zerop (mask)) |
| 4739 | { |
| 4740 | tree tem = make_ssa_name (dest_type); |
| 4741 | tree load_src = unshare_expr (dest); |
| 4742 | /* The load might load some or all bits uninitialized, |
| 4743 | avoid -W*uninitialized warnings in that case. |
| 4744 | As optimization, it would be nice if all the bits are |
| 4745 | provably uninitialized (no stores at all yet or previous |
| 4746 | store a CLOBBER) we'd optimize away the load and replace |
| 4747 | it e.g. with 0. */ |
| 4748 | suppress_warning (load_src, OPT_Wuninitialized); |
| 4749 | stmt = gimple_build_assign (tem, load_src); |
| 4750 | gimple_set_location (stmt, loc); |
| 4751 | gimple_set_vuse (stmt, new_vuse); |
| 4752 | gimple_seq_add_stmt_without_update (&seq, stmt); |
| 4753 | |
| 4754 | /* FIXME: If there is a single chunk of zero bits in mask, |
| 4755 | perhaps use BIT_INSERT_EXPR instead? */ |
| 4756 | stmt = gimple_build_assign (make_ssa_name (dest_type), |
| 4757 | BIT_AND_EXPR, tem, mask); |
| 4758 | gimple_set_location (stmt, loc); |
| 4759 | gimple_seq_add_stmt_without_update (&seq, stmt); |
| 4760 | tem = gimple_assign_lhs (stmt); |
| 4761 | |
| 4762 | if (TREE_CODE (src) == INTEGER_CST) |
| 4763 | src = wide_int_to_tree (dest_type, |
| 4764 | wi::bit_and_not (wi::to_wide (src), |
| 4765 | wi::to_wide (mask))); |
| 4766 | else |
| 4767 | { |
| 4768 | tree nmask |
| 4769 | = wide_int_to_tree (dest_type, |
| 4770 | wi::bit_not (wi::to_wide (mask))); |
| 4771 | stmt = gimple_build_assign (make_ssa_name (dest_type), |
| 4772 | BIT_AND_EXPR, src, nmask); |
| 4773 | gimple_set_location (stmt, loc); |
| 4774 | gimple_seq_add_stmt_without_update (&seq, stmt); |
| 4775 | src = gimple_assign_lhs (stmt); |
| 4776 | } |
| 4777 | stmt = gimple_build_assign (make_ssa_name (dest_type), |
| 4778 | BIT_IOR_EXPR, tem, src); |
| 4779 | gimple_set_location (stmt, loc); |
| 4780 | gimple_seq_add_stmt_without_update (&seq, stmt); |
| 4781 | src = gimple_assign_lhs (stmt); |
| 4782 | } |
| 4783 | } |
| 4784 | |
| 4785 | stmt = gimple_build_assign (dest, src); |
| 4786 | gimple_set_location (stmt, loc); |
| 4787 | gimple_set_vuse (stmt, new_vuse); |
| 4788 | gimple_seq_add_stmt_without_update (&seq, stmt); |
| 4789 | |
| 4790 | if (group->lp_nr && stmt_could_throw_p (cfun, stmt)) |
| 4791 | add_stmt_to_eh_lp (stmt, group->lp_nr); |
| 4792 | |
| 4793 | tree new_vdef; |
| 4794 | if (i < split_stores.length () - 1) |
| 4795 | new_vdef = make_ssa_name (gimple_vop (cfun), stmt); |
| 4796 | else |
| 4797 | new_vdef = last_vdef; |
| 4798 | |
| 4799 | gimple_set_vdef (stmt, new_vdef); |
| 4800 | SSA_NAME_DEF_STMT (new_vdef) = stmt; |
| 4801 | new_vuse = new_vdef; |
| 4802 | } |
| 4803 | |
| 4804 | FOR_EACH_VEC_ELT (split_stores, i, split_store) |
| 4805 | delete split_store; |
| 4806 | |
| 4807 | gcc_assert (seq); |
| 4808 | if (dump_file) |
| 4809 | { |
| 4810 | fprintf (dump_file, |
| 4811 | "New sequence of %u stores to replace old one of %u stores\n", |
| 4812 | split_stores.length (), orig_num_stmts); |
| 4813 | if (dump_flags & TDF_DETAILS) |
| 4814 | print_gimple_seq (dump_file, seq, 0, TDF_VOPS | TDF_MEMSYMS); |
| 4815 | } |
| 4816 | |
| 4817 | if (gimple_clobber_p (group->last_stmt)) |
| 4818 | update_stmt (group->last_stmt); |
| 4819 | |
| 4820 | if (group->lp_nr > 0) |
| 4821 | { |
| 4822 | /* We're going to insert a sequence of (potentially) throwing stores |
| 4823 | into an active EH region. This means that we're going to create |
| 4824 | new basic blocks with EH edges pointing to the post landing pad |
| 4825 | and, therefore, to have to update its PHI nodes, if any. For the |
| 4826 | virtual PHI node, we're going to use the VDEFs created above, but |
| 4827 | for the other nodes, we need to record the original reaching defs. */ |
| 4828 | eh_landing_pad lp = get_eh_landing_pad_from_number (group->lp_nr); |
| 4829 | basic_block lp_bb = label_to_block (cfun, lp->post_landing_pad); |
| 4830 | basic_block last_bb = gimple_bb (group->last_stmt); |
| 4831 | edge last_edge = find_edge (last_bb, lp_bb); |
| 4832 | auto_vec<tree, 16> last_defs; |
| 4833 | gphi_iterator gpi; |
| 4834 | for (gpi = gsi_start_phis (lp_bb); !gsi_end_p (gpi); gsi_next (&gpi)) |
| 4835 | { |
| 4836 | gphi *phi = gpi.phi (); |
| 4837 | tree last_def; |
| 4838 | if (virtual_operand_p (gimple_phi_result (phi))) |
| 4839 | last_def = NULL_TREE; |
| 4840 | else |
| 4841 | last_def = gimple_phi_arg_def (phi, last_edge->dest_idx); |
| 4842 | last_defs.safe_push (last_def); |
| 4843 | } |
| 4844 | |
| 4845 | /* Do the insertion. Then, if new basic blocks have been created in the |
| 4846 | process, rewind the chain of VDEFs create above to walk the new basic |
| 4847 | blocks and update the corresponding arguments of the PHI nodes. */ |
| 4848 | update_modified_stmts (seq); |
| 4849 | if (gimple_find_sub_bbs (seq, &last_gsi)) |
| 4850 | while (last_vdef != gimple_vuse (group->last_stmt)) |
| 4851 | { |
| 4852 | gimple *stmt = SSA_NAME_DEF_STMT (last_vdef); |
| 4853 | if (stmt_could_throw_p (cfun, stmt)) |
| 4854 | { |
| 4855 | edge new_edge = find_edge (gimple_bb (stmt), lp_bb); |
| 4856 | unsigned int i; |
| 4857 | for (gpi = gsi_start_phis (lp_bb), i = 0; |
| 4858 | !gsi_end_p (gpi); |
| 4859 | gsi_next (&gpi), i++) |
| 4860 | { |
| 4861 | gphi *phi = gpi.phi (); |
| 4862 | tree new_def; |
| 4863 | if (virtual_operand_p (gimple_phi_result (phi))) |
| 4864 | new_def = last_vdef; |
| 4865 | else |
| 4866 | new_def = last_defs[i]; |
| 4867 | add_phi_arg (phi, new_def, new_edge, UNKNOWN_LOCATION); |
| 4868 | } |
| 4869 | } |
| 4870 | last_vdef = gimple_vuse (stmt); |
| 4871 | } |
| 4872 | } |
| 4873 | else |
| 4874 | gsi_insert_seq_after (&last_gsi, seq, GSI_SAME_STMT); |
| 4875 | |
| 4876 | for (int j = 0; j < 2; ++j) |
| 4877 | if (load_seq[j]) |
| 4878 | gsi_insert_seq_after (&load_gsi[j], load_seq[j], GSI_SAME_STMT); |
| 4879 | |
| 4880 | return true; |
| 4881 | } |
| 4882 | |
| 4883 | /* Process the merged_store_group objects created in the coalescing phase. |
| 4884 | The stores are all against the base object BASE. |
| 4885 | Try to output the widened stores and delete the original statements if |
| 4886 | successful. Return true iff any changes were made. */ |
| 4887 | |
| 4888 | bool |
| 4889 | imm_store_chain_info::output_merged_stores () |
| 4890 | { |
| 4891 | unsigned int i; |
| 4892 | merged_store_group *merged_store; |
| 4893 | bool ret = false; |
| 4894 | FOR_EACH_VEC_ELT (m_merged_store_groups, i, merged_store) |
| 4895 | { |
| 4896 | if (dbg_cnt (store_merging) |
| 4897 | && output_merged_store (merged_store)) |
| 4898 | { |
| 4899 | unsigned int j; |
| 4900 | store_immediate_info *store; |
| 4901 | FOR_EACH_VEC_ELT (merged_store->stores, j, store) |
| 4902 | { |
| 4903 | gimple *stmt = store->stmt; |
| 4904 | gimple_stmt_iterator gsi = gsi_for_stmt (stmt); |
| 4905 | /* Don't remove clobbers, they are still useful even if |
| 4906 | everything is overwritten afterwards. */ |
| 4907 | if (gimple_clobber_p (stmt)) |
| 4908 | continue; |
| 4909 | gsi_remove (&gsi, true); |
| 4910 | if (store->lp_nr) |
| 4911 | remove_stmt_from_eh_lp (stmt); |
| 4912 | if (stmt != merged_store->last_stmt) |
| 4913 | { |
| 4914 | unlink_stmt_vdef (stmt); |
| 4915 | release_defs (stmt); |
| 4916 | } |
| 4917 | } |
| 4918 | ret = true; |
| 4919 | } |
| 4920 | } |
| 4921 | if (ret && dump_file) |
| 4922 | fprintf (dump_file, "Merging successful!\n"); |
| 4923 | |
| 4924 | return ret; |
| 4925 | } |
| 4926 | |
| 4927 | /* Coalesce the store_immediate_info objects recorded against the base object |
| 4928 | BASE in the first phase and output them. |
| 4929 | Delete the allocated structures. |
| 4930 | Return true if any changes were made. */ |
| 4931 | |
| 4932 | bool |
| 4933 | imm_store_chain_info::terminate_and_process_chain () |
| 4934 | { |
| 4935 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 4936 | fprintf (dump_file, "Terminating chain with %u stores\n", |
| 4937 | m_store_info.length ()); |
| 4938 | /* Process store chain. */ |
| 4939 | bool ret = false; |
| 4940 | if (m_store_info.length () > 1) |
| 4941 | { |
| 4942 | ret = coalesce_immediate_stores (); |
| 4943 | if (ret) |
| 4944 | ret = output_merged_stores (); |
| 4945 | } |
| 4946 | |
| 4947 | /* Delete all the entries we allocated ourselves. */ |
| 4948 | store_immediate_info *info; |
| 4949 | unsigned int i; |
| 4950 | FOR_EACH_VEC_ELT (m_store_info, i, info) |
| 4951 | delete info; |
| 4952 | |
| 4953 | merged_store_group *merged_info; |
| 4954 | FOR_EACH_VEC_ELT (m_merged_store_groups, i, merged_info) |
| 4955 | delete merged_info; |
| 4956 | |
| 4957 | return ret; |
| 4958 | } |
| 4959 | |
| 4960 | /* Return true iff LHS is a destination potentially interesting for |
| 4961 | store merging. In practice these are the codes that get_inner_reference |
| 4962 | can process. */ |
| 4963 | |
| 4964 | static bool |
| 4965 | lhs_valid_for_store_merging_p (tree lhs) |
| 4966 | { |
| 4967 | if (DECL_P (lhs)) |
| 4968 | return true; |
| 4969 | |
| 4970 | switch (TREE_CODE (lhs)) |
| 4971 | { |
| 4972 | case ARRAY_REF: |
| 4973 | case ARRAY_RANGE_REF: |
| 4974 | case BIT_FIELD_REF: |
| 4975 | case COMPONENT_REF: |
| 4976 | case MEM_REF: |
| 4977 | case VIEW_CONVERT_EXPR: |
| 4978 | return true; |
| 4979 | default: |
| 4980 | return false; |
| 4981 | } |
| 4982 | } |
| 4983 | |
| 4984 | /* Return true if the tree RHS is a constant we want to consider |
| 4985 | during store merging. In practice accept all codes that |
| 4986 | native_encode_expr accepts. */ |
| 4987 | |
| 4988 | static bool |
| 4989 | rhs_valid_for_store_merging_p (tree rhs) |
| 4990 | { |
| 4991 | unsigned HOST_WIDE_INT size; |
| 4992 | if (TREE_CODE (rhs) == CONSTRUCTOR |
| 4993 | && CONSTRUCTOR_NELTS (rhs) == 0 |
| 4994 | && TYPE_SIZE_UNIT (TREE_TYPE (rhs)) |
| 4995 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (rhs)))) |
| 4996 | return true; |
| 4997 | return (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (rhs))).is_constant (&size) |
| 4998 | && native_encode_expr (rhs, NULL, size) != 0); |
| 4999 | } |
| 5000 | |
| 5001 | /* Adjust *PBITPOS, *PBITREGION_START and *PBITREGION_END by BYTE_OFF bytes |
| 5002 | and return true on success or false on failure. */ |
| 5003 | |
| 5004 | static bool |
| 5005 | adjust_bit_pos (poly_offset_int byte_off, |
| 5006 | poly_int64 *pbitpos, |
| 5007 | poly_uint64 *pbitregion_start, |
| 5008 | poly_uint64 *pbitregion_end) |
| 5009 | { |
| 5010 | poly_offset_int bit_off = byte_off << LOG2_BITS_PER_UNIT; |
| 5011 | bit_off += *pbitpos; |
| 5012 | |
| 5013 | if (known_ge (bit_off, 0) && bit_off.to_shwi (pbitpos)) |
| 5014 | { |
| 5015 | if (maybe_ne (*pbitregion_end, 0U)) |
| 5016 | { |
| 5017 | bit_off = byte_off << LOG2_BITS_PER_UNIT; |
| 5018 | bit_off += *pbitregion_start; |
| 5019 | if (bit_off.to_uhwi (pbitregion_start)) |
| 5020 | { |
| 5021 | bit_off = byte_off << LOG2_BITS_PER_UNIT; |
| 5022 | bit_off += *pbitregion_end; |
| 5023 | if (!bit_off.to_uhwi (pbitregion_end)) |
| 5024 | *pbitregion_end = 0; |
| 5025 | } |
| 5026 | else |
| 5027 | *pbitregion_end = 0; |
| 5028 | } |
| 5029 | return true; |
| 5030 | } |
| 5031 | else |
| 5032 | return false; |
| 5033 | } |
| 5034 | |
| 5035 | /* If MEM is a memory reference usable for store merging (either as |
| 5036 | store destination or for loads), return the non-NULL base_addr |
| 5037 | and set *PBITSIZE, *PBITPOS, *PBITREGION_START and *PBITREGION_END. |
| 5038 | Otherwise return NULL, *PBITPOS should be still valid even for that |
| 5039 | case. */ |
| 5040 | |
| 5041 | static tree |
| 5042 | mem_valid_for_store_merging (tree mem, poly_uint64 *pbitsize, |
| 5043 | poly_uint64 *pbitpos, |
| 5044 | poly_uint64 *pbitregion_start, |
| 5045 | poly_uint64 *pbitregion_end) |
| 5046 | { |
| 5047 | poly_int64 bitsize, bitpos; |
| 5048 | poly_uint64 bitregion_start = 0, bitregion_end = 0; |
| 5049 | machine_mode mode; |
| 5050 | int unsignedp = 0, reversep = 0, volatilep = 0; |
| 5051 | tree offset; |
| 5052 | tree base_addr = get_inner_reference (mem, &bitsize, &bitpos, &offset, &mode, |
| 5053 | &unsignedp, &reversep, &volatilep); |
| 5054 | *pbitsize = bitsize; |
| 5055 | if (known_le (bitsize, 0)) |
| 5056 | return NULL_TREE; |
| 5057 | |
| 5058 | if (TREE_CODE (mem) == COMPONENT_REF |
| 5059 | && DECL_BIT_FIELD_TYPE (TREE_OPERAND (mem, 1))) |
| 5060 | { |
| 5061 | get_bit_range (&bitregion_start, &bitregion_end, mem, &bitpos, &offset); |
| 5062 | if (maybe_ne (bitregion_end, 0U)) |
| 5063 | bitregion_end += 1; |
| 5064 | } |
| 5065 | |
| 5066 | if (reversep) |
| 5067 | return NULL_TREE; |
| 5068 | |
| 5069 | /* We do not want to rewrite TARGET_MEM_REFs. */ |
| 5070 | if (TREE_CODE (base_addr) == TARGET_MEM_REF) |
| 5071 | return NULL_TREE; |
| 5072 | /* In some cases get_inner_reference may return a |
| 5073 | MEM_REF [ptr + byteoffset]. For the purposes of this pass |
| 5074 | canonicalize the base_addr to MEM_REF [ptr] and take |
| 5075 | byteoffset into account in the bitpos. This occurs in |
| 5076 | PR 23684 and this way we can catch more chains. */ |
| 5077 | else if (TREE_CODE (base_addr) == MEM_REF) |
| 5078 | { |
| 5079 | if (!adjust_bit_pos (mem_ref_offset (base_addr), &bitpos, |
| 5080 | &bitregion_start, &bitregion_end)) |
| 5081 | return NULL_TREE; |
| 5082 | base_addr = TREE_OPERAND (base_addr, 0); |
| 5083 | } |
| 5084 | /* get_inner_reference returns the base object, get at its |
| 5085 | address now. */ |
| 5086 | else |
| 5087 | { |
| 5088 | if (maybe_lt (bitpos, 0)) |
| 5089 | return NULL_TREE; |
| 5090 | base_addr = build_fold_addr_expr (base_addr); |
| 5091 | } |
| 5092 | |
| 5093 | if (offset) |
| 5094 | { |
| 5095 | /* If the access is variable offset then a base decl has to be |
| 5096 | address-taken to be able to emit pointer-based stores to it. |
| 5097 | ??? We might be able to get away with re-using the original |
| 5098 | base up to the first variable part and then wrapping that inside |
| 5099 | a BIT_FIELD_REF. */ |
| 5100 | tree base = get_base_address (base_addr); |
| 5101 | if (!base || (DECL_P (base) && !TREE_ADDRESSABLE (base))) |
| 5102 | return NULL_TREE; |
| 5103 | |
| 5104 | /* Similarly to above for the base, remove constant from the offset. */ |
| 5105 | if (TREE_CODE (offset) == PLUS_EXPR |
| 5106 | && TREE_CODE (TREE_OPERAND (offset, 1)) == INTEGER_CST |
| 5107 | && adjust_bit_pos (wi::to_poly_offset (TREE_OPERAND (offset, 1)), |
| 5108 | &bitpos, &bitregion_start, &bitregion_end)) |
| 5109 | offset = TREE_OPERAND (offset, 0); |
| 5110 | |
| 5111 | base_addr = build2 (POINTER_PLUS_EXPR, TREE_TYPE (base_addr), |
| 5112 | base_addr, offset); |
| 5113 | } |
| 5114 | |
| 5115 | if (known_eq (bitregion_end, 0U)) |
| 5116 | { |
| 5117 | bitregion_start = round_down_to_byte_boundary (bitpos); |
| 5118 | bitregion_end = round_up_to_byte_boundary (bitpos + bitsize); |
| 5119 | } |
| 5120 | |
| 5121 | *pbitsize = bitsize; |
| 5122 | *pbitpos = bitpos; |
| 5123 | *pbitregion_start = bitregion_start; |
| 5124 | *pbitregion_end = bitregion_end; |
| 5125 | return base_addr; |
| 5126 | } |
| 5127 | |
| 5128 | /* Return true if STMT is a load that can be used for store merging. |
| 5129 | In that case fill in *OP. BITSIZE, BITPOS, BITREGION_START and |
| 5130 | BITREGION_END are properties of the corresponding store. */ |
| 5131 | |
| 5132 | static bool |
| 5133 | handled_load (gimple *stmt, store_operand_info *op, |
| 5134 | poly_uint64 bitsize, poly_uint64 bitpos, |
| 5135 | poly_uint64 bitregion_start, poly_uint64 bitregion_end) |
| 5136 | { |
| 5137 | if (!is_gimple_assign (stmt)) |
| 5138 | return false; |
| 5139 | if (gimple_assign_rhs_code (stmt) == BIT_NOT_EXPR) |
| 5140 | { |
| 5141 | tree rhs1 = gimple_assign_rhs1 (stmt); |
| 5142 | if (TREE_CODE (rhs1) == SSA_NAME |
| 5143 | && handled_load (SSA_NAME_DEF_STMT (rhs1), op, bitsize, bitpos, |
| 5144 | bitregion_start, bitregion_end)) |
| 5145 | { |
| 5146 | /* Don't allow _1 = load; _2 = ~1; _3 = ~_2; which should have |
| 5147 | been optimized earlier, but if allowed here, would confuse the |
| 5148 | multiple uses counting. */ |
| 5149 | if (op->bit_not_p) |
| 5150 | return false; |
| 5151 | op->bit_not_p = !op->bit_not_p; |
| 5152 | return true; |
| 5153 | } |
| 5154 | return false; |
| 5155 | } |
| 5156 | if (gimple_vuse (stmt) |
| 5157 | && gimple_assign_load_p (stmt) |
| 5158 | && !stmt_can_throw_internal (cfun, stmt) |
| 5159 | && !gimple_has_volatile_ops (stmt)) |
| 5160 | { |
| 5161 | tree mem = gimple_assign_rhs1 (stmt); |
| 5162 | op->base_addr |
| 5163 | = mem_valid_for_store_merging (mem, &op->bitsize, &op->bitpos, |
| 5164 | &op->bitregion_start, |
| 5165 | &op->bitregion_end); |
| 5166 | if (op->base_addr != NULL_TREE |
| 5167 | && known_eq (op->bitsize, bitsize) |
| 5168 | && multiple_p (op->bitpos - bitpos, BITS_PER_UNIT) |
| 5169 | && known_ge (op->bitpos - op->bitregion_start, |
| 5170 | bitpos - bitregion_start) |
| 5171 | && known_ge (op->bitregion_end - op->bitpos, |
| 5172 | bitregion_end - bitpos)) |
| 5173 | { |
| 5174 | op->stmt = stmt; |
| 5175 | op->val = mem; |
| 5176 | op->bit_not_p = false; |
| 5177 | return true; |
| 5178 | } |
| 5179 | } |
| 5180 | return false; |
| 5181 | } |
| 5182 | |
| 5183 | /* Return the index number of the landing pad for STMT, if any. */ |
| 5184 | |
| 5185 | static int |
| 5186 | lp_nr_for_store (gimple *stmt) |
| 5187 | { |
| 5188 | if (!cfun->can_throw_non_call_exceptions || !cfun->eh) |
| 5189 | return 0; |
| 5190 | |
| 5191 | if (!stmt_could_throw_p (cfun, stmt)) |
| 5192 | return 0; |
| 5193 | |
| 5194 | return lookup_stmt_eh_lp (stmt); |
| 5195 | } |
| 5196 | |
| 5197 | /* Record the store STMT for store merging optimization if it can be |
| 5198 | optimized. Return true if any changes were made. */ |
| 5199 | |
| 5200 | bool |
| 5201 | pass_store_merging::process_store (gimple *stmt) |
| 5202 | { |
| 5203 | tree lhs = gimple_assign_lhs (stmt); |
| 5204 | tree rhs = gimple_assign_rhs1 (stmt); |
| 5205 | poly_uint64 bitsize, bitpos = 0; |
| 5206 | poly_uint64 bitregion_start = 0, bitregion_end = 0; |
| 5207 | tree base_addr |
| 5208 | = mem_valid_for_store_merging (lhs, &bitsize, &bitpos, |
| 5209 | &bitregion_start, &bitregion_end); |
| 5210 | if (known_eq (bitsize, 0U)) |
| 5211 | return false; |
| 5212 | |
| 5213 | bool invalid = (base_addr == NULL_TREE |
| 5214 | || (maybe_gt (bitsize, |
| 5215 | (unsigned int) MAX_BITSIZE_MODE_ANY_INT) |
| 5216 | && TREE_CODE (rhs) != INTEGER_CST |
| 5217 | && (TREE_CODE (rhs) != CONSTRUCTOR |
| 5218 | || CONSTRUCTOR_NELTS (rhs) != 0))); |
| 5219 | enum tree_code rhs_code = ERROR_MARK; |
| 5220 | bool bit_not_p = false; |
| 5221 | struct symbolic_number n; |
| 5222 | gimple *ins_stmt = NULL; |
| 5223 | store_operand_info ops[2]; |
| 5224 | if (invalid) |
| 5225 | ; |
| 5226 | else if (TREE_CODE (rhs) == STRING_CST) |
| 5227 | { |
| 5228 | rhs_code = STRING_CST; |
| 5229 | ops[0].val = rhs; |
| 5230 | } |
| 5231 | else if (rhs_valid_for_store_merging_p (rhs)) |
| 5232 | { |
| 5233 | rhs_code = INTEGER_CST; |
| 5234 | ops[0].val = rhs; |
| 5235 | } |
| 5236 | else if (TREE_CODE (rhs) == SSA_NAME) |
| 5237 | { |
| 5238 | gimple *def_stmt = SSA_NAME_DEF_STMT (rhs), *def_stmt1, *def_stmt2; |
| 5239 | if (!is_gimple_assign (def_stmt)) |
| 5240 | invalid = true; |
| 5241 | else if (handled_load (def_stmt, &ops[0], bitsize, bitpos, |
| 5242 | bitregion_start, bitregion_end)) |
| 5243 | rhs_code = MEM_REF; |
| 5244 | else if (gimple_assign_rhs_code (def_stmt) == BIT_NOT_EXPR) |
| 5245 | { |
| 5246 | tree rhs1 = gimple_assign_rhs1 (def_stmt); |
| 5247 | if (TREE_CODE (rhs1) == SSA_NAME |
| 5248 | && is_gimple_assign (SSA_NAME_DEF_STMT (rhs1))) |
| 5249 | { |
| 5250 | bit_not_p = true; |
| 5251 | def_stmt = SSA_NAME_DEF_STMT (rhs1); |
| 5252 | } |
| 5253 | } |
| 5254 | |
| 5255 | if (rhs_code == ERROR_MARK && !invalid) |
| 5256 | switch ((rhs_code = gimple_assign_rhs_code (def_stmt))) |
| 5257 | { |
| 5258 | case BIT_AND_EXPR: |
| 5259 | case BIT_IOR_EXPR: |
| 5260 | case BIT_XOR_EXPR: |
| 5261 | tree rhs1, rhs2; |
| 5262 | rhs1 = gimple_assign_rhs1 (def_stmt); |
| 5263 | rhs2 = gimple_assign_rhs2 (def_stmt); |
| 5264 | invalid = true; |
| 5265 | if (TREE_CODE (rhs1) != SSA_NAME) |
| 5266 | break; |
| 5267 | def_stmt1 = SSA_NAME_DEF_STMT (rhs1); |
| 5268 | if (!is_gimple_assign (def_stmt1) |
| 5269 | || !handled_load (def_stmt1, &ops[0], bitsize, bitpos, |
| 5270 | bitregion_start, bitregion_end)) |
| 5271 | break; |
| 5272 | if (rhs_valid_for_store_merging_p (rhs2)) |
| 5273 | ops[1].val = rhs2; |
| 5274 | else if (TREE_CODE (rhs2) != SSA_NAME) |
| 5275 | break; |
| 5276 | else |
| 5277 | { |
| 5278 | def_stmt2 = SSA_NAME_DEF_STMT (rhs2); |
| 5279 | if (!is_gimple_assign (def_stmt2)) |
| 5280 | break; |
| 5281 | else if (!handled_load (def_stmt2, &ops[1], bitsize, bitpos, |
| 5282 | bitregion_start, bitregion_end)) |
| 5283 | break; |
| 5284 | } |
| 5285 | invalid = false; |
| 5286 | break; |
| 5287 | default: |
| 5288 | invalid = true; |
| 5289 | break; |
| 5290 | } |
| 5291 | |
| 5292 | unsigned HOST_WIDE_INT const_bitsize; |
| 5293 | if (bitsize.is_constant (&const_bitsize) |
| 5294 | && (const_bitsize % BITS_PER_UNIT) == 0 |
| 5295 | && const_bitsize <= 64 |
| 5296 | && multiple_p (bitpos, BITS_PER_UNIT)) |
| 5297 | { |
| 5298 | ins_stmt = find_bswap_or_nop_1 (def_stmt, &n, 12); |
| 5299 | if (ins_stmt) |
| 5300 | { |
| 5301 | uint64_t nn = n.n; |
| 5302 | for (unsigned HOST_WIDE_INT i = 0; |
| 5303 | i < const_bitsize; |
| 5304 | i += BITS_PER_UNIT, nn >>= BITS_PER_MARKER) |
| 5305 | if ((nn & MARKER_MASK) == 0 |
| 5306 | || (nn & MARKER_MASK) == MARKER_BYTE_UNKNOWN) |
| 5307 | { |
| 5308 | ins_stmt = NULL; |
| 5309 | break; |
| 5310 | } |
| 5311 | if (ins_stmt) |
| 5312 | { |
| 5313 | if (invalid) |
| 5314 | { |
| 5315 | rhs_code = LROTATE_EXPR; |
| 5316 | ops[0].base_addr = NULL_TREE; |
| 5317 | ops[1].base_addr = NULL_TREE; |
| 5318 | } |
| 5319 | invalid = false; |
| 5320 | } |
| 5321 | } |
| 5322 | } |
| 5323 | |
| 5324 | if (invalid |
| 5325 | && bitsize.is_constant (&const_bitsize) |
| 5326 | && ((const_bitsize % BITS_PER_UNIT) != 0 |
| 5327 | || !multiple_p (bitpos, BITS_PER_UNIT)) |
| 5328 | && const_bitsize <= MAX_FIXED_MODE_SIZE) |
| 5329 | { |
| 5330 | /* Bypass a conversion to the bit-field type. */ |
| 5331 | if (!bit_not_p |
| 5332 | && is_gimple_assign (def_stmt) |
| 5333 | && CONVERT_EXPR_CODE_P (rhs_code)) |
| 5334 | { |
| 5335 | tree rhs1 = gimple_assign_rhs1 (def_stmt); |
| 5336 | if (TREE_CODE (rhs1) == SSA_NAME |
| 5337 | && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) |
| 5338 | rhs = rhs1; |
| 5339 | } |
| 5340 | rhs_code = BIT_INSERT_EXPR; |
| 5341 | bit_not_p = false; |
| 5342 | ops[0].val = rhs; |
| 5343 | ops[0].base_addr = NULL_TREE; |
| 5344 | ops[1].base_addr = NULL_TREE; |
| 5345 | invalid = false; |
| 5346 | } |
| 5347 | } |
| 5348 | else |
| 5349 | invalid = true; |
| 5350 | |
| 5351 | unsigned HOST_WIDE_INT const_bitsize, const_bitpos; |
| 5352 | unsigned HOST_WIDE_INT const_bitregion_start, const_bitregion_end; |
| 5353 | if (invalid |
| 5354 | || !bitsize.is_constant (&const_bitsize) |
| 5355 | || !bitpos.is_constant (&const_bitpos) |
| 5356 | || !bitregion_start.is_constant (&const_bitregion_start) |
| 5357 | || !bitregion_end.is_constant (&const_bitregion_end) |
| 5358 | || ((const_bitregion_end - const_bitregion_start + 1) / BITS_PER_UNIT |
| 5359 | > (unsigned) param_store_merging_max_size)) |
| 5360 | return terminate_all_aliasing_chains (NULL, stmt); |
| 5361 | |
| 5362 | if (!ins_stmt) |
| 5363 | memset (&n, 0, sizeof (n)); |
| 5364 | |
| 5365 | class imm_store_chain_info **chain_info = NULL; |
| 5366 | bool ret = false; |
| 5367 | if (base_addr) |
| 5368 | chain_info = m_stores.get (base_addr); |
| 5369 | |
| 5370 | store_immediate_info *info; |
| 5371 | if (chain_info) |
| 5372 | { |
| 5373 | unsigned int ord = (*chain_info)->m_store_info.length (); |
| 5374 | info = new store_immediate_info (const_bitsize, const_bitpos, |
| 5375 | const_bitregion_start, |
| 5376 | const_bitregion_end, |
| 5377 | stmt, ord, rhs_code, n, ins_stmt, |
| 5378 | bit_not_p, lp_nr_for_store (stmt), |
| 5379 | ops[0], ops[1]); |
| 5380 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 5381 | { |
| 5382 | fprintf (dump_file, "Recording immediate store from stmt:\n"); |
| 5383 | print_gimple_stmt (dump_file, stmt, 0); |
| 5384 | } |
| 5385 | (*chain_info)->m_store_info.safe_push (info); |
| 5386 | m_n_stores++; |
| 5387 | ret |= terminate_all_aliasing_chains (chain_info, stmt); |
| 5388 | /* If we reach the limit of stores to merge in a chain terminate and |
| 5389 | process the chain now. */ |
| 5390 | if ((*chain_info)->m_store_info.length () |
| 5391 | == (unsigned int) param_max_stores_to_merge) |
| 5392 | { |
| 5393 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 5394 | fprintf (dump_file, |
| 5395 | "Reached maximum number of statements to merge:\n"); |
| 5396 | ret |= terminate_and_process_chain (*chain_info); |
| 5397 | } |
| 5398 | } |
| 5399 | else |
| 5400 | { |
| 5401 | /* Store aliases any existing chain? */ |
| 5402 | ret |= terminate_all_aliasing_chains (NULL, stmt); |
| 5403 | |
| 5404 | /* Start a new chain. */ |
| 5405 | class imm_store_chain_info *new_chain |
| 5406 | = new imm_store_chain_info (m_stores_head, base_addr); |
| 5407 | info = new store_immediate_info (const_bitsize, const_bitpos, |
| 5408 | const_bitregion_start, |
| 5409 | const_bitregion_end, |
| 5410 | stmt, 0, rhs_code, n, ins_stmt, |
| 5411 | bit_not_p, lp_nr_for_store (stmt), |
| 5412 | ops[0], ops[1]); |
| 5413 | new_chain->m_store_info.safe_push (info); |
| 5414 | m_n_stores++; |
| 5415 | m_stores.put (base_addr, new_chain); |
| 5416 | m_n_chains++; |
| 5417 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 5418 | { |
| 5419 | fprintf (dump_file, "Starting active chain number %u with statement:\n", |
| 5420 | m_n_chains); |
| 5421 | print_gimple_stmt (dump_file, stmt, 0); |
| 5422 | fprintf (dump_file, "The base object is:\n"); |
| 5423 | print_generic_expr (dump_file, base_addr); |
| 5424 | fprintf (dump_file, "\n"); |
| 5425 | } |
| 5426 | } |
| 5427 | |
| 5428 | /* Prune oldest chains so that after adding the chain or store above |
| 5429 | we're again within the limits set by the params. */ |
| 5430 | if (m_n_chains > (unsigned)param_max_store_chains_to_track |
| 5431 | || m_n_stores > (unsigned)param_max_stores_to_track) |
| 5432 | { |
| 5433 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 5434 | fprintf (dump_file, "Too many chains (%u > %d) or stores (%u > %d), " |
| 5435 | "terminating oldest chain(s).\n", m_n_chains, |
| 5436 | param_max_store_chains_to_track, m_n_stores, |
| 5437 | param_max_stores_to_track); |
| 5438 | imm_store_chain_info **e = &m_stores_head; |
| 5439 | unsigned idx = 0; |
| 5440 | unsigned n_stores = 0; |
| 5441 | while (*e) |
| 5442 | { |
| 5443 | if (idx >= (unsigned)param_max_store_chains_to_track |
| 5444 | || (n_stores + (*e)->m_store_info.length () |
| 5445 | > (unsigned)param_max_stores_to_track)) |
| 5446 | ret |= terminate_and_process_chain (*e); |
| 5447 | else |
| 5448 | { |
| 5449 | n_stores += (*e)->m_store_info.length (); |
| 5450 | e = &(*e)->next; |
| 5451 | ++idx; |
| 5452 | } |
| 5453 | } |
| 5454 | } |
| 5455 | |
| 5456 | return ret; |
| 5457 | } |
| 5458 | |
| 5459 | /* Return true if STMT is a store valid for store merging. */ |
| 5460 | |
| 5461 | static bool |
| 5462 | store_valid_for_store_merging_p (gimple *stmt) |
| 5463 | { |
| 5464 | return gimple_assign_single_p (stmt) |
| 5465 | && gimple_vdef (stmt) |
| 5466 | && lhs_valid_for_store_merging_p (gimple_assign_lhs (stmt)) |
| 5467 | && (!gimple_has_volatile_ops (stmt) || gimple_clobber_p (stmt)); |
| 5468 | } |
| 5469 | |
| 5470 | enum basic_block_status { BB_INVALID, BB_VALID, BB_EXTENDED_VALID }; |
| 5471 | |
| 5472 | /* Return the status of basic block BB wrt store merging. */ |
| 5473 | |
| 5474 | static enum basic_block_status |
| 5475 | get_status_for_store_merging (basic_block bb) |
| 5476 | { |
| 5477 | unsigned int num_statements = 0; |
| 5478 | unsigned int num_constructors = 0; |
| 5479 | gimple_stmt_iterator gsi; |
| 5480 | edge e; |
| 5481 | gimple *last_stmt = NULL; |
| 5482 | |
| 5483 | for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
| 5484 | { |
| 5485 | gimple *stmt = gsi_stmt (gsi); |
| 5486 | |
| 5487 | if (is_gimple_debug (stmt)) |
| 5488 | continue; |
| 5489 | |
| 5490 | last_stmt = stmt; |
| 5491 | |
| 5492 | if (store_valid_for_store_merging_p (stmt) && ++num_statements >= 2) |
| 5493 | break; |
| 5494 | |
| 5495 | if (is_gimple_assign (stmt) |
| 5496 | && gimple_assign_rhs_code (stmt) == CONSTRUCTOR) |
| 5497 | { |
| 5498 | tree rhs = gimple_assign_rhs1 (stmt); |
| 5499 | if (VECTOR_TYPE_P (TREE_TYPE (rhs)) |
| 5500 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (rhs))) |
| 5501 | && gimple_assign_lhs (stmt) != NULL_TREE) |
| 5502 | { |
| 5503 | HOST_WIDE_INT sz |
| 5504 | = int_size_in_bytes (TREE_TYPE (rhs)) * BITS_PER_UNIT; |
| 5505 | if (sz == 16 || sz == 32 || sz == 64) |
| 5506 | { |
| 5507 | num_constructors = 1; |
| 5508 | break; |
| 5509 | } |
| 5510 | } |
| 5511 | } |
| 5512 | } |
| 5513 | |
| 5514 | if (num_statements == 0 && num_constructors == 0) |
| 5515 | return BB_INVALID; |
| 5516 | |
| 5517 | if (cfun->can_throw_non_call_exceptions && cfun->eh |
| 5518 | && store_valid_for_store_merging_p (last_stmt) |
| 5519 | && (e = find_fallthru_edge (bb->succs)) |
| 5520 | && e->dest == bb->next_bb) |
| 5521 | return BB_EXTENDED_VALID; |
| 5522 | |
| 5523 | return (num_statements >= 2 || num_constructors) ? BB_VALID : BB_INVALID; |
| 5524 | } |
| 5525 | |
| 5526 | /* Entry point for the pass. Go over each basic block recording chains of |
| 5527 | immediate stores. Upon encountering a terminating statement (as defined |
| 5528 | by stmt_terminates_chain_p) process the recorded stores and emit the widened |
| 5529 | variants. */ |
| 5530 | |
| 5531 | unsigned int |
| 5532 | pass_store_merging::execute (function *fun) |
| 5533 | { |
| 5534 | basic_block bb; |
| 5535 | hash_set<gimple *> orig_stmts; |
| 5536 | bool changed = false, open_chains = false; |
| 5537 | |
| 5538 | /* If the function can throw and catch non-call exceptions, we'll be trying |
| 5539 | to merge stores across different basic blocks so we need to first unsplit |
| 5540 | the EH edges in order to streamline the CFG of the function. */ |
| 5541 | if (cfun->can_throw_non_call_exceptions && cfun->eh) |
| 5542 | unsplit_eh_edges (); |
| 5543 | |
| 5544 | calculate_dominance_info (CDI_DOMINATORS); |
| 5545 | |
| 5546 | FOR_EACH_BB_FN (bb, fun) |
| 5547 | { |
| 5548 | const basic_block_status bb_status = get_status_for_store_merging (bb); |
| 5549 | gimple_stmt_iterator gsi; |
| 5550 | |
| 5551 | if (open_chains && (bb_status == BB_INVALID || !single_pred_p (bb))) |
| 5552 | { |
| 5553 | changed |= terminate_and_process_all_chains (); |
| 5554 | open_chains = false; |
| 5555 | } |
| 5556 | |
| 5557 | if (bb_status == BB_INVALID) |
| 5558 | continue; |
| 5559 | |
| 5560 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 5561 | fprintf (dump_file, "Processing basic block <%d>:\n", bb->index); |
| 5562 | |
| 5563 | for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); ) |
| 5564 | { |
| 5565 | gimple *stmt = gsi_stmt (gsi); |
| 5566 | gsi_next (&gsi); |
| 5567 | |
| 5568 | if (is_gimple_debug (stmt)) |
| 5569 | continue; |
| 5570 | |
| 5571 | if (gimple_has_volatile_ops (stmt) && !gimple_clobber_p (stmt)) |
| 5572 | { |
| 5573 | /* Terminate all chains. */ |
| 5574 | if (dump_file && (dump_flags & TDF_DETAILS)) |
| 5575 | fprintf (dump_file, "Volatile access terminates " |
| 5576 | "all chains\n"); |
| 5577 | changed |= terminate_and_process_all_chains (); |
| 5578 | open_chains = false; |
| 5579 | continue; |
| 5580 | } |
| 5581 | |
| 5582 | if (is_gimple_assign (stmt) |
| 5583 | && gimple_assign_rhs_code (stmt) == CONSTRUCTOR |
| 5584 | && maybe_optimize_vector_constructor (stmt)) |
| 5585 | continue; |
| 5586 | |
| 5587 | if (store_valid_for_store_merging_p (stmt)) |
| 5588 | changed |= process_store (stmt); |
| 5589 | else |
| 5590 | changed |= terminate_all_aliasing_chains (NULL, stmt); |
| 5591 | } |
| 5592 | |
| 5593 | if (bb_status == BB_EXTENDED_VALID) |
| 5594 | open_chains = true; |
| 5595 | else |
| 5596 | { |
| 5597 | changed |= terminate_and_process_all_chains (); |
| 5598 | open_chains = false; |
| 5599 | } |
| 5600 | } |
| 5601 | |
| 5602 | if (open_chains) |
| 5603 | changed |= terminate_and_process_all_chains (); |
| 5604 | |
| 5605 | /* If the function can throw and catch non-call exceptions and something |
| 5606 | changed during the pass, then the CFG has (very likely) changed too. */ |
| 5607 | if (cfun->can_throw_non_call_exceptions && cfun->eh && changed) |
| 5608 | { |
| 5609 | free_dominance_info (CDI_DOMINATORS); |
| 5610 | return TODO_cleanup_cfg; |
| 5611 | } |
| 5612 | |
| 5613 | return 0; |
| 5614 | } |
| 5615 | |
| 5616 | } // anon namespace |
| 5617 | |
| 5618 | /* Construct and return a store merging pass object. */ |
| 5619 | |
| 5620 | gimple_opt_pass * |
| 5621 | make_pass_store_merging (gcc::context *ctxt) |
| 5622 | { |
| 5623 | return new pass_store_merging (ctxt); |
| 5624 | } |
| 5625 | |
| 5626 | #if CHECKING_P |
| 5627 | |
| 5628 | namespace selftest { |
| 5629 | |
| 5630 | /* Selftests for store merging helpers. */ |
| 5631 | |
| 5632 | /* Assert that all elements of the byte arrays X and Y, both of length N |
| 5633 | are equal. */ |
| 5634 | |
| 5635 | static void |
| 5636 | verify_array_eq (unsigned char *x, unsigned char *y, unsigned int n) |
| 5637 | { |
| 5638 | for (unsigned int i = 0; i < n; i++) |
| 5639 | { |
| 5640 | if (x[i] != y[i]) |
| 5641 | { |
| 5642 | fprintf (stderr, "Arrays do not match. X:\n"); |
| 5643 | dump_char_array (stderr, x, n); |
| 5644 | fprintf (stderr, "Y:\n"); |
| 5645 | dump_char_array (stderr, y, n); |
| 5646 | } |
| 5647 | ASSERT_EQ (x[i], y[i]); |
| 5648 | } |
| 5649 | } |
| 5650 | |
| 5651 | /* Test shift_bytes_in_array_left and that it carries bits across between |
| 5652 | bytes correctly. */ |
| 5653 | |
| 5654 | static void |
| 5655 | verify_shift_bytes_in_array_left (void) |
| 5656 | { |
| 5657 | /* byte 1 | byte 0 |
| 5658 | 00011111 | 11100000. */ |
| 5659 | unsigned char orig[2] = { 0xe0, 0x1f }; |
| 5660 | unsigned char in[2]; |
| 5661 | memcpy (in, orig, sizeof orig); |
| 5662 | |
| 5663 | unsigned char expected[2] = { 0x80, 0x7f }; |
| 5664 | shift_bytes_in_array_left (in, sizeof (in), 2); |
| 5665 | verify_array_eq (in, expected, sizeof (in)); |
| 5666 | |
| 5667 | memcpy (in, orig, sizeof orig); |
| 5668 | memcpy (expected, orig, sizeof orig); |
| 5669 | /* Check that shifting by zero doesn't change anything. */ |
| 5670 | shift_bytes_in_array_left (in, sizeof (in), 0); |
| 5671 | verify_array_eq (in, expected, sizeof (in)); |
| 5672 | |
| 5673 | } |
| 5674 | |
| 5675 | /* Test shift_bytes_in_array_right and that it carries bits across between |
| 5676 | bytes correctly. */ |
| 5677 | |
| 5678 | static void |
| 5679 | verify_shift_bytes_in_array_right (void) |
| 5680 | { |
| 5681 | /* byte 1 | byte 0 |
| 5682 | 00011111 | 11100000. */ |
| 5683 | unsigned char orig[2] = { 0x1f, 0xe0}; |
| 5684 | unsigned char in[2]; |
| 5685 | memcpy (in, orig, sizeof orig); |
| 5686 | unsigned char expected[2] = { 0x07, 0xf8}; |
| 5687 | shift_bytes_in_array_right (in, sizeof (in), 2); |
| 5688 | verify_array_eq (in, expected, sizeof (in)); |
| 5689 | |
| 5690 | memcpy (in, orig, sizeof orig); |
| 5691 | memcpy (expected, orig, sizeof orig); |
| 5692 | /* Check that shifting by zero doesn't change anything. */ |
| 5693 | shift_bytes_in_array_right (in, sizeof (in), 0); |
| 5694 | verify_array_eq (in, expected, sizeof (in)); |
| 5695 | } |
| 5696 | |
| 5697 | /* Test clear_bit_region that it clears exactly the bits asked and |
| 5698 | nothing more. */ |
| 5699 | |
| 5700 | static void |
| 5701 | verify_clear_bit_region (void) |
| 5702 | { |
| 5703 | /* Start with all bits set and test clearing various patterns in them. */ |
| 5704 | unsigned char orig[3] = { 0xff, 0xff, 0xff}; |
| 5705 | unsigned char in[3]; |
| 5706 | unsigned char expected[3]; |
| 5707 | memcpy (in, orig, sizeof in); |
| 5708 | |
| 5709 | /* Check zeroing out all the bits. */ |
| 5710 | clear_bit_region (in, 0, 3 * BITS_PER_UNIT); |
| 5711 | expected[0] = expected[1] = expected[2] = 0; |
| 5712 | verify_array_eq (in, expected, sizeof in); |
| 5713 | |
| 5714 | memcpy (in, orig, sizeof in); |
| 5715 | /* Leave the first and last bits intact. */ |
| 5716 | clear_bit_region (in, 1, 3 * BITS_PER_UNIT - 2); |
| 5717 | expected[0] = 0x1; |
| 5718 | expected[1] = 0; |
| 5719 | expected[2] = 0x80; |
| 5720 | verify_array_eq (in, expected, sizeof in); |
| 5721 | } |
| 5722 | |
| 5723 | /* Test clear_bit_region_be that it clears exactly the bits asked and |
| 5724 | nothing more. */ |
| 5725 | |
| 5726 | static void |
| 5727 | verify_clear_bit_region_be (void) |
| 5728 | { |
| 5729 | /* Start with all bits set and test clearing various patterns in them. */ |
| 5730 | unsigned char orig[3] = { 0xff, 0xff, 0xff}; |
| 5731 | unsigned char in[3]; |
| 5732 | unsigned char expected[3]; |
| 5733 | memcpy (in, orig, sizeof in); |
| 5734 | |
| 5735 | /* Check zeroing out all the bits. */ |
| 5736 | clear_bit_region_be (in, BITS_PER_UNIT - 1, 3 * BITS_PER_UNIT); |
| 5737 | expected[0] = expected[1] = expected[2] = 0; |
| 5738 | verify_array_eq (in, expected, sizeof in); |
| 5739 | |
| 5740 | memcpy (in, orig, sizeof in); |
| 5741 | /* Leave the first and last bits intact. */ |
| 5742 | clear_bit_region_be (in, BITS_PER_UNIT - 2, 3 * BITS_PER_UNIT - 2); |
| 5743 | expected[0] = 0x80; |
| 5744 | expected[1] = 0; |
| 5745 | expected[2] = 0x1; |
| 5746 | verify_array_eq (in, expected, sizeof in); |
| 5747 | } |
| 5748 | |
| 5749 | |
| 5750 | /* Run all of the selftests within this file. */ |
| 5751 | |
| 5752 | void |
| 5753 | store_merging_cc_tests (void) |
| 5754 | { |
| 5755 | verify_shift_bytes_in_array_left (); |
| 5756 | verify_shift_bytes_in_array_right (); |
| 5757 | verify_clear_bit_region (); |
| 5758 | verify_clear_bit_region_be (); |
| 5759 | } |
| 5760 | |
| 5761 | } // namespace selftest |
| 5762 | #endif /* CHECKING_P. */ |