[LLVMbugs] [Bug 20935] New: Detecting byteswap sequence

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sat Sep 13 10:29:16 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=20935

            Bug ID: 20935
           Summary: Detecting byteswap sequence
           Product: clang
           Version: unspecified
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: bisqwit at iki.fi
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

This is just silly. Clang optimizes the first function into single opcode
(bswapl, but not the other. For GCC, it's the other way around.

unsigned byteswap_clang(unsigned result)
{
    result = ((result & 0xFF00FF00u) >> 8) | ((result & 0x00FF00FFu) << 8);
    result = ((result & 0xFFFF0000u) >>16) | ((result & 0x0000FFFFu) <<16);
    return result;
}
unsigned byteswap_gcc(unsigned result)
{
    result = ((result & 0xFFFF0000u) >>16) | ((result & 0x0000FFFFu) <<16);
    result = ((result & 0xFF00FF00u) >> 8) | ((result & 0x00FF00FFu) << 8);
    return result;
}

unsigned byteswap(unsigned v)
{
    #ifdef __clang__
     return byteswap_clang(v);
    #else
     return byteswap_gcc(v);
    #endif
}

Clang output:

   byteswap_clang:                         # @byteswap_clang
        bswapl  %edi
        movl    %edi, %eax
        retq

    byteswap_gcc:                           # @byteswap_gcc
        roll    $16, %edi
        movl    %edi, %eax
        shrl    $8, %eax
        andl    $16711935, %eax         # imm = 0xFF00FF
        shll    $8, %edi
        andl    $-16711936, %edi        # imm = 0xFFFFFFFFFF00FF00
        orl     %eax, %edi
        movl    %edi, %eax
        retq

    byteswap:                               # @byteswap
        bswapl  %edi
        movl    %edi, %eax
        retq


GCC output:

    byteswap_clang:
        movl    %edi, %eax
        andl    $-16711936, %eax
        shrl    $8, %eax
        movl    %eax, %edx
        movl    %edi, %eax
        andl    $16711935, %eax
        sall    $8, %eax
        orl     %edx, %eax
        roll    $16, %eax
        ret

    byteswap_gcc:
        movl    %edi, %eax
        bswap   %eax
        ret

    byteswap:
        movl    %edi, %eax
        bswap   %eax
        ret

Tested both -m32 and -m64, with options: -Ofast -S
Tested versions:
- Debian clang version 3.5.0-+rc1-2 (tags/RELEASE_35/rc1) (based on LLVM 3.5.0)
 Target: x86_64-pc-linux-gnu
- gcc (Debian 4.9.1-11) 4.9.1  Target: x86_64-linux-gnu

-- 
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/20140913/ec66ad49/attachment.html>


More information about the llvm-bugs mailing list