From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id A6B1338618FE for ; Tue, 7 Nov 2023 09:20:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A6B1338618FE Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A6B1338618FE Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::429 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699348839; cv=none; b=PhubB5aHnKc0gs2Qo9KRcB+nIGiH8/E2V1034AvhKfamHrAroXJvo20fmsE0hy5saZnumdXjpfa3IeITptvNAGlOlx5BJOShgQRQXTR/ECtzcBT4vrC3iY6XJQ8jBltp+Wi7JNBr1Nnv8QXRq0+fV7u5Lxm1xr4NZAqIivFoth4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699348839; c=relaxed/simple; bh=UlEa+8atbaZx7ptpV7iIi5GnGDrkY7dvKXq9wMJHl8g=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=al5IU2QGY1J/UphV0TB7G9YMC1Hzewjo/gMNvoig0jcAF6Z39UaF33i5L1qcYyYSFuUdncOwtHSb/pNBkzBLuRLUjPaYxJfBX+sgPKtb0gRp66YzPpkR1PHH2HP88kyC3SGgCr+Q6JVzIfkcrwG5vNo+FdXTplu8y769VCnPJBk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-32f8441dfb5so3697731f8f.0 for ; Tue, 07 Nov 2023 01:20:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1699348836; x=1699953636; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=kCYtFJsA5qDP7HMuX5MBVzvesnmvPBMiCbzAyjfIEM0=; b=FqUXoYoe0BhJp5IUToni5gSqekZXHg4my01MMbKE8MzN8rCjir7Zd1Ty3IuUMulwW4 c1C4KJ8su9ot2KVuTnHa+RSgnkysyegkP5MfzS+nO5Ww8xFTyo8YlcJvaU7FyZdI5Uzj X3B7T/Zmwh5FfGN7BbXTBfVAaM2jeKitQKPxGAwm3A8gLstgWyGS+h5cogEsQgbQiHTf xq9ZeefbMmUpZnGspVSgkvvErzYOeUQYTWqyNuWKb6UHc7xoh0zwWUS8IZGxmOAhhgxj KIdX4Z/6HaqxT9DiTDDMSDt7MMuyapmKvEsFmUogH4ZB1IsCvFeI8rai/3COYiB2pDAW 6SIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699348836; x=1699953636; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=kCYtFJsA5qDP7HMuX5MBVzvesnmvPBMiCbzAyjfIEM0=; b=krSieImm+Du0f8t+gOhRlkxdh2G98X6RHEVklrx7qMntYVOv8nLIc++mop4kOlHlsD o8A4His4tTqPXDhewp+Kn5x3EGNNN42IE55x+W2evBdBJDzvJwuWnqMq8P0h94YeVwFQ UO7TppAjdHc8IK93VwZs2Ic4scn4q70IlhTK4it5gAE0sb/gHu7M05xS1HWNBHZ4UudM IGs6TrmCwBmpyGxrON5y1jhUuSqE0jcexm1yeyMchF1/ph8N7XCDdv7lD7BP3DR9tFd0 2pRRF/18gmmmqaHA/5EWKMvmg+YsdsheboXeALz+7H4ZYJaQ3JHPPM6AeuYfVCPnd1Ws tcHg== X-Gm-Message-State: AOJu0Ywqji9UgK315XzH8jMANm4EXFWKXR0zBUgVm9EGzS3K26A7W1L9 bglfKebYWr/v0mVp2NBLB0NC8wqbu+7NpBKPfIMCsQ== X-Google-Smtp-Source: AGHT+IGMzhZ/h0Bj1eYRMhGTCpwoZwjhChMQwW2DsTDVNkDETjEIeDMklKGNWWfbwMbFGk4EncG3Jw== X-Received: by 2002:a5d:5446:0:b0:31f:d52a:82b3 with SMTP id w6-20020a5d5446000000b0031fd52a82b3mr17679378wrv.46.1699348836425; Tue, 07 Nov 2023 01:20:36 -0800 (PST) Received: from localhost.localdomain ([2001:861:3382:1a90:dbc1:a1d1:2e58:4040]) by smtp.gmail.com with ESMTPSA id r1-20020a5d4941000000b0032da8fb0d05sm1783344wrs.110.2023.11.07.01.20.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 01:20:35 -0800 (PST) From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED] ada: Elide temporary for aliased array with unconstrained nominal subtype Date: Tue, 7 Nov 2023 10:20:33 +0100 Message-ID: <20231107092034.3906691-1-poulhies@adacore.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Eric Botcazou 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