[COMMITTED] ada: Elide the copy in extended returns for nonlimited by-reference types

2023-09-05 Thread Marc Poulhiès via Gcc-patches
From: Eric Botcazou 

gcc/ada/

* gcc-interface/trans.cc (gnat_to_gnu): Really test Storage_Pool on
the simple return statement.

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

---
 gcc/ada/gcc-interface/trans.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gcc/ada/gcc-interface/trans.cc b/gcc/ada/gcc-interface/trans.cc
index 5d93060c6d8..e99fbb4eb5e 100644
--- a/gcc/ada/gcc-interface/trans.cc
+++ b/gcc/ada/gcc-interface/trans.cc
@@ -8519,7 +8519,7 @@ gnat_to_gnu (Node_Id gnat_node)
   && ((Nkind (Parent (gnat_node)) == N_Attribute_Reference
&& lvalue_required_for_attribute_p (Parent (gnat_node)))
   || (Nkind (Parent (gnat_node)) == N_Simple_Return_Statement
-  && No (Storage_Pool (gnat_node)
+  && No (Storage_Pool (Parent (gnat_node))
 ;
 
   else if (TREE_TYPE (gnu_result) != gnu_result_type)
-- 
2.40.0



[COMMITTED] ada: Elide the copy in extended returns for nonlimited by-reference types

2023-07-28 Thread Marc Poulhiès via Gcc-patches
From: Eric Botcazou 

gcc/ada/

* gcc-interface/trans.cc (gnat_to_gnu): Restrict previous change to
the case where the simple return statement has got no storage pool.

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

---
 gcc/ada/gcc-interface/trans.cc | 7 ---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/gcc/ada/gcc-interface/trans.cc b/gcc/ada/gcc-interface/trans.cc
index fd85facaf70..5d93060c6d8 100644
--- a/gcc/ada/gcc-interface/trans.cc
+++ b/gcc/ada/gcc-interface/trans.cc
@@ -8451,8 +8451,8 @@ gnat_to_gnu (Node_Id gnat_node)
 
5. If this is a reference to an unconstrained array which is used either
  as the prefix of an attribute reference that requires an lvalue or in
- a return statement, then return the result unmodified because we want
- to return the original bounds.
+ a return statement without storage pool, return the result unmodified
+ because we want to return the original bounds.
 
6. Finally, if the type of the result is already correct.  */
 
@@ -8518,7 +8518,8 @@ gnat_to_gnu (Node_Id gnat_node)
   && Present (Parent (gnat_node))
   && ((Nkind (Parent (gnat_node)) == N_Attribute_Reference
&& lvalue_required_for_attribute_p (Parent (gnat_node)))
-  || Nkind (Parent (gnat_node)) == N_Simple_Return_Statement))
+  || (Nkind (Parent (gnat_node)) == N_Simple_Return_Statement
+  && No (Storage_Pool (gnat_node)
 ;
 
   else if (TREE_TYPE (gnu_result) != gnu_result_type)
-- 
2.40.0



[COMMITTED] ada: Elide the copy in extended returns for nonlimited by-reference types

2022-12-06 Thread Marc Poulhiès via Gcc-patches
From: Eric Botcazou 

This implements elision of the copy operation for extended return statements
in the case of nonlimited by-reference types (the copy operation is already
elided for limited types by the front-end and nonlimited non-by-reference
types by the code generator), which comprise controlled and tagged types.

The implementation partly reuses the machinery implemented for limited types
(the build-in-place machinery) to allocate the return object directly on the
primary or the secondary stack, depending on whether the result type of the
function is constrained or not.

This requires further special-casing for the allocators generated by this
machinery as well as an adjustment to the implementation of a specific case
of string concatenation.

gcc/ada/

* einfo.ads (Actual_Subtype): Document additional usage.
* exp_aggr.adb (Expand_Array_Aggregate): Replace test on
Is_Build_In_Place_Return_Object with Is_Special_Return_Object.
* exp_ch3.adb (Expand_N_Object_Declaration): Factor out parts of the
processing done for build-in-place return objects and reuse them to
implement a similar processing for specific return objects.
* exp_ch4.adb (Expand_Allocator_Expression): Do not generate a tag
assignment or an adjustment if the allocator was made for a special
return object.
(Expand_Concatenate): If the result is allocated on the secondary
stack, use an unconstrained allocation.
* exp_ch6.ads (Apply_CW_Accessibility_Check): New declaration.
(Is_By_Reference_Return_Object): Likewise.
(Is_Secondary_Stack_Return_Object): Likewise.
(Is_Special_Return_Object): Likewise.
* exp_ch6.adb (Expand_Ctrl_Function_Call): Do not bail out for the
expression in the declaration of a special return object.
(Expand_N_Extended_Return_Statement): Add missing guard and move
the class-wide accessibility check to Expand_N_Object_Declaration.
(Expand_Simple_Function_Return): Delete obsolete commentary.
Skip the special processing for types that require finalization or
are returned on the secondary stack if the return originally comes
from an extended return statement.  Add missing Constant_Present.
(Is_By_Reference_Return_Object): New predicate.
(Is_Secondary_Stack_Return_Object): Likewise.
(Is_Special_Return_Object): Likewise.
* exp_util.adb (Is_Related_To_Func_Return): Also return true if the
parent of the expression is the renaming declaration generated for
the expansion of a return object.
* gen_il-fields.ads (Opt_Field_Enum): Replace Alloc_For_BIP_Return
with For_Special_Return_Object.
* gen_il-gen-gen_nodes.adb (N_Allocator): Likewise.
* gen_il-internals.adb (Image): Remove Alloc_For_BIP_Return.
* sem_ch3.adb (Check_Return_Subtype_Indication): New procedure
moved from sem_ch6.adb.
(Analyze_Object_Declaration): Call it on a return object.
* sem_ch4.adb: Add with and use clauses for Rtsfind.
(Analyze_Allocator): Test For_Special_Return_Object to skip checks
for allocators made for special return objects.
Do not report restriction violations for the return stack pool.
* sem_ch5.adb (Analyze_Assignment.Set_Assignment_Type): Return the
Actual_Subtype for return objects that live on the secondary stack.
* sem_ch6.adb (Check_Return_Subtype_Indication): Move procedure to
sem_ch3.adb.
(Analyze_Function_Return): Do not call above procedure.
* sem_res.adb (Resolve_Allocator): Replace Alloc_For_BIP_Return
with For_Special_Return_Object.
* sinfo.ads: Likewise.
* treepr.adb (Image): Remove Alloc_For_BIP_Return.
* gcc-interface/trans.cc (gnat_to_gnu): Do not convert to the result
type in the unconstrained array type case if the parent is a simple
return statement.

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

---
 gcc/ada/einfo.ads|   8 +-
 gcc/ada/exp_aggr.adb |   2 +-
 gcc/ada/exp_ch3.adb  | 468 +++
 gcc/ada/exp_ch4.adb  | 126 +
 gcc/ada/exp_ch6.adb  | 101 ---
 gcc/ada/exp_ch6.ads  |  27 +-
 gcc/ada/exp_util.adb |   6 +-
 gcc/ada/gcc-interface/trans.cc   |  12 +-
 gcc/ada/gen_il-fields.ads|   2 +-
 gcc/ada/gen_il-gen-gen_nodes.adb |   2 +-
 gcc/ada/gen_il-internals.adb |   2 -
 gcc/ada/sem_ch3.adb  | 139 +
 gcc/ada/sem_ch4.adb  |  64 ++---
 gcc/ada/sem_ch5.adb  |   6 +-
 gcc/ada/sem_ch6.adb  | 135 -
 gcc/ada/sem_res.adb  |   2 +-
 gcc/ada/sinfo.ads|  10 +-
 gcc/ada/treepr.adb   |   5 +-
 18 files changed, 706 insertions(+), 411 deletions(-)

diff --git a/gcc/ada/einfo.ads