From 63b3db6e1f3e4aba90d76780fb64f6875c5dd2ab Mon Sep 17 00:00:00 2001 From: huzife <634763349@qq.com> Date: Wed, 26 Feb 2025 01:24:45 +0800 Subject: [PATCH] Avoid doing sfc with struct_split and compressing dead fields --- gcc/ipa-struct-reorg/ipa-struct-reorg.cc | 13 ++++++-- .../gcc.dg/struct/sfc-shadow_dead_field.c | 31 ++++++++++++++++++ gcc/testsuite/gcc.dg/struct/sfc_dead_field.c | 32 +++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/struct/sfc-shadow_dead_field.c create mode 100644 gcc/testsuite/gcc.dg/struct/sfc_dead_field.c diff --git a/gcc/ipa-struct-reorg/ipa-struct-reorg.cc b/gcc/ipa-struct-reorg/ipa-struct-reorg.cc index f2660c95262..fcc26d6a4b5 100644 --- a/gcc/ipa-struct-reorg/ipa-struct-reorg.cc +++ b/gcc/ipa-struct-reorg/ipa-struct-reorg.cc @@ -8970,7 +8970,8 @@ void ipa_struct_reorg::classify_fields (fc_type_info *info) { for (auto *srf : info->type->fields) - info->record_field_class (srf); + if (!srf->dead_field_p ()) + info->record_field_class (srf); if (dump_file && (dump_flags & TDF_DETAILS)) { @@ -8997,6 +8998,10 @@ ipa_struct_reorg::find_static_fc_fields (fc_type_info *info) for (auto *srf : info->type->fields) { + /* Skip dead field. */ + if (srf->dead_field_p ()) + continue; + /* Avoid compressing non-integer type. */ if (TREE_CODE (srf->fieldtype) != INTEGER_TYPE) continue; @@ -10064,7 +10069,8 @@ ipa_struct_reorg::execute (unsigned int opt) check_and_prune_struct_for_pointer_compression (); if (opt >= SEMI_RELAYOUT) check_and_prune_struct_for_semi_relayout (); - if (flag_ipa_struct_sfc) + /* Avoid doing static field compression in STRUCT_SPLIT. */ + if (opt >= STRUCT_REORDER_FIELDS && flag_ipa_struct_sfc) check_and_prune_struct_for_field_compression (); ret = rewrite_functions (); } @@ -10148,6 +10154,9 @@ public: if (level >= STRUCT_REORDER_FIELDS) ret = ipa_struct_reorg ().execute (level); + if (ret & TODO_remove_functions) + symtab->remove_unreachable_nodes (dump_file); + if (level >= COMPLETE_STRUCT_RELAYOUT) { /* Preserved for backward compatibility. diff --git a/gcc/testsuite/gcc.dg/struct/sfc-shadow_dead_field.c b/gcc/testsuite/gcc.dg/struct/sfc-shadow_dead_field.c new file mode 100644 index 00000000000..dbe8633cc75 --- /dev/null +++ b/gcc/testsuite/gcc.dg/struct/sfc-shadow_dead_field.c @@ -0,0 +1,31 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-fipa-struct-reorg=3" } */ + +#include +#include + +struct arc { + unsigned long a; + unsigned long b; +}; +typedef struct arc arc_t; + +#define MAX 16 + +int main() { + arc_t* arcs = (arc_t*)calloc(MAX, sizeof(arc_t)); + for (int i = 0; i < MAX; i++) { + arcs[i].a = i; + arcs[i].b = i; + } + + for (int i = 0; i < MAX; i++) { + if (arcs[i].b != i) + abort (); + } + + return 0; +} + + +/* { dg-final { scan-ipa-dump "\\\[field compress\\\] Fail finding static fc fields" "struct_reorg" } } */ diff --git a/gcc/testsuite/gcc.dg/struct/sfc_dead_field.c b/gcc/testsuite/gcc.dg/struct/sfc_dead_field.c new file mode 100644 index 00000000000..ccd8339abfe --- /dev/null +++ b/gcc/testsuite/gcc.dg/struct/sfc_dead_field.c @@ -0,0 +1,32 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-fipa-struct-reorg=3" } */ + +#include +#include + +struct arc { + unsigned long a; + unsigned long b; +}; +typedef struct arc arc_t; + +#define MAX 16 + +int main() { + arc_t* arcs = (arc_t*)calloc(MAX, sizeof(arc_t)); + for (int i = 0; i < MAX; i++) { + arcs[i].a = 10000; + arcs[i].b = 10; + } + + for (int i = 0; i < MAX; i++) { + if (arcs[i].a != 10000) + abort (); + } + + return 0; +} + +/* { dg-final { scan-ipa-dump "\\\[field compress\\\] Found a static compression field: a, max_value = 10000" "struct_reorg" } } */ +/* { dg-final { scan-ipa-dump "\\\[field compress\\\] Found a static compression field: b" "struct_reorg" { xfail *-*-* } } } */ +/* { dg-final { scan-ipa-dump "size : 2" "struct_reorg" } } */ -- Gitee