https://gcc.gnu.org/g:d851e08ba97f5d0671711763f6a7d4c94afb8457
commit r15-744-gd851e08ba97f5d0671711763f6a7d4c94afb8457 Author: Eric Botcazou <ebotca...@adacore.com> Date: Tue Mar 12 17:56:00 2024 +0100 ada: Make detection of useless copy for return more robust In the return-by-invisible-reference case, the return object of an extended return statement is allocated directly on the return stack and, therefore, the copy operation on return is useless. The code detecting this was not robust enough and missed some complex cases. gcc/ada/ * gcc-interface/trans.cc (gnat_to_gnu) <N_Simple_Return_Statement>: In the return-by-invisible-reference case, remove conversions before looking for a dereference in the return values and building the test protecting against a useless copy operation. Diff: --- gcc/ada/gcc-interface/trans.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gcc/ada/gcc-interface/trans.cc b/gcc/ada/gcc-interface/trans.cc index a6b86ec8b51..4ae599b8b4c 100644 --- a/gcc/ada/gcc-interface/trans.cc +++ b/gcc/ada/gcc-interface/trans.cc @@ -7767,11 +7767,12 @@ gnat_to_gnu (Node_Id gnat_node) gnu_result = build2 (INIT_EXPR, void_type_node, gnu_ret_deref, gnu_ret_val); /* Avoid a useless copy with __builtin_return_slot. */ - if (INDIRECT_REF_P (gnu_ret_val)) + tree gnu_inner_val = remove_conversions (gnu_ret_val, false); + if (INDIRECT_REF_P (gnu_inner_val)) gnu_result = build3 (COND_EXPR, void_type_node, fold_build2 (NE_EXPR, boolean_type_node, - TREE_OPERAND (gnu_ret_val, 0), + TREE_OPERAND (gnu_inner_val, 0), gnu_ret_obj), gnu_result, NULL_TREE); add_stmt_with_node (gnu_result, gnat_node);