public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/108821] New: Extra volatile access with -O2 -ftree-loop-im since GCC-11
@ 2023-02-16 15:25 sirl at gcc dot gnu.org
  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
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: sirl at gcc dot gnu.org @ 2023-02-16 15:25 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-05-02 13:26 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-16 15:25 [Bug tree-optimization/108821] New: Extra volatile access with -O2 -ftree-loop-im since GCC-11 sirl at gcc dot gnu.org
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

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).