<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 - Arithmetic shift optimization for signed numbers"
   href="https://bugs.llvm.org/show_bug.cgi?id=36760">36760</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Arithmetic shift optimization for signed numbers
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>6.0
          </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>enhancement
          </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>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This was reported a while ago, but seems clang/llvm still does not optimize
this case well. (For the examples below -O2 option is used)

1. Consider the following example:

long long func(long long a)
{
    if (a < 0)
        return 0;
    return (a >> 63 ) ^ a;
}

Basically, since a is already checked to be non-negative, (a >> 63) must be 0.
Therefore, the shift operation should be optimized out.

clang/llvm 6.0 generates the following:
        movq    %rdi, %rcx
        sarq    $63, %rcx
        xorq    %rdi, %rcx
        xorl    %eax, %eax
        testq   %rdi, %rdi
        cmovnsq %rcx, %rax
        retq

whereas gcc can optimize it well:
        testq   %rdi, %rdi
        movl    $0, %eax
        cmovns  %rdi, %rax
        ret

2. Another example is when we reverse the condition:

long long func(long long a)
{
    if (a >= 0)
        return 0;
    return (a >> 63 ) ^ a;
}

In this case, again, the shift is unnecessary since (a >> 63) are all 1's.
Therefore, return statement is basically ~a.

clang/llvm:
        movq    %rdi, %rcx
        sarq    $63, %rcx
        xorq    %rdi, %rcx
        xorl    %eax, %eax
        testq   %rdi, %rdi
        cmovsq  %rcx, %rax
        retq

gcc:
        movq    %rdi, %rax
        testq   %rdi, %rdi
        movl    $0, %edx
        notq    %rax
        cmovns  %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>