public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Pierre-Marie de Rodat <pmderodat@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-668] [Ada] Avoid copy operation for returns involving function calls
Date: Thu, 19 May 2022 14:09:12 +0000 (GMT)	[thread overview]
Message-ID: <20220519140912.8ABE9383F847@sourceware.org> (raw)

https://gcc.gnu.org/g:9aa9ba95ea3d4800974135da85fd4d9cd310b69e

commit r13-668-g9aa9ba95ea3d4800974135da85fd4d9cd310b69e
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Tue Apr 12 23:34:48 2022 +0200

    [Ada] Avoid copy operation for returns involving function calls
    
    gcc/ada/
    
            * gcc-interface/decl.cc (gnat_to_gnu_entity) <E_Constant>: Deal with
            a constant related to a return in a function specially.
            * gcc-interface/trans.cc (Call_to_gnu): Use return slot optimization
            if the target is a return object.
            (gnat_to_gnu) <N_Object_Declaration>: Deal with a constant related
            to a return in a function specially.

Diff:
---
 gcc/ada/gcc-interface/decl.cc  | 11 +++++++++++
 gcc/ada/gcc-interface/trans.cc | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/gcc/ada/gcc-interface/decl.cc b/gcc/ada/gcc-interface/decl.cc
index 2d9b41f984c..50d17f75562 100644
--- a/gcc/ada/gcc-interface/decl.cc
+++ b/gcc/ada/gcc-interface/decl.cc
@@ -630,6 +630,17 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, bool definition)
       break;
 
     case E_Constant:
+      /* If this is a constant related to a return in a function returning by
+	 invisible reference without expression, get the return object.  */
+      if (Is_Related_To_Func_Return (gnat_entity)
+	  && current_function_decl
+	  && TREE_ADDRESSABLE (TREE_TYPE (current_function_decl))
+	  && !gnu_expr)
+	{
+	  gnu_decl = DECL_RESULT (current_function_decl);
+	  break;
+	}
+
       /* Ignore constant definitions already marked with the error node.  See
 	 the N_Object_Declaration case of gnat_to_gnu for the rationale.  */
       if (definition
diff --git a/gcc/ada/gcc-interface/trans.cc b/gcc/ada/gcc-interface/trans.cc
index 3e8842f4a92..e80200ee46a 100644
--- a/gcc/ada/gcc-interface/trans.cc
+++ b/gcc/ada/gcc-interface/trans.cc
@@ -5281,6 +5281,15 @@ Call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target,
 		   && return_slot_opt_for_pure_call_p (gnu_target, gnu_call))
 	    op_code = INIT_EXPR;
 
+	  /* If this is the initialization of a return object in a function
+	     returning by invisible reference, we can always use the return
+	     slot optimization.  */
+	  else if (TREE_CODE (gnu_target) == INDIRECT_REF
+		   && TREE_CODE (TREE_OPERAND (gnu_target, 0)) == RESULT_DECL
+		   && current_function_decl
+		   && TREE_ADDRESSABLE (TREE_TYPE (current_function_decl)))
+	    op_code = INIT_EXPR;
+
 	  else
 	    op_code = MODIFY_EXPR;
 
@@ -6380,6 +6389,39 @@ gnat_to_gnu (Node_Id gnat_node)
 	      || Is_Concurrent_Type (Etype (gnat_temp))))
 	break;
 
+      /* If this is a constant related to a return initialized by a reference
+	 to a function call in a function returning by invisible reference:
+
+	   type Ann is access all Result_Type;
+	   Rnn : constant Ann := Func'reference;
+	   [...]
+	   return Rnn.all;
+
+	 then elide the temporary by forwarding the return object to Func:
+
+	   *<retval> = Func (); [return slot optimization]
+	   [...]
+	   return <retval>;
+
+	 That's necessary if the result type needs finalization because the
+	 temporary would never be adjusted as Expand_Simple_Function_Return
+	 also elides the temporary in this case.  */
+      if (Ekind (gnat_temp) == E_Constant
+	  && Is_Related_To_Func_Return (gnat_temp)
+	  && Nkind (Expression (gnat_node)) == N_Reference
+	  && Nkind (Prefix (Expression (gnat_node))) == N_Function_Call
+	  && current_function_decl
+	  && TREE_ADDRESSABLE (TREE_TYPE (current_function_decl)))
+	{
+	  gnu_result = gnat_to_gnu_entity (gnat_temp, NULL_TREE, true);
+	  gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
+	  gnu_result
+	    = Call_to_gnu (Prefix (Expression (gnat_node)),
+			   &gnu_result_type, gnu_result,
+			   NOT_ATOMIC, false);
+	  break;
+	}
+
       if (Present (Expression (gnat_node))
 	  && !(kind == N_Object_Declaration && No_Initialization (gnat_node))
 	  && (!type_annotate_only


                 reply	other threads:[~2022-05-19 14:09 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=20220519140912.8ABE9383F847@sourceware.org \
    --to=pmderodat@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).