public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <rguenther@suse.de>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] middle-end/27800 - avoid unnecessary temporary during gimplification
Date: Tue, 7 May 2024 13:21:15 +0200 (CEST)	[thread overview]
Message-ID: <20240507112115.QTZtzaI1-yy7YsCu3mTksN4Nt_sX76DwgurYHjdPhHQ@z> (raw)

This avoids a tempoary when gimplifying reg = a ? b : c, re-using
the LHS of an assignment if that's a register.

Re-bootstrap and regtest running on x86_64-unknown-linux-gnu.

	PR middle-end/27800
	* gimplify.cc (gimplify_modify_expr_rhs): For a COND_EXPR
	avoid a temporary from gimplify_cond_expr when the LHS is
	a register by pushing the assignment into the COND_EXPR arms.

	* gcc.dg/pr27800.c: New testcase.
---
 gcc/gimplify.cc                |  7 +++++--
 gcc/testsuite/gcc.dg/pr27800.c | 11 +++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr27800.c

diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index 763843f45a7..26e96ada4c7 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -6082,8 +6082,11 @@ gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
 	  /* If we're assigning to a non-register type, push the assignment
 	     down into the branches.  This is mandatory for ADDRESSABLE types,
 	     since we cannot generate temporaries for such, but it saves a
-	     copy in other cases as well.  */
-	  if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
+	     copy in other cases as well.
+	     Also avoid an extra temporary and copy when assigning to
+	     a register.  */
+	  if (!is_gimple_reg_type (TREE_TYPE (*from_p))
+	      || (is_gimple_reg (*to_p) && !gimplify_ctxp->allow_rhs_cond_expr))
 	    {
 	      /* This code should mirror the code in gimplify_cond_expr. */
 	      enum tree_code code = TREE_CODE (*expr_p);
diff --git a/gcc/testsuite/gcc.dg/pr27800.c b/gcc/testsuite/gcc.dg/pr27800.c
new file mode 100644
index 00000000000..e92ebc22e6f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr27800.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-gimple" } */
+
+int iii (int a, int b, int c)
+{
+  return a ? b : c;
+}
+
+/* Verify we end up with two assignments and not an extra copy
+   resulting from another temporary generated from gimplify_cond_expr.  */
+/* { dg-final { scan-tree-dump-times " = " 2 "gimple" } } */
-- 
2.35.3

                 reply	other threads:[~2024-05-07 11:21 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=20240507112115.QTZtzaI1-yy7YsCu3mTksN4Nt_sX76DwgurYHjdPhHQ@z \
    --to=rguenther@suse.de \
    --cc=gcc-patches@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).