From: "Marc Poulhiès" <poulhies@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Eric Botcazou <ebotcazou@adacore.com>
Subject: [COMMITTED] ada: Elide temporary for aliased array with unconstrained nominal subtype
Date: Tue, 7 Nov 2023 10:20:33 +0100 [thread overview]
Message-ID: <20231107092034.3906691-1-poulhies@adacore.com> (raw)
From: Eric Botcazou <ebotcazou@adacore.com>
When the array is initialized with the result of a call to a function whose
result type is unconstrained, then the result is allocated with its bounds,
so the array can be rewritten as a renaming of the result in this case too.
gcc/ada/
* exp_ch3.adb (Expand_N_Object_Declaration): Fold initialization
expression of Nominal_Subtype_Is_Constrained_Array constant into
the computation of Rewrite_As_Renaming and remove the constant.
Set it to True for an aliased array with unconstrained nominal
subtype if the subtype of the expression is also unconstrained.
Tested on x86_64-pc-linux-gnu, committed on master.
---
gcc/ada/exp_ch3.adb | 47 +++++++++++++++++++++++----------------------
1 file changed, 24 insertions(+), 23 deletions(-)
diff --git a/gcc/ada/exp_ch3.adb b/gcc/ada/exp_ch3.adb
index 511d4c09b22..f88ac7e6542 100644
--- a/gcc/ada/exp_ch3.adb
+++ b/gcc/ada/exp_ch3.adb
@@ -7348,13 +7348,6 @@ package body Exp_Ch3 is
Rewrite_As_Renaming : Boolean := False;
-- Whether to turn the declaration into a renaming at the end
- Nominal_Subtype_Is_Constrained_Array : constant Boolean :=
- Comes_From_Source (Obj_Def)
- and then Is_Array_Type (Typ) and then Is_Constrained (Typ);
- -- Used to avoid rewriting as a renaming for constrained arrays,
- -- which is only a problem for source arrays; others have the
- -- correct bounds (see below).
-
-- Start of processing for Expand_N_Object_Declaration
begin
@@ -8098,10 +8091,25 @@ package body Exp_Ch3 is
Is_Entity_Name (Original_Node (Obj_Def))
- -- Nor if it is effectively an unconstrained declaration
+ -- If we have "X : S := ...;", and S is a constrained array
+ -- subtype, then we cannot rename, because renamings ignore
+ -- the constraints of S, so that would change the semantics
+ -- (sliding would not occur on the initial value). This is
+ -- only a problem for source objects though, the others have
+ -- the correct bounds.
+
+ and then not (Comes_From_Source (Obj_Def)
+ and then Is_Array_Type (Typ)
+ and then Is_Constrained (Typ))
+
+ -- Moreover, if we have "X : aliased S := "...;" and S is an
+ -- unconstrained array type, then we can rename only if the
+ -- initialization expression has an unconstrained subtype too,
+ -- because the bounds must be present within X.
and then not (Is_Array_Type (Typ)
- and then Is_Constr_Subt_For_UN_Aliased (Typ))
+ and then Is_Constr_Subt_For_UN_Aliased (Typ)
+ and then Is_Constrained (Etype (Expr_Q)))
-- We may use a renaming if the initialization expression is a
-- captured function call that meets a few conditions.
@@ -8109,23 +8117,16 @@ package body Exp_Ch3 is
and then
(Is_Renamable_Function_Call (Expr_Q)
- -- Or else if it is a variable with OK_To_Rename set
+ -- Or else if it is a variable with OK_To_Rename set
- or else (OK_To_Rename_Ref (Expr_Q)
- and then not Special_Ret_Obj)
+ or else (OK_To_Rename_Ref (Expr_Q)
+ and then not Special_Ret_Obj)
- -- Or else if it is a slice of such a variable
-
- or else (Nkind (Expr_Q) = N_Slice
- and then OK_To_Rename_Ref (Prefix (Expr_Q))
- and then not Special_Ret_Obj))
-
- -- If we have "X : S := ...;", and S is a constrained array
- -- subtype, then we cannot rename, because renamings ignore
- -- the constraints of S, so that would change the semantics
- -- (sliding would not occur on the initial value).
+ -- Or else if it is a slice of such a variable
- and then not Nominal_Subtype_Is_Constrained_Array;
+ or else (Nkind (Expr_Q) = N_Slice
+ and then OK_To_Rename_Ref (Prefix (Expr_Q))
+ and then not Special_Ret_Obj));
-- If the type needs finalization and is not inherently limited,
-- then the target is adjusted after the copy and attached to the
--
2.42.0
reply other threads:[~2023-11-07 9:20 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231107092034.3906691-1-poulhies@adacore.com \
--to=poulhies@adacore.com \
--cc=ebotcazou@adacore.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).