Array bounds checks

Bryce McKinlay bryce@albatross.co.nz
Thu Oct 26 17:09:00 GMT 2000


Per Bothner wrote:

> But as for the questions why the compiler cannot optimize away the
> bounds check in your example, I don't know.

Upon further investigation, it appears that gcc doesn't do value range
propagation at all, yet. There is a patch to do it, however:

http://gcc.gnu.org/ml/gcc/2000-02/msg00103.html
http://gcc.gnu.org/ml/gcc-patches/2000-07/msg00968.html

My understanding is that it would only work for loops bounded by
constants, so the bounds check would be removed for a case such as the
following:

static final int LEN = ...;

int[] array = new int[LEN];

for (int i=0; i < LEN; i++)
  {
    array[i] = ...;
  }

In reality I don't think it will, however, because the optimizer won't see
array.length as a constant. Presumably, it would it help things if the
front end were to set array.length explicitly, rather than having the
runtime do it via _Jv_NewArray(), thus allowing constant propagation to
take effect?

regards

  [ bryce ]




More information about the Java mailing list