This is the mail archive of the java@gcc.gnu.org mailing list for the Java 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]

gcj non-optimization curiosity


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


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