<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 --- - carry detection missed optimization in chained add-with-carry"
   href="https://llvm.org/bugs/show_bug.cgi?id=31755">31755</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>carry detection missed optimization in chained add-with-carry
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>3.9
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>vincent-llvm@vinc17.net
          </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>A carry can be detected so that an adcq instruction can be generated on x86_64.
But this does not work when the carry-out of this second addition is used for a
third addition. I have tested only on x86_64 (see below), but other similar
targets by be affected too.

Example:

typedef unsigned long T;

void f (T, T, T);

void add0 (T a, T b, T c, T w)
{
  T u = a, v = b;
  T carry1;

  a = u + w;
  carry1 = a < u;
  b = v + carry1;
  f (a, b, c);
}

void add1 (T a, T b, T c, T w)
{
  T u = a, v = b;
  T carry1, carry2;

  a = u + w;
  carry1 = a < u;
  b = v + carry1;
  carry2 = b < v;
  c += carry2;
  f (a, b, c);
}

Under Debian/unstable with Clang 3.9.1, using -O3, I get:

add0:                                   # @add0
        .cfi_startproc
# BB#0:
        addq    %rcx, %rdi
        adcq    $0, %rsi
        jmp     f                       # TAILCALL

Here, one can see that an adcq instruction is generated for the second
addition.

add1:                                   # @add1
        .cfi_startproc
# BB#0:
        addq    %rcx, %rdi
        sbbq    %rax, %rax
        andl    $1, %eax
        addq    %rax, %rsi
        adcq    $0, %rdx
        jmp     f                       # TAILCALL

The adcq instruction is generated for the third addition, but not for the
second one. The sbbq / andl / addq sequence could have been replaced by a
single adcq.</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>