Array bounds checks

Per Bothner per@bothner.com
Thu Oct 26 08:54:00 GMT 2000


Bryce McKinlay <bryce@albatross.co.nz> writes:

> Should not the optimizer be able to eliminate the bounds check, or at
> least move it out of the inner loop? Basically this loop expands to:

Moving a bounds check out the of inner loop is in general not possible,
because the exception would happen prematurely.  The the program would
behave differently, and contrary to the JLS.

Some compilers generate code twice:

   if (upper < array.length)
     for (int i = 0;  i < upper; i++)
        ... no bounds checking ...
   else
     for (int i = 0;  i < upper; i++)
        ... with bounds checking ...

I don't particularly care for this idea ...

But as for the questions why the compiler cannot optimize away the
bounds check in your example, I don't know.  I thought there was code
in gcc to do some range analysis and bounds check removal.  However,
I'm not sure what the current status is.  It may also be the case that
some minor tweak (letting the compiler know about array.length for
example) might fix it.
-- 
	--Per Bothner
per@bothner.com   http://www.bothner.com/~per/


More information about the Java mailing list