This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Fix gimplification of const var initialization from COND_EXPR (PR c++/80129)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Richard Biener <rguenther at suse dot de>
- Cc: Jason Merrill <jason at redhat dot com>, gcc-patches at gcc dot gnu dot org
- Date: Wed, 22 Mar 2017 14:13:28 +0100
- Subject: Re: [PATCH] Fix gimplification of const var initialization from COND_EXPR (PR c++/80129)
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=jakub at redhat dot com
- Dkim-filter: OpenDKIM Filter v2.11.0 mx1.redhat.com CFA9761E50
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com CFA9761E50
- References: <20170321202620.GB11094@tucnak> <alpine.LSU.2.20.1703220918330.30051@zhemvz.fhfr.qr> <20170322083648.GC11094@tucnak> <alpine.LSU.2.20.1703220955160.30051@zhemvz.fhfr.qr>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Wed, Mar 22, 2017 at 09:59:09AM +0100, Richard Biener wrote:
> Yeah, changing it to VAR_P and expanding the comment so it says
> it avoids incorrect promotion to readonly-static. (so it _is_
> fishy that we have multiple assignments to TREE_READONLY objects
> given that code looks at a single assignment only)
So like this?
2017-03-22 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,14 @@ 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 incorrectly promote
+ the variable to static const and initialize it at compile
+ time in one of the branches. */
+ if (VAR_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