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

            Bug ID: 101758
           Summary: Inconsistent optimizations with UBSan
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: sanitizer
          Assignee: unassigned at gcc dot gnu.org
          Reporter: blubban at gmail dot com
                CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
                    jakub at gcc dot gnu.org, kcc at gcc dot gnu.org, marxin at 
gcc dot gnu.org
  Target Milestone: ---

Input:

gcc -fsanitize=undefined -O2 -fno-strict-aliasing -Wall

float b(unsigned a) { return *(float*)&a; }
float c(unsigned a) { return *(float*)&a; }
float d(unsigned a) { return *(float*)&a; }


Output:

b:
        mov     DWORD PTR [rsp-4], edi
        movss   xmm0, DWORD PTR [rsp-4]
        ret
c:
        movd    xmm0, edi
        ret
d:
        movd    xmm0, edi
        ret

or, on ARM

b:
        sub     sp, sp, #8
        str     r0, [sp, #4]
        vldr.32 s0, [sp, #4]
        add     sp, sp, #8
        bx      lr
c:
        sub     sp, sp, #8
        vmov    s0, r0
        add     sp, sp, #8
        bx      lr
d:
        sub     sp, sp, #8
        vmov    s0, r0
        add     sp, sp, #8
        bx      lr


Expected output:

Same assembly for all three. And maybe don't set up an unused stack frame on
ARM (it's correctly optimized out without UBSan).


Compiler Explorer: https://godbolt.org/z/Tfs5qazqM


(Found it while trying to determine if that function is a strict aliasing
violation (it is), and whether it can be fixed with memcpy or a union (both
work).)

Reply via email to