[COMMITTED,28/31] ada: Do not create temporaries for initialization statements

Message ID 20250107125350.619654-28-poulhies@adacore.com
State New
Headers
Series [COMMITTED,01/31] ada: Restrict previous change made to expansion of allocators |

Commit Message

Marc Poulhiès Jan. 7, 2025, 12:53 p.m. UTC
  From: Eric Botcazou <ebotcazou@adacore.com>

Assignment statements marked with the No_Ctrl_Actions or No_Finalize_Actions
flag are initialization statements and, therefore, no temporaries are needed
to hold the value of the right-hand side for them.

gcc/ada/ChangeLog:

	* gcc-interface/trans.cc (Call_to_gnu): Always use the return slot
	optimization if the parent node is an initialization statement.
	(gnat_to_gnu) <N_Assignment_Statement>: Build an INIT_EXPR instead
	of a MODIFY_EXPR if this is an initialization statement.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/gcc-interface/trans.cc | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
  

Patch

diff --git a/gcc/ada/gcc-interface/trans.cc b/gcc/ada/gcc-interface/trans.cc
index cda73d509e8..b65a846ee66 100644
--- a/gcc/ada/gcc-interface/trans.cc
+++ b/gcc/ada/gcc-interface/trans.cc
@@ -5517,10 +5517,17 @@  Call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target,
 
 	  gigi_checking_assert (!Do_Range_Check (gnat_node));
 
+	  /* If the parent is an initialization statement, we can use the
+	     return slot optimization.  */
+	  if (Nkind (gnat_parent) == N_Assignment_Statement
+	      && (No_Ctrl_Actions (gnat_parent)
+		  || No_Finalize_Actions (gnat_parent)))
+	    op_code = INIT_EXPR;
+
 	  /* ??? If the return type has variable size, then force the return
 	     slot optimization as we would not be able to create a temporary.
 	     That's what has been done historically.  */
-	  if (return_type_with_variable_size_p (gnu_result_type))
+	  else if (return_type_with_variable_size_p (gnu_result_type))
 	    op_code = INIT_EXPR;
 
 	  /* If this is a call to a pure function returning an array of scalar
@@ -7811,6 +7818,12 @@  gnat_to_gnu (Node_Id gnat_node)
 		    = build_unary_op (ADDR_EXPR, TREE_TYPE (arg), gnu_lhs);
 		}
 
+	      /* If the statement is an initialization, build one too.  */
+              else if (No_Ctrl_Actions (gnat_node)
+		       || No_Finalize_Actions (gnat_node))
+		gnu_result
+		  = build_binary_op (INIT_EXPR, NULL_TREE, gnu_lhs, gnu_rhs);
+
 	      /* Otherwise build a regular assignment.  */
 	      else
 		gnu_result