From patchwork Tue Nov 28 09:39:40 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: 170652 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp3792794vqx; Tue, 28 Nov 2023 01:40:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IG3bQ93a5tzi2SW1o92OLhmk9GsQizchXQTd7RINMonKpDvRQe+w9CCRTl0zs98cvp4uzJ3 X-Received: by 2002:a05:6214:10eb:b0:671:567d:b134 with SMTP id q11-20020a05621410eb00b00671567db134mr13295197qvt.56.1701164435018; Tue, 28 Nov 2023 01:40:35 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701164435; cv=pass; d=google.com; s=arc-20160816; b=PjPfmDEFd+cCMC8aF7/7/MP1UILr0vwUOrGRAsxO8RcIbeWQ9dSVCe+E1GanJyYN8B cm4NAUxwWHND/mQwKKi8bB+G1yBld476fkmtX3HV2m00SuV9NDHNFdMpagzhM5Sdjhk1 8w2ah5dLtaEkUQufhptiPU2BZ2QraCRpNeZfdzmHhnrXD145slPrgh2eJWS260760tH8 MHiMHM2lihb3TfeCA7Nrl5ax+4nRzWXNYxi6T/JDT1c9X8jhmdNtGwBiFge9263jIEpT kzk+3KZBw3GcWCiaXJaI6jKY+faSCPeJDhuSfrm3hOrh6fcfcaWduf+zahyLOl0cOgdr m/Rw== 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=XfMlljzc0puA4GXiKLfBSwjJbgo7ZtL6XBr6O4pbR5s=; fh=hOhsC1930eHatDPYd2EALQ9Ry/g++vvN4vxlQkRuX+0=; b=Yk6xk2EwpcBzkI+bJrBkcdWTqjoGqt/rUOD3+7hJTJcKbHb9vfgifXioNP5DvlztxD A3Y9c5ntEj79bQyjBJ+ai0NOc2iHg9OusDLC7jfwH5/ppGJpnYdn/VGPe8a/kuvPCrZ5 z8tieODCQ27tcQREyeunoNJ28SVLemS9tt3+2e4m/ilwmJxWA9HTeRLhRZvIkxKEwnWP vjzIC6cKyJsJ3ipxIGtOTAWKXTJt6KHko6dPsq8vFqE8yxR2l1G/xwADsY8oLOCgKDu3 2tquxx91pXsIvkrC3jkhC7TJdvxku+Ia7pw5hji21fT1gkIR0GDPcfNbh6Ozdx3w3ZVo Vg8g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@adacore.com header.s=google header.b=e70OrAxI; 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 m16-20020ad44d50000000b0067a24d07800si6862773qvm.93.2023.11.28.01.40.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 01:40:34 -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=e70OrAxI; 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 C42BC3948472 for ; Tue, 28 Nov 2023 09:40:19 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id 5AF5C394843A for ; Tue, 28 Nov 2023 09:39:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5AF5C394843A 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 5AF5C394843A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::436 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701164392; cv=none; b=dcnawLWRdWlmMEtGICAW9OTostAk/Y7pBGwu3oQPoZMFz38PTdoiJWRwRT6A4p1bGh8XK8NFhsnC3nH3yzk8aR7fJI9IcS7kBWwV7fm5j9+Ua/tCAfY8ALoPRZ6HtfmUFVAAKXcSikWssIKUY2UvQkXw7WzPeK25fKVHQBq8l6I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701164392; c=relaxed/simple; bh=06bA21JAi8FTvZLfR4ARkSAnm/A0w7nEt3uw7L0CtkA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=KNo30B0p4GH01Zdmua9Q13WTicOTPpxcAOin4eedtp7+7vemfbMD0xM0ZRlCJq87ypNQMbdHnzdVEvJ1V1a9vIa6UZ0ot3e1kFZTa21NpGPjjD9J9G9MtccUWJ2YWEbwZcYrZs2eRZg9CwCqJI+Lc1ZMtaW7HF9cTgiCZVSkWZo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-6b709048f32so4560691b3a.0 for ; Tue, 28 Nov 2023 01:39:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1701164389; x=1701769189; 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=XfMlljzc0puA4GXiKLfBSwjJbgo7ZtL6XBr6O4pbR5s=; b=e70OrAxI7V7mDyE5YnIlNtAcCLXylW6I4eNezrT19CrYL3gv5lJFZodJ86BwAqA4UG xRJAfTLwlok3uJxGtAdjSBay3gqOiminIbzy4in85nj7t9FvuHxqseOa1Ei1KNoht5k6 bDKHC4rcGCc4b07Ij74Vck9MaBc+IK019OHr9q0aYMrS9/QqVKT6MSpiOYspMg3kNHNU ApsXKT1QX3chmUY120B9ScDMh4xEN0HJuATiQnLFd9WP90OX5o69ME078DYHzmXuU+Nj wyFZIkOixtt8gUa2VMc7Hf4WvfCjZOsAySBMY3V1ekbZfFgTaxXzrHBqSvSZJEsAo7JR deEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701164389; x=1701769189; 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=XfMlljzc0puA4GXiKLfBSwjJbgo7ZtL6XBr6O4pbR5s=; b=vG3Uv+/kXXDNVjzJlwIHQyXJjl42Dbkxzjk3RK9wsVzMo1xM/Cw1eAEKMRK9swDbB9 huBPhC2UXmCKvOa8fkkoSIeLDPFF7unyh5L4J/9iuzP0jUC76hR5J3YgFL6DlWOa3QN2 aN1kEc22VtV+UDO2UbpLxooY4MFDDM/HvGz9ie33R7/n+Rkqf9eq2IHGg3IfjUUImnnX t3EdaOh3IiVNDr9at8eyp1phbnEqg5Qlj6sxaDUG3WHqxU6jih9okanEQ9f6L51Vn/m+ YICVsCEIbJXHwuv77tNDq2sDFjbyaRyCYIvyIs84rGYogKgNRS6i02glNQbJkfBtWLpz C+Pg== X-Gm-Message-State: AOJu0YyMcSYUa/dhA86g6CxwFqIEkHqzQfjTXRfFm04UPKS88PeOaV+E rhvuXsRHCxP2OmaobZN2YGupXRW836HMtp5cVJSNlg== X-Received: by 2002:a05:6a20:9145:b0:18b:e6c7:7c72 with SMTP id x5-20020a056a20914500b0018be6c77c72mr14214256pzc.57.1701164389151; Tue, 28 Nov 2023 01:39:49 -0800 (PST) Received: from localhost.localdomain ([2001:861:3382:1a90:a24b:a65f:9576:c701]) by smtp.gmail.com with ESMTPSA id jc2-20020a17090325c200b001b7cbc5871csm9788471plb.53.2023.11.28.01.39.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 01:39:48 -0800 (PST) From: =?utf-8?q?Marc_Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED] ada: Further cleanup in finalization machinery Date: Tue, 28 Nov 2023 10:39:40 +0100 Message-ID: <20231128093940.2971116-1-poulhies@adacore.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, 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: 1783800198983631773 X-GMAIL-MSGID: 1783800198983631773 From: Eric Botcazou When transient scopes are being materialized, they can give rise to a block created around the construct being wrapped or not, depending on the kind of construct. In both cases finalization actions for the transient objects of the scope are generated the same way, with normal finalization done manually immediately after the construct and exceptional finalization deferred to the enclosing scope by means of a hooking mechanism. Now when the block is generated, it becomes this enclosing scope, so the normal finalization that comes with it would also be done immediately after the construct, even without normal finalization generated manually. Therefore this change gets rid of the manual finalization as well as of the hooking in the cases where the block is generated, leading to a significant streamlining of the expanded code in these cases. This requires fixing a small inaccuracy of the Within_Case_Or_If_Expression predicate, which must only be concerned with the dependent expressions, since those are the only ones to be treated specially by the finalization machinery. It also contains a small cleanup for the description of the transient scope management present at the beginning of the exp_ch7.adb file. gcc/ada/ * exp_ch7.ads (Expand_Cleanup_Actions): Move declaration to the Finalization Management section. * exp_ch7.adb (Transient Scope Management): Move description down to after that of the general finalization and make a few changes. (Insert_Actions_In_Scope_Around): Call Process_Transients_In_Scope only if cleanups are being handled. (Process_Transients_In_Scope): Remove redundant test on Clean. * exp_util.ads (Within_Case_Or_If_Expression): Adjust description. * exp_util.adb (Within_Case_Or_If_Expression): Only return true if within the dependent expressions of the conditional expressions. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch7.adb | 149 +++++++++++++++++++++++++------------------ gcc/ada/exp_ch7.ads | 12 ++-- gcc/ada/exp_util.adb | 16 ++++- gcc/ada/exp_util.ads | 7 +- 4 files changed, 110 insertions(+), 74 deletions(-) diff --git a/gcc/ada/exp_ch7.adb b/gcc/ada/exp_ch7.adb index f8c12b73e9b..f5d9b0fe802 100644 --- a/gcc/ada/exp_ch7.adb +++ b/gcc/ada/exp_ch7.adb @@ -70,59 +70,6 @@ with Uintp; use Uintp; package body Exp_Ch7 is - -------------------------------- - -- Transient Scope Management -- - -------------------------------- - - -- A transient scope is needed when certain temporary objects are created - -- by the compiler. These temporary objects are allocated on the secondary - -- stack and/or need finalization, and the transient scope is responsible - -- for finalizing the objects and reclaiming the memory of the secondary - -- stack at the appropriate time. They are generally objects allocated to - -- store the result of a function returning an unconstrained or controlled - -- value. Expressions needing to be wrapped in a transient scope may appear - -- in three different contexts which lead to different kinds of transient - -- scope expansion: - - -- 1. In a simple statement (procedure call, assignment, ...). In this - -- case the instruction is wrapped into a transient block. See - -- Wrap_Transient_Statement for details. - - -- 2. In an expression of a control structure (test in a IF statement, - -- expression in a CASE statement, ...). See Wrap_Transient_Expression - -- for details. - - -- 3. In a expression of an object_declaration. No wrapping is possible - -- here, so the finalization actions, if any, are done right after the - -- declaration and the secondary stack deallocation is done in the - -- proper enclosing scope. See Wrap_Transient_Declaration for details. - - -------------------------------------------------- - -- Transient Blocks and Finalization Management -- - -------------------------------------------------- - - procedure Insert_Actions_In_Scope_Around - (N : Node_Id; - Clean : Boolean; - Manage_SS : Boolean); - -- Insert the before-actions kept in the scope stack before N, and the - -- after-actions after N, which must be a member of a list. If flag Clean - -- is set, insert any cleanup actions. If flag Manage_SS is set, insert - -- calls to mark and release the secondary stack. - - function Make_Transient_Block - (Loc : Source_Ptr; - Action : Node_Id; - Par : Node_Id) return Node_Id; - -- Action is a single statement or object declaration. Par is the proper - -- parent of the generated block. Create a transient block whose name is - -- the current scope and the only handled statement is Action. If Action - -- involves controlled objects or secondary stack usage, the corresponding - -- cleanup actions are performed at the end of the block. - - procedure Store_Actions_In_Scope (AK : Scope_Action_Kind; L : List_Id); - -- Shared processing for Store_xxx_Actions_In_Scope - ----------------------------- -- Finalization Management -- ----------------------------- @@ -292,6 +239,84 @@ package body Exp_Ch7 is -- Build the deep Initialize/Adjust/Finalize for a record Typ with -- Has_Component_Component set and store them using the TSS mechanism. + -------------------------------- + -- Transient Scope Management -- + -------------------------------- + + -- A transient scope is needed when certain temporary objects are created + -- by the compiler. These temporary objects are allocated on the secondary + -- stack and/or need finalization, and the transient scope is responsible + -- for finalizing the objects and reclaiming the memory of the secondary + -- stack at the appropriate time. They are generally objects allocated to + -- store the result of a function returning an unconstrained or controlled + -- value. Expressions needing to be wrapped in a transient scope may appear + -- in three different contexts, which lead to different kinds of transient + -- scope expansion: + + -- 1. In a simple statement (procedure call, assignment, ...). In this + -- case the statement is wrapped into a transient block, which takes + -- care of the finalization actions as well as the secondary stack + -- deallocation, See Wrap_Transient_Statement for details. + + -- 2. In an expression of a control structure (test in a If statement, + -- expression in a Case statement, ...). In this case the expression + -- is replaced by a temporary and the enclosing statement is wrapped + -- into a transient block, which takes care of the finalization actions + -- and the secondary stack deallocation. See Wrap_Transient_Expression + -- for details. + + -- 3. In an expression of an object declaration. No wrapping is possible + -- here, so the finalization actions performed on the normal path, if + -- any, are done right after the declaration, and those performed on + -- the exceptional path, as well as the secondary stack deallocation, + -- are deferred to the enclosing scope. See Wrap_Transient_Declaration + -- for details. + + -- A transient scope is created by calling Establish_Transient_Scope on the + -- node that needs to be serviced by it (the serviced node can subsequently + -- be retrieved by invoking Node_To_Be_Wrapped when the current scope is a + -- transient scope). Once this has been done, the normal processing of the + -- Insert_Actions procedures is blocked and the procedures are redirected + -- to the Store_xxx_Actions_In_Scope procedures and Store_Actions_In_Scope + -- is ultimately invoked to store the pending actions. + + -- A transient scope is finalized by calling one of the Wrap_Transient_xxx + -- procedures depending on the context as explained above. They ultimately + -- invoke Insert_Actions_In_Scope_Around as per the following picture: + + -- Wrap_Transient_Expression Wrap_Transient_Statement + -- | | + -- V V + -- Make_Transient_Block + -- | + -- Wrap_Transient_Declaration | + -- | | + -- V V + -- Insert_Actions_In_Scope_Around + + procedure Insert_Actions_In_Scope_Around + (N : Node_Id; + Clean : Boolean; + Manage_SS : Boolean); + -- Insert the before-actions kept in the scope stack before N, and the + -- after-actions after N, which must be a member of a list. If Clean is + -- true, insert any cleanup actions kept in the scope stack and generate + -- required finalization actions for the before-actions and after-actions. + -- If Manage_SS is true, insert calls to mark/release the secondary stack. + + function Make_Transient_Block + (Loc : Source_Ptr; + Action : Node_Id; + Par : Node_Id) return Node_Id; + -- Action is a single statement or object declaration. Par is the proper + -- parent of the generated block. Create a transient block whose name is + -- the current scope and the only handled statement is Action. If Action + -- involves controlled objects or secondary stack usage, the corresponding + -- cleanup actions are performed at the end of the block. + + procedure Store_Actions_In_Scope (AK : Scope_Action_Kind; L : List_Id); + -- Shared processing for Store_xxx_Actions_In_Scope + ------------------------------------------- -- Unnesting procedures for CCG and LLVM -- ------------------------------------------- @@ -5641,9 +5666,7 @@ package body Exp_Ch7 is Blk_Ins := Last_Object; end if; - if Clean then - Insert_List_After_And_Analyze (Blk_Ins, Act_Cleanup); - end if; + Insert_List_After_And_Analyze (Blk_Ins, Act_Cleanup); -- Examine all objects in the list First_Object .. Last_Object @@ -5824,13 +5847,15 @@ package body Exp_Ch7 is (Last_Obj, Build_SS_Release_Call (Loc, Mark_Id)); end if; - -- Check for transient objects associated with Target and generate the - -- appropriate finalization actions for them. + -- If we are handling cleanups, check for transient objects associated + -- with Target and generate the required finalization actions for them. - Process_Transients_In_Scope - (First_Object => First_Obj, - Last_Object => Last_Obj, - Related_Node => Target); + if Clean then + Process_Transients_In_Scope + (First_Object => First_Obj, + Last_Object => Last_Obj, + Related_Node => Target); + end if; -- Reset the action lists diff --git a/gcc/ada/exp_ch7.ads b/gcc/ada/exp_ch7.ads index a131e55f5c3..105aa7e3d2f 100644 --- a/gcc/ada/exp_ch7.ads +++ b/gcc/ada/exp_ch7.ads @@ -176,6 +176,12 @@ package Exp_Ch7 is -- triggered by an abort, E_Id denotes the defining identifier of a local -- exception occurrence, Raised_Id is the entity of a local boolean flag. + procedure Expand_Cleanup_Actions (N : Node_Id); + -- Expand the necessary stuff into a scope to enable finalization of local + -- objects and deallocation of transient data when exiting the scope. N is + -- one of N_Block_Statement, N_Subprogram_Body, N_Task_Body, N_Entry_Body, + -- or N_Extended_Return_Statement. + function Make_Adjust_Call (Obj_Ref : Node_Id; Typ : Entity_Id; @@ -275,12 +281,6 @@ package Exp_Ch7 is -- Transient Scope Management -- -------------------------------- - procedure Expand_Cleanup_Actions (N : Node_Id); - -- Expand the necessary stuff into a scope to enable finalization of local - -- objects and deallocation of transient data when exiting the scope. N is - -- one of N_Block_Statement, N_Subprogram_Body, N_Task_Body, N_Entry_Body, - -- or N_Extended_Return_Statement. - procedure Establish_Transient_Scope (N : Node_Id; Manage_Sec_Stack : Boolean); diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb index 3952a161bd7..3dc34fcb493 100644 --- a/gcc/ada/exp_util.adb +++ b/gcc/ada/exp_util.adb @@ -14401,6 +14401,7 @@ package body Exp_Util is ---------------------------------- function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is + Nod : Node_Id; Par : Node_Id; begin @@ -14408,9 +14409,17 @@ package body Exp_Util is -- can be expanded into Expression_With_Actions, hence the test of the -- original node. - Par := Parent (N); + Nod := N; + Par := Parent (Nod); + while Present (Par) loop - if Nkind (Original_Node (Par)) in N_Case_Expression | N_If_Expression + if Nkind (Original_Node (Par)) = N_Case_Expression + and then Nod /= Expression (Original_Node (Par)) + then + return True; + + elsif Nkind (Original_Node (Par)) = N_If_Expression + and then Nod /= First (Expressions (Original_Node (Par))) then return True; @@ -14430,7 +14439,8 @@ package body Exp_Util is return False; end if; - Par := Parent (Par); + Nod := Par; + Par := Parent (Nod); end loop; return False; diff --git a/gcc/ada/exp_util.ads b/gcc/ada/exp_util.ads index 932bf3fdcbc..eb737d67769 100644 --- a/gcc/ada/exp_util.ads +++ b/gcc/ada/exp_util.ads @@ -1255,9 +1255,10 @@ package Exp_Util is -- extension to verify legality rules on inherited conditions. function Within_Case_Or_If_Expression (N : Node_Id) return Boolean; - -- Determine whether arbitrary node N is immediately within a case or an if - -- expression. The criterion is whether temporaries created by the actions - -- attached to N need to outlive an enclosing case or if expression. + -- Determine whether arbitrary node N is immediately within a dependent + -- expression of a case or an if expression. The criterion is whether + -- temporaries created by the actions attached to N need to outlive an + -- enclosing case or if expression. private pragma Inline (Duplicate_Subexpr);