https://gcc.gnu.org/g:800b3977031dd4f14d09ced975276e09457dfff7

commit r16-25-g800b3977031dd4f14d09ced975276e09457dfff7
Author: Andrew Pinski <quic_apin...@quicinc.com>
Date:   Mon Apr 7 17:06:17 2025 -0700

    DSE: Support triming of some more memset [PR87901]
    
    DSE has support for trimming memset (and memset like) statements.
    In this case we have `MEM <unsigned char[17]> [(char * {ref-all})&z] = {};` 
in
    the IR and when we go to trim it, we call build_fold_addr_expr which leaves 
around
    a cast from one pointer type to another. This is due to build_fold_addr_expr
    being generic but in gimple you don't need these casts.
    
            PR tree-optimization/87901
    
    gcc/ChangeLog:
    
            * tree-ssa-dse.cc (maybe_trim_constructor_store): Strip over 
useless type
            conversions after taking the address of the MEM_REF.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/ssa-dse-52.c: New test.
    
    Signed-off-by: Andrew Pinski <quic_apin...@quicinc.com>

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-52.c | 30 ++++++++++++++++++++++++++++++
 gcc/tree-ssa-dse.cc                        |  2 ++
 2 files changed, 32 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-52.c 
b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-52.c
new file mode 100644
index 000000000000..9e605ac8b1f5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-52.c
@@ -0,0 +1,30 @@
+/* { dg-options "-O2 -fdump-tree-dse-details -fno-tree-fre 
-fdump-tree-optimized" } */
+
+/* PR tree-optimization/87901 */
+
+char z[32];
+void foo1(void)
+{
+  char z1[17];
+  char z2[24];
+  __builtin_memset (z1, 0, 17);
+  __builtin_memcpy (z, z1, 17);
+  __builtin_memset (z2, 0, 24);
+  __builtin_memcpy (z+8, z2, 24);
+}
+
+/* we should get:
+  MEM <unsigned char[8]> [(char * {ref-all})&z] = {};
+  MEM <unsigned char[24]> [(char * {ref-all})&z + 8B] = {};
+  after DSE; trimming the first memset to z (which was memcpy) to 8 bytes
+  from the original 17.
+  and not have a [17] in the IR after DSE.
+  The two memset to z1/z2 will also be removed.
+ */
+/* { dg-final { scan-tree-dump-not "\\\[17\\\]" "optimized" } } */
+/* { dg-final { scan-tree-dump "\\\[8\\\]" "dse1" } } */
+
+/* { dg-final { scan-tree-dump-times "Trimming statement " 1 "dse1" } } */
+/* { dg-final { scan-tree-dump-times "Deleted dead call:" 2 "dse1" } } */
+
+
diff --git a/gcc/tree-ssa-dse.cc b/gcc/tree-ssa-dse.cc
index bc632e384841..82ebc99686c4 100644
--- a/gcc/tree-ssa-dse.cc
+++ b/gcc/tree-ssa-dse.cc
@@ -588,6 +588,8 @@ maybe_trim_constructor_store (ao_ref *ref, sbitmap live, 
gimple *stmt)
       /* We want &lhs for the MEM_REF expression.  */
       tree lhs_addr = build_fold_addr_expr (gimple_assign_lhs (stmt));
 
+      STRIP_USELESS_TYPE_CONVERSION (lhs_addr);
+
       if (! is_gimple_min_invariant (lhs_addr))
        return;

Reply via email to