public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Richard Biener <rguenther@suse.de>, Jason Merrill <jason@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: [PATCH] Fix gimplification of const var initialization from COND_EXPR (PR c++/80129)
Date: Tue, 21 Mar 2017 20:26:00 -0000	[thread overview]
Message-ID: <20170321202620.GB11094@tucnak> (raw)

Hi!

For non-gimple reg types var = x ? y : z; is gimplified as
x ? (var = y) : (var = z);.  The problem is that if var is TREE_READONLY,
we can gimplify one of those assignments by promoting them to TREE_STATIC
(if not addressable) and making the initializer into their DECL_INITIAL.
That doesn't work well, because then we conditionally store into that and
during optimizations, as it is a TREE_READONLY var with DECL_INITIAL, just
use that initializer for optimizations.

The following patch fixes that by clearing TREE_READONLY flag if we are
going to store it more than once.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2017-03-21  Jakub Jelinek  <jakub@redhat.com>

	PR c++/80129
	* gimplify.c (gimplify_modify_expr_rhs) <case COND_EXPR>: Clear
	TREE_READONLY on result if writing it more than once.

	* g++.dg/torture/pr80129.C: New test.

--- gcc/gimplify.c.jj	2017-03-21 07:56:55.000000000 +0100
+++ gcc/gimplify.c	2017-03-21 13:37:45.555612652 +0100
@@ -5098,6 +5098,13 @@ gimplify_modify_expr_rhs (tree *expr_p,
 	      if (ret != GS_ERROR)
 		ret = GS_OK;
 
+	      /* If we are going to write RESULT more than once, clear
+		 TREE_READONLY flag, otherwise we might gimplify it
+		 incorrectly.  */
+	      if (DECL_P (result)
+		  && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
+		  && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
+		TREE_READONLY (result) = 0;
 	      if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
 		TREE_OPERAND (cond, 1)
 		  = build2 (code, void_type_node, result,
--- gcc/testsuite/g++.dg/torture/pr80129.C.jj	2017-03-21 13:40:04.179852313 +0100
+++ gcc/testsuite/g++.dg/torture/pr80129.C	2017-03-21 13:41:32.121735570 +0100
@@ -0,0 +1,14 @@
+// PR c++/80129
+// { dg-do run }
+// { dg-options "-std=c++11" }
+
+struct A { bool a; int b; };
+
+int
+main ()
+{
+  bool c = false;
+  const A x = c ? A {true, 1} : A {false, 0};
+  if (x.a)
+    __builtin_abort ();
+}

	Jakub

             reply	other threads:[~2017-03-21 20:26 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-21 20:26 Jakub Jelinek [this message]
2017-03-22  8:20 ` Richard Biener
2017-03-22  8:36   ` Jakub Jelinek
2017-03-22  8:59     ` Richard Biener
2017-03-22 13:13       ` Jakub Jelinek
2017-03-22 13:26         ` Richard Biener

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=20170321202620.GB11094@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=rguenther@suse.de \
    /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).