<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 --- - strangely pessimized code for double-word operations"
   href="https://llvm.org/bugs/show_bug.cgi?id=24983">24983</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>strangely pessimized code for double-word operations
          </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>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Backend: X86
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>bonzini@gnu.org
          </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>For this function:

#include <stdint.h>
void mul64(uint64_t *lo, uint64_t *hi, uint64_t a, uint64_t b)
{
    typedef union {
        uint64_t ll;
        struct { uint32_t high, low; } l;
    } LL;
    LL *ra = (LL *)&a;
    LL *rb = (LL *)&b;
    LL r0, r32, rt;

    /* Build the two products that will go in bits 32-95 */
    rt.ll = (uint64_t)ra->l.high * rb->l.low;
    r32.ll = (uint64_t)ra->l.low * rb->l.high;

    /* Reorganize these two factors into two 33-bit numbers */
    *hi = (uint64_t)r32.l.high + rt.l.high;       // bits 64-127
    r32.ll = (uint64_t)r32.l.low + rt.l.low;      // bits 32-95

    /* Now build the result 32 bits at a time.  Finish bits 0-31... */
    r0.ll = (uint64_t)ra->l.low * rb->l.low;

    /* ... then bits 32-63... */
    r32.ll += r0.l.high;
    r0.l.high = r32.l.low;
    *lo = r0.ll;

    /* ... then bits 64-127. */
    *hi += r32.l.high;
    *hi += (uint64_t)ra->l.high * rb->l.high;
}

LLVM generally produces nicely optimized code, except for some strange bits:

   0:    55                       push   %ebp
   1:    53                       push   %ebx
   2:    57                       push   %edi
   3:    56                       push   %esi
   4:    8b 44 24 24              mov    0x24(%esp),%eax
   8:    8b 6c 24 28              mov    0x28(%esp),%ebp
   c:    8b 74 24 1c              mov    0x1c(%esp),%esi
  10:    f7 64 24 20              mull   0x20(%esp)
  14:    89 d7                    mov    %edx,%edi
  16:    89 c3                    mov    %eax,%ebx
  18:    89 e8                    mov    %ebp,%eax
  1a:    f7 e6                    mul    %esi
  1c:    01 fa                    add    %edi,%edx
  1e:    8b 7c 24 18              mov    0x18(%esp),%edi
  22:    89 17                    mov    %edx,(%edi)
  24:    19 d2                    sbb    %edx,%edx
  26:    83 e2 01                 and    $0x1,%edx
  29:    89 57 04                 mov    %edx,0x4(%edi)
  2c:    01 d8                    add    %ebx,%eax
  2e:    19 d2                    sbb    %edx,%edx
  30:    bb ff ff ff ff           mov    $0xffffffff,%ebx   ; here it starts...
  35:    83 c3 01                 add    $0x1,%ebx          ; EBX = 0, CF = 1?
  38:    19 c9                    sbb    %ecx,%ecx          ; ECX = -1?
  3a:    21 d1                    and    %edx,%ecx          ; AND into -1???
  3c:    21 c3                    and    %eax,%ebx          ; AND into 0???
  3e:    09 c3                    or     %eax,%ebx          ; OR into 0???
  40:    83 e1 01                 and    $0x1,%ecx

which could be changed into simply

   mov %edx, %ecx
   mov %eax, %ebx

The moves could even be coalesced with the two previous instructions, giving:

   add %eax, %ebx
   sbb %ecx, %ecx

because both %eax and %edx are dead at this point:

  43:    8b 44 24 24              mov    0x24(%esp),%eax
  47:    f7 e6                    mul    %esi
  49:    01 d3                    add    %edx,%ebx
  4b:    8b 54 24 14              mov    0x14(%esp),%edx
  ...</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>