From patchwork Thu May 5 14:53:21 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 94280 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 96FF6B6FE5 for ; Fri, 6 May 2011 00:53:50 +1000 (EST) Received: (qmail 1983 invoked by alias); 5 May 2011 14:53:45 -0000 Received: (qmail 1967 invoked by uid 22791); 5 May 2011 14:53:43 -0000 X-SWARE-Spam-Status: No, hits=-6.3 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 05 May 2011 14:53:22 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p45ErMUD022403 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 5 May 2011 10:53:22 -0400 Received: from [127.0.0.1] (ovpn-113-126.phx2.redhat.com [10.3.113.126]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p45ErLHX024062 for ; Thu, 5 May 2011 10:53:22 -0400 Message-ID: <4DC2B9E1.2020200@redhat.com> Date: Thu, 05 May 2011 10:53:21 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110421 Fedora/3.1.9-2.fc14 Lightning/1.0b2 Thunderbird/3.1.9 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/48873 (unnecessary dtor calls in new-expressions) Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Here the issue was that build_new_1 wants to stabilize any constructor arguments so that the EH region for deleting the allocated memory can be as small as possible. But stabilize_expr was being over-eager, making a copy of a class with trivial constructors, but a non-trivial (and indeed non-callable) destructor. And even if it didn't have a problematic destructor, there's no reason to introduce extra copies of trivially copyable classes. So this patch changes stabilize_expr to only make copies of scalar types and class prvalues. Tested x86_64-pc-linux-gnu, applying to trunk. commit 21a411247eb5e59895aa54ff9a85e3cf8795a81a Author: Jason Merrill Date: Wed May 4 11:15:30 2011 -0400 PR c++/48873 * tree.c (stabilize_expr): Don't make gratuitous copies of classes. diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c index 0f2f86c..9a6e26d 100644 --- a/gcc/cp/tree.c +++ b/gcc/cp/tree.c @@ -3119,7 +3119,10 @@ stabilize_expr (tree exp, tree* initp) if (!TREE_SIDE_EFFECTS (exp)) init_expr = NULL_TREE; - else if (!TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (exp)) + /* There are no expressions with REFERENCE_TYPE, but there can be call + arguments with such a type; just treat it as a pointer. */ + else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE + || SCALAR_TYPE_P (exp) || !lvalue_or_rvalue_with_address_p (exp)) { init_expr = get_target_expr (exp); diff --git a/gcc/testsuite/g++.dg/init/new32.C b/gcc/testsuite/g++.dg/init/new32.C new file mode 100644 index 0000000..f827857 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/new32.C @@ -0,0 +1,16 @@ +// PR c++/48873 + +#include + +struct D { +private: + ~D(); +}; + +template +T& create(); + +void f() +{ + D* dp = new (((void*) 0)) D(create()); // # +}