[COMMITTED,27/31] ada: Make detection of useless copy for return more robust

Message ID 20240521073035.314024-27-poulhies@adacore.com
State Committed
Commit d851e08ba97f5d0671711763f6a7d4c94afb8457
Series [COMMITTED,01/31] ada: Add new Mingw task priority mapping |

Commit Message

Marc Poulhiès May 21, 2024, 7:30 a.m. UTC
  From: Eric Botcazou <ebotcazou@adacore.com>

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-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.

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

 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))
 		    = 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_result, NULL_TREE);
 		add_stmt_with_node (gnu_result, gnat_node);