<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 - __builtin_ia32_addcarry_u64 produces better code then __builtin_addcll."
   href="https://bugs.llvm.org/show_bug.cgi?id=39464">39464</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>__builtin_ia32_addcarry_u64 produces better code then __builtin_addcll.
          </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>Windows NT
          </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>Backend: X86
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>ilya.lesokhin@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>craig.topper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When __builtin_ia32_addcarry_u64  is used the compiler is able to take
advantage of the ADC command to do carry propagation.

But it is unable to do so with __builtin_addcll.

<a href="https://godbolt.org/z/UQTOJX">https://godbolt.org/z/UQTOJX</a>

compiling 
#define N 3
auto add_ia32 (const std::array<uint64_t, N> &a, const std::array<uint64_t, N>
&b)
{
    uint64_t carry = 0;
    std::array<uint64_t, N> r;

     for (int i = 0; i < N; ++i) {
      carry = __builtin_ia32_addcarry_u64(carry, a[i], b[i], (unsigned long
long *)&r[i]);
    }

    return r;
}
results in:
  movq %rdi, %rax
  movq (%rsi), %rcx
  xorl %edi, %edi
  addb $-1, %dil
  adcq (%rdx), %rcx
  movq %rcx, (%rax)
  movq 8(%rsi), %rcx
  adcq 8(%rdx), %rcx
  movq %rcx, 8(%rax)
  movq 16(%rsi), %rcx
  adcq 16(%rdx), %rcx
  movq %rcx, 16(%rax)
  retq

while
#define N 3
auto add_builtin (const std::array<uint64_t, N> &a, const std::array<uint64_t,
N> &b)
{
    unsigned long long carry = 0;
    std::array<uint64_t, N> r;

     for (int i = 0; i < N; ++i) {
       r[i] = __builtin_addcll(a[i], b[i], carry, &carry);
    }

    return r;
}

results in: 
  movq %rdi, %rax
  movq (%rsi), %rcx
  movq (%rdx), %r8
  leaq (%rcx,%r8), %rdi
  movq %rdi, (%rax)
  movq 8(%rsi), %rdi
  addq 8(%rdx), %rdi
  setb %r9b
  addq %r8, %rcx
  adcq $0, %rdi
  setb %cl
  orb %r9b, %cl
  movzbl %cl, %ecx
  movq %rdi, 8(%rax)
  movq 16(%rsi), %rsi
  addq 16(%rdx), %rsi
  addq %rcx, %rsi
  movq %rsi, 16(%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>