This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

PATCH: Re: gcj non-optimization curiosity

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
>    .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:

    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 

OK to commit?



2001-12-17  Bryce McKinlay  <>

	* expr.c (build_java_array_length_access): Don't force null pointer
	check unless flag_check_references is set.

Index: expr.c
RCS file: /cvs/gcc/gcc/gcc/java/expr.c,v
retrieving revision 1.125
diff -u -r1.125 expr.c
--- expr.c	2001/12/15 08:31:48	1.125
+++ expr.c	2001/12/17 03:41:54
@@ -709,7 +709,8 @@
     return build_int_2 (length, 0);
   node = build1 (INDIRECT_REF, int_type_node,
 		 fold (build (PLUS_EXPR, ptr_type_node,
-			      java_check_reference (node, 1), 
+			      java_check_reference (node, 
+						    flag_check_references), 
   return fold (node);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]