This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
gcj non-optimization curiosity
- From: Tom Tromey <tromey at redhat dot com>
- To: java at gcc dot gnu dot org
- Date: 16 Dec 2001 13:55:39 -0700
- Subject: gcj non-optimization curiosity
- Reply-to: tromey at redhat dot com
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