[llvm-bugs] [Bug 41782] New: Deduce memcpy copy range

via llvm-bugs llvm-bugs at lists.llvm.org
Tue May 7 00:50:48 PDT 2019


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

            Bug ID: 41782
           Summary: Deduce memcpy copy range
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: llvm-bugs at lists.llvm.org

bool foo(int *a, int *b, unsigned n) {
    memcpy(a, b, 8); // memcpy expanded
    return a == b;
}

foo(int*, int*, unsigned int):                            # @foo(int*, int*,
unsigned int)
        mov     rax, qword ptr [rsi]
        mov     qword ptr [rdi], rax
        cmp     rdi, rsi
        sete    al
        ret

bool foo1(int *a, int *b, unsigned n) {
    n = (n % 9);
    memcpy(a, b, n);
    return a == b;
}

bool foo2(int *a, int *b, unsigned n) {
    while (n > 8) {
        --n;
    }
    memcpy(a, b, n);
    return a == b;
}

Clang emits memcpy call in foo1 and foo2, Clang does not expand it as in the
"foo" case, but it should.

foo1(int*, int*, unsigned int):                            # @foo(int*, int*,
unsigned int)
        push    r14
        push    rbx
        push    rax
        mov     r14, rsi
        mov     rbx, rdi
        mov     eax, edx
        imul    rax, rax, 954437177
        shr     rax, 33
        lea     eax, [rax + 8*rax]
        sub     edx, eax
        call    memcpy
        cmp     rbx, r14
        sete    al
        add     rsp, 8
        pop     rbx
        pop     r14
        ret

foo2(int*, int*, unsigned int):                            # @foo(int*, int*,
unsigned int)
        push    r14
        push    rbx
        push    rax
        mov     r14, rsi
        mov     rbx, rdi
        cmp     edx, 8
        mov     eax, 8
        cmovae  edx, eax
        call    memcpy
        cmp     rbx, r14
        sete    al
        add     rsp, 8
        pop     rbx
        pop     r14
        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/20190507/dbb1b9f5/attachment.html>


More information about the llvm-bugs mailing list