From patchwork Thu May 19 14:16:06 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: 1633284 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=ciCeoeAo; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4L3sf11yZ5z9s0w for ; Fri, 20 May 2022 00:28:09 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 253953839C6C for ; Thu, 19 May 2022 14:28:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 253953839C6C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652970486; bh=NF+5aqa+8WAl33R2QmuQOdvJTkhq2l5Jlvh4/7lrttU=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=ciCeoeAoH0M67ZxmnsLUpA2+rP7rmZOIORP+jJhD+RFeBs9ZxweY78ixfmTbWq7kg obPvZnRC0Hc2eES9X9MDhJn01lrxC75yd6fnW6PbGJDixG/geryYQFjYuPE8/m0UHl CoUVHS+rE+Gygs0v0LgQIJ1UB/JzWuuAO4llTIuo= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by sourceware.org (Postfix) with ESMTPS id 54DDB383D838 for ; Thu, 19 May 2022 14:16:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 54DDB383D838 Received: by mail-wr1-x42b.google.com with SMTP id s28so7254947wrb.7 for ; Thu, 19 May 2022 07:16:09 -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=NF+5aqa+8WAl33R2QmuQOdvJTkhq2l5Jlvh4/7lrttU=; b=5ENj+1+7YH4aKrxBljQN4JMaRWNU28IT0cErpRb1z8h9YwEswnb6WnZipIWjACN1lt SAGTkxCWhJD9Uem835/n28iNOmshvG/Oi8isyoCzEFA6LxBiEW5NBWtVrGf12x5/zLFq etQ3+geKiysffgfBeQeIiuDx019Blp2gmqXlU3ts57iPwxGMdtnu8KPAcDXwadKume1B 5mkuszpsi6uDraRmxyjTLNIXmkYkWgepAMYBVcVIdIZ1fRtuQ6MMt1PiYz/IrZ6d4F7V 1UXeES/Mhs2JgUTv5FPz2RpZEK94+lqMrR3UwSUoLoEe7s7aZ7OztjfdvmZpJlCrRmuH Kqog== X-Gm-Message-State: AOAM531zu1vds/xEiXO0BE0cq5TmhqfDnjdorErvnNpb7wd3Fry0ThpC y/9NQoFGh5MGK7gjzBjwhDA9WwVpTqh3oQ== X-Google-Smtp-Source: ABdhPJyoOSDey2tKxBN+B87IrM568Vp+ucFCjuj1ql1A89xCZ8WR+LdiT65Fm8Lu4ZEIe5uvvq85ww== X-Received: by 2002:a05:6000:1445:b0:20c:731c:c1d4 with SMTP id v5-20020a056000144500b0020c731cc1d4mr4332514wrx.76.1652969768141; Thu, 19 May 2022 07:16:08 -0700 (PDT) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id m3-20020adfc583000000b0020c7ec0fdf4sm6041646wrg.117.2022.05.19.07.16.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 07:16:07 -0700 (PDT) Date: Thu, 19 May 2022 14:16:06 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Avoid copy operation for returns involving function calls Message-ID: <20220519141606.GA3723267@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.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.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: Eric Botcazou Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The underlying issue is that the front-end does not create transient scopes for return statements, so objects copied for these statements can never be finalized properly. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * exp_ch6.adb (Expand_Call_Helper): Adjust comment. (Expand_Simple_Function_Return): For the case of a type which needs finalization and is returned on the primary stack, do not create a copy if the expression originates from a function call. * exp_ch7.adb (Transient Scope Management): Adjust comment. * exp_util.ads (Is_Related_To_Func_Return): Add WARNING line. * fe.h (Is_Related_To_Func_Return): Declare. diff --git a/gcc/ada/exp_ch6.adb b/gcc/ada/exp_ch6.adb --- a/gcc/ada/exp_ch6.adb +++ b/gcc/ada/exp_ch6.adb @@ -4899,8 +4899,8 @@ package body Exp_Ch6 is -- the return type is limited, then the context is initialization and -- different processing applies. If the call is to a protected function, -- the expansion above will call Expand_Call recursively. Otherwise the - -- function call is transformed into a temporary which obtains the - -- result from the secondary stack. + -- function call is transformed into a reference to the result that has + -- been built either on the return or the secondary stack. if Needs_Finalization (Etype (Subp)) then if not Is_Build_In_Place_Function_Call (Call_Node) @@ -7369,7 +7369,7 @@ package body Exp_Ch6 is if Present (Utyp) and then Needs_Finalization (Utyp) - and then not (Nkind (Exp) = N_Function_Call + and then not (Exp_Is_Function_Call and then Needs_Finalization (Exp_Typ)) then declare diff --git a/gcc/ada/exp_ch7.adb b/gcc/ada/exp_ch7.adb --- a/gcc/ada/exp_ch7.adb +++ b/gcc/ada/exp_ch7.adb @@ -109,18 +109,13 @@ package body Exp_Ch7 is -- pass the address of a constrained object as the target object for the -- function result. - -- By allocating tagged results in the secondary stack a number of + -- By always allocating tagged results in the secondary stack, a couple of -- implementation difficulties are avoided: - -- - If it is a dispatching function call, the computation of the size of - -- the result is possible but complex from the outside. + -- - If this is a dispatching function call, the computation of the size + -- of the result is possible but complex from the outside. - -- - If the returned type is controlled, the assignment of the returned - -- value to the anonymous object involves an Adjust, and we have no - -- easy way to access the anonymous object created by the back end. - - -- - If the returned type is class-wide, this is an unconstrained type - -- anyway. + -- - If the result type is class-wide, it is unconstrained anyway. -- Furthermore, the small loss in efficiency which is the result of this -- decision is not such a big deal because functions returning tagged types diff --git a/gcc/ada/exp_util.ads b/gcc/ada/exp_util.ads --- a/gcc/ada/exp_util.ads +++ b/gcc/ada/exp_util.ads @@ -820,6 +820,8 @@ package Exp_Util is -- Determine whether object Id is related to an expanded return statement. -- The case concerned is "return Id.all;". + -- WARNING: There is a matching C declaration of this subprogram in fe.h + function Is_Renamed_Object (N : Node_Id) return Boolean; -- Returns True if the node N is a renamed object. An expression is -- considered to be a renamed object if either it is the Name of an object diff --git a/gcc/ada/fe.h b/gcc/ada/fe.h --- a/gcc/ada/fe.h +++ b/gcc/ada/fe.h @@ -183,9 +183,11 @@ extern Boolean Is_Init_Proc (Entity_Id); /* exp_util: */ #define Is_Fully_Repped_Tagged_Type exp_util__is_fully_repped_tagged_type +#define Is_Related_To_Func_Return exp_util__is_related_to_func_return #define Find_Interface_Tag exp_util__find_interface_tag -extern Boolean Is_Fully_Repped_Tagged_Type (Entity_Id); +extern Boolean Is_Fully_Repped_Tagged_Type (Entity_Id); +extern Boolean Is_Related_To_Func_Return (Entity_Id); extern Entity_Id Find_Interface_Tag (Entity_Id, Entity_Id); /* lib: */