From 83a35da4910fc7d8f29ced3e0ff8adddeb537731 Mon Sep 17 00:00:00 2001 From: huangxiaoquan Date: Fri, 27 Aug 2021 14:53:18 +0800 Subject: [PATCH 1/2] [StructReorderFields] Add pointer offset check The pointer offset check is added for the expr that is dereferenced in the memory, and escapes struct pointer offset operations involving field order. --- gcc/ipa-struct-reorg/ipa-struct-reorg.c | 11 ++++++ gcc/testsuite/gcc.dg/struct/rf_ptr2void_lto.c | 2 +- gcc/testsuite/gcc.dg/struct/rf_ptr_offset.c | 34 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/struct/rf_ptr_offset.c diff --git a/gcc/ipa-struct-reorg/ipa-struct-reorg.c b/gcc/ipa-struct-reorg/ipa-struct-reorg.c index 85986ce5803..b0d4fe80797 100644 --- a/gcc/ipa-struct-reorg/ipa-struct-reorg.c +++ b/gcc/ipa-struct-reorg/ipa-struct-reorg.c @@ -3876,6 +3876,17 @@ ipa_struct_reorg::get_type_field (tree expr, tree &base, bool &indirect, return false; } + /* Escape the operation of fetching field with pointer offset such as: + *(&(t->right)) = malloc (0); -> MEM[(struct node * *)_1 + 8B] = malloc (0); + */ + if (current_mode != NORMAL + && (TREE_CODE (expr) == MEM_REF) && (offset != 0)) + { + gcc_assert (can_escape); + t->mark_escape (escape_non_multiply_size, NULL); + return false; + } + if (wholeaccess (expr, base, accesstype, t)) { field = NULL; diff --git a/gcc/testsuite/gcc.dg/struct/rf_ptr2void_lto.c b/gcc/testsuite/gcc.dg/struct/rf_ptr2void_lto.c index 190b9418275..2ae46fb3112 100644 --- a/gcc/testsuite/gcc.dg/struct/rf_ptr2void_lto.c +++ b/gcc/testsuite/gcc.dg/struct/rf_ptr2void_lto.c @@ -84,4 +84,4 @@ main () return cnt; } -/* { dg-final { scan-ipa-dump "Number of structures to transform is 1" "reorder_fields" } } */ \ No newline at end of file +/* { dg-final { scan-ipa-dump "No structures to transform." "reorder_fields" } } */ \ No newline at end of file diff --git a/gcc/testsuite/gcc.dg/struct/rf_ptr_offset.c b/gcc/testsuite/gcc.dg/struct/rf_ptr_offset.c new file mode 100644 index 00000000000..317aafa5f72 --- /dev/null +++ b/gcc/testsuite/gcc.dg/struct/rf_ptr_offset.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ + +#include +#include + +struct node +{ + struct node *left, *right; + double a, b, c, d, e, f; +} +*a; +int b, c; +void +CreateNode (struct node **p1) +{ + *p1 = calloc (10, sizeof (struct node)); +} + +int +main () +{ + a->left = 0; + struct node *t = a; + CreateNode (&t->right); + + struct node p = *a; + b = 1; + if (p.left) + b = 0; + if (b) + printf (" Tree.\n"); +} + +/* { dg-final { scan-ipa-dump "No structures to transform." "reorder_fields" } } */ \ No newline at end of file -- Gitee From 0ee0f0ebeb098787cb9698887c237606b6ab10c6 Mon Sep 17 00:00:00 2001 From: huangxiaoquan Date: Wed, 1 Sep 2021 17:07:22 +0800 Subject: [PATCH 2/2] [StructReorderFields] Add lto and whole-program gate Only enable struct reorder fields optimizations in lto or whole-program. This prevents some .c files from being struct reorder fields optimized while some of them are not optimized during project compilation. --- gcc/ipa-struct-reorg/ipa-struct-reorg.c | 8 ++++++-- gcc/testsuite/gcc.dg/struct/struct_reorg-1.c | 8 +++++++- gcc/testsuite/gcc.dg/struct/struct_reorg-3.c | 9 +++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/gcc/ipa-struct-reorg/ipa-struct-reorg.c b/gcc/ipa-struct-reorg/ipa-struct-reorg.c index b0d4fe80797..2bf41e0d83b 100644 --- a/gcc/ipa-struct-reorg/ipa-struct-reorg.c +++ b/gcc/ipa-struct-reorg/ipa-struct-reorg.c @@ -6655,7 +6655,9 @@ pass_ipa_struct_reorg::gate (function *) && flag_lto_partition == LTO_PARTITION_ONE /* Only enable struct optimizations in C since other languages' grammar forbid. */ - && lang_c_p ()); + && lang_c_p () + /* Only enable struct optimizations in lto or whole_program. */ + && (in_lto_p || flag_whole_program)); } const pass_data pass_data_ipa_reorder_fields = @@ -6699,7 +6701,9 @@ pass_ipa_reorder_fields::gate (function *) && flag_lto_partition == LTO_PARTITION_ONE /* Only enable struct optimizations in C since other languages' grammar forbid. */ - && lang_c_p ()); + && lang_c_p () + /* Only enable struct optimizations in lto or whole_program. */ + && (in_lto_p || flag_whole_program)); } } // anon namespace diff --git a/gcc/testsuite/gcc.dg/struct/struct_reorg-1.c b/gcc/testsuite/gcc.dg/struct/struct_reorg-1.c index 6565fe8dd63..23444fe8b0d 100644 --- a/gcc/testsuite/gcc.dg/struct/struct_reorg-1.c +++ b/gcc/testsuite/gcc.dg/struct/struct_reorg-1.c @@ -1,5 +1,5 @@ // { dg-do compile } -// { dg-options "-O3 -flto-partition=one -fipa-struct-reorg -fdump-ipa-all" } +// { dg-options "-O3 -flto-partition=one -fipa-struct-reorg -fdump-ipa-all -fwhole-program" } struct a { @@ -21,4 +21,10 @@ int g(void) return b->t; } +int main() +{ + f (); + return g (); +} + /* { dg-final { scan-ipa-dump "No structures to transform." "struct_reorg" } } */ diff --git a/gcc/testsuite/gcc.dg/struct/struct_reorg-3.c b/gcc/testsuite/gcc.dg/struct/struct_reorg-3.c index 5864ad46fd3..2d1f95c9935 100644 --- a/gcc/testsuite/gcc.dg/struct/struct_reorg-3.c +++ b/gcc/testsuite/gcc.dg/struct/struct_reorg-3.c @@ -1,5 +1,5 @@ // { dg-do compile } -// { dg-options "-O3 -flto-partition=one -fipa-struct-reorg -fdump-ipa-all" } +// { dg-options "-O3 -flto-partition=one -fipa-struct-reorg -fdump-ipa-all -fwhole-program" } #include typedef struct { @@ -10,7 +10,7 @@ typedef struct { compile_stack_elt_t *stack; unsigned size; } compile_stack_type; -void f (const char *p, const char *pend, int c) +__attribute__((noinline)) void f (const char *p, const char *pend, int c) { compile_stack_type compile_stack; while (p != pend) @@ -20,4 +20,9 @@ void f (const char *p, const char *pend, int c) * sizeof (compile_stack_elt_t)); } +int main() +{ + f (NULL, NULL, 1); +} + /* { dg-final { scan-ipa-dump "Number of structures to transform is 1" "struct_reorg" } } */ -- Gitee