Thread overview
ldc doesn't elide bounds check
Mar 05, 2018
Kagamin
Mar 05, 2018
Kagamin
Mar 07, 2018
kinke
Mar 25, 2019
Kagamin
March 05, 2018
void f(byte[] a, byte[] b)
{
	if(a.length<b.length)b=b[0..a.length];
	foreach(i,c;b)
	{
		a[i]=c;
		if(c==10)break;
	}
}
void g(ulong a, ulong b)
{
	if(a<b)b=a;
	foreach(i;0..b)
	{
		assert(i<=a);
		if(i==10)break;
	}
}

In the function f the code for assert failure is retained, but in the function g it disappears at optimization levels -Os and above. Is it just me is there some limit for the optimizer?
March 05, 2018
Ah, I see, for assert(i<a); the failure handler is retained again, but I wonder why.
March 07, 2018
On Monday, 5 March 2018 at 19:36:20 UTC, Kagamin wrote:
> void f(byte[] a, byte[] b)
> {
> 	if(a.length<b.length)b=b[0..a.length];
> 	foreach(i,c;b)
> 	{
> 		a[i]=c;
> 		if(c==10)break;
> 	}
> }
> void g(ulong a, ulong b)
> {
> 	if(a<b)b=a;
> 	foreach(i;0..b)
> 	{
> 		assert(i<=a);
> 		if(i==10)break;
> 	}
> }
>
> In the function f the code for assert failure is retained, but in the function g it disappears at optimization levels -Os and above. Is it just me is there some limit for the optimizer?

Thx for noticing; I filed an LDC issue about it (https://github.com/ldc-developers/ldc/issues/2607), although gcc and clang are also unable to optimize away these checks for analogous C code: https://godbolt.org/g/AfyMJM
March 25, 2019
It's probably related to wrapping. If you iterate with step n and limit m and m is big enough, at some point i+n>m.max and will wrap, and the cycle will continue. The optimizer might check that it can't happen, but apparently doesn't.