public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-2963] c++: reduce temporaries in ?:
Date: Thu, 29 Sep 2022 17:52:20 +0000 (GMT)	[thread overview]
Message-ID: <20220929175220.E28E53854801@sourceware.org> (raw)

https://gcc.gnu.org/g:32b2eb59fb904926eff77811e08766a8ae6804a7

commit r13-2963-g32b2eb59fb904926eff77811e08766a8ae6804a7
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Sep 19 19:08:10 2022 +0200

    c++: reduce temporaries in ?:
    
    When the sides of ?: are class prvalues, we wrap the COND_EXPR in a
    TARGET_EXPR so that both sides will initialize the same temporary.  But in
    this case we were stripping the outer TARGET_EXPR and conditionally creating
    different temporaries, unnecessarily using extra stack.  The
    recently added TARGET_EXPR_NO_ELIDE flag avoids this.
    
    gcc/cp/ChangeLog:
    
            * call.cc (build_conditional_expr): Set TARGET_EXPR_NO_ELIDE on the
            outer TARGET_EXPR.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/tree-ssa/cond-temp1.C: New test.

Diff:
---
 gcc/cp/call.cc                             |  8 +++++++-
 gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C | 16 ++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 3506b0fcfbb..9fad3cb950b 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -6009,7 +6009,13 @@ build_conditional_expr (const op_location_t &loc,
 	 but now we sometimes wrap them in NOP_EXPRs so the test would
 	 fail.  */
       if (CLASS_TYPE_P (TREE_TYPE (result)))
-	result = get_target_expr (result, complain);
+	{
+	  result = get_target_expr (result, complain);
+	  /* Tell gimplify_modify_expr_rhs not to strip this in
+	     assignment context: we want both arms to initialize
+	     the same temporary.  */
+	  TARGET_EXPR_NO_ELIDE (result) = true;
+	}
       /* If this expression is an rvalue, but might be mistaken for an
 	 lvalue, we must add a NON_LVALUE_EXPR.  */
       result = rvalue (result);
diff --git a/gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C b/gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C
new file mode 100644
index 00000000000..b15635853f2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C
@@ -0,0 +1,16 @@
+// Test that the ?: only creates one temporary.
+// { dg-additional-options "-fdump-tree-gimple" }
+// { dg-final { scan-tree-dump-times "struct A" 2 "gimple" } }
+
+struct A
+{
+  int i;
+  A(int);
+};
+
+bool b;
+int main()
+{
+  A a = 1;
+  a = b ? A(2) : A(3);
+}

                 reply	other threads:[~2022-09-29 17:52 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220929175220.E28E53854801@sourceware.org \
    --to=jason@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /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).