<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Detecting byteswap sequence"
   href="http://llvm.org/bugs/show_bug.cgi?id=20935">20935</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Detecting byteswap sequence
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>LLVM Codegen
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>bisqwit@iki.fi
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>