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

            Bug ID: 127851
           Summary: combine can reorder two volatile reads by folding them
                    into one insn
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: vineetg at gcc dot gnu.org
          Reporter: vineetg at gcc dot gnu.org
  Target Milestone: ---

Consider the following test:

```
extern volatile int x, y;

int
f (void)
{
  int a = x;     /* sequence point after this full expression */
  int b = y;
  return a < b;
}
```

x must be read before y: the individual assignment constitute the sequence
points.

However on x86 the generated code is

-O2     
f:                               
    movl  y(%rip), %eax   # y 1st
    cmpl  %eax, x(%rip)   # x 2nd
    setl  %al                    
    movzbl %al, %eax             
    ret                          

FWIW at -O2, -ffuse-ops-with-volatile-access is enabled, but as documented it
can't change the semantics which above clearly does.

```
Allow limited optimization of operations with volatile memory access
when doing so does not change the semantics outlined in ....
```

This can cause subtle reordering accesses in Linux kernel where multiple
READ_ONCE could be reordered due to this bug.

Not seen on aarch64 because the it doesnt have `(compare (mem/v x) (mem/v y))'
form which x86 supports.

Reply via email to