From patchwork Fri Jul 2 22:46:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1500314 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: 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=Nta0drv7; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GGqwW2GWgz9sT6 for ; Sat, 3 Jul 2021 08:47:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E0B313857C78 for ; Fri, 2 Jul 2021 22:47:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E0B313857C78 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1625266054; bh=FnYAUf0OAlmKOgS5f3m47s4Q2S6lVVuwuaEN9J9G8CI=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Nta0drv7oR6iRUZfn4r1mr1rPC5OINdsnmt4tCH0l2P2frfAmxnXNhE8K5VA0jLpC Nk4rNJJBwSbdDr6YmpBub63VTu8IbJDfXIR4b3LJ/oWONMWk2tsBgcltUhBRN6FVPW fms1YgoWRSnzTjdDrq3+NnRMVUYMUX7ThmXKms34= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [IPv6:2001:67c:2050::465:102]) by sourceware.org (Postfix) with ESMTPS id 2086B3857C77 for ; Fri, 2 Jul 2021 22:46:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2086B3857C77 Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4GGqvY6CRXzQj5h; Sat, 3 Jul 2021 00:46:49 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030) with ESMTP id 4745pehZx_8r; Sat, 3 Jul 2021 00:46:43 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [committed] d: RHS value lost when a target_expr modifies LHS in a cond_expr (PR101282) Date: Sat, 3 Jul 2021 00:46:41 +0200 Message-Id: <20210702224641.3508084-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-MBO-SPAM-Probability: ** X-Rspamd-Score: 1.89 / 15.00 / 15.00 X-Rspamd-Queue-Id: B9EBE11FA X-Rspamd-UID: 706733 X-Spam-Status: No, score=-15.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP 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: Iain Buclaw via Gcc-patches From: Iain Buclaw Reply-To: Iain Buclaw Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi, This patch forces a target_expr on the RHS of an assignment when it is a non-POD type. To prevent the RHS of an assignment modifying the LHS before the assignment proper, a target_expr is forced so that function calls that return with slot optimization modify the temporary instead. This did not work for conditional expressions however, to give one example. So now the RHS is always forced to a temporary. Bootstrapped and regression tested on x86_64-linux-gnu/-m64/-m32/-mx32. Committed to mainline, and backported to the gcc-11 release branch. Regards, Iain --- gcc/d/ChangeLog: PR d/101282 * d-codegen.cc (build_assign): Force target_expr on RHS for non-POD assignment expressions. gcc/testsuite/ChangeLog: PR d/101282 * gdc.dg/torture/pr101282.d: New test. --- gcc/d/d-codegen.cc | 7 +++++++ gcc/testsuite/gdc.dg/torture/pr101282.d | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 gcc/testsuite/gdc.dg/torture/pr101282.d diff --git a/gcc/d/d-codegen.cc b/gcc/d/d-codegen.cc index 9a9447371aa..ce7c17baaaf 100644 --- a/gcc/d/d-codegen.cc +++ b/gcc/d/d-codegen.cc @@ -1344,6 +1344,13 @@ build_assign (tree_code code, tree lhs, tree rhs) d_mark_addressable (lhs); CALL_EXPR_RETURN_SLOT_OPT (rhs) = true; } + /* If modifying an LHS whose type is marked TREE_ADDRESSABLE. */ + else if (code == MODIFY_EXPR && TREE_ADDRESSABLE (TREE_TYPE (lhs)) + && TREE_SIDE_EFFECTS (rhs) && TREE_CODE (rhs) != TARGET_EXPR) + { + /* LHS may be referenced by the RHS expression, so force a temporary. */ + rhs = force_target_expr (rhs); + } /* The LHS assignment replaces the temporary in TARGET_EXPR_SLOT. */ if (TREE_CODE (rhs) == TARGET_EXPR) diff --git a/gcc/testsuite/gdc.dg/torture/pr101282.d b/gcc/testsuite/gdc.dg/torture/pr101282.d new file mode 100644 index 00000000000..b75d5fc678f --- /dev/null +++ b/gcc/testsuite/gdc.dg/torture/pr101282.d @@ -0,0 +1,23 @@ +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101282 +// { dg-do run } + +void main() +{ + struct S101282 + { + int impl; + S101282 opUnary(string op : "-")() + { + return S101282(-impl); + } + int opCmp(int i) + { + return (impl < i) ? -1 : (impl > i) ? 1 : 0; + } + } + auto a = S101282(120); + a = -a; + assert(a.impl == -120); + a = a >= 0 ? a : -a; + assert(a.impl == 120); +}