public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "tkoenig at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/108227] New: Unnecessary division when looping over array with size of elements not a power of two
Date: Mon, 26 Dec 2022 09:20:16 +0000	[thread overview]
Message-ID: <bug-108227-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108227

            Bug ID: 108227
           Summary: Unnecessary division when looping over array with size
                    of elements not a power of two
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tkoenig at gcc dot gnu.org
  Target Milestone: ---

Consider

typedef struct coord {
  double x, y, z;
} coord;

void foo(coord *from, coord *to)
{
  unsigned long int n = to - from;
  for (unsigned long int i=0; i < n; i++)
    {
      from[i].x = from[i].x + 1.0;
    }
}

void bar (coord *from, coord *to)
{
  char *c_from = (char *) from, *c_to = (char *) to;
  coord *p = from;
  long int c_n = c_to - c_from;
  for (long int i=0; i < c_n; i+= sizeof(coord))
    {
      p->x = p->x + 1.0;
      p++;
    }
}

The code is functionally equivalent, but the assembly somewhat different:

foo has

foo:
.LFB0:
        .cfi_startproc
        movabsq $-6148914691236517205, %rax
        movq    %rsi, %rdx
        subq    %rdi, %rdx
        sarq    $3, %rdx
        imulq   %rax, %rdx
        cmpq    %rdi, %rsi
        je      .L1
        movsd   .LC0(%rip), %xmm1
        xorl    %eax, %eax
        .p2align 4,,10
        .p2align 3
.L3:
        movsd   (%rdi), %xmm0
        addq    $1, %rax
        addq    $24, %rdi
        addsd   %xmm1, %xmm0
        movsd   %xmm0, -24(%rdi)
        cmpq    %rdx, %rax
        jb      .L3
.L1:
        ret

so it first divides by 12 (efficiently) to determine n. There are 7
instructions in the loop itself.

bar has

bar:
.LFB1:
        .cfi_startproc
        subq    %rdi, %rsi
        testq   %rsi, %rsi
        jle     .L6
        movsd   .LC0(%rip), %xmm1
        xorl    %eax, %eax
        .p2align 4,,10
        .p2align 3
.L8:
        movsd   (%rdi,%rax), %xmm0
        addsd   %xmm1, %xmm0
        movsd   %xmm0, (%rdi,%rax)
        addq    $24, %rax
        cmpq    %rax, %rsi
        jg      .L8
.L6:
        ret

no need to divide, and one instruction less in the loop.

I would expect foo to match bar.

             reply	other threads:[~2022-12-26  9:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-26  9:20 tkoenig at gcc dot gnu.org [this message]
2022-12-26  9:21 ` [Bug tree-optimization/108227] " tkoenig at gcc dot gnu.org
2022-12-27  7:08 ` pinskia at gcc dot gnu.org
2023-01-09 13:58 ` rguenth at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-108227-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).