public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r11-6922] varpool: Restore GENERIC TREE_READONLY automatic var optimization [PR7260]
Date: Wed, 27 Jan 2021 09:10:49 +0000 (GMT)	[thread overview]
Message-ID: <20210127091049.25317393BC39@sourceware.org> (raw)

https://gcc.gnu.org/g:efc9ccbfd0ca4da6bf0aba913df6f77f56044dc2

commit r11-6922-gefc9ccbfd0ca4da6bf0aba913df6f77f56044dc2
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jan 27 10:08:46 2021 +0100

    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.

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/pr97260.c | 11 +++++++++++
 gcc/varpool.c                           |  8 ++++++++
 2 files changed, 19 insertions(+)

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 00000000000..9b3723b7834
--- /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 e0488ed5a12..86f16052984 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;
     }


                 reply	other threads:[~2021-01-27  9:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210127091049.25317393BC39@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).