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

            Bug ID: 89872
           Summary: GCC does not generate read access to volatile compound
                    literal
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pascal_cuoq at hotmail dot com
  Target Milestone: ---

This report is similar to but different from my previous report
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82340

Consider the C code below. The report is about the compilation of the functions
g and hg. The other functions are included only for discussion of the expected
behavior.

void f(void) {
    volatile int y=1, z=2;
    y + z;
}

void g(void)
{
  (volatile int){1} + (volatile int){2};
}

void k(void)
{
  (volatile int){1};
}

void hf(void)
{
    for (int i = 0; i < 1000; i++) f();
}

void hg(void)
{
    for (int i = 0; i < 1000; i++) g();
}

void hk(void)
{
    for (int i = 0; i < 1000; i++) k();
}

When compiling with -O3, the versions trunk and 8.3 of GCC on Compiler Explorer
(https://gcc.godbolt.org/z/2Il4GG ) produce the following x86-64:

f:
        movl    $1, -8(%rsp)
        movl    $2, -4(%rsp)
        movl    -8(%rsp), %eax
        movl    -4(%rsp), %eax
        ret
g:
        ret
k:
        movl    $1, -4(%rsp)
        movl    -4(%rsp), %eax
        ret
hf:
        movl    $1000, %eax
.L6:
        movl    $1, -8(%rsp)
        movl    $2, -4(%rsp)
        movl    -8(%rsp), %edx
        movl    -4(%rsp), %edx
        subl    $1, %eax
        jne     .L6
        ret
hg:
        ret
hk:
        movl    $1000, %eax
.L10:
        movl    $1, -4(%rsp)
        movl    -4(%rsp), %edx
        subl    $1, %eax
        jne     .L10
        ret

The functions g and hg are compiled to “ret”. Because reading from a volatile
lvalue is an observable side-effect (C11 5.1.2.3:6
https://port70.net/~nsz/c/c11/n1570.html#5.1.2.3p6 ) I would have expected them
to be compiled more similarly to f and hf respectively.

Reply via email to