[llvm-bugs] [Bug 50197] New: 128 bit arithmetic --- inefficient logic test

via llvm-bugs llvm-bugs at lists.llvm.org
Sun May 2 04:40:25 PDT 2021


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

            Bug ID: 50197
           Summary: 128 bit arithmetic --- inefficient logic test
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: zero at smallinteger.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org

Created attachment 24821
  --> https://bugs.llvm.org/attachment.cgi?id=24821&action=edit
Sample code

See attached code, compiled with -O2.  The condition inside the while () test
is unnecessarily evaluated with a 128 bit shift instruction,

square:                                 # @square
        mov     rax, rdi
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        add     rax, 1
        adc     rsi, 0
        mov     rcx, rsi
        shld    rcx, rax, 4
        mov     rdx, rsi
        shr     rdx, 60
        or      rdx, rcx
        jne     .LBB0_1
        ret

even though a 64 bit shift instruction suffices.  However, changing || to | in
the logic condition yields the more efficient code below.

square:                                 # @square
        mov     rax, rdi
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        add     rax, 1
        adc     rsi, 0
        mov     rcx, rax
        shr     rcx, 60
        or      rcx, rsi
        jne     .LBB0_1
        ret

Found with clang-10 on Ubuntu 20.04 LTS, verified for clang 10, 11, and trunk
using godbolt.  Note that gcc -O2 handles both of these cases emitting the more
efficient code.

-- 
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/20210502/1c5a2e69/attachment.html>


More information about the llvm-bugs mailing list