[llvm-bugs] [Bug 48268] New: Unnecessary fld+fstp pair in double to float conversion

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Nov 23 08:37:43 PST 2020


https://bugs.llvm.org/show_bug.cgi?id=48268

            Bug ID: 48268
           Summary: Unnecessary fld+fstp pair in double to float
                    conversion
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C
          Assignee: unassignedclangbugs at nondot.org
          Reporter: i-bugs-llvm-org-uQW3n8LD3W at rf.risimo.net
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

See https://godbolt.org/z/d5ssq1

Compiling

-----------------
#include <stdio.h>

union floatu {
    float fv;
    unsigned char bytes[4];
};

void print_as_hex(double fv) {
    union floatu u;
    u.fv=fv;
    printf("got a float with       0x%02x 0x%02x 0x%02x 0x%02x\n",
u.bytes[3],u.bytes[2],u.bytes[1],u.bytes[0]);
}
-----------------

with "-mno-sse -m32 -O9"

results in

-----------------
print_as_hex:                           # @print_as_hex
        push    esi
        sub     esp, 8
        fld     qword ptr [esp + 16]    <=== (1) ok
        fstp    dword ptr [esp]         <=== (2) ok
        fld     dword ptr [esp]         <=== (3) Unnecessary
        fstp    dword ptr [esp + 4]     <=== (4) Unnecessary
        mov     eax, dword ptr [esp + 4]
        mov     ecx, eax
        movzx   edx, ah
        movzx   esi, al
        shr     eax, 24
        shr     ecx, 16
        movzx   ecx, cl
        sub     esp, 12
        push    esi
        push    edx
        push    ecx
        push    eax
        push    offset .L.str
        call    printf
        add     esp, 40
        pop     esi
        ret
-----------------

(1) and (2) are required to convert the double to float. But (3) and (4)
perform no conversion and just copy data around.

(2) should write to [esp + 4] and (3) and (4) can be removed.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201123/ae374a04/attachment.html>


More information about the llvm-bugs mailing list