From: Jakub Jelinek Date: Tue, 30 May 2017 08:17:18 +0000 (+0200) Subject: backport: re PR c++/80129 (wrong code with ternary struct assignment to const) X-Git-Tag: releases/gcc-5.5.0~251 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=108358c7692648b5d4b84ed0877504a208bf5034;p=thirdparty%2Fgcc.git backport: re PR c++/80129 (wrong code with ternary struct assignment to const) Backported from mainline 2017-03-22 Jakub Jelinek PR c++/80129 * gimplify.c (gimplify_modify_expr_rhs) : Clear TREE_READONLY on result if writing it more than once. * g++.dg/torture/pr80129.C: New test. From-SVN: r248662 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 09a6a4bf9f74..5ec64f66e44d 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,6 +1,12 @@ 2017-05-30 Jakub Jelinek Backported from mainline + 2017-03-22 Jakub Jelinek + + PR c++/80129 + * gimplify.c (gimplify_modify_expr_rhs) : Clear + TREE_READONLY on result if writing it more than once. + 2017-03-09 Jakub Jelinek PR sanitizer/79944 diff --git a/gcc/gimplify.c b/gcc/gimplify.c index b7aa11d67fcd..052fc08aba77 100644 --- a/gcc/gimplify.c +++ b/gcc/gimplify.c @@ -4293,6 +4293,14 @@ gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_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, diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 7f1612091ef3..895f61037d20 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,6 +1,11 @@ 2017-05-30 Jakub Jelinek Backported from mainline + 2017-03-22 Jakub Jelinek + + PR c++/80129 + * g++.dg/torture/pr80129.C: New test. + 2017-03-21 Jakub Jelinek PR c/80097 diff --git a/gcc/testsuite/g++.dg/torture/pr80129.C b/gcc/testsuite/g++.dg/torture/pr80129.C new file mode 100644 index 000000000000..134293cd0855 --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/pr80129.C @@ -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 (); +}