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

            Bug ID: 91091
           Summary: [missed optimization] Missing aliasing optimization
           Product: gcc
           Version: 5.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: aleksey.covacevice at gmail dot com
  Target Milestone: ---

Consider the following:

    struct s { int x; };
    struct t { int x; };

    void swap(struct s* p, struct t* q) {
        p->x = q->x;
        q->x = p->x;
    }


Aliasing rules forbid `p` and `q` to point to the same object; yet, GCC 5.4 and
most subsequent versions produce (-O3):

    swap(s*, t*):
            mov     eax, DWORD PTR [rsi]
            mov     DWORD PTR [rdi], eax
            mov     DWORD PTR [rsi], eax // Possible alias between p and q
            ret

whereas GCC versions 4.5.3 to 5.3 and versions 8.1 to 8.2 correctly produce:

    swap(s*, t*):
            mov     eax, DWORD PTR [rsi]
            mov     DWORD PTR [rdi], eax
            ret

All versions produce the correct code if __restrict__ is used on any pointer.

This behavior can be verified on Godbolt: https://godbolt.org/z/WYMoFI

Reply via email to