LCOV - code coverage report
Current view: top level - gcc - gimple-ssa-store-merging.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.6 % 2655 2539
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 71 71
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.