From patchwork Tue Aug 4 08:26:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1340729 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=8.43.85.97; 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=S7oMrH5f; dkim-atps=neutral Received: from sourceware.org (server2.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 (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BLSXF0FClz9sR4 for ; Tue, 4 Aug 2020 18:26:36 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E1B7F3842426; Tue, 4 Aug 2020 08:26:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1B7F3842426 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1596529590; bh=sYiPGKoXq6KpDQrYebxIupyH4p6BRmnuMoqN5nS6hvw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=S7oMrH5fFhUMsf3QNTFMyEFRC3y9ckm90inMS6gBlz7iD6GqMBJtvb5aSFVJVdk6J 0hoT4Nf1oKXnmiyilSDEf+Go2yRRBWHvJkzQrVn3JYSL4OXxJorv9E8Ht2axWawAdD Hz9FGydGCvsx8nnHdm9mfjtl1qEXJxh14DDoadFA= 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 [80.241.56.152]) by sourceware.org (Postfix) with ESMTPS id 129C13842419 for ; Tue, 4 Aug 2020 08:26:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 129C13842419 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-102.mailbox.org (Postfix) with ESMTPS id 4BLSX24xlMzKmWJ; Tue, 4 Aug 2020 10:26:26 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by gerste.heinlein-support.de (gerste.heinlein-support.de [91.198.250.173]) (amavisd-new, port 10030) with ESMTP id jLYXyiAgcqLU; Tue, 4 Aug 2020 10:26:22 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [committed] d: Fix PR96429: Pointer subtraction uses TRUNC_DIV_EXPR Date: Tue, 4 Aug 2020 10:26:21 +0200 Message-Id: <20200804082621.2240602-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-MBO-SPAM-Probability: 33 X-Rspamd-Score: 5.10 / 15.00 / 15.00 X-Rspamd-Queue-Id: 55E4E183A X-Rspamd-UID: 145221 X-Spam-Status: No, score=-16.1 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, URIBL_ABUSE_SURBL, URIBL_RED 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 detects the pattern for pointer substraction in the front-end AST and uses EXACT_DIV_EXPR rather than TRUNC_DIV_EXPR. Bootstrapped and regression tested on x86_64-linux-gnu with multilib configurations -m32/-mx32. Committed to mainline. Regards Iain --- gcc/d/ChangeLog: PR d/96429 * expr.cc (ExprVisitor::visit (BinExp*)): Use EXACT_DIV_EXPR for pointer diff expressions. gcc/testsuite/ChangeLog: PR d/96429 * gdc.dg/pr96429.d: New test. --- gcc/d/expr.cc | 12 ++++++++++++ gcc/testsuite/gdc.dg/pr96429.d | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 gcc/testsuite/gdc.dg/pr96429.d diff --git a/gcc/d/expr.cc b/gcc/d/expr.cc index 20ab49d7b8c..ac3d4aaa171 100644 --- a/gcc/d/expr.cc +++ b/gcc/d/expr.cc @@ -620,6 +620,18 @@ public: break; case TOKdiv: + /* Determine if the div expression is a lowered pointer diff operation. + The front-end rewrites `(p1 - p2)' into `(p1 - p2) / stride'. */ + if (MinExp *me = e->e1->isMinExp ()) + { + if (me->e1->type->ty == Tpointer && me->e2->type->ty == Tpointer + && e->e2->op == TOKint64) + { + code = EXACT_DIV_EXPR; + break; + } + } + code = e->e1->type->isintegral () ? TRUNC_DIV_EXPR : RDIV_EXPR; break; diff --git a/gcc/testsuite/gdc.dg/pr96429.d b/gcc/testsuite/gdc.dg/pr96429.d new file mode 100644 index 00000000000..af096e26b5a --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr96429.d @@ -0,0 +1,26 @@ +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96429 +// { dg-do compile } +// { dg-options "-fdump-tree-original" } +ptrdiff_t subbyte(byte* bp1, byte* bp2) +{ + // { dg-final { scan-tree-dump "bp1 - bp2;" "original" } } + return bp1 - bp2; +} + +ptrdiff_t subshort(short* sp1, short* sp2) +{ + // { dg-final { scan-tree-dump "\\\(sp1 - sp2\\\) /\\\[ex\\\] 2;" "original" } } + return sp1 - sp2; +} + +ptrdiff_t subint(int* ip1, int* ip2) +{ + // { dg-final { scan-tree-dump "\\\(ip1 - ip2\\\) /\\\[ex\\\] 4;" "original" } } + return ip1 - ip2; +} + +ptrdiff_t sublong(long* lp1, long* lp2) +{ + // { dg-final { scan-tree-dump "\\\(lp1 - lp2\\\) /\\\[ex\\\] 8;" "original" } } + return lp1 - lp2; +}