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

            Bug ID: 99258
           Summary: volatile struct access optimized away
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: borysp at invisiblethingslab dot com
  Target Milestone: ---

Access to a volatile struct is optimized away, if the size of the struct is
>16B (at least on x64 cpu with sse). Small repro:
[borys@mowmiwuju test]$ cat a.c
struct A {
    long a;
    long b;
};

struct B {
    long a;
    long b;
    long c;
};

void f(volatile struct A* x) {
    *x;
}

void g(volatile struct B* x) {
    *x;
}
[borys@mowmiwuju test]$ gcc --version 
gcc (GCC) 10.2.0
[borys@mowmiwuju test]$ gcc -Wall -Wextra -std=c11 -O2 -c a.c
[borys@mowmiwuju test]$ objdump -d -Mintel a.o

a.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <f>:
   0:   f3 0f 6f 07             movdqu xmm0,XMMWORD PTR [rdi]
   4:   c3                      ret    
   5:   66 66 2e 0f 1f 84 00    data16 cs nop WORD PTR [rax+rax*1+0x0]
   c:   00 00 00 00 

0000000000000010 <g>:
  10:   c3                      ret 


Testing on https://godbolt.org/ indicates this issue is present in all gcc
versions. clang emits correct code for this test case.

Reply via email to