From patchwork Tue Jan 3 09:35:45 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: 38332 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp4524252wrt; Tue, 3 Jan 2023 01:38:08 -0800 (PST) X-Google-Smtp-Source: AMrXdXvk/RaZmPs7bGag2JGTJ+OSDgkKNFuqm7etrgXveoxTOKkZsbWPhqwlFAeXYpzs8zNEKQPM X-Received: by 2002:a17:906:6dcb:b0:7c0:d60b:2887 with SMTP id j11-20020a1709066dcb00b007c0d60b2887mr37570523ejt.69.1672738687865; Tue, 03 Jan 2023 01:38:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672738687; cv=none; d=google.com; s=arc-20160816; b=PlhqLXsNci9qxY9dI2zQ1zLFjzY13lVxM0AdZqPYJjAFaeznjia6H4PtPmIYgtTpcP e0FFRmjauZKvHhp8ybqVCPmD6fpwAEuRh0tK26pxNcAu5r95eIsnpmWvfZvSg8MYGxye NXO7FO5l3wK49LKqU/LCm/o+Ysg+UgI6SO7DB45Iom4mrp0EdBYXvFbxixG5h5Dg2nNM Ln3VC2XcCUhaYGDrYyeS1nLAdnPLfTtuWMscKiGtA5GggR1fvlbPkg7gBx5I0jRKXOPA fMawl4KIUsgBjd5k/3RKmLOMiGr6GZn8uB6UkFli/mfYOVi3UcFKq3gWJFDgof4DtMUY y4mA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:delivered-to:dkim-signature:dkim-filter; bh=UpMZFEChYmiwQQkKkno2LKyuXJij5Z5iFftSxr4L2p4=; b=zLNf6trMUx7rtpU4txQ1VhIVs4UEuujbcnQzwdUqwoeyWXf0GbI9i7D+S02wQkzW7v 6MUitnU+c8Z5vbxu/EDtUUlLtR+y0oF81B7TXZ5h39oN27lsNMmnnLTZ0SaQbH9rjT/N e5OVk5vbdJKRd1OMYNr+cE/YAMLRcEBB/mOL7RKL/Osa/xIdFWpIyCyKLsrkOpfB+Sxf N7qIEskb64GZsQB5E0/hT/5KJ1WVlkGisFLYqy8x7HVMXtaANo2DR1OLoqlxlAjzsemo 7+vbtGKmYERXaOEKmyILVnKR4LEmSPVTmFwzaGSkjgqkP92ZdYuMHxc2FCpheTffH1Gc u/ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Py8r2V3I; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id xj10-20020a170906db0a00b007c199324a9fsi23757775ejb.461.2023.01.03.01.38.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Jan 2023 01:38:07 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Py8r2V3I; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5DA51382FAC6 for ; Tue, 3 Jan 2023 09:36:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5DA51382FAC6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1672738617; bh=UpMZFEChYmiwQQkKkno2LKyuXJij5Z5iFftSxr4L2p4=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Py8r2V3IRvEhyBNRPQo0KzHb6PlG8xoPuIMRBy5+5BybPuzSfrWvrWTDaEWOt4jJG O+QUE3dTr2my8bTg519ZRwos0K1FtY/QvIi9PPeZNbIBeXMG6vERjbkz7MuqL1Cv4a 5TAjG7gT8zRM8Tv1/nbgq3QwREACQXAMWGmlxuH8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id 603A13858C50 for ; Tue, 3 Jan 2023 09:35:48 +0000 (GMT) Received: by mail-wr1-x42c.google.com with SMTP id d9so267171wrp.10 for ; Tue, 03 Jan 2023 01:35:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=UpMZFEChYmiwQQkKkno2LKyuXJij5Z5iFftSxr4L2p4=; b=QG0FnhK6vHLcEbWFXCtXl+m3eq/uSNFuv2C4xU2sF0ektbcxvHJMaxoe4ITPa9K17N wLv9yFuxhgPM/noN0sA1LypC4fNGvx5BQe0DXyYr4Kcg8tDcWXMgnQ1eS8knr7kmSh2q pOyJi0FmJ7sYl0XP2ciRegfxU/UjmdmaA+gZIJFavxkCWhTyNI33WcAm7rfOtUnYjiTs Az7RrjkJDlDuAk8Z/4aK65jkO29j8bvF+V66cykQz8TdjAb5HvZJQCBDrt5R5npaJUBT 8/ZBhz7cQKz1uWv4QQBMAG8ysmVzOK3WggoWlHD15det+1i0HDe1OzclfyGYZV99+APO OR6g== X-Gm-Message-State: AFqh2kpPcAHXCw4NbWesqxloaCRwSdjzjw1JmfNC9OazehYKWeaxrnY1 BO8/wKUaplsLQY/DVvg19RXdtbmgk3mjnbm/k5s= X-Received: by 2002:a5d:4687:0:b0:289:552f:ed16 with SMTP id u7-20020a5d4687000000b00289552fed16mr12959943wrq.34.1672738547941; Tue, 03 Jan 2023 01:35:47 -0800 (PST) Received: from localhost.localdomain (static-176-191-105-132.ftth.abo.bbox.fr. [176.191.105.132]) by smtp.gmail.com with ESMTPSA id p3-20020adfcc83000000b0027a57c1a6fbsm25076253wrj.22.2023.01.03.01.35.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Jan 2023 01:35:47 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: =?utf-8?q?Marc_Poulhi=C3=A8s?= Subject: [COMMITTED] ada: Adapt frontend optimization for aggregate assignment Date: Tue, 3 Jan 2023 10:35:45 +0100 Message-Id: <20230103093545.76495-1-poulhies@adacore.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-13.1 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 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: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: =?utf-8?q?Marc_Poulhi=C3=A8s?= Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753993642002449766?= X-GMAIL-MSGID: =?utf-8?q?1753993642002449766?= The frontend currently relies on gigi to use efficient assignment in particular cases like: Some_Var.all := (others => (others => 0)); gigi would use memset to clear memory pointed to by Some_Var. In the case of an access with a Designated_Storage_Model aspect with a Copy_To procedure, memset can't be used directly. Instead of simply disabling this frontend/gigi optimization and having the frontend emit several assignments, a temporary is used (through the new Build_Assignment_With_Temporary): gigi can still memset it, and this temporary is then copied into the original target (and the regular storage model mechanism handles it). gcc/ada/ * exp_aggr.adb (Build_Assignment_With_Temporary): New. (Expand_Array_Aggregate): Tune backend optimization and insert a temporary in the case of an access with Designated_Storage_Model aspect. (Convert_Array_Aggr_In_Allocator): Likewise. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_aggr.adb | 106 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 92 insertions(+), 14 deletions(-) diff --git a/gcc/ada/exp_aggr.adb b/gcc/ada/exp_aggr.adb index 4d8bb817b80..30f32a78453 100644 --- a/gcc/ada/exp_aggr.adb +++ b/gcc/ada/exp_aggr.adb @@ -62,6 +62,7 @@ with Sem_Eval; use Sem_Eval; with Sem_Mech; use Sem_Mech; with Sem_Res; use Sem_Res; with Sem_Util; use Sem_Util; +use Sem_Util.Storage_Model_Support; with Sinfo; use Sinfo; with Sinfo.Nodes; use Sinfo.Nodes; with Sinfo.Utils; use Sinfo.Utils; @@ -75,6 +76,15 @@ with Warnsw; use Warnsw; package body Exp_Aggr is + function Build_Assignment_With_Temporary + (Target : Node_Id; + Typ : Node_Id; + Source : Node_Id) return List_Id; + -- Returns a list of actions to assign Source to Target of type Typ using + -- an extra temporary: + -- Tmp := Source; + -- Target := Tmp; + type Case_Bounds is record Choice_Lo : Node_Id; Choice_Hi : Node_Id; @@ -2508,6 +2518,42 @@ package body Exp_Aggr is return New_Code; end Build_Array_Aggr_Code; + ------------------------------------- + -- Build_Assignment_With_Temporary -- + ------------------------------------- + + function Build_Assignment_With_Temporary + (Target : Node_Id; + Typ : Node_Id; + Source : Node_Id) return List_Id + is + Loc : constant Source_Ptr := Sloc (Source); + + Aggr_Code : List_Id; + Tmp : Entity_Id; + Tmp_Decl : Node_Id; + + begin + Tmp := Make_Temporary (Loc, 'A', Source); + Tmp_Decl := + Make_Object_Declaration (Loc, + Defining_Identifier => Tmp, + Object_Definition => New_Occurrence_Of (Typ, Loc)); + Set_No_Initialization (Tmp_Decl, True); + + Aggr_Code := New_List (Tmp_Decl); + Append_To (Aggr_Code, + Make_OK_Assignment_Statement (Loc, + Name => New_Occurrence_Of (Tmp, Loc), + Expression => Source)); + + Append_To (Aggr_Code, + Make_OK_Assignment_Statement (Loc, + Name => Target, + Expression => New_Occurrence_Of (Tmp, Loc))); + return Aggr_Code; + end Build_Assignment_With_Temporary; + ---------------------------- -- Build_Record_Aggr_Code -- ---------------------------- @@ -4514,15 +4560,29 @@ package body Exp_Aggr is New_Aggr := New_Copy_Tree (Aggr); Set_Expansion_Delayed (New_Aggr, False); - Aggr_Code := - New_List ( - Make_OK_Assignment_Statement (Sloc (New_Aggr), - Name => Target, - Expression => New_Aggr)); + -- In the case of Target's type using the Designated_Storage_Model + -- aspect with a Copy_To procedure, insert a temporary and have the + -- back end handle the assignment to it. Copy the result to the + -- original target. + + if Has_Designated_Storage_Model_Aspect + (Etype (Prefix (Expression (Target)))) + and then Present (Storage_Model_Copy_To + (Storage_Model_Object + (Etype (Prefix (Expression (Target)))))) + then + Aggr_Code := Build_Assignment_With_Temporary (Target, + Typ, New_Aggr); + else + Aggr_Code := + New_List ( + Make_OK_Assignment_Statement (Sloc (New_Aggr), + Name => Target, + Expression => New_Aggr)); + end if; -- Or else, generate component assignments to it, as for an aggregate -- that appears on the right-hand side of an assignment statement. - else Aggr_Code := Build_Array_Aggr_Code (Aggr, @@ -7065,16 +7125,34 @@ package body Exp_Aggr is and then not Is_Possibly_Unaligned_Slice (Target) and then Aggr_Assignment_OK_For_Backend (N) then - if Maybe_In_Place_OK then - return; - end if; - Aggr_Code := - New_List ( - Make_Assignment_Statement (Loc, - Name => Target, - Expression => New_Copy_Tree (N))); + -- In the case of an assignment using an access with the + -- Designated_Storage_Model aspect with a Copy_To procedure, + -- insert a temporary and have the back end handle the assignment + -- to it. Copy the result to the original target. + + if Parent_Kind = N_Assignment_Statement + and then Nkind (Name (Parent_Node)) = N_Explicit_Dereference + and then Has_Designated_Storage_Model_Aspect + (Etype (Prefix (Name (Parent_Node)))) + and then Present (Storage_Model_Copy_To + (Storage_Model_Object + (Etype (Prefix (Name (Parent_Node)))))) + then + Aggr_Code := Build_Assignment_With_Temporary (Target, + Typ, New_Copy_Tree (N)); + else + if Maybe_In_Place_OK then + return; + end if; + Aggr_Code := + New_List ( + Make_Assignment_Statement (Loc, + Name => Target, + Expression => New_Copy_Tree (N))); + + end if; else Aggr_Code := Build_Array_Aggr_Code (N,