From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by sourceware.org (Postfix) with ESMTPS id 518A33858C33 for ; Tue, 27 Jun 2023 12:08:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 518A33858C33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-3fa96fd79feso24803245e9.2 for ; Tue, 27 Jun 2023 05:08:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1687867691; x=1690459691; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=OJKWe05T4uXpf66/uMa5sFL+QDRU+pCDYn6wJbyK7HY=; b=XmBCLWuQIxAxqKzgfiCG5IVhO20iSJSKfWWs4rm2k8IW9t+KsVYjPWJyMtL0yiPQG/ t7CV8NWRwfu/45UVi+9GanyIh/C88zqcPdlh4ItbXaKw5GI78zIilMmjmFn208j4Ewnf s6c9scd3J4VmG/M4cEyYEuKOpq03JVeh1G/Im08af3iwj0WcPvhZyeuJ8Nzpf0zVgdPr 2BKMTqMvhZi8ccWmZ/5WcHWg8QBwpoVyFMPXOtVdNUgL3Zbxsty9Pk2CFBIoWBaWio/1 0QM81hF9b/exK3ZrlKG9rDb5yXJNsByAjC/+if586SkM8sNyuNEdaCp0gRy//RQ+zGl/ wK+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687867691; x=1690459691; 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=OJKWe05T4uXpf66/uMa5sFL+QDRU+pCDYn6wJbyK7HY=; b=SgJFnrnSeo9KYegSNt2nl4SS19FajqhvinPT5DoQRhQuTuA8s7UHXRB423IVeESRuP 0p3V1vO53Pzp4aHbzH4140phvDc+h2YRNm/tn5DmG5+dHZtmOSOmLKGLUoIrIjKY45n7 grOGrCNe0ZU84KO+zb5w/N4UqMPwQfhAB6J8BdOG4YUigd4/eLYZcGwS/7G7l5NVeiTH tOWnNLVMddYnY6ltrE0Q3vNxCTjp5GEx0FtDmyQmSbB1hmZaJu4aq5pJTSKPAsZ3htF4 5nM0GOTrUVV9qofjhNk81ap2aCv56uerE4s5XEa4Ars7bCpYuH8rxnxt3H+cx91PNR37 N5Kw== X-Gm-Message-State: AC+VfDxfILYgHGAXGfJqeqZtmnWknAV2ieoNJ7vgGuOJthFYcnOBrREd ODB5b0bJSqrbGb1+Vf4G7ti0YR2wd8ixsqVb3XL+SQ== X-Google-Smtp-Source: ACHHUZ4a3nRKfwvZsFTVgV+t2MQOGbsZ9x3d1z6ppCmLt8JDameK1ClhTzy21cvzsCExX1XfWCGclQ== X-Received: by 2002:a1c:e90d:0:b0:3f8:fc2a:c7eb with SMTP id q13-20020a1ce90d000000b003f8fc2ac7ebmr22771970wmc.5.1687867691052; Tue, 27 Jun 2023 05:08:11 -0700 (PDT) Received: from poulhies-Precision-5550.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id x9-20020a05600c21c900b003fb41491670sm3191421wmj.24.2023.06.27.05.08.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jun 2023 05:08:10 -0700 (PDT) From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED] ada: Fix double finalization of case expression in concatenation Date: Tue, 27 Jun 2023 14:08:09 +0200 Message-Id: <20230627120809.3420308-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPAM_BODY,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 This streamlines the expansion of case expressions by not wrapping them in an Expression_With_Actions node when the type is not by copy, which avoids the creation of a temporary and the associated finalization issues. That's the same strategy as the one used for the expansion of if expressions when the type is by reference, unless Back_End_Handles_Limited_Types is set to True. Given that it is never set to True, except by a debug switch, and has never been implemented, this parameter is removed in the process. gcc/ada/ * debug.adb (d.L): Remove documentation. * exp_ch4.adb (Expand_N_Case_Expression): In the not-by-copy case, do not wrap the case statement in an Expression_With_Actions node. (Expand_N_If_Expression): Do not test Back_End_Handles_Limited_Types * gnat1drv.adb (Adjust_Global_Switches): Do not set it. * opt.ads (Back_End_Handles_Limited_Types): Delete. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/debug.adb | 6 ----- gcc/ada/exp_ch4.adb | 58 ++++++++++++++++++-------------------------- gcc/ada/gnat1drv.adb | 21 ---------------- gcc/ada/opt.ads | 10 -------- 4 files changed, 23 insertions(+), 72 deletions(-) diff --git a/gcc/ada/debug.adb b/gcc/ada/debug.adb index fd94203faf8..9c2a2b0e8d0 100644 --- a/gcc/ada/debug.adb +++ b/gcc/ada/debug.adb @@ -123,7 +123,6 @@ package body Debug is -- d.I Do not ignore enum representation clauses in CodePeer mode -- d.J Relaxed rules for pragma No_Return -- d.K Do not reject components in extensions overlapping with parent - -- d.L Depend on back end for limited types in if and case expressions -- d.M Relaxed RM semantics -- d.N Use rounding when converting from floating point to fixed point -- d.O Dump internal SCO tables @@ -898,11 +897,6 @@ package body Debug is -- clause but they cannot be fully supported by the GCC type system. -- This switch nevertheless allows them for the sake of compatibility. - -- d.L Normally the front end generates special expansion for conditional - -- expressions of a limited type. This debug flag removes this special - -- case expansion, leaving it up to the back end to handle conditional - -- expressions correctly. - -- d.M Relaxed RM semantics. This flag sets Opt.Relaxed_RM_Semantics -- See Opt.Relaxed_RM_Semantics for more details. diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb index fdaeb50512f..7af6dc087a4 100644 --- a/gcc/ada/exp_ch4.adb +++ b/gcc/ada/exp_ch4.adb @@ -5306,7 +5306,6 @@ package body Exp_Ch4 is Alt : Node_Id; Case_Stmt : Node_Id; Decl : Node_Id; - Expr : Node_Id; Target : Entity_Id := Empty; Target_Typ : Entity_Id; @@ -5361,7 +5360,6 @@ package body Exp_Ch4 is -- In all other cases expand into - -- do -- type Ptr_Typ is access all Typ; -- Target : Ptr_Typ; -- case X is @@ -5371,7 +5369,8 @@ package body Exp_Ch4 is -- Target := BX'Unrestricted_Access; -- ... -- end case; - -- in Target.all end; + + -- and replace the case expression by a reference to Target.all. -- This approach avoids extra copies of potentially large objects. It -- also allows handling of values of limited or unconstrained types. @@ -5514,20 +5513,21 @@ package body Exp_Ch4 is Prepend_List (Actions (Alt), Stmts); end if; - -- Finalize any transient objects on exit from the alternative. - -- This is done only in the return optimization case because - -- otherwise the case expression is converted into an expression - -- with actions which already contains this form of processing. - - if Optimize_Return_Stmt then - Process_If_Case_Statements (N, Stmts); - end if; - Append_To (Alternatives (Case_Stmt), Make_Case_Statement_Alternative (Sloc (Alt), Discrete_Choices => Discrete_Choices (Alt), Statements => Stmts)); + + -- Finalize any transient objects on exit from the alternative. + -- This needs to be done only when the case expression is _not_ + -- later converted into an expression with actions, which already + -- contains this form of processing, and after Stmts is attached + -- to the Alternatives list above (for Safe_To_Capture_Value). + + if Optimize_Return_Stmt or else not Is_Copy_Type (Typ) then + Process_If_Case_Statements (N, Stmts); + end if; end; Next (Alt); @@ -5539,30 +5539,24 @@ package body Exp_Ch4 is Rewrite (Par, Case_Stmt); Analyze (Par); - -- Otherwise convert the case expression into an expression with actions + -- Otherwise rewrite the case expression itself else Append_To (Acts, Case_Stmt); if Is_Copy_Type (Typ) then - Expr := New_Occurrence_Of (Target, Loc); + Rewrite (N, + Make_Expression_With_Actions (Loc, + Expression => New_Occurrence_Of (Target, Loc), + Actions => Acts)); else - Expr := + Insert_Actions (N, Acts); + Rewrite (N, Make_Explicit_Dereference (Loc, - Prefix => New_Occurrence_Of (Target, Loc)); + Prefix => New_Occurrence_Of (Target, Loc))); end if; - -- Generate: - -- do - -- ... - -- in Target[.all] end; - - Rewrite (N, - Make_Expression_With_Actions (Loc, - Expression => Expr, - Actions => Acts)); - Analyze_And_Resolve (N, Typ); end if; end Expand_N_Case_Expression; @@ -5937,9 +5931,8 @@ package body Exp_Ch4 is Set_From_Conditional_Expression (New_If); - -- If the type is limited, and the back end does not handle limited - -- types, then we expand as follows to avoid the possibility of - -- improper copying. + -- If the type is by reference, then we expand as follows to avoid the + -- possibility of improper copying. -- type Ptr is access all Typ; -- Cnn : Ptr; @@ -5953,12 +5946,7 @@ package body Exp_Ch4 is -- and replace the if expression by a reference to Cnn.all. - -- This special case can be skipped if the back end handles limited - -- types properly and ensures that no incorrect copies are made. - - elsif Is_By_Reference_Type (Typ) - and then not Back_End_Handles_Limited_Types - then + elsif Is_By_Reference_Type (Typ) then -- When the "then" or "else" expressions involve controlled function -- calls, generated temporaries are chained on the corresponding list -- of actions. These temporaries need to be finalized after the if diff --git a/gcc/ada/gnat1drv.adb b/gcc/ada/gnat1drv.adb index e74036e506a..78c4968aba2 100644 --- a/gcc/ada/gnat1drv.adb +++ b/gcc/ada/gnat1drv.adb @@ -712,27 +712,6 @@ procedure Gnat1drv is Suppress_Options.Suppress (Alignment_Check) := True; end if; - -- Set switch indicating if back end can handle limited types, and - -- guarantee that no incorrect copies are made (e.g. in the context - -- of an if or case expression). - - -- Debug flag -gnatd.L decisively sets usage on - - if Debug_Flag_Dot_LL then - Back_End_Handles_Limited_Types := True; - - -- If no debug flag, usage off for SCIL cases - - elsif Generate_SCIL then - Back_End_Handles_Limited_Types := False; - - -- Otherwise normal gcc back end, for now still turn flag off by - -- default, since there are unresolved problems in the front end. - - else - Back_End_Handles_Limited_Types := False; - end if; - -- Return slot support is disabled if -gnatd_r is specified if Debug_Flag_Underscore_R then diff --git a/gcc/ada/opt.ads b/gcc/ada/opt.ads index 87399c8a9d3..59a24be83a4 100644 --- a/gcc/ada/opt.ads +++ b/gcc/ada/opt.ads @@ -209,16 +209,6 @@ package Opt is -- Unchecked_Conversion instantiations require checking based on annotated -- values. - Back_End_Handles_Limited_Types : Boolean; - -- This flag is set True if the back end can properly handle limited or - -- other by reference types, and avoid copies. If this flag is False, then - -- the front end does special expansion for if/case expressions to make - -- sure that no copy occurs. If the flag is True, then the expansion for - -- if and case expressions relies on the back end properly handling things. - -- Currently the default is False for all cases (set in gnat1drv). The - -- default can be modified using -gnatd.L (sets the flag True). This is - -- used to test the possibility of having the backend handle this. - Back_End_Inlining : Boolean := False; -- GNAT -- Set True to activate inlining by back-end expansion. This is the normal -- 2.40.0