ada: Elide the copy in extended returns for nonlimited by-reference types
commitea588d41f39428b0c3b02f016353dceb1aaaaa39
authorEric Botcazou <ebotcazou@adacore.com>
Fri, 2 Dec 2022 09:55:49 +0000 (2 10:55 +0100)
committerMarc Poulhiès <poulhies@adacore.com>
Tue, 6 Dec 2022 13:58:48 +0000 (6 14:58 +0100)
treece91f5abdcd15583a7e4e5f4ed70e44022c10d07
parenta444c05623faa2b6bd1bbc8f7908b8ea3d83b475
ada: Elide the copy in extended returns for nonlimited by-reference types

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.
18 files changed:
gcc/ada/einfo.ads
gcc/ada/exp_aggr.adb
gcc/ada/exp_ch3.adb
gcc/ada/exp_ch4.adb
gcc/ada/exp_ch6.adb
gcc/ada/exp_ch6.ads
gcc/ada/exp_util.adb
gcc/ada/gcc-interface/trans.cc
gcc/ada/gen_il-fields.ads
gcc/ada/gen_il-gen-gen_nodes.adb
gcc/ada/gen_il-internals.adb
gcc/ada/sem_ch3.adb
gcc/ada/sem_ch4.adb
gcc/ada/sem_ch5.adb
gcc/ada/sem_ch6.adb
gcc/ada/sem_res.adb
gcc/ada/sinfo.ads
gcc/ada/treepr.adb