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

    <tr>
        <th>Summary</th>
        <td>
            _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR is not optimized out in range-for loops
        </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>
    Filing this since we lost track of https://github.com/llvm/llvm-project/pull/78929#issuecomment-1936582711 (oops)

#78929 had to round the bounds on `std::vector` up from `size()` to `capacity()` because the standard promises more permissive iterator invalidation. This had the side effect of making the checks harder to optimize.

See this compiler output for a demonstration:
https://godbolt.org/z/9c688Moab

I think this will require both STL and compiler improvements to fix. There are a couple type-level invariants that the compiler needs to be informed of. Those we can add with `_LIBCPP_ASSUME` after #91801 lands.

However, that is not sufficient. It also requires Clang to perform some loop reasoning:
https://godbolt.org/z/h78T188r9

I'll file a separate bug for that one. One that is fixed and #91801 lands, I can add the missing assumptions to libc++. (Happy to add them now, but since we're missing a compiler check, it'd be speculative without the compiler half.)

(CC @ldionne @var-const @danakj)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVE1z4jgQ_TXi0hXKlgk2Bx8CCTVUzUymEmavVFtqY01kySvJMMmv32rne_ayewCMbb1-_d7rxhjN0RHV4nItLq9nOKbOh1rjyeiG3Kzx-rHeGmvcEVJnIkTjFMGZwPqYIAVUD-Bb6FIaoiiuhNwKuT2a1I3NXPleyK21p9efiyH4X6SSkNthtFbIbVmt5ErIwsQ4kvJ9Ty5d5KtieVnJMs9ByMr7IQq5Etm1yK5evmUxHYQONSQPwY9OQ-oIGr6K4B2IZRaTZkrF1YlU8kEsMxgHaIPvp6fmiYSsGHqZMYpYZgoHVCY9vt9vSOEYaQKPCZ3GoGEIvjeRI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