From patchwork Thu Jul 30 16:07:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1338970 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@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org 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=YyC44X/E; 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 4BHb04406Xz9sRN for ; Fri, 31 Jul 2020 02:07:16 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A9E15388CC18; Thu, 30 Jul 2020 16:07:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A9E15388CC18 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1596125233; bh=z8YFIvxXXrcBFGAHqwIuyDiNsd8YbEmksL8kS55vBYw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=YyC44X/E8pikewnj7voo9Pahw6Dhs396dMnH37yOgw+nPg1Unndo6bjEvkD/MR/iJ EZ3dcHQixVEYd41qV2GG42wkXOkNSV66QW9MlQdFtFWp2D2KS7s2N2EH15kNpSL8XP e1tzQHIxiKpNZrVGQGizi1hNZIrCqsQ9mSdP7k94= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) by sourceware.org (Postfix) with ESMTPS id 1F2BE388C036 for ; Thu, 30 Jul 2020 16:07:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1F2BE388C036 Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:105:465:1:1:0]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4BHZzx6nKPzKmV9; Thu, 30 Jul 2020 18:07:09 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter01.heinlein-hosting.de (spamfilter01.heinlein-hosting.de [80.241.56.115]) (amavisd-new, port 10030) with ESMTP id sASS_4atZLuE; Thu, 30 Jul 2020 18:07:06 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [committed] d: Inline bounds checking for simple array assignments. Date: Thu, 30 Jul 2020 18:07:02 +0200 Message-Id: <20200730160702.2526414-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-MBO-SPAM-Probability: 48 X-Rspamd-Score: 7.33 / 15.00 / 15.00 X-Rspamd-Queue-Id: B74FC1807 X-Rspamd-UID: c0cd08 X-Spam-Status: No, score=-16.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_ABUSE_SURBL autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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@gcc.gnu.org Sender: "Gcc-patches" Hi, This patch implements a small optimization to the code generation of simple array assignments, inlining the array bounds checking code so there is no reliance on the library routine _d_arraycopy(), which also deals with postblit and copy constructors for non-trivial arrays. Bootstrapped and regression tested on x86_64-linux-gnu, and committed to mainline. Regards Iain --- gcc/d/ChangeLog: * expr.cc (ExprVisitor::visit (AssignExp *)): Inline bounds checking for simple array assignments. gcc/testsuite/ChangeLog: * gdc.dg/array1.d: New test. --- gcc/d/expr.cc | 45 ++++++++++++++++++++++++++++++----- gcc/testsuite/gdc.dg/array1.d | 14 +++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/array1.d diff --git a/gcc/d/expr.cc b/gcc/d/expr.cc index 58d4943ef2b..355561a481e 100644 --- a/gcc/d/expr.cc +++ b/gcc/d/expr.cc @@ -962,14 +962,47 @@ public: /* Perform a memcpy operation. */ gcc_assert (e->e2->type->ty != Tpointer); - if (!postblit && !destructor && !array_bounds_check ()) + if (!postblit && !destructor) { tree t1 = d_save_expr (d_array_convert (e->e1)); - tree t2 = d_array_convert (e->e2); - tree size = size_mult_expr (d_array_length (t1), - size_int (etype->size ())); - tree result = build_memcpy_call (d_array_ptr (t1), - d_array_ptr (t2), size); + tree t2 = d_save_expr (d_array_convert (e->e2)); + + /* References to array data. */ + tree t1ptr = d_array_ptr (t1); + tree t1len = d_array_length (t1); + tree t2ptr = d_array_ptr (t2); + + /* Generate: memcpy(to, from, size) */ + tree size = size_mult_expr (t1len, size_int (etype->size ())); + tree result = build_memcpy_call (t1ptr, t2ptr, size); + + /* Insert check that array lengths match and do not overlap. */ + if (array_bounds_check ()) + { + /* tlencmp = (t1len == t2len) */ + tree t2len = d_array_length (t2); + tree tlencmp = build_boolop (EQ_EXPR, t1len, t2len); + + /* toverlap = (t1ptr + size <= t2ptr + || t2ptr + size <= t1ptr) */ + tree t1ptrcmp = build_boolop (LE_EXPR, + build_offset (t1ptr, size), + t2ptr); + tree t2ptrcmp = build_boolop (LE_EXPR, + build_offset (t2ptr, size), + t1ptr); + tree toverlap = build_boolop (TRUTH_ORIF_EXPR, t1ptrcmp, + t2ptrcmp); + + /* (tlencmp && toverlap) ? memcpy() : _d_arraybounds() */ + tree tassert = build_array_bounds_call (e->loc); + tree tboundscheck = build_boolop (TRUTH_ANDIF_EXPR, + tlencmp, toverlap); + + result = build_condition (void_type_node, tboundscheck, + result, tassert); + } + this->result_ = compound_expr (result, t1); } else if ((postblit || destructor) && e->op != TOKblit) diff --git a/gcc/testsuite/gdc.dg/array1.d b/gcc/testsuite/gdc.dg/array1.d new file mode 100644 index 00000000000..af81813e736 --- /dev/null +++ b/gcc/testsuite/gdc.dg/array1.d @@ -0,0 +1,14 @@ +// { dg-do compile } +// { dg-final { scan-assembler-not "_d_arraycopy" } } + +void test1() +{ + int[10] a1 = void; + int[10] a2 = void; + a1[] = a2[]; +} + +void test2(int[] a1, int[] a2) +{ + a1[] = a2[]; +}