Re:What every compiler writer should know about programmers ...
by Someone on Saturday March 05, 2016 @09:10PM
(#11232156)
No, it would not. Compilers can remove boundary checks that provably happen after accessing an item of an array, not those before the array is accessed.
For your example, the Pascal code (at least, I hope this is Pascal; it has been a while since I wrote any):
if (i>=0 and i<16) then
begin
x := a[i]
end
else
begin
ReportError;
end
is equivalent to the C code: if (i>=0 && i<16)
{
if(i>=0 && i<16)
{
x = a[i]
} else {
RuntimeAbort(“Array access outside bounds");
}
} else {
ReportError();
}
A good Pascal compiler, like a good C compiler, would optimise away that second boundary check and the call to RuntimeAbort. Neither compiler is allowed to optimise away the first check.
Re:What every compiler writer should know about programmers ...
by AntonErtl on Sunday March 06, 2016 @05:35AM
(#11233209)
The point of my posting was that you don't need "optimizations" to optimize away redundant bounds checks, optimization* is able to do it just fine. Sorry if I did not get that across clearly. What does your "No, it would not" refer to?
for (i=0; i<16; i++) { if (i>=0 && i<16) do something with a[i] else report an error;
you can certainly optimize the if to if(1). That's an optimization*.