From 6de9d3011cda914ab1fecd73f1d517cc3b82f0cb Mon Sep 17 00:00:00 2001 From: huzife <634763349@qq.com> Date: Fri, 23 May 2025 14:48:13 +0800 Subject: [PATCH] [dfc] Fix error in function wholeaccess --- gcc/ipa-struct-reorg/ipa-struct-reorg.cc | 32 ++++++------------- gcc/testsuite/gcc.dg/struct/dfc_wholeaccess.c | 18 +++++++++++ 2 files changed, 27 insertions(+), 23 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/struct/dfc_wholeaccess.c diff --git a/gcc/ipa-struct-reorg/ipa-struct-reorg.cc b/gcc/ipa-struct-reorg/ipa-struct-reorg.cc index 851bda65cd5..e8a84abbdf2 100644 --- a/gcc/ipa-struct-reorg/ipa-struct-reorg.cc +++ b/gcc/ipa-struct-reorg/ipa-struct-reorg.cc @@ -5014,35 +5014,21 @@ ipa_struct_reorg::wholeaccess (tree expr, tree base, if (TREE_CODE (expr) == ADDR_EXPR && TREE_OPERAND (expr, 0) == base) return true; - if (!accesstype) + if (!accesstype || !handled_type (TREE_TYPE (expr)) || !t || !t->type) return false; - if (!types_compatible_p (TREE_TYPE (expr), TREE_TYPE (accesstype))) - return false; - - if (!handled_type (TREE_TYPE (expr))) - return false; - - if (!t || !t->type) - return false; - - tree type = TYPE_MAIN_VARIANT (t->type); + /* T *_1; _2 = MEM[(T *)_1]. */ if (TREE_CODE (expr) == MEM_REF - && POINTER_TYPE_P (TREE_TYPE (expr)) - && POINTER_TYPE_P (accesstype) - && POINTER_TYPE_P (TREE_TYPE (accesstype)) + && integer_zerop (TREE_OPERAND (expr, 1)) && 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))); + && types_compatible_p (TREE_TYPE (TREE_TYPE (base)), t->type)) + return POINTER_TYPE_P (accesstype) + && types_compatible_p (TREE_TYPE (accesstype), t->type); - if (t == other_type) - return true; + if (!types_compatible_p (TREE_TYPE (expr), TREE_TYPE (accesstype))) + return false; - return false; + return t == find_type (inner_type (TREE_TYPE (expr))); } bool diff --git a/gcc/testsuite/gcc.dg/struct/dfc_wholeaccess.c b/gcc/testsuite/gcc.dg/struct/dfc_wholeaccess.c new file mode 100644 index 00000000000..60d73f0f0ed --- /dev/null +++ b/gcc/testsuite/gcc.dg/struct/dfc_wholeaccess.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ + +struct Type_A; + +// Optimized type. +struct Type_B { + int b; +}; + +__attribute__((used)) static void test() { + struct Type_A* a; + struct Type_B* b; + + // MEM[(Type_B*)a] = *b; + *((struct Type_B*)a) = *b; +} + +// This testsuite should be compiled successfully without ICE. -- Gitee