Bug 129305 - [compiler] Could optimize "return null" in presence of subroutine
Summary: [compiler] Could optimize "return null" in presence of subroutine
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.2 M6   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-02-24 04:21 EST by Philipe Mulet CLA
Modified: 2006-03-27 11:13 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Philipe Mulet CLA 2006-02-24 04:21:39 EST
Build 3.2M5

There is no need to save value on stack before invoking subroutine in the case of "return null".

public class X {
	public static void main(String[] args) {
		foo();
	} 
	static Object foo() {
		try {
			return null;
		} finally {
			System.out.println("SUCCESS");
		}
	}
}
Comment 1 Philipe Mulet CLA 2006-02-24 04:22:30 EST
Currently generates:

  // Method descriptor #19 ()Ljava/lang/Object;
  // Stack: 2, Locals: 2
  static Object foo();
     0  aconst_null
     1  astore_1
     2  getstatic System.out : PrintStream [22]
     5  ldc <String "SUCCESS"> [28]
     7  invokevirtual PrintStream.println(String) : void [30]
    10  aload_1
    11  areturn
    12  astore_0
    13  getstatic System.out : PrintStream [22]
    16  ldc <String "SUCCESS"> [28]
    18  invokevirtual PrintStream.println(String) : void [30]
    21  aload_0
    22  athrow
      Exception Table:
        [pc: 0, pc: 2] -> 12 when : any
Comment 2 Philipe Mulet CLA 2006-02-24 04:24:30 EST
Added GenericTypeTest#test044
Comment 3 Philipe Mulet CLA 2006-02-25 17:13:38 EST
Now consider constant expressions and null literal in optimized scenario of return statements.
Comment 4 Philipe Mulet CLA 2006-02-25 17:32:17 EST
To verify this bug is fixed, the generated bytecode pattern should be shorter:

In 1.5 target mode, instead of:

  // Method descriptor #19 ()Ljava/lang/Object;
  // Stack: 2, Locals: 2
  static Object foo();
     0  aconst_null
     1  astore_1
     2  getstatic System.out : PrintStream [22]
     5  ldc <String "SUCCESS"> [28]
     7  invokevirtual PrintStream.println(String) : void [30]
    10  aload_1
    11  areturn
    ...

it should be:

  // Method descriptor #19 ()Ljava/lang/Object;
  // Stack: 2, Locals: 1
  static Object foo();
     0  getstatic System.out : PrintStream [22]
     3  ldc <String "SUCCESS"> [28]
     5  invokevirtual PrintStream.println(String) : void [30]
     8  aconst_null
     9  areturn
     ...

Observe no temp 1 got allocated (one local less), and aconst_null directly precedes the return instruction.
Comment 5 David Audel CLA 2006-03-27 11:13:56 EST
Verified for 3.2 M6 using build I20060327-0010