diff --git a/gcc/ipa-struct-reorg/ipa-struct-reorg.c b/gcc/ipa-struct-reorg/ipa-struct-reorg.c index 85986ce58032688a56b8d3b3686b7884fdaf8159..2bf41e0d83bb292a7cec57d2fb91078375a20f19 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; @@ -6644,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 = @@ -6688,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/rf_ptr2void_lto.c b/gcc/testsuite/gcc.dg/struct/rf_ptr2void_lto.c index 190b94182752b3fd57202c05d414527cc1e9477d..2ae46fb3112934f657c550f0ed505954a4dbc833 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 0000000000000000000000000000000000000000..317aafa5f72d80be0bb37e459354d05294eee1f2 --- /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 diff --git a/gcc/testsuite/gcc.dg/struct/struct_reorg-1.c b/gcc/testsuite/gcc.dg/struct/struct_reorg-1.c index 6565fe8dd633a408539c75c366ce92bb2a2a7833..23444fe8b0de3ff8c48eae1589393417b2fc2608 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 5864ad46fd383020401b7b6593a76ce79e141d91..2d1f95c99350b95d2d5e08a3fa533eec2f37fea7 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" } } */