<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - clang does not properly optimize xor expressions"
   href="https://bugs.llvm.org/show_bug.cgi?id=36622">36622</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang does not properly optimize xor expressions
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>5.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>FreeBSD
          </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>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>avg@FreeBSD.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>It appears that in some code sequences Clang fails to recognize that
x xor C xor C == x
where C is a compile-time constant.

For instance:
-----------------------------------------
uint32_t
combine(uint16_t x, uint16_t y)
{
        uint32_t r = 0x5a5a7777;

        r ^= (r ^ x) & 0xffff;
        r ^= ((r >> 16) ^ y) << 16;

        return (r);
}
-----------------------------------------

In this code the initial value of r is not important and the code should be
equivalent to:
-----------------------------------------
uint32_t
combine(uint16_t x, uint16_t y)
{
        return (x | ((uint32_t)y << 16));
}
-----------------------------------------


But even with -O3 Clang produces the following assembler code:
-----------------------------------------
        orl     $1515847680, %edi       # imm = 0x5A5A0000
        xorl    $23130, %esi            # imm = 0x5A5A
        shll    $16, %esi
        xorl    %edi, %esi
        movl    %esi, %eax
-----------------------------------------

It's easy to see that 0x5A5A will always cancel out through double application
via xor, but Clang does not recognize that.</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>