[llvm-bugs] [Bug 35792] New: Missed optimization: Failure to emit BLSR for 32 bit inputs (or less)

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Jan 2 00:44:46 PST 2018


https://bugs.llvm.org/show_bug.cgi?id=35792

            Bug ID: 35792
           Summary: Missed optimization: Failure to emit BLSR for 32 bit
                    inputs (or less)
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: mpopov at fastmail.fm
                CC: llvm-bugs at lists.llvm.org

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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180102/dc91e494/attachment.html>


More information about the llvm-bugs mailing list