LCOV - code coverage report
Current view: top level - gcc - tree-ssa-dse.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 98.3 % 784 771
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 31 31
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Dead and redundant store elimination
       2              :    Copyright (C) 2004-2026 Free Software Foundation, Inc.
       3              : 
       4              : This file is part of GCC.
       5              : 
       6              : GCC is free software; you can redistribute it and/or modify
       7              : it under the terms of the GNU General Public License as published by
       8              : the Free Software Foundation; either version 3, or (at your option)
       9              : any later version.
      10              : 
      11              : GCC is distributed in the hope that it will be useful,
      12              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      13              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14              : GNU General Public License for more details.
      15              : 
      16              : You should have received a copy of the GNU General Public License
      17              : along with GCC; see the file COPYING3.  If not see
      18              : <http://www.gnu.org/licenses/>.  */
      19              : 
      20              : #include "config.h"
      21              : #include "system.h"
      22              : #include "coretypes.h"
      23              : #include "backend.h"
      24              : #include "rtl.h"
      25              : #include "tree.h"
      26              : #include "gimple.h"
      27              : #include "tree-pass.h"
      28              : #include "ssa.h"
      29              : #include "gimple-pretty-print.h"
      30              : #include "fold-const.h"
      31              : #include "gimple-iterator.h"
      32              : #include "tree-cfg.h"
      33              : #include "tree-dfa.h"
      34              : #include "tree-cfgcleanup.h"
      35              : #include "alias.h"
      36              : #include "tree-ssa-loop.h"
      37              : #include "tree-ssa-dse.h"
      38              : #include "builtins.h"
      39              : #include "gimple-fold.h"
      40              : #include "tree-eh.h"
      41              : #include "cfganal.h"
      42              : #include "cgraph.h"
      43              : #include "ipa-modref-tree.h"
      44              : #include "ipa-modref.h"
      45              : #include "target.h"
      46              : #include "tree-ssa-loop-niter.h"
      47              : #include "cfgloop.h"
      48              : #include "tree-data-ref.h"
      49              : #include "internal-fn.h"
      50              : #include "tree-ssa.h"
      51              : 
      52              : /* This file implements dead store elimination.
      53              : 
      54              :    A dead store is a store into a memory location which will later be
      55              :    overwritten by another store without any intervening loads.  In this
      56              :    case the earlier store can be deleted or trimmed if the store
      57              :    was partially dead.
      58              : 
      59              :    A redundant store is a store into a memory location which stores
      60              :    the exact same value as a prior store to the same memory location.
      61              :    While this can often be handled by dead store elimination, removing
      62              :    the redundant store is often better than removing or trimming the
      63              :    dead store.
      64              : 
      65              :    In our SSA + virtual operand world we use immediate uses of virtual
      66              :    operands to detect these cases.  If a store's virtual definition
      67              :    is used precisely once by a later store to the same location which
      68              :    post dominates the first store, then the first store is dead.  If
      69              :    the data stored is the same, then the second store is redundant.
      70              : 
      71              :    The single use of the store's virtual definition ensures that
      72              :    there are no intervening aliased loads and the requirement that
      73              :    the second load post dominate the first ensures that if the earlier
      74              :    store executes, then the later stores will execute before the function
      75              :    exits.
      76              : 
      77              :    It may help to think of this as first moving the earlier store to
      78              :    the point immediately before the later store.  Again, the single
      79              :    use of the virtual definition and the post-dominance relationship
      80              :    ensure that such movement would be safe.  Clearly if there are
      81              :    back to back stores, then the second is makes the first dead.  If
      82              :    the second store stores the same value, then the second store is
      83              :    redundant.
      84              : 
      85              :    Reviewing section 10.7.2 in Morgan's "Building an Optimizing Compiler"
      86              :    may also help in understanding this code since it discusses the
      87              :    relationship between dead store and redundant load elimination.  In
      88              :    fact, they are the same transformation applied to different views of
      89              :    the CFG.  */
      90              : 
      91              : static void delete_dead_or_redundant_call (gimple_stmt_iterator *, const char *);
      92              : 
      93              : /* Bitmap of blocks that have had EH statements cleaned.  We should
      94              :    remove their dead edges eventually.  */
      95              : static bitmap need_eh_cleanup;
      96              : static bitmap need_ab_cleanup;
      97              : 
      98              : /* STMT is a statement that may write into memory.  Analyze it and
      99              :    initialize WRITE to describe how STMT affects memory.  When
     100              :    MAY_DEF_OK is true then the function initializes WRITE to what
     101              :    the stmt may define.
     102              : 
     103              :    Return TRUE if the statement was analyzed, FALSE otherwise.
     104              : 
     105              :    It is always safe to return FALSE.  But typically better optimization
     106              :    can be achieved by analyzing more statements.  */
     107              : 
     108              : static bool
     109    244769101 : initialize_ao_ref_for_dse (gimple *stmt, ao_ref *write, bool may_def_ok = false)
     110              : {
     111              :   /* It's advantageous to handle certain mem* functions.  */
     112    244769101 :   if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
     113              :     {
     114      5451518 :       switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
     115              :         {
     116      1116395 :         case BUILT_IN_MEMCPY:
     117      1116395 :         case BUILT_IN_MEMMOVE:
     118      1116395 :         case BUILT_IN_MEMSET:
     119      1116395 :         case BUILT_IN_MEMCPY_CHK:
     120      1116395 :         case BUILT_IN_MEMMOVE_CHK:
     121      1116395 :         case BUILT_IN_MEMSET_CHK:
     122      1116395 :         case BUILT_IN_STRNCPY:
     123      1116395 :         case BUILT_IN_STRNCPY_CHK:
     124      1116395 :           {
     125      1116395 :             tree size = gimple_call_arg (stmt, 2);
     126      1116395 :             tree ptr = gimple_call_arg (stmt, 0);
     127      1116395 :             ao_ref_init_from_ptr_and_size (write, ptr, size);
     128      1116395 :             return true;
     129              :           }
     130              : 
     131              :         /* A calloc call can never be dead, but it can make
     132              :            subsequent stores redundant if they store 0 into
     133              :            the same memory locations.  */
     134         3755 :         case BUILT_IN_CALLOC:
     135         3755 :           {
     136         3755 :             tree nelem = gimple_call_arg (stmt, 0);
     137         3755 :             tree selem = gimple_call_arg (stmt, 1);
     138         3755 :             tree lhs;
     139         3755 :             if (TREE_CODE (nelem) == INTEGER_CST
     140         3178 :                 && TREE_CODE (selem) == INTEGER_CST
     141         6716 :                 && (lhs = gimple_call_lhs (stmt)) != NULL_TREE)
     142              :               {
     143         2950 :                 tree size = fold_build2 (MULT_EXPR, TREE_TYPE (nelem),
     144              :                                          nelem, selem);
     145         2950 :                 ao_ref_init_from_ptr_and_size (write, lhs, size);
     146         2950 :                 return true;
     147              :               }
     148              :           }
     149              : 
     150              :         default:
     151              :           break;
     152              :         }
     153              :     }
     154    239317583 :   else if (is_gimple_call (stmt)
     155    239317583 :            && gimple_call_internal_p (stmt))
     156              :     {
     157       530741 :       switch (gimple_call_internal_fn (stmt))
     158              :         {
     159         1374 :         case IFN_LEN_STORE:
     160         1374 :         case IFN_MASK_STORE:
     161         1374 :         case IFN_MASK_LEN_STORE:
     162         1374 :           {
     163         1374 :             internal_fn ifn = gimple_call_internal_fn (stmt);
     164         1374 :             int stored_value_index = internal_fn_stored_value_index (ifn);
     165         1374 :             int len_index = internal_fn_len_index (ifn);
     166         1374 :             if (ifn == IFN_LEN_STORE)
     167              :               {
     168            0 :                 tree len = gimple_call_arg (stmt, len_index);
     169            0 :                 tree bias = gimple_call_arg (stmt, len_index + 1);
     170            0 :                 if (tree_fits_uhwi_p (len))
     171              :                   {
     172            0 :                     ao_ref_init_from_ptr_and_size (write,
     173              :                                                    gimple_call_arg (stmt, 0),
     174              :                                                    int_const_binop (MINUS_EXPR,
     175              :                                                                     len, bias));
     176            0 :                     return true;
     177              :                   }
     178              :               }
     179              :             /* We cannot initialize a must-def ao_ref (in all cases) but we
     180              :                can provide a may-def variant.  */
     181         1374 :             if (may_def_ok)
     182              :               {
     183         1336 :                 ao_ref_init_from_ptr_and_range (
     184              :                   write, gimple_call_arg (stmt, 0), true, 0, -1,
     185         1336 :                   tree_to_poly_int64 (TYPE_SIZE (
     186              :                     TREE_TYPE (gimple_call_arg (stmt, stored_value_index)))));
     187         1336 :                 return true;
     188              :               }
     189              :             break;
     190              :           }
     191              :         default:;
     192              :         }
     193              :     }
     194    243648420 :   if (tree lhs = gimple_get_lhs (stmt))
     195              :     {
     196    228565466 :       if (TREE_CODE (lhs) != SSA_NAME
     197    228565466 :           && (may_def_ok || !stmt_could_throw_p (cfun, stmt)))
     198              :         {
     199    211484846 :           ao_ref_init (write, lhs);
     200    211484846 :           return true;
     201              :         }
     202              :     }
     203              :   return false;
     204              : }
     205              : 
     206              : /* Given REF from the alias oracle, return TRUE if it is a valid
     207              :    kill memory reference for dead store elimination, false otherwise.
     208              : 
     209              :    In particular, the reference must have a known base, known maximum
     210              :    size, start at a byte offset and have a size that is one or more
     211              :    bytes.  */
     212              : 
     213              : static bool
     214    178473511 : valid_ao_ref_kill_for_dse (ao_ref *ref)
     215              : {
     216    178473511 :   return (ao_ref_base (ref)
     217    178473511 :           && known_size_p (ref->max_size)
     218    178122619 :           && maybe_ne (ref->size, 0)
     219    178104798 :           && known_eq (ref->max_size, ref->size)
     220    355984752 :           && known_ge (ref->offset, 0));
     221              : }
     222              : 
     223              : /* Given REF from the alias oracle, return TRUE if it is a valid
     224              :    load or store memory reference for dead store elimination, false otherwise.
     225              : 
     226              :    Unlike for valid_ao_ref_kill_for_dse we can accept writes where max_size
     227              :    is not same as size since we can handle conservatively the larger range.  */
     228              : 
     229              : static bool
     230     39428619 : valid_ao_ref_for_dse (ao_ref *ref)
     231              : {
     232     39428619 :   return (ao_ref_base (ref)
     233     39428619 :           && known_size_p (ref->max_size)
     234     78345263 :           && known_ge (ref->offset, 0));
     235              : }
     236              : 
     237              : /* Initialize OFFSET and SIZE to a range known to contain REF
     238              :    where the boundaries are divisible by BITS_PER_UNIT (bit still in bits).
     239              :    Return false if this is impossible.  */
     240              : 
     241              : static bool
     242    107756788 : get_byte_aligned_range_containing_ref (ao_ref *ref, poly_int64 *offset,
     243              :                                        HOST_WIDE_INT *size)
     244              : {
     245            0 :   if (!known_size_p (ref->max_size))
     246              :     return false;
     247    107756788 :   *offset = aligned_lower_bound (ref->offset, BITS_PER_UNIT);
     248    107756788 :   poly_int64 end = aligned_upper_bound (ref->offset + ref->max_size,
     249              :                                         BITS_PER_UNIT);
     250    107756788 :   return (end - *offset).is_constant (size);
     251              : }
     252              : 
     253              : /* Initialize OFFSET and SIZE to a range known to be contained REF
     254              :    where the boundaries are divisible by BITS_PER_UNIT (but still in bits).
     255              :    Return false if this is impossible.  */
     256              : 
     257              : static bool
     258    100475119 : get_byte_aligned_range_contained_in_ref (ao_ref *ref, poly_int64 *offset,
     259              :                                          HOST_WIDE_INT *size)
     260              : {
     261    100475119 :   if (!known_size_p (ref->size)
     262    100475119 :       || !known_eq (ref->size, ref->max_size))
     263              :     return false;
     264    100475119 :   *offset = aligned_upper_bound (ref->offset, BITS_PER_UNIT);
     265    100475119 :   poly_int64 end = aligned_lower_bound (ref->offset + ref->max_size,
     266              :                                         BITS_PER_UNIT);
     267              :   /* For bit accesses we can get -1 here, but also 0 sized kill is not
     268              :      useful.  */
     269    100475119 :   if (!known_gt (end, *offset))
     270              :     return false;
     271    100323152 :   return (end - *offset).is_constant (size);
     272              : }
     273              : 
     274              : /* Compute byte range (returned iN REF_OFFSET and RET_SIZE) for access COPY
     275              :    inside REF.  If KILL is true, then COPY represent a kill and the byte range
     276              :    needs to be fully contained in bit range given by COPY.  If KILL is false
     277              :    then the byte range returned must contain the range of COPY.  */
     278              : 
     279              : static bool
     280    104191937 : get_byte_range (ao_ref *copy, ao_ref *ref, bool kill,
     281              :                 HOST_WIDE_INT *ret_offset, HOST_WIDE_INT *ret_size)
     282              : {
     283    104191937 :   HOST_WIDE_INT copy_size, ref_size;
     284    104191937 :   poly_int64 copy_offset, ref_offset;
     285    104191937 :   HOST_WIDE_INT diff;
     286              : 
     287              :   /* First translate from bits to bytes, rounding to bigger or smaller ranges
     288              :      as needed.  Kills needs to be always rounded to smaller ranges while
     289              :      uses and stores to larger ranges.  */
     290    104191937 :   if (kill)
     291              :     {
     292    100475119 :       if (!get_byte_aligned_range_contained_in_ref (copy, &copy_offset,
     293              :                                                     &copy_size))
     294              :         return false;
     295              :     }
     296              :   else
     297              :     {
     298      3716818 :       if (!get_byte_aligned_range_containing_ref (copy, &copy_offset,
     299              :                                                   &copy_size))
     300              :         return false;
     301              :     }
     302              : 
     303    104039970 :   if (!get_byte_aligned_range_containing_ref (ref, &ref_offset, &ref_size)
     304              :       || !ordered_p (copy_offset, ref_offset))
     305              :     return false;
     306              : 
     307              :   /* Switch sizes from bits to bytes so we do not need to care about
     308              :      overflows.  Offset calculation needs to stay in bits until we compute
     309              :      the difference and can switch to HOST_WIDE_INT.  */
     310    104039970 :   copy_size /= BITS_PER_UNIT;
     311    104039970 :   ref_size /= BITS_PER_UNIT;
     312              : 
     313              :   /* If COPY starts before REF, then reset the beginning of
     314              :      COPY to match REF and decrease the size of COPY by the
     315              :      number of bytes removed from COPY.  */
     316    104039970 :   if (maybe_lt (copy_offset, ref_offset))
     317              :     {
     318     10039476 :       if (!(ref_offset - copy_offset).is_constant (&diff)
     319     10039476 :           || copy_size < diff / BITS_PER_UNIT)
     320              :         return false;
     321      2842040 :       copy_size -= diff / BITS_PER_UNIT;
     322      2842040 :       copy_offset = ref_offset;
     323              :     }
     324              : 
     325     96842534 :   if (!(copy_offset - ref_offset).is_constant (&diff)
     326     96842534 :       || ref_size <= diff / BITS_PER_UNIT)
     327              :     return false;
     328              : 
     329              :   /* If COPY extends beyond REF, chop off its size appropriately.  */
     330      6473664 :   HOST_WIDE_INT limit = ref_size - diff / BITS_PER_UNIT;
     331              : 
     332      6473664 :   if (copy_size > limit)
     333      1162313 :     copy_size = limit;
     334      6473664 :   *ret_size = copy_size;
     335      6473664 :   if (!(copy_offset - ref_offset).is_constant (ret_offset))
     336              :     return false;
     337      6473664 :   *ret_offset /= BITS_PER_UNIT;
     338      6473664 :   return true;
     339              : }
     340              : 
     341              : /* Update LIVE_BYTES tracking REF for write to WRITE:
     342              :    Verify we have the same base memory address, the write
     343              :    has a known size and overlaps with REF.  */
     344              : static void
     345    178473511 : clear_live_bytes_for_ref (sbitmap live_bytes, ao_ref *ref, ao_ref *write)
     346              : {
     347    178473511 :   HOST_WIDE_INT start, size;
     348              : 
     349    178473511 :   if (valid_ao_ref_kill_for_dse (write)
     350    177510962 :       && operand_equal_p (write->base, ref->base, OEP_ADDRESS_OF)
     351    278948630 :       && get_byte_range (write, ref, true, &start, &size))
     352      2756846 :     bitmap_clear_range (live_bytes, start, size);
     353    178473511 : }
     354              : 
     355              : /* Clear any bytes written by STMT from the bitmap LIVE_BYTES.  The base
     356              :    address written by STMT must match the one found in REF, which must
     357              :    have its base address previously initialized.
     358              : 
     359              :    This routine must be conservative.  If we don't know the offset or
     360              :    actual size written, assume nothing was written.  */
     361              : 
     362              : static void
     363    192616208 : clear_bytes_written_by (sbitmap live_bytes, gimple *stmt, ao_ref *ref)
     364              : {
     365    192616208 :   ao_ref write;
     366              : 
     367    192616208 :   if (gcall *call = dyn_cast <gcall *> (stmt))
     368              :     {
     369      5082168 :       bool interposed;
     370      5082168 :       modref_summary *summary = get_modref_function_summary (call, &interposed);
     371              : 
     372      5082168 :       if (summary && !interposed)
     373       798285 :         for (auto kill : summary->kills)
     374        61617 :           if (kill.get_ao_ref (as_a <gcall *> (stmt), &write))
     375        61593 :             clear_live_bytes_for_ref (live_bytes, ref, &write);
     376              :     }
     377    192616208 :   if (!initialize_ao_ref_for_dse (stmt, &write))
     378              :     return;
     379              : 
     380    178411918 :   clear_live_bytes_for_ref (live_bytes, ref, &write);
     381              : }
     382              : 
     383              : /* REF is a memory write.  Extract relevant information from it and
     384              :    initialize the LIVE_BYTES bitmap.  If successful, return TRUE.
     385              :    Otherwise return FALSE.  */
     386              : 
     387              : static bool
     388     32804480 : setup_live_bytes_from_ref (ao_ref *ref, sbitmap live_bytes)
     389              : {
     390     32804480 :   HOST_WIDE_INT const_size;
     391     32804480 :   if (valid_ao_ref_for_dse (ref)
     392     32331325 :       && ((aligned_upper_bound (ref->offset + ref->max_size, BITS_PER_UNIT)
     393     32331325 :            - aligned_lower_bound (ref->offset,
     394     32331325 :                                   BITS_PER_UNIT)).is_constant (&const_size))
     395     32331325 :       && (const_size / BITS_PER_UNIT <= param_dse_max_object_size)
     396     64797880 :       && const_size > 1)
     397              :     {
     398     31993173 :       bitmap_clear (live_bytes);
     399     31993173 :       bitmap_set_range (live_bytes, 0, const_size / BITS_PER_UNIT);
     400     31993173 :       return true;
     401              :     }
     402              :   return false;
     403              : }
     404              : 
     405              : /* Compute the number of stored bytes that we can trim from the head and
     406              :    tail of REF.  LIVE is the bitmap of stores to REF that are still live.
     407              : 
     408              :    Store the number of bytes trimmed from the head and tail in TRIM_HEAD
     409              :    and TRIM_TAIL respectively.
     410              : 
     411              :    STMT is the statement being trimmed and is used for debugging dump
     412              :    output only.  */
     413              : 
     414              : static void
     415      3525608 : compute_trims (ao_ref *ref, sbitmap live, int *trim_head, int *trim_tail,
     416              :                gimple *stmt)
     417              : {
     418      3525608 :   *trim_head = 0;
     419      3525608 :   *trim_tail = 0;
     420              : 
     421              :   /* We use bitmaps biased such that ref->offset is contained in bit zero and
     422              :      the bitmap extends through ref->max_size, so we know that in the original
     423              :      bitmap bits 0 .. ref->max_size were true.  But we need to check that this
     424              :      covers the bytes of REF exactly.  */
     425      3525608 :   const unsigned int offset_align = known_alignment (ref->offset);
     426      3525608 :   const unsigned int size_align = known_alignment (ref->size);
     427      3525608 :   if ((offset_align > 0 && offset_align < BITS_PER_UNIT)
     428      3525325 :       || (size_align > 0 && size_align < BITS_PER_UNIT)
     429      7050933 :       || !known_eq (ref->size, ref->max_size))
     430        12519 :     return;
     431              : 
     432              :   /* Now identify how much, if any of the tail we can chop off.  */
     433      3513089 :   HOST_WIDE_INT const_size;
     434      3513089 :   int last_live = bitmap_last_set_bit (live);
     435      3513089 :   if (ref->size.is_constant (&const_size))
     436              :     {
     437      3513089 :       int last_orig = (const_size / BITS_PER_UNIT) - 1;
     438              :       /* We can leave inconvenient amounts on the tail as
     439              :          residual handling in mem* and str* functions is usually
     440              :          reasonably efficient.  */
     441      3513089 :       *trim_tail = last_orig - last_live;
     442              : 
     443              :       /* But don't trim away out of bounds accesses, as this defeats
     444              :          proper warnings.
     445              : 
     446              :          We could have a type with no TYPE_SIZE_UNIT or we could have a VLA
     447              :          where TYPE_SIZE_UNIT is not a constant.  */
     448      3513089 :       if (*trim_tail
     449        10419 :           && TYPE_SIZE_UNIT (TREE_TYPE (ref->base))
     450        10419 :           && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref->base))) == INTEGER_CST
     451      3523507 :           && compare_tree_int (TYPE_SIZE_UNIT (TREE_TYPE (ref->base)),
     452              :                                last_orig) <= 0)
     453          128 :         *trim_tail = 0;
     454              :     }
     455              : 
     456              :   /* Identify how much, if any of the head we can chop off.  */
     457      3513089 :   int first_orig = 0;
     458      3513089 :   int first_live = bitmap_first_set_bit (live);
     459      3513089 :   *trim_head = first_live - first_orig;
     460              : 
     461              :   /* If REF is aligned, try to maintain this alignment if it reduces
     462              :      the number of (power-of-two sized aligned) writes to memory.  */
     463      3513089 :   unsigned int align_bits;
     464      3513089 :   unsigned HOST_WIDE_INT bitpos;
     465      3415857 :   if ((*trim_head || *trim_tail)
     466       102799 :       && last_live - first_live >= 2
     467       100340 :       && ao_ref_alignment (ref, &align_bits, &bitpos)
     468        81559 :       && align_bits >= 32
     469        81181 :       && bitpos == 0
     470      3589894 :       && align_bits % BITS_PER_UNIT == 0)
     471              :     {
     472        76805 :       unsigned int align_units = align_bits / BITS_PER_UNIT;
     473        76805 :       if (align_units > 16)
     474              :         align_units = 16;
     475        78768 :       while ((first_live | (align_units - 1)) > (unsigned int)last_live)
     476         1963 :         align_units >>= 1;
     477              : 
     478        76805 :       if (*trim_head)
     479              :         {
     480        72439 :           unsigned int pos = first_live & (align_units - 1);
     481        80466 :           for (unsigned int i = 1; i <= align_units; i <<= 1)
     482              :             {
     483        80466 :               unsigned int mask = ~(i - 1);
     484        80466 :               unsigned int bytes = align_units - (pos & mask);
     485        80466 :               if (wi::popcount (bytes) <= 1)
     486              :                 {
     487        72439 :                   *trim_head &= mask;
     488        72439 :                   break;
     489              :                 }
     490              :             }
     491              :         }
     492              : 
     493        76805 :       if (*trim_tail)
     494              :         {
     495         7211 :           unsigned int pos = last_live & (align_units - 1);
     496        10480 :           for (unsigned int i = 1; i <= align_units; i <<= 1)
     497              :             {
     498        10480 :               int mask = i - 1;
     499        10480 :               unsigned int bytes = (pos | mask) + 1;
     500        10480 :               if ((last_live | mask) > (last_live + *trim_tail))
     501              :                 break;
     502        10480 :               if (wi::popcount (bytes) <= 1)
     503              :                 {
     504         7211 :                   unsigned int extra = (last_live | mask) - last_live;
     505         7211 :                   *trim_tail -= extra;
     506         7211 :                   break;
     507              :                 }
     508              :             }
     509              :         }
     510              :     }
     511              : 
     512      3513089 :   if ((*trim_head || *trim_tail) && dump_file && (dump_flags & TDF_DETAILS))
     513              :     {
     514           18 :       fprintf (dump_file, "  Trimming statement (head = %d, tail = %d): ",
     515              :                *trim_head, *trim_tail);
     516           18 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
     517           18 :       fprintf (dump_file, "\n");
     518              :     }
     519              : }
     520              : 
     521              : /* STMT initializes an object from COMPLEX_CST where one or more of the bytes
     522              :    written may be dead stores.  REF is a representation of the memory written.
     523              :    LIVE is the bitmap of stores to REF that are still live.
     524              : 
     525              :    Attempt to rewrite STMT so that only the real or the imaginary part of the
     526              :    object is actually stored.  */
     527              : 
     528              : static void
     529         6091 : maybe_trim_complex_store (ao_ref *ref, sbitmap live, gassign *stmt)
     530              : {
     531         6091 :   int trim_head, trim_tail;
     532         6091 :   compute_trims (ref, live, &trim_head, &trim_tail, stmt);
     533              : 
     534              :   /* The amount of data trimmed from the head or tail must be at
     535              :      least half the size of the object to ensure we're trimming
     536              :      the entire real or imaginary half.  By writing things this
     537              :      way we avoid more O(n) bitmap operations.  */
     538         6091 :   if (known_ge (trim_tail * 2 * BITS_PER_UNIT, ref->size))
     539              :     {
     540              :       /* TREE_REALPART is live */
     541            2 :       tree x = TREE_REALPART (gimple_assign_rhs1 (stmt));
     542            2 :       tree y = gimple_assign_lhs (stmt);
     543            2 :       y = build1 (REALPART_EXPR, TREE_TYPE (x), y);
     544            2 :       gimple_assign_set_lhs (stmt, y);
     545            2 :       gimple_assign_set_rhs1 (stmt, x);
     546              :     }
     547         6089 :   else if (known_ge (trim_head * 2 * BITS_PER_UNIT, ref->size))
     548              :     {
     549              :       /* TREE_IMAGPART is live */
     550            3 :       tree x = TREE_IMAGPART (gimple_assign_rhs1 (stmt));
     551            3 :       tree y = gimple_assign_lhs (stmt);
     552            3 :       y = build1 (IMAGPART_EXPR, TREE_TYPE (x), y);
     553            3 :       gimple_assign_set_lhs (stmt, y);
     554            3 :       gimple_assign_set_rhs1 (stmt, x);
     555              :     }
     556              : 
     557              :   /* Other cases indicate parts of both the real and imag subobjects
     558              :      are live.  We do not try to optimize those cases.  */
     559         6091 : }
     560              : 
     561              : /* STMT initializes an object using a CONSTRUCTOR where one or more of the
     562              :    bytes written are dead stores.  REF is a representation of the memory
     563              :    written.  LIVE is the bitmap of stores to REF that are still live.
     564              : 
     565              :    Attempt to rewrite STMT so that it writes fewer memory locations.
     566              : 
     567              :    The most common case for getting here is a CONSTRUCTOR with no elements
     568              :    being used to zero initialize an object.  We do not try to handle other
     569              :    cases as those would force us to fully cover the object with the
     570              :    CONSTRUCTOR node except for the components that are dead.
     571              :    Also handles integer stores of 0 which can happen with memset/memcpy optimizations.  */
     572              : 
     573              : static void
     574      3378105 : maybe_trim_constructor_store (ao_ref *ref, sbitmap live, gassign *stmt,
     575              :                               bool was_integer_cst)
     576              : {
     577      3378105 :   tree ctor = gimple_assign_rhs1 (stmt);
     578              : 
     579              :   /* This is the only case we currently handle.  It actually seems to
     580              :      catch most cases of actual interest.  */
     581      3892060 :   gcc_assert (was_integer_cst ? integer_zerop (ctor) : CONSTRUCTOR_NELTS (ctor) == 0);
     582              : 
     583      3378105 :   int head_trim = 0;
     584      3378105 :   int tail_trim = 0;
     585      3378105 :   compute_trims (ref, live, &head_trim, &tail_trim, stmt);
     586              : 
     587              :   /* Now we want to replace the constructor initializer
     588              :      with memset (object + head_trim, 0, size - head_trim - tail_trim).  */
     589      3378105 :   if (head_trim || tail_trim)
     590              :     {
     591              :       /* We want &lhs for the MEM_REF expression.  */
     592        96066 :       tree lhs_addr = build_fold_addr_expr (gimple_assign_lhs (stmt));
     593              : 
     594        96066 :       STRIP_USELESS_TYPE_CONVERSION (lhs_addr);
     595              : 
     596        96066 :       if (! is_gimple_min_invariant (lhs_addr))
     597        18972 :         return;
     598              : 
     599              :       /* The number of bytes for the new constructor.  */
     600        77094 :       poly_int64 ref_bytes = exact_div (ref->size, BITS_PER_UNIT);
     601        77094 :       poly_int64 count = ref_bytes - head_trim - tail_trim;
     602              : 
     603              :       /* And the new type for the CONSTRUCTOR.  Essentially it's just
     604              :          a char array large enough to cover the non-trimmed parts of
     605              :          the original CONSTRUCTOR.  Note we want explicit bounds here
     606              :          so that we know how many bytes to clear when expanding the
     607              :          CONSTRUCTOR.  */
     608        77094 :       tree type = build_array_type_nelts (char_type_node, count);
     609              : 
     610              :       /* Build a suitable alias type rather than using alias set zero
     611              :          to avoid pessimizing.  */
     612        77094 :       tree alias_type = reference_alias_ptr_type (gimple_assign_lhs (stmt));
     613              : 
     614              :       /* Build a MEM_REF representing the whole accessed area, starting
     615              :          at the first byte not trimmed.  */
     616        77094 :       tree exp = fold_build2 (MEM_REF, type, lhs_addr,
     617              :                               build_int_cst (alias_type, head_trim));
     618              : 
     619              :       /* Now update STMT with a new RHS and LHS.  */
     620        77094 :       gimple_assign_set_lhs (stmt, exp);
     621        77094 :       gimple_assign_set_rhs1 (stmt, build_constructor (type, NULL));
     622              :     }
     623              : }
     624              : 
     625              : /* STMT is a memcpy, memmove or memset.  Decrement the number of bytes
     626              :    copied/set by DECREMENT.  */
     627              : static void
     628          865 : decrement_count (gimple *stmt, int decrement)
     629              : {
     630          865 :   tree *countp = gimple_call_arg_ptr (stmt, 2);
     631          865 :   gcc_assert (TREE_CODE (*countp) == INTEGER_CST);
     632         1730 :   *countp = wide_int_to_tree (TREE_TYPE (*countp), (TREE_INT_CST_LOW (*countp)
     633          865 :                                                     - decrement));
     634          865 : }
     635              : 
     636              : static void
     637          800 : increment_start_addr (gimple *stmt, tree *where, int increment)
     638              : {
     639          800 :   if (tree lhs = gimple_call_lhs (stmt))
     640            6 :     if (where == gimple_call_arg_ptr (stmt, 0))
     641              :       {
     642            6 :         gassign *newop = gimple_build_assign (lhs, unshare_expr (*where));
     643            6 :         gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
     644            6 :         gsi_insert_after (&gsi, newop, GSI_SAME_STMT);
     645            6 :         gimple_call_set_lhs (stmt, NULL_TREE);
     646            6 :         update_stmt (stmt);
     647              :       }
     648              : 
     649          800 :   if (TREE_CODE (*where) == SSA_NAME)
     650              :     {
     651          231 :       tree tem = make_ssa_name (TREE_TYPE (*where));
     652          231 :       gassign *newop
     653          231 :         = gimple_build_assign (tem, POINTER_PLUS_EXPR, *where,
     654          231 :                                build_int_cst (sizetype, increment));
     655          231 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
     656          231 :       gsi_insert_before (&gsi, newop, GSI_SAME_STMT);
     657          231 :       *where = tem;
     658          231 :       update_stmt (stmt);
     659          231 :       return;
     660              :     }
     661              : 
     662          569 :   *where = build_fold_addr_expr (fold_build2 (MEM_REF, char_type_node,
     663              :                                               *where,
     664              :                                               build_int_cst (ptr_type_node,
     665              :                                                              increment)));
     666          569 :   STRIP_USELESS_TYPE_CONVERSION (*where);
     667              : }
     668              : 
     669              : /* STMT is builtin call that writes bytes in bitmap ORIG, some bytes are dead
     670              :    (ORIG & ~NEW) and need not be stored.  Try to rewrite STMT to reduce
     671              :    the amount of data it actually writes.
     672              : 
     673              :    Right now we only support trimming from the head or the tail of the
     674              :    memory region.  In theory we could split the mem* call, but it's
     675              :    likely of marginal value.  */
     676              : 
     677              : static void
     678       141412 : maybe_trim_memstar_call (ao_ref *ref, sbitmap live, gimple *stmt)
     679              : {
     680       141412 :   int head_trim, tail_trim;
     681       141412 :   switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
     682              :     {
     683         4645 :     case BUILT_IN_STRNCPY:
     684         4645 :     case BUILT_IN_STRNCPY_CHK:
     685         4645 :       compute_trims (ref, live, &head_trim, &tail_trim, stmt);
     686         4645 :       if (head_trim)
     687              :         {
     688              :           /* Head trimming of strncpy is only possible if we can
     689              :              prove all bytes we would trim are non-zero (or we could
     690              :              turn the strncpy into memset if there must be zero
     691              :              among the head trimmed bytes).  If we don't know anything
     692              :              about those bytes, the presence or absence of '\0' bytes
     693              :              in there will affect whether it acts for the non-trimmed
     694              :              bytes as memset or memcpy/strncpy.  */
     695           74 :           c_strlen_data lendata = { };
     696           74 :           int orig_head_trim = head_trim;
     697           74 :           tree srcstr = gimple_call_arg (stmt, 1);
     698           74 :           if (!get_range_strlen (srcstr, &lendata, /*eltsize=*/1)
     699           74 :               || !tree_fits_uhwi_p (lendata.minlen))
     700            8 :             head_trim = 0;
     701           66 :           else if (tree_to_uhwi (lendata.minlen) < (unsigned) head_trim)
     702              :             {
     703           60 :               head_trim = tree_to_uhwi (lendata.minlen);
     704           60 :               if ((orig_head_trim & (UNITS_PER_WORD - 1)) == 0)
     705            0 :                 head_trim &= ~(UNITS_PER_WORD - 1);
     706              :             }
     707           74 :           if (orig_head_trim != head_trim
     708           68 :               && dump_file
     709           82 :               && (dump_flags & TDF_DETAILS))
     710            8 :             fprintf (dump_file,
     711              :                      "  Adjusting strncpy trimming to (head = %d,"
     712              :                      " tail = %d)\n", head_trim, tail_trim);
     713              :         }
     714         4645 :       goto do_memcpy;
     715              : 
     716        96201 :     case BUILT_IN_MEMCPY:
     717        96201 :     case BUILT_IN_MEMMOVE:
     718        96201 :     case BUILT_IN_MEMCPY_CHK:
     719        96201 :     case BUILT_IN_MEMMOVE_CHK:
     720        96201 :       compute_trims (ref, live, &head_trim, &tail_trim, stmt);
     721              : 
     722       100846 :     do_memcpy:
     723              :       /* Tail trimming is easy, we can just reduce the count.  */
     724       100846 :       if (tail_trim)
     725           75 :         decrement_count (stmt, tail_trim);
     726              : 
     727              :       /* Head trimming requires adjusting all the arguments.  */
     728       100846 :       if (head_trim)
     729              :         {
     730              :           /* For __*_chk need to adjust also the last argument.  */
     731          126 :           if (gimple_call_num_args (stmt) == 4)
     732              :             {
     733           49 :               tree size = gimple_call_arg (stmt, 3);
     734           49 :               if (!tree_fits_uhwi_p (size))
     735              :                 break;
     736            7 :               if (!integer_all_onesp (size))
     737              :                 {
     738            7 :                   unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
     739            7 :                   if (sz < (unsigned) head_trim)
     740              :                     break;
     741            7 :                   tree arg = wide_int_to_tree (TREE_TYPE (size),
     742            7 :                                                sz - head_trim);
     743            7 :                   gimple_call_set_arg (stmt, 3, arg);
     744              :                 }
     745              :             }
     746           84 :           tree *dst = gimple_call_arg_ptr (stmt, 0);
     747           84 :           increment_start_addr (stmt, dst, head_trim);
     748           84 :           tree *src = gimple_call_arg_ptr (stmt, 1);
     749           84 :           increment_start_addr (stmt, src, head_trim);
     750           84 :           decrement_count (stmt, head_trim);
     751              :         }
     752              :       break;
     753              : 
     754        40566 :     case BUILT_IN_MEMSET:
     755        40566 :     case BUILT_IN_MEMSET_CHK:
     756        40566 :       compute_trims (ref, live, &head_trim, &tail_trim, stmt);
     757              : 
     758              :       /* Tail trimming is easy, we can just reduce the count.  */
     759        40566 :       if (tail_trim)
     760           74 :         decrement_count (stmt, tail_trim);
     761              : 
     762              :       /* Head trimming requires adjusting all the arguments.  */
     763        40566 :       if (head_trim)
     764              :         {
     765              :           /* For __*_chk need to adjust also the last argument.  */
     766          632 :           if (gimple_call_num_args (stmt) == 4)
     767              :             {
     768            7 :               tree size = gimple_call_arg (stmt, 3);
     769            7 :               if (!tree_fits_uhwi_p (size))
     770              :                 break;
     771            7 :               if (!integer_all_onesp (size))
     772              :                 {
     773            7 :                   unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
     774            7 :                   if (sz < (unsigned) head_trim)
     775              :                     break;
     776            7 :                   tree arg = wide_int_to_tree (TREE_TYPE (size),
     777            7 :                                                sz - head_trim);
     778            7 :                   gimple_call_set_arg (stmt, 3, arg);
     779              :                 }
     780              :             }
     781          632 :           tree *dst = gimple_call_arg_ptr (stmt, 0);
     782          632 :           increment_start_addr (stmt, dst, head_trim);
     783          632 :           decrement_count (stmt, head_trim);
     784              :         }
     785              :       break;
     786              : 
     787              :     default:
     788              :       break;
     789              :     }
     790       141412 : }
     791              : 
     792              : /* STMT is a memory write where one or more bytes written are dead stores.
     793              :    REF is a representation of the memory written.  LIVE is the bitmap of
     794              :    stores to REF that are still live.
     795              : 
     796              :    Attempt to rewrite STMT so that it writes fewer memory locations.  Right
     797              :    now we only support trimming at the start or end of the memory region.
     798              :    It's not clear how much there is to be gained by trimming from the middle
     799              :    of the region.  */
     800              : 
     801              : static void
     802     27121570 : maybe_trim_partially_dead_store (ao_ref *ref, sbitmap live, gimple *stmt)
     803              : {
     804     27121570 :   auto assign = dyn_cast<gassign *> (stmt);
     805     25797981 :   if (assign && TREE_CODE (gimple_assign_lhs (assign)) != TARGET_MEM_REF)
     806              :     {
     807     51513940 :       switch (gimple_assign_rhs_code (assign))
     808              :         {
     809       513955 :         case CONSTRUCTOR:
     810       513955 :           maybe_trim_constructor_store (ref, live, assign, false);
     811       513955 :           break;
     812         6091 :         case COMPLEX_CST:
     813         6091 :           maybe_trim_complex_store (ref, live, assign);
     814         6091 :           break;
     815      9101487 :         case INTEGER_CST:
     816      9101487 :           if (integer_zerop (gimple_assign_rhs1 (assign))
     817      9101487 :               && type_has_mode_precision_p (TREE_TYPE
     818              :                                            (gimple_assign_lhs (assign))))
     819      2864150 :             maybe_trim_constructor_store (ref, live, assign, true);
     820              :           break;
     821              :         default:
     822              :           break;
     823              :         }
     824              :     }
     825     27121570 : }
     826              : 
     827              : /* Return TRUE if USE_REF reads bytes from LIVE where live is
     828              :    derived from REF, a write reference.
     829              : 
     830              :    While this routine may modify USE_REF, it's passed by value, not
     831              :    location.  So callers do not see those modifications.  */
     832              : 
     833              : static bool
     834      3716818 : live_bytes_read (ao_ref *use_ref, ao_ref *ref, sbitmap live)
     835              : {
     836              :   /* We have already verified that USE_REF and REF hit the same object.
     837              :      Now verify that there's actually an overlap between USE_REF and REF.  */
     838      3716818 :   HOST_WIDE_INT start, size;
     839      3716818 :   if (get_byte_range (use_ref, ref, false, &start, &size))
     840              :     {
     841              :       /* If USE_REF covers all of REF, then it will hit one or more
     842              :          live bytes.   This avoids useless iteration over the bitmap
     843              :          below.  */
     844      3716818 :       if (start == 0 && known_eq (size * 8, ref->size))
     845              :         return true;
     846              : 
     847              :       /* Now check if any of the remaining bits in use_ref are set in LIVE.  */
     848       994296 :       return bitmap_any_bit_in_range_p (live, start, (start + size - 1));
     849              :     }
     850              :   return true;
     851              : }
     852              : 
     853              : /* Callback for dse_classify_store calling for_each_index.  Verify that
     854              :    indices are invariant in the loop with backedge PHI in basic-block DATA.  */
     855              : 
     856              : static bool
     857      2921982 : check_name (tree, tree *idx, void *data)
     858              : {
     859      2921982 :   basic_block phi_bb = (basic_block) data;
     860      2921982 :   if (TREE_CODE (*idx) == SSA_NAME
     861      1877821 :       && !SSA_NAME_IS_DEFAULT_DEF (*idx)
     862      4656289 :       && dominated_by_p (CDI_DOMINATORS, gimple_bb (SSA_NAME_DEF_STMT (*idx)),
     863              :                          phi_bb))
     864      1555389 :     return false;
     865              :   return true;
     866              : }
     867              : 
     868              : /* STMT stores the value 0 into one or more memory locations
     869              :    (via memset, empty constructor, calloc call, etc).
     870              : 
     871              :    See if there is a subsequent store of the value 0 to one
     872              :    or more of the same memory location(s).  If so, the subsequent
     873              :    store is redundant and can be removed.
     874              : 
     875              :    The subsequent stores could be via memset, empty constructors,
     876              :    simple MEM stores, etc.  */
     877              : 
     878              : static void
     879      4370966 : dse_optimize_redundant_stores (gimple *stmt)
     880              : {
     881      4370966 :   int cnt = 0;
     882              : 
     883              :   /* TBAA state of STMT, if it is a call it is effectively alias-set zero.  */
     884      4370966 :   alias_set_type earlier_set = 0;
     885      4370966 :   alias_set_type earlier_base_set = 0;
     886      4370966 :   if (auto assign = dyn_cast<gassign *> (stmt))
     887              :     {
     888      4308239 :       ao_ref lhs_ref;
     889      4308239 :       ao_ref_init (&lhs_ref, gimple_assign_lhs (assign));
     890      4308239 :       earlier_set = ao_ref_alias_set (&lhs_ref);
     891      4308239 :       earlier_base_set = ao_ref_base_alias_set (&lhs_ref);
     892              :     }
     893              : 
     894              :   /* We could do something fairly complex and look through PHIs
     895              :      like DSE_CLASSIFY_STORE, but it doesn't seem to be worth
     896              :      the effort.
     897              : 
     898              :      Look at all the immediate uses of the VDEF (which are obviously
     899              :      dominated by STMT).   See if one or more stores 0 into the same
     900              :      memory locations a STMT, if so remove the immediate use statements.  */
     901      4370966 :   tree defvar = gimple_vdef (stmt);
     902      4370966 :   imm_use_iterator ui;
     903      4370966 :   gimple *use_stmt;
     904      9794850 :   FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
     905              :     {
     906              :       /* Limit stmt walking.  */
     907      5445265 :       if (++cnt > param_dse_max_alias_queries_per_store)
     908              :         break;
     909              : 
     910              :       /* If USE_STMT stores 0 into one or more of the same locations
     911              :          as STMT and STMT would kill USE_STMT, then we can just remove
     912              :          USE_STMT.  */
     913      5445265 :       tree fndecl;
     914      7831401 :       auto assign = dyn_cast<gassign *> (use_stmt);
     915      3846987 :       if ((assign
     916      3846987 :            && gimple_vdef (assign)
     917      3159213 :            && (gimple_assign_single_p (assign)
     918      3159213 :                && !gimple_has_volatile_ops (use_stmt)
     919      2997628 :                && initializer_zerop (gimple_assign_rhs1 (assign))))
     920      3984414 :           || (gimple_call_builtin_p (use_stmt, BUILT_IN_NORMAL)
     921       171381 :               && (fndecl = gimple_call_fndecl (use_stmt)) != NULL
     922       171381 :               && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET
     923       149689 :                   || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET_CHK)
     924        21783 :               && integer_zerop (gimple_call_arg (use_stmt, 1))))
     925              :         {
     926      1480096 :           ao_ref write;
     927              : 
     928      1480096 :           if (!initialize_ao_ref_for_dse (use_stmt, &write))
     929              :             break;
     930              : 
     931      1458715 :           if (valid_ao_ref_for_dse (&write)
     932      1458715 :               && stmt_kills_ref_p (stmt, &write))
     933              :             {
     934         5522 :               gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
     935         5522 :               if (auto use_assign = dyn_cast<gassign *> (use_stmt))
     936              :                 {
     937         5472 :                   ao_ref lhs_ref;
     938         5472 :                   ao_ref_init (&lhs_ref, gimple_assign_lhs (use_assign));
     939         5472 :                   if ((earlier_set == ao_ref_alias_set (&lhs_ref)
     940          789 :                        || alias_set_subset_of (ao_ref_alias_set (&lhs_ref),
     941              :                                                earlier_set))
     942         5648 :                       && (earlier_base_set == ao_ref_base_alias_set (&lhs_ref)
     943          482 :                           || alias_set_subset_of
     944          482 :                                (ao_ref_base_alias_set (&lhs_ref),
     945              :                                                   earlier_base_set)))
     946         4785 :                     delete_dead_or_redundant_assignment (&gsi, "redundant",
     947              :                                                          need_eh_cleanup,
     948              :                                                          need_ab_cleanup);
     949              :                 }
     950           50 :               else if (is_gimple_call (use_stmt))
     951              :                 {
     952           50 :                   if ((earlier_set == 0
     953            1 :                        || alias_set_subset_of (0, earlier_set))
     954           50 :                       && (earlier_base_set == 0
     955            0 :                           || alias_set_subset_of (0, earlier_base_set)))
     956           49 :                   delete_dead_or_redundant_call (&gsi, "redundant");
     957              :                 }
     958              :               else
     959            0 :                 gcc_unreachable ();
     960              :             }
     961              :         }
     962      4370966 :     }
     963      4370966 : }
     964              : 
     965              : /* Return whether PHI contains ARG as an argument.  */
     966              : 
     967              : static bool
     968      4404264 : contains_phi_arg (gphi *phi, tree arg)
     969              : {
     970     33478994 :   for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
     971     29335249 :     if (gimple_phi_arg_def (phi, i) == arg)
     972              :       return true;
     973              :   return false;
     974              : }
     975              : 
     976              : /* Hash map of the memory use in a GIMPLE assignment to its
     977              :    data reference.  If NULL data-ref analysis isn't used.  */
     978              : static hash_map<gimple *, data_reference_p> *dse_stmt_to_dr_map;
     979              : 
     980              : /* A helper of dse_optimize_stmt.
     981              :    Given a GIMPLE_ASSIGN in STMT that writes to REF, classify it
     982              :    according to downstream uses and defs.  Sets *BY_CLOBBER_P to true
     983              :    if only clobber statements influenced the classification result.
     984              :    Returns the classification.  */
     985              : 
     986              : static dse_store_status
     987     44194877 : dse_classify_store (ao_ref *ref, gimple *stmt,
     988              :                     bool byte_tracking_enabled, sbitmap live_bytes,
     989              :                     bool *by_clobber_p, tree stop_at_vuse, int &cnt,
     990              :                     bitmap visited)
     991              : {
     992     44194877 :   gimple *temp;
     993     44194877 :   std::unique_ptr<data_reference, void(*)(data_reference_p)>
     994     44194877 :     dra (nullptr, free_data_ref);
     995              : 
     996     44194877 :   if (by_clobber_p)
     997     43595046 :     *by_clobber_p = true;
     998              : 
     999              :   /* Find the first dominated statement that clobbers (part of) the
    1000              :      memory stmt stores to with no intermediate statement that may use
    1001              :      part of the memory stmt stores.  That is, find a store that may
    1002              :      prove stmt to be a dead store.  */
    1003     44194877 :   temp = stmt;
    1004    566959639 :   do
    1005              :     {
    1006    305577258 :       gimple *use_stmt;
    1007    305577258 :       imm_use_iterator ui;
    1008    305577258 :       bool fail = false;
    1009    305577258 :       tree defvar;
    1010              : 
    1011    305577258 :       if (gimple_code (temp) == GIMPLE_PHI)
    1012              :         {
    1013     21297519 :           defvar = PHI_RESULT (temp);
    1014     21297519 :           bitmap_set_bit (visited, SSA_NAME_VERSION (defvar));
    1015              :         }
    1016              :       else
    1017    568559478 :         defvar = gimple_vdef (temp);
    1018              : 
    1019    305577258 :       auto_vec<gimple *, 10> defs;
    1020    305577258 :       gphi *first_phi_def = NULL;
    1021    305577258 :       gphi *last_phi_def = NULL;
    1022              : 
    1023    305577258 :       auto_vec<tree, 10> worklist;
    1024    305577258 :       worklist.quick_push (defvar);
    1025              : 
    1026    308562439 :       do
    1027              :         {
    1028    308562439 :           defvar = worklist.pop ();
    1029              :           /* If we're instructed to stop walking at region boundary, do so.  */
    1030    308562439 :           if (defvar == stop_at_vuse)
    1031              :             return DSE_STORE_LIVE;
    1032              : 
    1033    308550368 :           use_operand_p usep;
    1034    663004118 :           FOR_EACH_IMM_USE_FAST (usep, ui, defvar)
    1035              :             {
    1036    384652850 :               use_stmt = USE_STMT (usep);
    1037              : 
    1038              :               /* Limit stmt walking.  */
    1039    384652850 :               if (++cnt > param_dse_max_alias_queries_per_store)
    1040              :                 {
    1041              :                   fail = true;
    1042              :                   break;
    1043              :                 }
    1044              : 
    1045              :               /* In simple cases we can look through PHI nodes, but we
    1046              :                  have to be careful with loops and with memory references
    1047              :                  containing operands that are also operands of PHI nodes.
    1048              :                  See gcc.c-torture/execute/20051110-*.c.  */
    1049    384337902 :               if (gphi *phi = dyn_cast <gphi *> (use_stmt))
    1050              :                 {
    1051              :                   /* Look through single-argument PHIs.  */
    1052     42152711 :                   if (gimple_phi_num_args (phi) == 1)
    1053      3525499 :                     worklist.safe_push (gimple_phi_result (phi));
    1054              :                   else
    1055              :                     {
    1056              :                       /* If we visit this PHI by following a backedge then we
    1057              :                          have to make sure ref->ref only refers to SSA names
    1058              :                          that are invariant with respect to the loop
    1059              :                          represented by this PHI node.  We handle irreducible
    1060              :                          regions by relying on backedge marking and identifying
    1061              :                          the head of the (sub-)region.  */
    1062     38627212 :                       edge e = gimple_phi_arg_edge
    1063     38627212 :                                  (phi, PHI_ARG_INDEX_FROM_USE (usep));
    1064     38627212 :                       if (e->flags & EDGE_DFS_BACK)
    1065              :                         {
    1066      3590880 :                           basic_block rgn_head
    1067      3590880 :                             = nearest_common_dominator (CDI_DOMINATORS,
    1068              :                                                         gimple_bb (phi),
    1069              :                                                         e->src);
    1070      3590880 :                           if (!for_each_index (ref->ref
    1071              :                                                ? &ref->ref : &ref->base,
    1072              :                                                check_name, rgn_head))
    1073      1555389 :                             return DSE_STORE_LIVE;
    1074              :                         }
    1075              :                       /* If we already visited this PHI ignore it for further
    1076              :                          processing.  But note we have to check each incoming
    1077              :                          edge above.  */
    1078     74143646 :                       if (!bitmap_bit_p (visited,
    1079     37071823 :                                          SSA_NAME_VERSION (PHI_RESULT (phi))))
    1080              :                         {
    1081     27580724 :                           defs.safe_push (phi);
    1082     27580724 :                           if (!first_phi_def)
    1083     23431886 :                             first_phi_def = phi;;
    1084              :                           last_phi_def = phi;
    1085              :                         }
    1086              :                     }
    1087              :                 }
    1088              :               /* If the statement is a use the store is not dead.  */
    1089    342185191 :               else if (ref_maybe_used_by_stmt_p (use_stmt, ref))
    1090              :                 {
    1091     28341370 :                   auto assign = dyn_cast<gassign *> (use_stmt);
    1092     28341370 :                   if (dse_stmt_to_dr_map
    1093      6173318 :                       && ref->ref
    1094      6068925 :                       && assign)
    1095              :                     {
    1096      1236710 :                       if (!dra)
    1097      1230704 :                         dra.reset (create_data_ref (NULL, NULL, ref->ref, stmt,
    1098              :                                                     false, false));
    1099      1236710 :                       bool existed_p;
    1100      1236710 :                       data_reference_p &drb
    1101      1236710 :                         = dse_stmt_to_dr_map->get_or_insert (assign,
    1102              :                                                              &existed_p);
    1103      1236710 :                       if (!existed_p)
    1104       746684 :                         drb = create_data_ref (NULL, NULL,
    1105              :                                                gimple_assign_rhs1 (assign),
    1106              :                                                assign, false, false);
    1107      1236710 :                       if (!dr_may_alias_p (dra.get (), drb, NULL))
    1108              :                         {
    1109        20404 :                           if (gimple_vdef (assign))
    1110           16 :                             defs.safe_push (assign);
    1111        10202 :                           continue;
    1112              :                         }
    1113              :                     }
    1114              : 
    1115              :                   /* Handle common cases where we can easily build an ao_ref
    1116              :                      structure for USE_STMT and in doing so we find that the
    1117              :                      references hit non-live bytes and thus can be ignored.
    1118              : 
    1119              :                      TODO: We can also use modref summary to handle calls.  */
    1120     28331168 :                   if (byte_tracking_enabled
    1121     28331168 :                       && is_gimple_assign (use_stmt))
    1122              :                     {
    1123      5165424 :                       ao_ref use_ref;
    1124      5165424 :                       ao_ref_init (&use_ref, gimple_assign_rhs1 (use_stmt));
    1125      5165424 :                       if (valid_ao_ref_for_dse (&use_ref)
    1126      5130543 :                           && operand_equal_p (use_ref.base, ref->base,
    1127              :                                               OEP_ADDRESS_OF)
    1128      8882242 :                           && !live_bytes_read (&use_ref, ref, live_bytes))
    1129              :                         {
    1130              :                           /* If this is a store, remember it as we possibly
    1131              :                              need to walk the defs uses.  */
    1132         4810 :                           if (gimple_vdef (use_stmt))
    1133          452 :                             defs.safe_push (use_stmt);
    1134         2405 :                           continue;
    1135              :                         }
    1136              :                     }
    1137              : 
    1138              :                   fail = true;
    1139              :                   break;
    1140              :                 }
    1141              :               /* We have visited ourselves already so ignore STMT for the
    1142              :                  purpose of chaining.  */
    1143    313843821 :               else if (use_stmt == stmt)
    1144              :                 ;
    1145              :               /* If this is a store, remember it as we possibly need to walk the
    1146              :                  defs uses.  */
    1147    937779662 :               else if (gimple_vdef (use_stmt))
    1148    269557812 :                 defs.safe_push (use_stmt);
    1149      1555389 :             }
    1150              :         }
    1151    613989958 :       while (!fail && !worklist.is_empty ());
    1152              : 
    1153    304009798 :       if (fail)
    1154              :         {
    1155              :           /* STMT might be partially dead and we may be able to reduce
    1156              :              how many memory locations it stores into.  */
    1157     28643711 :           if (byte_tracking_enabled && !gimple_clobber_p (stmt))
    1158     25668527 :             return DSE_STORE_MAYBE_PARTIAL_DEAD;
    1159              :           return DSE_STORE_LIVE;
    1160              :         }
    1161              : 
    1162              :       /* If we didn't find any definition this means the store is dead
    1163              :          if it isn't a store to global reachable memory.  In this case
    1164              :          just pretend the stmt makes itself dead.  Otherwise fail.  */
    1165    275366087 :       if (defs.is_empty ())
    1166              :         {
    1167      2799406 :           if (ref_may_alias_global_p (ref, false))
    1168              :             {
    1169        41969 :               basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (defvar));
    1170              :               /* Assume that BUILT_IN_UNREACHABLE and BUILT_IN_UNREACHABLE_TRAP
    1171              :                  do not need to keep (global) memory side-effects live.
    1172              :                  We do not have virtual operands on BUILT_IN_UNREACHABLE
    1173              :                  but we can do poor mans reachability when the last
    1174              :                  definition we want to elide is in the block that ends
    1175              :                  in such a call.  */
    1176        41969 :               if (EDGE_COUNT (def_bb->succs) == 0)
    1177        57368 :                 if (gcall *last = dyn_cast <gcall *> (*gsi_last_bb (def_bb)))
    1178          607 :                   if (gimple_call_builtin_p (last, BUILT_IN_UNREACHABLE)
    1179          607 :                       || gimple_call_builtin_p (last,
    1180              :                                                 BUILT_IN_UNREACHABLE_TRAP))
    1181              :                     {
    1182          439 :                       if (by_clobber_p)
    1183          439 :                         *by_clobber_p = false;
    1184              :                       return DSE_STORE_DEAD;
    1185              :                     }
    1186              :               return DSE_STORE_LIVE;
    1187              :             }
    1188              : 
    1189      2757437 :           if (by_clobber_p)
    1190      2753824 :             *by_clobber_p = false;
    1191              :           return DSE_STORE_DEAD;
    1192              :         }
    1193              : 
    1194              :       /* Process defs and remove those we need not process further.  */
    1195    564062981 :       for (unsigned i = 0; i < defs.length ();)
    1196              :         {
    1197    291560707 :           gimple *def = defs[i];
    1198    291560707 :           gimple *use_stmt;
    1199    291560707 :           use_operand_p use_p;
    1200    291560707 :           tree vdef = (gimple_code (def) == GIMPLE_PHI
    1201    316920999 :                        ? gimple_phi_result (def) : gimple_vdef (def));
    1202    291560707 :           gphi *phi_def;
    1203              :           /* If the path to check starts with a kill we do not need to
    1204              :              process it further.
    1205              :              ???  With byte tracking we need only kill the bytes currently
    1206              :              live.  */
    1207    291560707 :           if (stmt_kills_ref_p (def, ref))
    1208              :             {
    1209      2728364 :               if (by_clobber_p && !gimple_clobber_p (def))
    1210       673029 :                 *by_clobber_p = false;
    1211      2728364 :               defs.unordered_remove (i);
    1212              :             }
    1213              :           /* If the path ends here we do not need to process it further.
    1214              :              This for example happens with calls to noreturn functions.  */
    1215    288832343 :           else if (has_zero_uses (vdef))
    1216              :             {
    1217              :               /* But if the store is to global memory it is definitely
    1218              :                  not dead.  */
    1219      3096584 :               if (ref_may_alias_global_p (ref, false))
    1220        64407 :                 return DSE_STORE_LIVE;
    1221      3032177 :               defs.unordered_remove (i);
    1222              :             }
    1223              :           /* In addition to kills we can remove defs whose only use
    1224              :              is another def in defs.  That can only ever be PHIs of which
    1225              :              we track two for simplicity reasons, the first and last in
    1226              :              {first,last}_phi_def (we fail for multiple PHIs anyways).
    1227              :              We can also ignore defs that feed only into
    1228              :              already visited PHIs.  */
    1229    285735759 :           else if (single_imm_use (vdef, &use_p, &use_stmt)
    1230    285735759 :                    && (use_stmt == first_phi_def
    1231    246979418 :                        || use_stmt == last_phi_def
    1232    246875464 :                        || (gimple_code (use_stmt) == GIMPLE_PHI
    1233     16420052 :                            && bitmap_bit_p (visited,
    1234     16420052 :                                             SSA_NAME_VERSION
    1235              :                                               (PHI_RESULT (use_stmt))))))
    1236              :             {
    1237      6936785 :               defs.unordered_remove (i);
    1238      6936785 :               if (def == first_phi_def)
    1239              :                 first_phi_def = NULL;
    1240      6196204 :               else if (def == last_phi_def)
    1241       273831 :                 last_phi_def = NULL;
    1242              :             }
    1243              :           /* If def is a PHI and one of its arguments is another PHI node still
    1244              :              in consideration we can defer processing it.  */
    1245    278798974 :           else if ((phi_def = dyn_cast <gphi *> (def))
    1246     24399701 :                    && ((last_phi_def
    1247     24399701 :                         && phi_def != last_phi_def
    1248      2329608 :                         && contains_phi_arg (phi_def,
    1249              :                                              gimple_phi_result (last_phi_def)))
    1250     24271277 :                        || (first_phi_def
    1251     24271277 :                            && phi_def != first_phi_def
    1252      2074656 :                            && contains_phi_arg
    1253      2074656 :                                 (phi_def, gimple_phi_result (first_phi_def)))))
    1254              :             {
    1255       260519 :               defs.unordered_remove (i);
    1256       260519 :               if (phi_def == first_phi_def)
    1257              :                 first_phi_def = NULL;
    1258       179754 :               else if (phi_def == last_phi_def)
    1259       273831 :                 last_phi_def = NULL;
    1260              :             }
    1261              :           else
    1262    278538455 :             ++i;
    1263              :         }
    1264              : 
    1265              :       /* If all defs kill the ref we are done.  */
    1266    316697151 :       if (defs.is_empty ())
    1267              :         return DSE_STORE_DEAD;
    1268              :       /* If more than one def survives we have to analyze multiple
    1269              :          paths.  We can handle this by recursing, sharing 'visited'
    1270              :          to avoid redundant work and limiting it by shared 'cnt'.
    1271              :          For now do not bother with byte-tracking in this case.  */
    1272    272816430 :       while (defs.length () > 1)
    1273              :         {
    1274     11376085 :           if (dse_classify_store (ref, defs.last (), false, NULL,
    1275              :                                   by_clobber_p, stop_at_vuse, cnt,
    1276              :                                   visited) != DSE_STORE_DEAD)
    1277              :             break;
    1278      7024293 :           byte_tracking_enabled = false;
    1279      7024293 :           defs.pop ();
    1280              :         }
    1281              :       /* If more than one def survives fail.  */
    1282    265792137 :       if (defs.length () > 1)
    1283              :         {
    1284              :           /* STMT might be partially dead and we may be able to reduce
    1285              :              how many memory locations it stores into.  */
    1286      4351792 :           if (byte_tracking_enabled && !gimple_clobber_p (stmt))
    1287      1661494 :             return DSE_STORE_MAYBE_PARTIAL_DEAD;
    1288              :           return DSE_STORE_LIVE;
    1289              :         }
    1290    261440345 :       temp = defs[0];
    1291              : 
    1292              :       /* Track partial kills.  */
    1293    261440345 :       if (byte_tracking_enabled)
    1294              :         {
    1295    192616208 :           clear_bytes_written_by (live_bytes, temp, ref);
    1296    192616208 :           if (bitmap_empty_p (live_bytes))
    1297              :             {
    1298        57964 :               if (by_clobber_p && !gimple_clobber_p (temp))
    1299        57770 :                 *by_clobber_p = false;
    1300              :               return DSE_STORE_DEAD;
    1301              :             }
    1302              :         }
    1303    305577258 :     }
    1304              :   /* Continue walking until there are no more live bytes.  */
    1305              :   while (1);
    1306     44194877 : }
    1307              : 
    1308              : dse_store_status
    1309     32818792 : dse_classify_store (ao_ref *ref, gimple *stmt,
    1310              :                     bool byte_tracking_enabled, sbitmap live_bytes,
    1311              :                     bool *by_clobber_p, tree stop_at_vuse)
    1312              : {
    1313     32818792 :   int cnt = 0;
    1314     32818792 :   auto_bitmap visited;
    1315     32818792 :   return dse_classify_store (ref, stmt, byte_tracking_enabled, live_bytes,
    1316     32818792 :                              by_clobber_p, stop_at_vuse, cnt, visited);
    1317     32818792 : }
    1318              : 
    1319              : 
    1320              : /* Delete a dead call at GSI, which is mem* call of some kind.  */
    1321              : static void
    1322         7284 : delete_dead_or_redundant_call (gimple_stmt_iterator *gsi, const char *type)
    1323              : {
    1324         7284 :   gimple *stmt = gsi_stmt (*gsi);
    1325         7284 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1326              :     {
    1327           18 :       fprintf (dump_file, "  Deleted %s call: ", type);
    1328           18 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1329           18 :       fprintf (dump_file, "\n");
    1330              :     }
    1331              : 
    1332         7284 :   basic_block bb = gimple_bb (stmt);
    1333         7284 :   tree lhs = gimple_call_lhs (stmt);
    1334         7284 :   if (lhs)
    1335              :     {
    1336         1220 :       tree ptr = gimple_call_arg (stmt, 0);
    1337         1220 :       gimple *new_stmt = gimple_build_assign (lhs, ptr);
    1338         1220 :       unlink_stmt_vdef (stmt);
    1339         1220 :       if (gsi_replace (gsi, new_stmt, true))
    1340          396 :         bitmap_set_bit (need_eh_cleanup, bb->index);
    1341              :     }
    1342              :   else
    1343              :     {
    1344              :       /* Then we need to fix the operand of the consuming stmt.  */
    1345         6064 :       unlink_stmt_vdef (stmt);
    1346              : 
    1347              :       /* Remove the dead store.  */
    1348         6064 :       if (gsi_remove (gsi, true))
    1349            3 :         bitmap_set_bit (need_eh_cleanup, bb->index);
    1350         6064 :       release_defs (stmt);
    1351              :     }
    1352         7284 : }
    1353              : 
    1354              : /* Delete a dead store at GSI, which is a gimple assignment. */
    1355              : 
    1356              : void
    1357      2347598 : delete_dead_or_redundant_assignment (gimple_stmt_iterator *gsi,
    1358              :                                      const char *type,
    1359              :                                      bitmap need_eh_cleanup,
    1360              :                                      bitmap need_ab_cleanup)
    1361              : {
    1362      2347598 :   gimple *stmt = gsi_stmt (*gsi);
    1363      2347598 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1364              :     {
    1365          112 :       fprintf (dump_file, "  Deleted %s store: ", type);
    1366          112 :       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1367          112 :       fprintf (dump_file, "\n");
    1368              :     }
    1369              : 
    1370              :   /* Then we need to fix the operand of the consuming stmt.  */
    1371      2347598 :   unlink_stmt_vdef (stmt);
    1372              : 
    1373              :   /* Remove the dead store.  */
    1374      2347598 :   basic_block bb = gimple_bb (stmt);
    1375      2347598 :   if (need_ab_cleanup && stmt_can_make_abnormal_goto (stmt))
    1376            5 :     bitmap_set_bit (need_ab_cleanup, bb->index);
    1377      2347598 :   if (gsi_remove (gsi, true) && need_eh_cleanup)
    1378          103 :     bitmap_set_bit (need_eh_cleanup, bb->index);
    1379              : 
    1380              :   /* And release any SSA_NAMEs set in this statement back to the
    1381              :      SSA_NAME manager.  */
    1382      2347598 :   release_defs (stmt);
    1383      2347598 : }
    1384              : 
    1385              : /* Try to prove, using modref summary, that all memory written to by a call is
    1386              :    dead and remove it.  Assume that if return value is written to memory
    1387              :    it is already proved to be dead.  */
    1388              : 
    1389              : static bool
    1390     17983192 : dse_optimize_call (gimple_stmt_iterator *gsi, sbitmap live_bytes)
    1391              : {
    1392     17983192 :   gcall *stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
    1393              : 
    1394     17788909 :   if (!stmt)
    1395              :     return false;
    1396              : 
    1397     17788909 :   tree callee = gimple_call_fndecl (stmt);
    1398              : 
    1399     17788909 :   if (!callee)
    1400              :     return false;
    1401              : 
    1402              :   /* Pure/const functions are optimized by normal DCE
    1403              :      or handled as store above.  */
    1404     17037068 :   int flags = gimple_call_flags (stmt);
    1405     17037068 :   if ((flags & (ECF_PURE|ECF_CONST|ECF_NOVOPS))
    1406           96 :       && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
    1407              :     return false;
    1408              : 
    1409     17037066 :   cgraph_node *node = cgraph_node::get (callee);
    1410     17037066 :   if (!node)
    1411              :     return false;
    1412              : 
    1413     17026290 :   if ((stmt_could_throw_p (cfun, stmt)
    1414      7459432 :        && !cfun->can_delete_dead_exceptions)
    1415     19747986 :       || ((gimple_call_flags (stmt) & ECF_NORETURN)
    1416      2215182 :           && gimple_call_ctrl_altering_p (stmt)))
    1417              :     return false;
    1418              : 
    1419              :   /* If return value is used the call is not dead.  */
    1420     10073403 :   tree lhs = gimple_call_lhs (stmt);
    1421     10073403 :   if (lhs && TREE_CODE (lhs) == SSA_NAME)
    1422              :     {
    1423      2540718 :       imm_use_iterator ui;
    1424      2540718 :       gimple *use_stmt;
    1425      2733545 :       FOR_EACH_IMM_USE_STMT (use_stmt, ui, lhs)
    1426      2674142 :         if (!is_gimple_debug (use_stmt))
    1427      2540718 :           return false;
    1428              :     }
    1429              : 
    1430              :   /* Verify that there are no side-effects except for return value
    1431              :      and memory writes tracked by modref.  */
    1432      7592088 :   modref_summary *summary = get_modref_function_summary (node);
    1433      7592088 :   if (!summary || !summary->try_dse)
    1434              :     return false;
    1435              : 
    1436        71994 :   bool by_clobber_p = false;
    1437              : 
    1438              :   /* Walk all memory writes and verify that they are dead.  */
    1439       218008 :   for (auto base_node : summary->stores->bases)
    1440       220942 :     for (auto ref_node : base_node->refs)
    1441       226057 :       for (auto access_node : ref_node->accesses)
    1442              :         {
    1443        74365 :           tree arg = access_node.get_call_arg (stmt);
    1444              : 
    1445        74365 :           if (!arg || !POINTER_TYPE_P (TREE_TYPE (arg)))
    1446        70368 :             return false;
    1447              : 
    1448        74364 :           if (integer_zerop (arg)
    1449        74376 :               && !targetm.addr_space.zero_address_valid
    1450           12 :                     (TYPE_ADDR_SPACE (TREE_TYPE (arg))))
    1451           12 :             continue;
    1452              : 
    1453        74352 :           ao_ref ref;
    1454              : 
    1455        74352 :           if (!access_node.get_ao_ref (stmt, &ref))
    1456              :             return false;
    1457        74352 :           ref.ref_alias_set = ref_node->ref;
    1458        74352 :           ref.base_alias_set = base_node->base;
    1459              : 
    1460        74352 :           bool byte_tracking_enabled
    1461        74352 :               = setup_live_bytes_from_ref (&ref, live_bytes);
    1462        74352 :           enum dse_store_status store_status;
    1463              : 
    1464        74352 :           store_status = dse_classify_store (&ref, stmt,
    1465              :                                              byte_tracking_enabled,
    1466              :                                              live_bytes, &by_clobber_p);
    1467        74352 :           if (store_status != DSE_STORE_DEAD)
    1468              :             return false;
    1469              :         }
    1470         1626 :   delete_dead_or_redundant_assignment (gsi, "dead", need_eh_cleanup,
    1471              :                                        need_ab_cleanup);
    1472         1626 :   return true;
    1473              : }
    1474              : 
    1475              : /* Attempt to eliminate dead stores in the statement referenced by BSI.
    1476              : 
    1477              :    A dead store is a store into a memory location which will later be
    1478              :    overwritten by another store without any intervening loads.  In this
    1479              :    case the earlier store can be deleted.
    1480              : 
    1481              :    In our SSA + virtual operand world we use immediate uses of virtual
    1482              :    operands to detect dead stores.  If a store's virtual definition
    1483              :    is used precisely once by a later store to the same location which
    1484              :    post dominates the first store, then the first store is dead.  */
    1485              : 
    1486              : static void
    1487     58141179 : dse_optimize_stmt (function *fun, gimple_stmt_iterator *gsi, sbitmap live_bytes)
    1488              : {
    1489     58141179 :   gimple *stmt = gsi_stmt (*gsi);
    1490              : 
    1491              :   /* Don't return early on *this_2(D) ={v} {CLOBBER}.  */
    1492     58141179 :   if (gimple_has_volatile_ops (stmt)
    1493     58141179 :       && (!gimple_clobber_p (stmt)
    1494      7041971 :           || TREE_CODE (gimple_assign_lhs (stmt)) != MEM_REF))
    1495     55713185 :     return;
    1496              : 
    1497     50672797 :   ao_ref ref;
    1498              :   /* If this is not a store we can still remove dead call using
    1499              :      modref summary.  Note we specifically allow ref to be initialized
    1500              :      to a conservative may-def since we are looking for followup stores
    1501              :      to kill all of it.  */
    1502     50672797 :   if (!initialize_ao_ref_for_dse (stmt, &ref, true))
    1503              :     {
    1504     17937903 :       dse_optimize_call (gsi, live_bytes);
    1505     17937903 :       return;
    1506              :     }
    1507              : 
    1508              :   /* We know we have virtual definitions.  We can handle assignments and
    1509              :      some builtin calls.  */
    1510     32734894 :   if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
    1511     32734894 :       && !gimple_call_ctrl_altering_p (stmt))
    1512              :     {
    1513       473626 :       tree fndecl = gimple_call_fndecl (stmt);
    1514       473626 :       switch (DECL_FUNCTION_CODE (fndecl))
    1515              :         {
    1516       471635 :         case BUILT_IN_MEMCPY:
    1517       471635 :         case BUILT_IN_MEMMOVE:
    1518       471635 :         case BUILT_IN_STRNCPY:
    1519       471635 :         case BUILT_IN_MEMSET:
    1520       471635 :         case BUILT_IN_MEMCPY_CHK:
    1521       471635 :         case BUILT_IN_MEMMOVE_CHK:
    1522       471635 :         case BUILT_IN_STRNCPY_CHK:
    1523       471635 :         case BUILT_IN_MEMSET_CHK:
    1524       471635 :           {
    1525              :             /* Occasionally calls with an explicit length of zero
    1526              :                show up in the IL.  It's pointless to do analysis
    1527              :                on them, they're trivially dead.  */
    1528       471635 :             tree size = gimple_call_arg (stmt, 2);
    1529       471635 :             if (integer_zerop (size))
    1530              :               {
    1531           50 :                 delete_dead_or_redundant_call (gsi, "dead");
    1532           50 :                 return;
    1533              :               }
    1534              : 
    1535              :             /* If this is a memset call that initializes an object
    1536              :                to zero, it may be redundant with an earlier memset
    1537              :                or empty CONSTRUCTOR of a larger object.  */
    1538       471585 :             if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET
    1539       366959 :                  || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET_CHK)
    1540       471888 :                 && integer_zerop (gimple_call_arg (stmt, 1)))
    1541        60736 :               dse_optimize_redundant_stores (stmt);
    1542              : 
    1543       471585 :             enum dse_store_status store_status;
    1544       471585 :             bool byte_tracking_enabled
    1545       471585 :               = setup_live_bytes_from_ref (&ref, live_bytes);
    1546       471585 :             store_status = dse_classify_store (&ref, stmt,
    1547              :                                                byte_tracking_enabled,
    1548              :                                                live_bytes);
    1549       471585 :             if (store_status == DSE_STORE_LIVE)
    1550              :               return;
    1551              : 
    1552       148597 :             if (store_status == DSE_STORE_MAYBE_PARTIAL_DEAD)
    1553              :               {
    1554       141412 :                 maybe_trim_memstar_call (&ref, live_bytes, stmt);
    1555       141412 :                 return;
    1556              :               }
    1557              : 
    1558         7185 :             if (store_status == DSE_STORE_DEAD)
    1559         7185 :               delete_dead_or_redundant_call (gsi, "dead");
    1560              :             return;
    1561              :           }
    1562              : 
    1563         1991 :         case BUILT_IN_CALLOC:
    1564              :           /* We already know the arguments are integer constants.  */
    1565         1991 :           dse_optimize_redundant_stores (stmt);
    1566         1991 :           return;
    1567              : 
    1568              :         default:
    1569              :           return;
    1570              :         }
    1571              :     }
    1572     32261268 :   else if (is_gimple_call (stmt)
    1573     32261268 :            && gimple_call_internal_p (stmt))
    1574              :     {
    1575       152129 :       switch (gimple_call_internal_fn (stmt))
    1576              :         {
    1577         1336 :         case IFN_LEN_STORE:
    1578         1336 :         case IFN_MASK_STORE:
    1579         1336 :         case IFN_MASK_LEN_STORE:
    1580         1336 :           {
    1581         1336 :             enum dse_store_status store_status;
    1582         1336 :             store_status = dse_classify_store (&ref, stmt, false, live_bytes);
    1583         1336 :             if (store_status == DSE_STORE_DEAD)
    1584            0 :               delete_dead_or_redundant_call (gsi, "dead");
    1585              :             return;
    1586              :           }
    1587              :         default:;
    1588              :         }
    1589              :     }
    1590              : 
    1591     32259932 :   bool by_clobber_p = false;
    1592              : 
    1593              :   /* Check if this statement stores zero to a memory location,
    1594              :      and if there is a subsequent store of zero to the same
    1595              :      memory location.  If so, remove the subsequent store.  */
    1596     32259932 :   if (gimple_assign_single_p (stmt)
    1597     32259932 :       && initializer_zerop (gimple_assign_rhs1 (stmt)))
    1598      4308239 :     dse_optimize_redundant_stores (stmt);
    1599              : 
    1600              :   /* Self-assignments are zombies.  */
    1601     32259932 :   auto assign = dyn_cast<gassign *> (stmt);
    1602     30764809 :   if (assign
    1603     30764809 :       && operand_equal_p (gimple_assign_rhs1 (assign),
    1604     30764809 :                           gimple_assign_lhs (assign), 0))
    1605              :     ;
    1606              :   else
    1607              :     {
    1608     32258543 :       bool byte_tracking_enabled
    1609     32258543 :           = setup_live_bytes_from_ref (&ref, live_bytes);
    1610     32258543 :       enum dse_store_status store_status;
    1611     32258543 :       store_status = dse_classify_store (&ref, stmt,
    1612              :                                          byte_tracking_enabled,
    1613              :                                          live_bytes, &by_clobber_p);
    1614     32258543 :       if (store_status == DSE_STORE_LIVE)
    1615              :         return;
    1616              : 
    1617     29611817 :       if (store_status == DSE_STORE_MAYBE_PARTIAL_DEAD)
    1618              :         {
    1619     27121570 :           maybe_trim_partially_dead_store (&ref, live_bytes, stmt);
    1620     27121570 :           return;
    1621              :         }
    1622              :     }
    1623              : 
    1624              :   /* Now we know that use_stmt kills the LHS of stmt.  */
    1625              : 
    1626              :   /* But only remove *this_2(D) ={v} {CLOBBER} if killed by
    1627              :      another clobber stmt.  */
    1628      2491636 :   if (gimple_clobber_p (stmt)
    1629      2491636 :       && !by_clobber_p)
    1630              :     return;
    1631              : 
    1632      2436458 :   if (is_gimple_call (stmt)
    1633      2436458 :       && (gimple_has_side_effects (stmt)
    1634        55408 :           || (stmt_could_throw_p (fun, stmt)
    1635            5 :               && !fun->can_delete_dead_exceptions)))
    1636              :     {
    1637              :       /* See if we can remove complete call.  */
    1638        45289 :       if (dse_optimize_call (gsi, live_bytes))
    1639              :         return;
    1640              :       /* Make sure we do not remove a return slot we cannot reconstruct
    1641              :          later.  */
    1642        45271 :       if (gimple_call_return_slot_opt_p (as_a <gcall *>(stmt))
    1643        45271 :           && (TREE_ADDRESSABLE (TREE_TYPE (gimple_call_fntype (stmt)))
    1644        14919 :               || !poly_int_tree_p
    1645        14919 :                     (TYPE_SIZE (TREE_TYPE (gimple_call_fntype (stmt))))))
    1646              :         return;
    1647        36825 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1648              :         {
    1649            1 :           fprintf (dump_file, "  Deleted dead store in call LHS: ");
    1650            1 :           print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1651            1 :           fprintf (dump_file, "\n");
    1652              :         }
    1653        36825 :       gimple_call_set_lhs (stmt, NULL_TREE);
    1654        36825 :       update_stmt (stmt);
    1655              :     }
    1656      2391169 :   else if (!stmt_could_throw_p (fun, stmt)
    1657      2391169 :            || fun->can_delete_dead_exceptions)
    1658      2340920 :     delete_dead_or_redundant_assignment (gsi, "dead", need_eh_cleanup,
    1659              :                                          need_ab_cleanup);
    1660              : }
    1661              : 
    1662              : namespace {
    1663              : 
    1664              : const pass_data pass_data_dse =
    1665              : {
    1666              :   GIMPLE_PASS, /* type */
    1667              :   "dse", /* name */
    1668              :   OPTGROUP_NONE, /* optinfo_flags */
    1669              :   TV_TREE_DSE, /* tv_id */
    1670              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
    1671              :   0, /* properties_provided */
    1672              :   0, /* properties_destroyed */
    1673              :   0, /* todo_flags_start */
    1674              :   0, /* todo_flags_finish */
    1675              : };
    1676              : 
    1677              : class pass_dse : public gimple_opt_pass
    1678              : {
    1679              : public:
    1680      1514925 :   pass_dse (gcc::context *ctxt)
    1681      3029850 :     : gimple_opt_pass (pass_data_dse, ctxt), use_dr_analysis_p (false)
    1682              :   {}
    1683              : 
    1684              :   /* opt_pass methods: */
    1685      1211940 :   opt_pass * clone () final override { return new pass_dse (m_ctxt); }
    1686       302985 :   void set_pass_param (unsigned n, bool param) final override
    1687              :     {
    1688       302985 :       gcc_assert (n == 0);
    1689       302985 :       use_dr_analysis_p = param;
    1690       302985 :     }
    1691      5766578 :   bool gate (function *) final override { return flag_tree_dse != 0; }
    1692              :   unsigned int execute (function *) final override;
    1693              : 
    1694              : private:
    1695              :   bool use_dr_analysis_p;
    1696              : }; // class pass_dse
    1697              : 
    1698              : unsigned int
    1699      5741854 : pass_dse::execute (function *fun)
    1700              : {
    1701      5741854 :   unsigned todo = 0;
    1702      5741854 :   bool released_def = false;
    1703              : 
    1704      5741854 :   need_eh_cleanup = BITMAP_ALLOC (NULL);
    1705      5741854 :   need_ab_cleanup = BITMAP_ALLOC (NULL);
    1706      5741854 :   auto_sbitmap live_bytes (param_dse_max_object_size);
    1707      5741854 :   if (flag_expensive_optimizations && use_dr_analysis_p)
    1708       982960 :     dse_stmt_to_dr_map = new hash_map<gimple *, data_reference_p>;
    1709              : 
    1710      5741854 :   renumber_gimple_stmt_uids (fun);
    1711              : 
    1712      5741854 :   calculate_dominance_info (CDI_DOMINATORS);
    1713              : 
    1714              :   /* Dead store elimination is fundamentally a reverse program order walk.  */
    1715      5741854 :   int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun) - NUM_FIXED_BLOCKS);
    1716      5741854 :   auto_bitmap exit_bbs;
    1717      5741854 :   bitmap_set_bit (exit_bbs, EXIT_BLOCK);
    1718      5741854 :   edge entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fun));
    1719      5741854 :   int n = rev_post_order_and_mark_dfs_back_seme (fun, entry,
    1720              :                                                  exit_bbs, false, rpo, NULL);
    1721     60476598 :   for (int i = n; i != 0; --i)
    1722              :     {
    1723     48992890 :       basic_block bb = BASIC_BLOCK_FOR_FN (fun, rpo[i-1]);
    1724     48992890 :       gimple_stmt_iterator gsi;
    1725              : 
    1726    459165125 :       for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
    1727              :         {
    1728    361179345 :           gimple *stmt = gsi_stmt (gsi);
    1729              : 
    1730    492285441 :           if (gimple_vdef (stmt))
    1731     58141179 :             dse_optimize_stmt (fun, &gsi, live_bytes);
    1732    606076332 :           else if (def_operand_p
    1733    303038166 :                      def_p = single_ssa_def_operand (stmt, SSA_OP_DEF))
    1734              :             {
    1735              :               /* When we remove dead stores make sure to also delete trivially
    1736              :                  dead SSA defs.  */
    1737     66702267 :               if (has_zero_uses (DEF_FROM_PTR (def_p))
    1738      2180329 :                   && !gimple_has_side_effects (stmt)
    1739      2169897 :                   && !is_ctrl_altering_stmt (stmt)
    1740     68870475 :                   && (!stmt_could_throw_p (fun, stmt)
    1741        90551 :                       || fun->can_delete_dead_exceptions))
    1742              :                 {
    1743      2077755 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    1744              :                     {
    1745           11 :                       fprintf (dump_file, "  Deleted trivially dead stmt: ");
    1746           11 :                       print_gimple_stmt (dump_file, stmt, 0, dump_flags);
    1747           11 :                       fprintf (dump_file, "\n");
    1748              :                     }
    1749      2077755 :                   if (gsi_remove (&gsi, true) && need_eh_cleanup)
    1750            2 :                     bitmap_set_bit (need_eh_cleanup, bb->index);
    1751      2077755 :                   release_defs (stmt);
    1752      2077755 :                   released_def = true;
    1753              :                 }
    1754              :             }
    1755    361179345 :           if (gsi_end_p (gsi))
    1756       605412 :             gsi = gsi_last_bb (bb);
    1757              :           else
    1758    404763650 :             gsi_prev (&gsi);
    1759              :         }
    1760     48992890 :       bool removed_phi = false;
    1761     67722266 :       for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);)
    1762              :         {
    1763     18729376 :           gphi *phi = si.phi ();
    1764     18729376 :           if (has_zero_uses (gimple_phi_result (phi)))
    1765              :             {
    1766       234543 :               if (dump_file && (dump_flags & TDF_DETAILS))
    1767              :                 {
    1768            0 :                   fprintf (dump_file, "  Deleted trivially dead PHI: ");
    1769            0 :                   print_gimple_stmt (dump_file, phi, 0, dump_flags);
    1770            0 :                   fprintf (dump_file, "\n");
    1771              :                 }
    1772       234543 :               remove_phi_node (&si, true);
    1773       234543 :               removed_phi = true;
    1774       234543 :               released_def = true;
    1775              :             }
    1776              :           else
    1777     18494833 :             gsi_next (&si);
    1778              :         }
    1779     48992890 :       if (removed_phi && gimple_seq_empty_p (phi_nodes (bb)))
    1780              :         todo |= TODO_cleanup_cfg;
    1781              :     }
    1782      5741854 :   free (rpo);
    1783              : 
    1784              :   /* Removal of stores may make some EH edges dead.  Purge such edges from
    1785              :      the CFG as needed.  */
    1786      5741854 :   if (!bitmap_empty_p (need_eh_cleanup))
    1787              :     {
    1788          401 :       gimple_purge_all_dead_eh_edges (need_eh_cleanup);
    1789          401 :       todo |= TODO_cleanup_cfg;
    1790              :     }
    1791      5741854 :   if (!bitmap_empty_p (need_ab_cleanup))
    1792              :     {
    1793            4 :       gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
    1794            4 :       todo |= TODO_cleanup_cfg;
    1795              :     }
    1796              : 
    1797      5741854 :   BITMAP_FREE (need_eh_cleanup);
    1798      5741854 :   BITMAP_FREE (need_ab_cleanup);
    1799              : 
    1800      5741854 :   if (released_def)
    1801       612421 :     free_numbers_of_iterations_estimates (fun);
    1802              : 
    1803      5741854 :   if (flag_expensive_optimizations && use_dr_analysis_p)
    1804              :     {
    1805      1729644 :       for (auto i = dse_stmt_to_dr_map->begin ();
    1806      1729644 :            i != dse_stmt_to_dr_map->end (); ++i)
    1807       746684 :         free_data_ref ((*i).second);
    1808      1965920 :       delete dse_stmt_to_dr_map;
    1809       982960 :       dse_stmt_to_dr_map = NULL;
    1810              :     }
    1811              : 
    1812      5741854 :   return todo;
    1813      5741854 : }
    1814              : 
    1815              : } // anon namespace
    1816              : 
    1817              : gimple_opt_pass *
    1818       302985 : make_pass_dse (gcc::context *ctxt)
    1819              : {
    1820       302985 :   return new pass_dse (ctxt);
    1821              : }
        

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.