<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 - Deduce memcpy copy range"
   href="https://bugs.llvm.org/show_bug.cgi?id=41782">41782</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Deduce memcpy copy range
          </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>Scalar Optimizations
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>david.bolvansky@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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</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>