<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 - Clang doesn't use add with carry when adding constants"
   href="https://bugs.llvm.org/show_bug.cgi?id=38590">38590</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang doesn't use add with carry when adding constants
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </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>new bugs
          </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>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=20729" name="attach_20729" title="source file">attachment 20729</a> <a href="attachment.cgi?id=20729&action=edit" title="source file">[details]</a></span>
source file

When adding a register or a memory operand clang is able to use add with carry
to generate more efficient code.
See for example add_with_memory or add_with_register.

However, when adding a constant this optimization is missed.
See add_with_small_constants and add_with_large_constants

<a href="https://godbolt.org/g/2gb1ow">https://godbolt.org/g/2gb1ow</a>

add_with_register():
 add    %rcx,%rdi
 adc    %r10,%rsi
 adc    %r9,%rdx
 adc    %r8,%rax
 retq   
 nopl   (%rax)
add_with_memory():
 add    0x200b09(%rip),%rcx        # 601030 <mem>
 adc    0x200b0a(%rip),%rdx        # 601038 <mem+0x8>
 adc    0x200b0b(%rip),%rsi        # 601040 <mem+0x10>
 adc    0x200b0c(%rip),%rax        # 601048 <mem+0x18>
 retq   
 nopl   (%rax)
add_with_small_constants():
 add    $0x1,%rcx
 adc    $0x0,%rdx
 setb   %al
 movzbl %al,%esi
 add    $0x2,%rdx
 adc    %rdi,%rsi
 setb   %al
 movzbl %al,%eax
 add    $0x3,%rsi
 adc    %r8,%rax
 add    $0x4,%rax
 retq   
 nopw   0x0(%rax,%rax,1)
add_with_large_constants_constant():
 movabs $0x3c208c16d87cfd47,%rdi
 add    %rax,%rdi
 adc    $0x0,%rcx
 setb   %al
 movzbl %al,%eax
 movabs $0x97816a916871ca8d,%rsi
 add    %rcx,%rsi
 adc    %rdx,%rax
 setb   %cl
 movzbl %cl,%ecx
 movabs $0xb85045b68181585d,%rdx
 add    %rax,%rdx
 adc    %r8,%rcx
 movabs $0x30644e72e131a029,%rax
 add    %rcx,%rax
 retq   
 nopl   0x0(%rax,%rax,1)</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>