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

            Bug ID: 121803
           Summary: gcc missed optimze manual byte swap to bswap
                    instrution
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rockeet at gmail dot com
  Target Milestone: ---

gcc missed optimization:
```
long long get_big_endian(const unsigned char* p) {
    long long r = 0;
    for (int i = 0; i < sizeof(long long); i++)
        r |= (long long)(p[i]) << ((sizeof(long long)-1-i)*8);
    return r;
}
```
clang(-O2) optimized this code to bswap:
```
get_big_endian(unsigned char const*):
        mov     rax, qword ptr [rdi]
        bswap   rax
        ret
```
but gcc(-O2) does not:
```
"get_big_endian(unsigned char const*)":
        mov     ecx, 56
        xor     edx, edx
.L2:
        movzx   eax, BYTE PTR [rdi]
        add     rdi, 1
        sal     rax, cl
        sub     ecx, 8
        or      rdx, rax
        cmp     ecx, -8
        jne     .L2
        mov     rax, rdx
        ret
```

see: https://godbolt.org/z/f9bcTGeah

Reply via email to