gcj non-optimization curiosity

Tom Tromey tromey@redhat.com
Sun Dec 16 12:49:00 GMT 2001


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:
L16 is the call to throw a null pointer exception (a non-returning
call), and L18 is the call to throw a bad array index exception
(another non-returning call).
There are a few things I don't understand about this code that I would
like to understand.
* Why isn't the null-pointer check hoisted out of the loop?
 We know `foo' doesn't change during the loop, so this would be safe.
* Why is the length of the array ("movl 4(%eax), %edx") re-loaded each
 time through the loop?
* Why isn't the array index check eliminated? We've already done the
 comparison once; we know this can't fail.
Tom


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /