<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 - Missed bounds check optimization"
   href="https://bugs.llvm.org/show_bug.cgi?id=47172">47172</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missed bounds check optimization
          </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>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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>david.bolvansky@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>From rust-lang github (<a href="https://github.com/rust-lang/rust/issues/75525">https://github.com/rust-lang/rust/issues/75525</a>):

#include <stddef.h>
#include <stdint.h>


uint8_t f1(size_t idx) {
    if (idx < 8) {
        // it's ok that this has a bounds check
        return (idx - 1) < 10;
    } else {
        return 0;
    }
}

Clang:
f1:                                     # @f1
        cmp     rdi, 8
        setb    cl
        add     rdi, -1
        cmp     rdi, 10
        setb    al
        and     al, cl
        ret

GCC:
f1:
        sub     rdi, 1
        cmp     rdi, 6
        setbe   al
        ret


Modified test case:

uint8_t f2(size_t idx) {
    if (idx <= 9) {
       return (idx - 1) > 10;
    } else {
        return 0;
    }
}

Clang:
f2:                                     # @f2
        cmp     rdi, 10
        setb    cl
        add     rdi, -1
        cmp     rdi, 10
        seta    al
        and     al, cl
        ret

GCC:
f2:
        test    rdi, rdi
        sete    al
        ret




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