<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 --- - Inefficient XOR calculation"
   href="http://llvm.org/bugs/show_bug.cgi?id=16671">16671</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Inefficient XOR calculation
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.2
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>Backend: X86
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>nruslan_devel@yahoo.com
          </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>The backend seems to be not smart enough to generate good code for the example
below. The line '(a >> 63) ^ a' will do NOT operation if the operand is
negative. Also suppose it already preceeds by a line which makes sure that the
operand is non-negative, i.e. '(a >> 63) ^ a' should be optimized to just 'a'.

long long func(long long a)
{
    if (a < 0) /* Condition, presumably somewhere before the code below */
        return 0;
    return (a >> 63 ) ^ a;
}

Clang produces the following code:

    testq   %rdi, %rdi
    js  .LBB0_1
    movq    %rdi, %rax
    sarq    $63, %rax
    xorq    %rdi, %rax
    ret
.LBB0_1:
    xorl    %eax, %eax
    ret

GCC produces optimized code:

    xorl    %eax, %eax
    testq   %rdi, %rdi
    cmovns  %rdi, %rax
    ret

Similar inefficiency if we reverse condition, i.e. now it should simply do NOT
operation:

long long func(long long a)
{
    if (a >= 0) /* Condition, presumably somewhere before the code below */
        return 0;
    return (a >> 63 ) ^ a;
}

Clang produces:

    testq   %rdi, %rdi
    js  .LBB0_2
    xorl    %eax, %eax
    ret
.LBB0_2:
    movq    %rdi, %rax
    sarq    $63, %rax
    xorq    %rdi, %rax
    ret

GCC produces:

    movq    %rdi, %rdx
    xorl    %eax, %eax
    testq   %rdi, %rdi
    notq    %rdx
    cmovs   %rdx, %rax
    ret</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>