aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Pinski <quic_apinski@quicinc.com>2025-04-07 17:06:17 -0700
committerAndrew Pinski <quic_apinski@quicinc.com>2025-04-18 09:13:48 -0700
commit800b3977031dd4f14d09ced975276e09457dfff7 (patch)
tree7b1d28eb84360625692bf05624ecee9c597e7ff6
parent16082bdc6beef1ca1485ed5ccdc0c52aabbe0f4c (diff)
downloadgcc-800b3977031dd4f14d09ced975276e09457dfff7.zip
gcc-800b3977031dd4f14d09ced975276e09457dfff7.tar.gz
gcc-800b3977031dd4f14d09ced975276e09457dfff7.tar.bz2
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_apinski@quicinc.com>
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-52.c30
-rw-r--r--gcc/tree-ssa-dse.cc2
2 files changed, 32 insertions, 0 deletions
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 0000000..9e605ac
--- /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 bc632e3..82ebc99 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;