From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id 78C513858D3C; Fri, 7 Oct 2022 13:22:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 78C513858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665148957; bh=HbNQWbg+jbclCnqycqYB+yGg65/J+VixTDiMnwlsMro=; h=From:To:Subject:Date:From; b=vniviqF+kY7hQ9wnf674NYItZ2zdb2bWWLCgy/j2HmrIVwwjWWstbZg5bI4iI/PJ5 R6VjZJUszVxEN3gsODNvFIT9K5O6edCxwYvRMW/GsIXhCH4ODnhshkGJj15Ge6OZDb XnF8bTeR5hWzb4n0YdbhtqMhx/DazIoIT6Z7KxKY= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3162] gimplify: prevent some C++ temporary elision X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/master X-Git-Oldrev: 89228e3985c5cdf6be58a3b5b1afcad91e9e3422 X-Git-Newrev: d3e5465757c599ea64f611290b7793d3141a6b7c Message-Id: <20221007132237.78C513858D3C@sourceware.org> Date: Fri, 7 Oct 2022 13:22:37 +0000 (GMT) List-Id: https://gcc.gnu.org/g:d3e5465757c599ea64f611290b7793d3141a6b7c commit r13-3162-gd3e5465757c599ea64f611290b7793d3141a6b7c Author: Jason Merrill Date: Wed Oct 5 11:50:59 2022 -0400 gimplify: prevent some C++ temporary elision In this testcase, we were optimizing away the temporary for f(), but C++17 and above are clear that there is a temporary, and because its destructor has visible side-effects we can't optimize it away under the as-if rule. So disable this optimization for TREE_ADDRESSABLE type. I moved the declaration of volatile_p after the call to gimple_fold_indirect_ref_rhs to minimize indentation changes; I don't see any way the value of that flag could be affected by the call. gcc/ChangeLog: * gimplify.cc (gimplify_modify_expr_rhs): Don't optimize x = *(A*)& to x = for a TREE_ADDRESSABLE type. gcc/testsuite/ChangeLog: * g++.dg/init/elide9.C: New test. Diff: --- gcc/gimplify.cc | 15 ++++++++------- gcc/testsuite/g++.dg/init/elide9.C | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc index 95e16f600b5..d4209ea74a3 100644 --- a/gcc/gimplify.cc +++ b/gcc/gimplify.cc @@ -5620,7 +5620,7 @@ gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, } break; case INDIRECT_REF: - { + if (!TREE_ADDRESSABLE (TREE_TYPE (*from_p))) /* If we have code like *(const A*)(A*)&x @@ -5629,11 +5629,13 @@ gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, of "A"), treat the entire expression as identical to "x". This kind of code arises in C++ when an object is bound to a const reference, and if "x" is a TARGET_EXPR we want - to take advantage of the optimization below. */ - bool volatile_p = TREE_THIS_VOLATILE (*from_p); - tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0)); - if (t) + to take advantage of the optimization below. But not if + the type is TREE_ADDRESSABLE; then C++17 says that the + TARGET_EXPR needs to be a temporary. */ + if (tree t + = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0))) { + bool volatile_p = TREE_THIS_VOLATILE (*from_p); if (TREE_THIS_VOLATILE (t) != volatile_p) { if (DECL_P (t)) @@ -5646,8 +5648,7 @@ gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, ret = GS_OK; changed = true; } - break; - } + break; case TARGET_EXPR: { diff --git a/gcc/testsuite/g++.dg/init/elide9.C b/gcc/testsuite/g++.dg/init/elide9.C new file mode 100644 index 00000000000..810d60a94a4 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/elide9.C @@ -0,0 +1,25 @@ +// The static_cast should prevent temporary elision. +// { dg-do run { target c++11 } } + +int d; +struct A +{ + int i; + A() { } + ~A() { ++d; } +}; + +A f() { return A(); } + +struct B +{ + A a; + B(): a(static_cast(f())) {} +}; + +int main() +{ + { B b; } + if (d != 2) + return -1; +}