From patchwork Wed May 11 08:54:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 53787 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D6D87395A023 for ; Wed, 11 May 2022 09:06:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D6D87395A023 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652260017; bh=ZSZbfdJAevdJSgQ0Lx9jkyaEnoUctLHC6t622EB2bkI=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=r+2CNyNS1fQ3n7mcmyTLZlRb1kzAfDCaYd/Tu82KLBgommdqlMgSY8EOKU3jlOU0A RUsa2N/TIkOwZ6QwMDGnW+1jRGh0FSuMwD7NOQZbOLLUUjGtWTAaafGJGjmSEz+qQg 05jyXJfgoTYEFVVsMrbVnJlEG9SsGNwDnuBBqfTg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id 722393835810 for ; Wed, 11 May 2022 08:54:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 722393835810 Received: by mail-wm1-x32e.google.com with SMTP id l38-20020a05600c1d2600b00395b809dfbaso532990wms.2 for ; Wed, 11 May 2022 01:54:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=ZSZbfdJAevdJSgQ0Lx9jkyaEnoUctLHC6t622EB2bkI=; b=FuK4qkPNNRzWSsRFUrWc11/08cqGOgMDblng0JBVpEfvevv8WukErMOUS0sgUFwegL NSDzlhjWRWZ36mV6Hu2fLlLWMTCeRkIOFhZHxk/5ay7IBRBuTDm9uegnK8rRpv3iyVRS CbS+9kPAhYXcsgYztdAs9cgOFLn3cFhzPS1KBSrVTS67wy4ML3muMc2e91u2FJkt5XAW 4pTmxO5MZneWHoH/ELQjJjme2C/fGecbsY10Y+IKfCSLhHNXb6zHkc7BaZOpw5DZUNRd iCkUYJK9UKgp2S2BvaVqkj5pTaEiw0XduMx2zRVeJNffUUCY0nORp81nvvr4oVSK3CrV 4W2Q== X-Gm-Message-State: AOAM533hohQ5QrVEg98mnjxduoBJ6JiUt1yUfQDsCH1ERmEENKrEjI9U rUcxKiv0Mr48sNRfCL/y2zyv69AJZ6U1YA== X-Google-Smtp-Source: ABdhPJyKN4KFr0ydycwDxu1djhUdVe6WuKUxEkCP6kk1YsqCrjV2RwXnwTjHzMnFJ444syhv6GN8HQ== X-Received: by 2002:a05:600c:19d1:b0:394:7661:6de9 with SMTP id u17-20020a05600c19d100b0039476616de9mr3731326wmq.76.1652259273214; Wed, 11 May 2022 01:54:33 -0700 (PDT) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id c13-20020adfed8d000000b0020c5253d8d7sm1141545wro.35.2022.05.11.01.54.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 01:54:32 -0700 (PDT) Date: Wed, 11 May 2022 08:54:32 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Excess finalization on assignment with target name symbol Message-ID: <20220511085432.GA2166541@adacore.com> MIME-Version: 1.0 Content-Disposition: inline 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, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Pierre-Marie de Rodat via Gcc-patches From: Pierre-Marie de Rodat Reply-To: Pierre-Marie de Rodat Cc: Gary Dismukes Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" In cases where the Ada 2022 feature of target name symbols (@) is used and the evaluation of the name is side-effect free, the compiler creates a temporary object to hold the value of the target object for use as the value of "@" symbols in the right-hand side expression. In the case where the target's type is controlled, or has controlled subcomponents, this can result in extra finalization calls (for the temporary object). The correction is to bypass the use of a temp and fall back on the more general approach of creating and calling a procedure with an in-out parameter for performing the assignment. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * exp_ch5.adb (Expand_Assign_With_Target_Names): Bypass the temp object approach in the case where the type of the LHS is controlled or has controlled subcomponents (Needs_Finalization is True), and use the procedure approach instead. diff --git a/gcc/ada/exp_ch5.adb b/gcc/ada/exp_ch5.adb --- a/gcc/ada/exp_ch5.adb +++ b/gcc/ada/exp_ch5.adb @@ -2246,9 +2246,15 @@ package body Exp_Ch5 is Expression => New_RHS)); -- The left-hand side is not a direct name, but is side-effect free. - -- Capture its value in a temporary to avoid multiple evaluations. - - elsif Side_Effect_Free (LHS) then + -- Capture its value in a temporary to avoid generating a procedure. + -- We don't do this optimization if the target object's type may need + -- finalization actions, because we don't want extra finalizations to + -- be done for the temp object, and instead we use the more general + -- procedure-based approach below. + + elsif Side_Effect_Free (LHS) + and then not Needs_Finalization (Etype (LHS)) + then Ent := Make_Temporary (Loc, 'T'); Replace_Target_Name (New_RHS);