]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Do not create temporaries for initialization statements
authorEric Botcazou <ebotcazou@adacore.com>
Tue, 10 Dec 2024 09:24:47 +0000 (10:24 +0100)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Tue, 7 Jan 2025 12:33:35 +0000 (13:33 +0100)
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.

gcc/ada/gcc-interface/trans.cc

index cda73d509e84787e82ad88b675ce0c3f47edea95..b65a846ee6671dc3b3f1cd9b1bdc8b787e786742 100644 (file)
@@ -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