JDK-6897150 : Hotspot optimises away a valid loop
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 6u16
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2009-11-02
  • Updated: 2011-02-16
  • Resolved: 2009-11-03
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Server VM (build 14.2-b01, mixed mode)


FULL OS VERSION :
Linux 2.6.28-16-generic #55-Ubuntu SMP Tue Oct 20 19:48:24 UTC 2009 i686 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
With the code listed below, when compiled by the eclipse compiler hotspot appears to remove the loop when the loop variable is compared agaist Integer.MAX_VALUE.  This does happen when the code is compiled with javac.  On inspection the eclipse compiler appears to generate correct byte code.  This only occurs with the server vm, client and interpreted mode (-Xint) work fine.

public class MaxIntegerLoopingBug
{
    public static void main(String[] args)
    {
        // This works
        loopAndPrint(Integer.MAX_VALUE -1);
        // This doesn't
        loopAndPrint(Integer.MAX_VALUE);
    }

    public static void loopAndPrint(int max)
    {
        int a = -1;
        int i = 1;
        for (; i < max; i++)
        {
            a = i;
        }
        System.out.println("Expected: " + (max - 1));
        System.out.println("Actual  : " + a);
    }
}

THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: No

THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: Yes

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Compile the code listed in the description with the eclipse compiler.
2. Run with the server hotspot vm.

EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected:
========
Expected: 2147483645
Actual  : 2147483645
Expected: 2147483646
Actual  : 2147483646

Actual
=====
Expected: 2147483645
Actual  : 2147483645
Expected: 2147483646
Actual  : 1024 (this number may vary across runs and may depend on system performance).


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class MaxIntegerLoopingBug
{
    public static void main(String[] args)
    {
        // This works
        loopAndPrint(Integer.MAX_VALUE -1);
        // This doesn't
        loopAndPrint(Integer.MAX_VALUE);
    }

    public static void loopAndPrint(int max)
    {
        int a = -1;
        int i = 1;
        for (; i < max; i++)
        {
            a = i;
        }
        System.out.println("Expected: " + (max - 1));
        System.out.println("Actual  : " + a);
    }
}
---------- END SOURCE ----------