Bug 96429 - d: Pointer subtraction uses TRUNC_DIV_EXPR
Summary: d: Pointer subtraction uses TRUNC_DIV_EXPR
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: d (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: ---
Assignee: Iain Buclaw
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-08-03 11:13 UTC by Iain Buclaw
Modified: 2020-08-04 08:27 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Iain Buclaw 2020-08-03 11:13:59 UTC
Given the pointer operation:

    long delta = smaller & (read - first);

This gets lowered as (-fdump-tree-original)

    long delta = (long) smaller & (read - first) / 8;

Instead of...

    long delta = (long) smaller & (read - first) /[ex] 8;

Switching will likely give some marginal gains on performance.
Comment 1 Iain Buclaw 2020-08-03 12:21:12 UTC
--- 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;
Comment 2 GCC Commits 2020-08-04 08:26:12 UTC
The master branch has been updated by Iain Buclaw <ibuclaw@gcc.gnu.org>:

https://gcc.gnu.org/g:3a3fda119036f46bfa70e06e7c69e04e78040079

commit r11-2536-g3a3fda119036f46bfa70e06e7c69e04e78040079
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Mon Aug 3 22:35:38 2020 +0200

    d: Fix PR96429: Pointer subtraction uses TRUNC_DIV_EXPR
    
    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.
Comment 3 Iain Buclaw 2020-08-04 08:27:47 UTC
Fixed.