From c2444a1259ac0f082f8ce8919a053bd1de504781 Mon Sep 17 00:00:00 2001 From: huzife <634763349@qq.com> Date: Thu, 20 Feb 2025 14:52:19 +0800 Subject: [PATCH] Fix errors in ipa-struct-sfc (IBMY84, IBN2JO, IBN42Q) --- gcc/ipa-struct-reorg/ipa-struct-reorg.cc | 31 ++++++++++++++++--- .../gcc.dg/struct/sfc-shadow_non_integer.c | 25 +++++++++++++++ gcc/testsuite/gcc.dg/struct/sfc_non_integer.c | 29 +++++++++++++++++ 3 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/struct/sfc-shadow_non_integer.c create mode 100644 gcc/testsuite/gcc.dg/struct/sfc_non_integer.c diff --git a/gcc/ipa-struct-reorg/ipa-struct-reorg.cc b/gcc/ipa-struct-reorg/ipa-struct-reorg.cc index d3beebc005d..f2660c95262 100644 --- a/gcc/ipa-struct-reorg/ipa-struct-reorg.cc +++ b/gcc/ipa-struct-reorg/ipa-struct-reorg.cc @@ -4367,6 +4367,20 @@ ipa_struct_reorg::wholeaccess (tree expr, tree base, if (!handled_type (TREE_TYPE (expr))) return false; + if (!t || !t->type) + return false; + + tree type = TYPE_MAIN_VARIANT (t->type); + if (TREE_CODE (expr) == MEM_REF + && POINTER_TYPE_P (TREE_TYPE (expr)) + && POINTER_TYPE_P (accesstype) + && POINTER_TYPE_P (TREE_TYPE (accesstype)) + && POINTER_TYPE_P (TREE_TYPE (base)) + && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base))) == type + && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (expr))) == type + && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (accesstype))) == type) + return false; + srtype *other_type = find_type (inner_type (TREE_TYPE (expr))); if (t == other_type) @@ -5372,12 +5386,9 @@ ipa_struct_reorg::record_function (cgraph_node *node) function *fn; tree parm, var; unsigned int i; - srfunction *sfn; + srfunction *sfn = NULL; escape_type escapes = does_not_escape; - sfn = new srfunction (node); - functions.safe_push (sfn); - if (dump_file && (dump_flags & TDF_DETAILS)) fprintf (dump_file, "\nRecording accesses and types from function: %s/%u\n", @@ -5395,6 +5406,9 @@ ipa_struct_reorg::record_function (cgraph_node *node) if (!fn) return sfn; + sfn = new srfunction (node); + functions.safe_push (sfn); + current_function = sfn; if (DECL_PRESERVE_P (node->decl)) @@ -8983,6 +8997,10 @@ ipa_struct_reorg::find_static_fc_fields (fc_type_info *info) for (auto *srf : info->type->fields) { + /* Avoid compressing non-integer type. */ + if (TREE_CODE (srf->fieldtype) != INTEGER_TYPE) + continue; + /* We have marked these fields as shadow, so skip them. */ if (fc_fields_contains (info->static_fc_fields, srf->fielddecl)) continue; @@ -9034,6 +9052,11 @@ ipa_struct_reorg::find_shadow_fields (fc_type_info *info) bool found_shadow = false; for (auto *field_class : info->field_classes) { + /* Avoid shadowing non-integer type, we can try to do this + in the future. */ + if (TREE_CODE (field_class->fieldtype) != INTEGER_TYPE) + continue; + /* Field shadowing requires two or more fields. */ if (field_class->size () < 2) continue; diff --git a/gcc/testsuite/gcc.dg/struct/sfc-shadow_non_integer.c b/gcc/testsuite/gcc.dg/struct/sfc-shadow_non_integer.c new file mode 100644 index 00000000000..44769a2a162 --- /dev/null +++ b/gcc/testsuite/gcc.dg/struct/sfc-shadow_non_integer.c @@ -0,0 +1,25 @@ +/* { dg-do compile } */ + +#include +#include + +struct arc { + double a; + double 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; + } + printf("%f, %f\n", arcs[10].a, arcs[10].b); + + 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_non_integer.c b/gcc/testsuite/gcc.dg/struct/sfc_non_integer.c new file mode 100644 index 00000000000..e76e30e701d --- /dev/null +++ b/gcc/testsuite/gcc.dg/struct/sfc_non_integer.c @@ -0,0 +1,29 @@ +/* { dg-do compile } */ + +#include +#include + +struct arc { + double a; + float 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].b = 2; + arcs[i].b = 1.0; + } + + for (int i = 0; i < MAX; i++) { + if (arcs[i].a < arcs[i].b) + abort (); + } + + return 0; +} + +/* { dg-final { scan-ipa-dump "\\\[field compress\\\] Fail finding static fc fields" "struct_reorg" } } */ -- Gitee