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
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> --- gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-52.c | 30 ++++++++++++++++++++++ gcc/tree-ssa-dse.cc | 2 ++ 2 files changed, 32 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-52.c 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 <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 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; -- 2.43.0