> Not necessarily

Fair. I should have said "when it doesn't optimize, there's now an additional load that happens" instead of "vectorize". I was more thinking about the -O0 cases, which I acknowledge talking about a few extra operations when optimizations are turned off isn't the most important use case to think about.

On Thu, Oct 31, 2024 at 3:26 PM Jonathan Wakely <cxx@kayari.org> wrote:


On Thu, 31 Oct 2024 at 20:05, Matthew Kolbe via Std-Proposals <std-proposals@lists.isocpp.org> wrote:
> You can tell the compiler that this is safe either by loading both values before the if

As I mentioned in my other email, this is true (and I believe the best work around). My two objections are that it's now two more lines of code. The other is that taking both paths is no longer an available optimization, but instead part of the code. So, even when it doesn't vectorize, there's now an additional load that happens for all compilations.


Not necessarily: https://godbolt.org/z/4h8eoe6Ea

The optimized code is identical whether you do two loads and only use one of them, or only do one load. In the case where you do two loads, one is dead code that can be removed. But it still tells the compiler that performing that load is not undefined, so the indices i and i+1 must be within the array bounds.



--
Matthew P. Kolbe
(312) 218-6595
matthew.kolbe@gmail.com