<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/74493>74493</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            __builtin_addc loops spill the carry flag unnecessarily
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          davidben
      </td>
    </tr>
</table>

<pre>
    I imagine this would be a very difficult optimization for the compiler to perform, but I figure I may as well file it. When adding bignums, it's common to make add/adc chains, like so:
https://godbolt.org/z/af7jexGET

When the width of the bignum is known, Clang seems to be able to avoid spilling the flag. However, sometimes code is not specialized for the bignum width. E.g. the project may prioritize code size, or perhaps the space of possible widths is too unbounded. (Usually when the latter happens in cryptography, my area of interest, it's because we screwed up somewhere in defining the primitive... but we've screwed up a lot in defining our older primitives.)

In that case, one might write a loop. Clang will then spill the carry flag everywhere. This is pretty understandable, because the loop condition itself is sensitive to flags. But both aarch64 and x86_64 have branch operations that don't touch flags. On aarch64, we can loop manage the loop counter with ADD/SUB instead of ADDS/SUBS and branch with CBNZ. On x86_64, we can manage the loop counter with LEA or DEC and branch with JNZ.

If Clang could figure that out, it could dramatically tighten that loop, and avoid places where, in BoringSSL, we've had to dip into handwritten assembly.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8VM1u5EYPfBrNhVhhrPk_zMH2eL7PwWJz8C4C7MWg1JTEdatbaLZGKz99wNY4aydBLvZIalYVWdVEEW4c0THb3GWb0wKH2PpwNHhhU5JblN5Mx0fgDht2BLFlgdEP1kBJgHChMIHhuuZqsBF8H7njV4zsHdQ-QGwJKt_1bClA9NBTqH3osuIeyiHCI9TcDIHgETqcAAVGshZqtgQcc_ijJQdoDLsGSm7c0ImWcsyKnShw553CdvhCei4rzmgqqFpkl05afiEQn61us-UpW962MfaiT8U5K86NN6W3MfehyYrzq1bX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