From patchwork Tue Nov 7 09:20:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 162345 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp113070vqo; Tue, 7 Nov 2023 01:25:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IH2Mq+8P4MljCzVSlbP4CW3a//fgr1ICXVkHG8iLYl6SlxdAr/vVqEq4x/+pyG0h0gdNFA/ X-Received: by 2002:a05:6214:1bcc:b0:65b:ed3:9a02 with SMTP id m12-20020a0562141bcc00b0065b0ed39a02mr41925462qvc.17.1699349107316; Tue, 07 Nov 2023 01:25:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1699349107; cv=pass; d=google.com; s=arc-20160816; b=0PIfgzeGn7WHmGlFN7dJen7A2rcgdVdVpZaKF3aBjwa2xtDXJNHJAUEM9FV5vlzeq9 EW0FP2ducyZbWW+vEAKt5kCbBvEznQFQBFaK5KJjw0KQwRcznCpwts6ByCGvFT+1gS59 BNHA/tbu2jhD9+pnZi9l+4oEUc74IvbLp6gMZl5RVGHdnKSMKheszfoOgy5wRJjsOfJQ JZYGFdVf9if1z7qllVSiU2Reu/EHCTzib52D2a0OhIc8XhaQ21rBQ64385rHqLLNwD4A 7xDya9AFUI+jOTS8NH1RPeWULPgg42/8o1nYi9sXSCu1nABHAg2F1lj86mM1mrnd0UbP ICgA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=kCYtFJsA5qDP7HMuX5MBVzvesnmvPBMiCbzAyjfIEM0=; fh=hOhsC1930eHatDPYd2EALQ9Ry/g++vvN4vxlQkRuX+0=; b=U1/bzzx1heglpRy3tuktP4pWSfEe8ISQdWDAC+TUEIBBGSv+VthM38XgQKDMsbt00b gtUjoDAKUHDfiyz46lIhuFTC5lbxnVnBXba1wvr7e+zv61Dgvw29yuGfMh5mu8PZ9VxC lPPNxU82+fqUHNNeZJWdGvXEAMRZZ2BbaT29G4WdNFhRr16G0uCL1nviLThXbtZsxHLu 0Io/q0TaM168PthYkqJvp93cWwkO08Rpl68+/5xJhOO7zSgZy7Hrhb7Jn5q+6tTUAKN4 ODfQbAVdMcPN53oyb4++p7xxN48jox4z/9bfTKd9I6VrGpftxG52ZbCoJHntu1lJaHqb 9frQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@adacore.com header.s=google header.b=FqUXoYoe; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=adacore.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id dl2-20020ad44e02000000b006709493cc6csi6578299qvb.273.2023.11.07.01.25.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 01:25:07 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@adacore.com header.s=google header.b=FqUXoYoe; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=adacore.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 135C03831E00 for ; Tue, 7 Nov 2023 09:24:21 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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-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_Poulhi=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 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781896689628569230 X-GMAIL-MSGID: 1781896689628569230 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