diff options
author | Jakub Jelinek <jakub@redhat.com> | 2021-01-27 10:08:46 +0100 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2021-01-27 10:08:46 +0100 |
commit | efc9ccbfd0ca4da6bf0aba913df6f77f56044dc2 (patch) | |
tree | c146b34a47d8ca6cb39ee98c939acb426514a8cd | |
parent | e62bb7f083470ccdadbd83ef885c6fe1e5cb9e67 (diff) | |
download | gcc-efc9ccbfd0ca4da6bf0aba913df6f77f56044dc2.zip gcc-efc9ccbfd0ca4da6bf0aba913df6f77f56044dc2.tar.gz gcc-efc9ccbfd0ca4da6bf0aba913df6f77f56044dc2.tar.bz2 |
varpool: Restore GENERIC TREE_READONLY automatic var optimization [PR7260]
In 4.8 and earlier we used to fold the following to 0 during GENERIC folding,
but we don't do that anymore because ctor_for_folding etc. has been turned into a
GIMPLE centric API, but as the testcase shows, it is invoked even during
GENERIC folding and there the automatic vars still should have meaningful
initializers. I've verified that the C++ FE drops TREE_READONLY on
automatic vars with const qualified types if they require non-constant
(runtime) initialization.
2021-01-27 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/97260
* varpool.c: Include tree-pass.h.
(ctor_for_folding): In GENERIC return DECL_INITIAL for TREE_READONLY
non-TREE_SIDE_EFFECTS automatic variables.
* gcc.dg/tree-ssa/pr97260.c: New test.
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/pr97260.c | 11 | ||||
-rw-r--r-- | gcc/varpool.c | 8 |
2 files changed, 19 insertions, 0 deletions
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr97260.c b/gcc/testsuite/gcc.dg/tree-ssa/pr97260.c new file mode 100644 index 0000000..9b3723b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr97260.c @@ -0,0 +1,11 @@ +/* PR tree-optimization/97260 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */ + +int +foo (void) +{ + const char a[] = "1234"; + return __builtin_memcmp (a, "1234", 4); +} diff --git a/gcc/varpool.c b/gcc/varpool.c index e0488ed..86f1605 100644 --- a/gcc/varpool.c +++ b/gcc/varpool.c @@ -35,6 +35,7 @@ along with GCC; see the file COPYING3. If not see #include "context.h" #include "stringpool.h" #include "attribs.h" +#include "tree-pass.h" const char * const tls_model_names[]={"none", "emulated", "global-dynamic", "local-dynamic", @@ -412,6 +413,13 @@ ctor_for_folding (tree decl) if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) { gcc_assert (!TREE_PUBLIC (decl)); + /* Unless this is called during FE folding. */ + if (cfun + && (cfun->curr_properties & (PROP_trees | PROP_rtl)) == 0 + && TREE_READONLY (decl) + && !TREE_SIDE_EFFECTS (decl) + && DECL_INITIAL (decl)) + return DECL_INITIAL (decl); return error_mark_node; } |