<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 - Poor code generated for __builtin_sub_overflow with constant RHS"
   href="https://bugs.llvm.org/show_bug.cgi?id=40125">40125</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Poor code generated for __builtin_sub_overflow with constant RHS
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>craig.topper@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This generates some impressively bad code considering its equivalent to
checking if x is 0.

void bar();

void foo(unsigned x) {
  unsigned y;
  if (__builtin_sub_overflow(x, 1, &y))
    bar();
}

X86 assembly 

        mov     eax, edi
        add     rax, -1
        movabs  rcx, 8589934591
        and     rcx, rax
        add     edi, -1
        cmp     rcx, rdi
        jne     .LBB0_2
        ret
.LBB0_2:
        jmp     bar()                 # TAILCALL


<a href="https://godbolt.org/z/0K1SuH">https://godbolt.org/z/0K1SuH</a></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>