public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "sirl at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/108821] New: Extra volatile access with -O2 -ftree-loop-im since GCC-11
Date: Thu, 16 Feb 2023 15:25:36 +0000	[thread overview]
Message-ID: <bug-108821-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 108821
           Summary: Extra volatile access with -O2 -ftree-loop-im since
                    GCC-11
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sirl at gcc dot gnu.org
  Target Milestone: ---

Hi,

this small example

extern volatile int *x;
static int gCrc;

static int crc16Add(int crc, int b) __attribute__((noinline));
static int crc16Add(int crc, int b)
{
  return crc + b;
}

void f(int data, int dataSz)
{
  int i;

  for(i=0;i<dataSz;i++)
  {
    gCrc = crc16Add(gCrc, data);
    *x = data;
  }
}

adds an extra volatile access after the loop (ARM assembler, but x64 shows the
same problem):

f(int, int):
        mov     r2, r1
        cmp     r2, #0
        ble     .L8
        push    {r3, r4, r5, lr}
        movw    r5, #:lower16:.LANCHOR0
        movt    r5, #:upper16:.LANCHOR0
        movw    r4, #:lower16:x
        movt    r4, #:upper16:x
        mov     r1, r0
        movs    r3, #0
        ldr     r0, [r5]
        ldr     r4, [r4]
.L5:
        adds    r3, r3, #1
        bl      crc16Add(int, int)
        cmp     r2, r3
        str     r1, [r4]  @ <-- the last store here
        bne     .L5
        str     r0, [r5]
        str     r1, [r4]  @ <-- is duplicated here
        pop     {r3, r4, r5, pc}
.L8:
        bx      lr


The tree dumps shows the extra access is added during the lim2 pass.
Compiling with -fno-tree-loop-im avoids the invalid extra access to volatile
memory.

I'm not enough of a language lawyer to be sure that the extra volatile access
is invalid in C/C++, but at least it's a bad optimization.

             reply	other threads:[~2023-02-16 15:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-16 15:25 sirl at gcc dot gnu.org [this message]
2023-02-16 17:20 ` [Bug tree-optimization/108821] [11/12/13 Regression] LIM reissuing a violatile store when it cannot/should not pinskia at gcc dot gnu.org
2023-02-16 17:27 ` sirl at gcc dot gnu.org
2023-02-17  8:00 ` rguenth at gcc dot gnu.org
2023-02-17 11:38 ` cvs-commit at gcc dot gnu.org
2023-02-17 11:39 ` [Bug tree-optimization/108821] [11/12 " rguenth at gcc dot gnu.org
2023-03-15  9:48 ` cvs-commit at gcc dot gnu.org
2023-05-02 13:24 ` [Bug tree-optimization/108821] [11 " cvs-commit at gcc dot gnu.org
2023-05-02 13:26 ` 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-108821-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).