bounds checking and optimization

Johan j at j.nl
Wed Jun 30 11:43:08 UTC 2021


On Wednesday, 30 June 2021 at 01:18:53 UTC, Bruce Carneal wrote:
>
> A related question: is there anything preventing the compiler 
> from lifting range checks out of loops and throwing early?  For 
> example, on something like:
>
>     foreach(i, ref dst; c[])
>         dst = a[i] + b[i];
>
> could c.length be extracted and asserted as being leq a.length 
> and b.length before we drop in to the loop?

That would be observable behavior change, so not possible. Unless 
the behavior upon out-of-bounds access is defined as UB.

> Finally, it seems like bounds checking optimizations of this 
> sort belong in the front end or am I mistaken?

I am wary of any optimization actually performed (rather than 
checking for validity) in the frontend.

-Johan



More information about the digitalmars-d-ldc mailing list