<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - useless moves generated by memcpy() to union"
   href="https://llvm.org/bugs/show_bug.cgi?id=27503">27503</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>useless moves generated by memcpy() to union
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.8
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>jhaberman@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Test case:

--

#include <string.h>

typedef union {
  char ch;
  float fl;
  double dbl;
} u;

u f(const void *p, int type) {
  u v;
  if (type == 1) {
    memcpy(&v, p, 1);
  } else if (type <= 5) {
    memcpy(&v, p, 4);
  } else if (type <= 8) {
    memcpy(&v, p, 8);
  }
  return v;
}


--

On every platform/version I've tested, compiling with -O3 I get bad code like:

--

0000000000000000 <f>:
   0:    83 fe 01                 cmp    esi,0x1
   3:    75 0e                    jne    13 <f+0x13>
   5:    0f b6 07                 movzx  eax,BYTE PTR [rdi]
   8:    66 48 0f 6e c0           movq   xmm0,rax
   d:    66 48 0f 7e c0           movq   rax,xmm0
  12:    c3                       ret    
  13:    83 fe 05                 cmp    esi,0x5
  16:    7f 0d                    jg     25 <f+0x25>
  18:    8b 07                    mov    eax,DWORD PTR [rdi]
  1a:    66 48 0f 6e c0           movq   xmm0,rax
  1f:    66 48 0f 7e c0           movq   rax,xmm0
  24:    c3                       ret    
  25:    83 fe 08                 cmp    esi,0x8
  28:    7f 04                    jg     2e <f+0x2e>
  2a:    f2 0f 10 07              movsd  xmm0,QWORD PTR [rdi]
  2e:    66 48 0f 7e c0           movq   rax,xmm0
  33:    c3                       ret

--

Note the move to eax, then to xmm0, then back to rax.</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>