Bug 337426 - Compiler could reduce the amount of arithmetical operations
Summary: Compiler could reduce the amount of arithmetical operations
Status: VERIFIED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.6.1   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: 3.7 M7   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-02-17 07:43 EST by Bernd Kolb CLA
Modified: 2011-04-25 04:55 EDT (History)
4 users (show)

See Also:


Attachments
The java file containing the methods (407 bytes, text/plain)
2011-02-17 07:43 EST, Bernd Kolb CLA
no flags Details
The compiled class file (742 bytes, application/octet-stream)
2011-02-17 07:43 EST, Bernd Kolb CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Bernd Kolb CLA 2011-02-17 07:43:08 EST
Created attachment 189173 [details]
The java file containing the methods

I have written a little class which does some simple calculations

My expectation would have been that those calculations get optimized by the compiler. However this seems to be not the case
Original:
public int calc (int param) {
	return 100*param/100;
}

Actual result:
	return 100*param/100;

Expected result:
	return param*100/100;

or even better
	return param;


Original:
public int calc (int param) {
	return 100*param/200;
}

Actual result:
	return 100*param/200;

Expected result:
	return param*0.5;


Original:
public int calc (int param) {
	return param*100/200;
}

Actual result:
	return param*100/200;

Expected result:
	return param*0.5;


Original:
public int calc (int param) {
	return param*100/100;
}

Actual result:
	return param*100/100;

Expected result:
	return param*1;

or even better
	return param;


Original:
public int calc (int param) {
	return param*(100/200)
}

Actual result:
	return param*0

Expected result:
	return param*0

or even better
	return 0


Original:
public int calc (int param) {
	return param*(100/100);
}

Actual result:
	return param*1;

Expected result:
	return param*1;

or even better
	return param
Comment 1 Bernd Kolb CLA 2011-02-17 07:43:38 EST
Created attachment 189174 [details]
The compiled class file
Comment 2 Bernd Kolb CLA 2011-02-17 07:52:12 EST
Obviously this optimizations could be done for other primitive types as well. I
checked for double and the results look similar.

Some more test:

public int calc7(int param) {
    return param * 0;
}

could be optimized directly to return 0;

public double calc7(int param) {
    return param / 0;
}

could lead directly to a compiler error
Comment 3 Olivier Thomann CLA 2011-02-17 08:21:14 EST
With today's JITs, I am not sure this would change anything. Optimizing the compiler for such cases is not free. So it would impact all cases for a possible performance gain.
Do you have numbers that would show the advantage of such optimization beside making the .class files smaller ?
Comment 4 Stephan Herrmann CLA 2011-02-17 08:34:59 EST
(In reply to comment #0)
> My expectation would have been that those calculations get optimized by the
> compiler. However this seems to be not the case
> Original:
> public int calc (int param) {
>     return 100*param/100;
> }
> 
> Actual result:
>     return 100*param/100;
> 
> Expected result:
>     return param*100/100;
> 
> or even better
>     return param;

My expectation is that an optimized program behaves equal to the original.
But: 

  int result = calc(Integer.MAX_VALUE);

Saying: we should always be *very* careful about corner cases.
Comment 5 Deepak Azad CLA 2011-02-17 08:41:48 EST
(In reply to comment #4)
> My expectation is that an optimized program behaves equal to the original.
> But: 
> 
>   int result = calc(Integer.MAX_VALUE);
> 
> Saying: we should always be *very* careful about corner cases.

Plus you cannot optimize much if floating point computation is involved (see JLS 15.7.3)
Comment 6 Bernd Kolb CLA 2011-02-17 08:53:00 EST
Well, the point came up when investigating some performance issues on an android device. So I have no numbers for Java itself and it might be very well the case that such stuff should be done on Android level. 

I agree that this is not for free. In the end I cannot judge how much performance this will cost during compile time and how much we will gain during runtime.
Comment 7 Olivier Thomann CLA 2011-02-24 15:18:43 EST
Not only this is not free, but it would be wrong in some cases.
param / 0 must lead to an runtime exception. This is not a compile error.

And as Stephan pointed out:

public class X {
	public static int calc (int param) {
    return 100*param/100;
	}


	public static void main(String[] args) {
		System.out.println(calc(Integer.MAX_VALUE));
	}
}

=> -1
and not Integer.MAX_VALUE.

Srikanth, I am tempted to close as WONTFIX.
Comment 8 Srikanth Sankaran CLA 2011-03-14 00:13:01 EDT
(In reply to comment #7)
> Srikanth, I am tempted to close as WONTFIX.

Agree with assessment. Resolving as WONTFIX.
Comment 9 Jay Arthanareeswaran CLA 2011-04-25 04:55:06 EDT
Verified for 3.7 M7.