From e875e4e7f3716aa268ffbbf55ee199ec82b6aeba Mon Sep 17 00:00:00 2001 From: Mingchuan Wu Date: Thu, 21 Dec 2023 15:50:34 +0800 Subject: [PATCH 2/2] [DFE] Add escape check. Fields with escape risks should not be processed. --- gcc/ipa-struct-reorg/ipa-struct-reorg.c | 15 +++++-- gcc/testsuite/gcc.dg/struct/dfe_escape.c | 50 ++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/struct/dfe_escape.c diff --git a/gcc/ipa-struct-reorg/ipa-struct-reorg.c b/gcc/ipa-struct-reorg/ipa-struct-reorg.c index 0064811ac..dcfa7cd95 100644 --- a/gcc/ipa-struct-reorg/ipa-struct-reorg.c +++ b/gcc/ipa-struct-reorg/ipa-struct-reorg.c @@ -444,8 +444,13 @@ srtype::has_dead_field (void) if (!(this_field->field_access & READ_FIELD) && !FUNCTION_POINTER_TYPE_P (this_field->fieldtype)) { - may_dfe = true; - break; + /* Fields with escape risks should not be processed. */ + if (this_field->type == NULL + || (this_field->type->escapes == does_not_escape)) + { + may_dfe = true; + break; + } } } return may_dfe; @@ -1030,7 +1035,11 @@ srtype::create_new_type (void) if (current_layout_opt_level & DEAD_FIELD_ELIMINATION && !(f->field_access & READ_FIELD) && !FUNCTION_POINTER_TYPE_P (f->fieldtype)) - continue; + { + /* Fields with escape risks should not be processed. */ + if (f->type == NULL || (f->type->escapes == does_not_escape)) + continue; + } f->create_new_fields (newtype, newfields, newlast); } diff --git a/gcc/testsuite/gcc.dg/struct/dfe_escape.c b/gcc/testsuite/gcc.dg/struct/dfe_escape.c new file mode 100644 index 000000000..1b143cd26 --- /dev/null +++ b/gcc/testsuite/gcc.dg/struct/dfe_escape.c @@ -0,0 +1,50 @@ +/* { dg-do compile } */ + +#include +#include + +typedef struct arc arc_t; +typedef struct arc *arc_p; + +typedef struct network +{ + int x; +} network_t; + +struct arc +{ + int flow; + network_t* net_add; +}; + +const int MAX = 100; + +/* let it escape_array, "Type is used in an array [not handled yet]". */ +network_t* net[2]; +arc_p stop_arcs = NULL; + +int +main () +{ + net[0] = (network_t*) calloc (1, sizeof(network_t)); + stop_arcs = (arc_p) calloc (MAX, sizeof (arc_t)); + + net[0]->x = 100; + + for (unsigned i = 0; i < 3; i++) + { + net[0]->x = net[0]->x + 2; + stop_arcs->flow = net[0]->x / 2; + stop_arcs->flow = stop_arcs->flow + 20; + stop_arcs->net_add = net[0]; + stop_arcs++; + } + + if( net[1] != 0 && stop_arcs != 0) + { + return -1; + } + return 0; +} + +/* { dg-final { scan-ipa-dump-times "Dead field elimination" 0 "struct_reorg" } } */ -- 2.33.0