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

            Bug ID: 125411
           Summary: Atomic stores defeat alias analysis
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Joost.VandeVondele at mat dot ethz.ch
  Target Milestone: ---

https://godbolt.org/z/T7qoKf7va

In the following example:

int example1(int *val, uint16_t* restrict other) {
    int k = *val;
    __atomic_store_n(other, 2, __ATOMIC_RELAXED);
    k += *val;
    return k;
}

Although `other` and `val` can't alias, the atomic store to `other` prevents
the redundant load from being optimized out. GCC trunk:

example1:
        ldr     w2, [x0]
        mov     w3, 2
        strh    w3, [x1]
        ldr     w0, [x0]
        add     w0, w2, w0
        ret

clang does perform the optimization:

example1:
        ldr     w8, [x0]
        lsl     w0, w8, #1
        mov     w8, #2
        strh    w8, [x1]
        ret

This doesn't depend on the target afaict

Reply via email to