<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 - Missed optimization: Failure to emit BLSR for 32 bit inputs (or less)"
   href="https://bugs.llvm.org/show_bug.cgi?id=35792">35792</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missed optimization: Failure to emit BLSR for 32 bit inputs (or less)
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </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>mpopov@fastmail.fm
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>While optimizing some bit-manipulation code in Julia, which uses LLVM as
backend, I came across the following curiosity: Code to reset the lowest set
bit, i.e.,

  x & (x - 1)

is compiled to a single instruction from the Bit Manipulation Instruction Set
(if it is available and enabled):

  blsr rax, rdi
  ret

However, this optimization only works for 64 bit types. For 32 bits or less, a
sequence of "ADD" and "AND" is emitted, e.g.,

  lea eax, [rdi + 268435455]
  and eax, edi
  ret

Forcing the argument to be 64 bits does not always help, because the problem
also occurs when LLVM detects that the argument has 32 or less significant
bits, e.g. if the high bits are masked off, like this:

  x &= 0xFFFF
  return x & (x - 1)

Thanks to godbolt.org I was able to verify that this behavior is present in
Clang 5.0.0 and Clang trunk, so I'm guessing that it is not a Julia codegen
problem. BTW, recent versions of GCC always emit "BLSR", regardless of argument
width.

Unfortunately, I don't know enough about the compilation pipeline of Julia
and/or LLVM to narrow down the problem much further.</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>