PATCH: Re: gcj non-optimization curiosity

Bryce McKinlay bryce@waitaki.otago.ac.nz
Wed Nov 14 08:18:00 GMT 2001


Tom Tromey wrote:

>Consider this Java program:
>
>    public class foo
>    {
>      public static void main (String[] args)
>      {
>	int[] foo = new int[32];
>	int x = 0;
>	for (int i = 0; i < foo.length; ++i)
>	  x += foo[i];
>	System.out.println (x);
>      }
>    }
>
>If I compile this with `-g0 -S -O2', the loop looks like this (x86
>platform):
>
>    .L2:
>	    testl   %eax, %eax
>	    je      .L16
>	    movl    4(%eax), %edx
>	    cmpl    %edx, %ebx
>	    jge     .L17
>	    cmpl    %edx, %ebx
>	    jae     .L18
>	    movl    (%ecx), %edx
>	    incl    %ebx
>	    addl    %edx, %esi
>	    addl    $4, %ecx
>	    jmp     .L2
>    .L17:
>

When explicit null pointer checks got added (for MMU-less targets), 
calls to java_check_reference were introduced with the 
flag_check_references argument. But for some reason - presumably a 
mistake - the check that occurs during array bounds checks uses "1"!

I guess that with the java_check_reference the optimizer could not 
eliminate the check because it doesn't know that "new" can never return 
null, but why it couldn't move it out of the loop is a mystery!

With this patch, it generates the somewhat more reasonable:

.L4:
    cmpl    %ecx, %edx
    jae    .L14
    addl    (%eax), %ebx
    incl    %edx
    addl    $4, %eax
    cmpl    %ecx, %edx
    jl    .L4

Where .L14 is the "throw ArrayBoundsException". The redundant bounds 
check is check is still there (it can never occur because the bounds of 
the loop are the same as the bounds of the array, so why can't the 
optimizer get rid of it?), but the generated code is much better than 
before.

OK to commit?

regards

Bryce.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: gcj-array-check.patch
URL: <http://gcc.gnu.org/pipermail/java-patches/attachments/20011114/55cfffc3/attachment.ksh>


More information about the Java-patches mailing list