[llvm-bugs] [Bug 48292] New: [aarch64] Inefficient code in __builtin_mul_overflow with unsigned < 32 bits

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Nov 24 21:30:55 PST 2020


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

            Bug ID: 48292
           Summary: [aarch64] Inefficient code in __builtin_mul_overflow
                    with unsigned < 32 bits
           Product: libraries
           Version: 10.0
          Hardware: Other
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: AArch64
          Assignee: unassignedbugs at nondot.org
          Reporter: husseydevin at gmail.com
                CC: arnaud.degrandmaison at arm.com,
                    llvm-bugs at lists.llvm.org, smithp352 at googlemail.com,
                    Ties.Stuij at arm.com

Given the following C code:


bool umul16_overflow(uint16_t a, uint16_t b, uint16_t *result)
{
    return __builtin_mul_overflow(a,b,result);
}

aarch64 clang 10.0.1 -O2:

umul16_overflow:
        // Cast args
        and     w9, w1, #0xffff
        and     w10, w0, #0xffff
        // 16-bit to 32-bit long multiply
        mul     w9, w10, w9
        // Store upper 16 bits in w10
        lsr     w10, w9, #16
        // Store zero in x8??????????
        mov     x8, xzr
        // Check if upper 16 bits (w10) are non-zero
        cmp     w10, #0
        cset    w10, ne
        // Check if zero (x8) is non-zero??????????
        // w8 = 0
        cmp     x8, #0
        cset    w8, ne
        // Or the result against zero??????????
        orr     w0, w10, w8
        // Store
        strh    w9, [x2]
        ret

I have no idea what x8 is being used for here, it will always be zero. This is
the code without the redundant instructions:

umul16_overflow:
        // Cast args
        and     w8, w1, #0xffff
        and     w9, w0, #0xffff
        // 16-bit to 32-bit long multiply
        mul     w8, w9, w8
        // Store upper 16 bits in w9
        lsr     w9, w8, #16
        // Check if the upper 16 bits (w9) are non-zero
        cmp     w9, #0
        cset    w0, ne
        // Store
        strh    w9, [x2]
        ret

Note that we could simplify this even further by using cmp more creatively, but
this appears to be a different issue.

umul16_overflow:
        // Cast args
        and     w8, w1, #0xffff
        and     w9, w0, #0xffff
        // 16-bit to 32-bit long multiply
        mul     w8, w9, w8
        // Check if the upper 16 bits are non-zero
        cmp     wzr, w8, lsr #16
        cset    w0, ne
        // Store
        strh    w8, [x2]
        ret

-- 
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/20201125/97033130/attachment.html>


More information about the llvm-bugs mailing list