From: Andrew Pinski Date: Tue, 8 Apr 2025 00:06:17 +0000 (-0700) Subject: DSE: Support triming of some more memset [PR87901] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=800b3977031dd4f14d09ced975276e09457dfff7;p=thirdparty%2Fgcc.git DSE: Support triming of some more memset [PR87901] DSE has support for trimming memset (and memset like) statements. In this case we have `MEM [(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 --- 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 00000000000..9e605ac8b1f --- /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 [(char * {ref-all})&z] = {}; + MEM [(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 bc632e38484..82ebc99686c 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;