Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6897150

Hotspot optimises away a valid loop

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 6u16
    • hotspot
    • x86
    • linux

      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 ----------

            cfang Changpeng Fang (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: