Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 5091921
Votes 1
Synopsis Sign flip issues in loop optimizer
Category hotspot:compiler2
Reported Against tiger
Release Fixed
State 5-Cause Known, bug
Priority: 3-Medium
Related Bugs 6357214 , 6519085 , 2148922 , 6700047 , 6186134 , 5094936 , 6196102
Submit Date 25-AUG-2004
Description


the tests below fail for server compiler, but pass for client compiler on the latest tiger build
/
* Test for the bug of transforming indx >= MININT to indx > MININT-1 */
/* Run with -Xcomp -XX:CompileOnly=ge1.test1 -XX:MaxInlineSize=1 */
public class ge1
{
  public static int result=1;
  public static int test1(int limit)
  {
    int indx;
    int sum = 0;
    for (indx = 500; indx >= limit; indx -= 2)
    {
      sum += 2000 / indx;
      result = sum;
    }
    return sum;
  }
  public static void main(String[] args)
  {
    int r = 0;
    try
    {
      r = test1(0x80000000);
      System.out.println(result);
      System.out.println("FAILED");
      System.exit(1);
    }
    catch (ArithmeticException e1)
    {
      System.out.println("Expected exception caught");
      if (result != 5986)
      {
        System.out.println(result);
        System.out.println("FAILED");
        System.exit(1);
      }
    }
    System.out.println("WORKED");
  }
}

====
/* Run with -Xcomp -XX:CompileOnly=wrap1.test1 -XX:MaxInlineSize=1 */
/* limit reset to ((limit-init+stride-1)/stride)*stride+init */
/* Calculation may overflow */
public class wrap1
{
  public static volatile int c = 1;
  public static int test1(int limit)
  {
    int indx;
    int sum = 0;
    for (indx = 0xffffffff; indx < limit; indx += 0x20000000)
    {
      sum += c;
    }
    return sum;
  }
  public static void main(String[] args)
  {
    int result;
    result = test1(0x7fffffff);
    if (result != 4)
    {
      System.out.println(result);
      System.out.println("FAILED");
      System.exit(1);
    }
    else
    {
      System.out.println("WORKED");
    }
  }
}
====
/* Test for range check elimination with bit flip issue for
   scale*i+offset<limit where offset is not 0 */
/* Run with -Xcomp -XX:CompileOnly=rce5.test1
 -XX:MaxInlineSize=1 */
public class rce5
{
  static int[] box = {1,2,3,4,5,6,7,8,9};
  public static int result=0;
  public static int test1(int[] b, int limit)
  {
    int indx;
    int sum = b[1];
    result = sum;
    for (indx = 0x80000000; indx < limit; ++indx)
    {
      if (indx > 0x80000000)
      {
        // this test is not issued in pre-loop but issued in main loop
        // trick rce into thinking expression is false when indx >= 0
        // in fact it is false when indx==0x80000001
        if (indx - 9 < -9)
        {
          sum += indx;
          result = sum;
          sum ^= b[indx & 7];
          result = sum;
        }
        else
          break;
      }
      else
      {
        sum += b[indx & 3];
        result = sum;
      }
    }
    return sum;
  }
  public static void main(String[] args)
  {
    int r = test1(box,0x80000100);
    if (result != 3)
    {
      System.out.println(result);
      System.exit(2);
    }
    else
    {
      System.out.println("WORKED");
    }
  }
}
====
/* Test for range check elimination with bit flip issue for
   scale*i<limit where scale > 1 */
/* Run with -Xcomp -XX:CompileOnly=rce6.test1
 -XX:MaxInlineSize=1 */
public class rce6
{
  static int[] box = {1,2,3,4,5,6,7,8,9};
  public static int result=0;
  public static int test1(int[] b, int limit)
  {
    int indx;
    int sum = b[1];
    result = sum;
    for (indx = 0x80000000; indx < limit; ++indx)
    {
      if (indx > 0x80000000)
      {
        // harmless rce target
        if (indx < 0)
        {
          sum += result;
          result = sum;
        }
        else
          break;
        // this test is not issued in pre-loop but issued in main loop
        // trick rce into thinking expression is false when indx >= 0
        // in fact it is false when indx==0x80000001
        // In compilers that transform mulI to shiftI may mask this issue.
        if (indx * 28 + 1 < 0)
        {
          sum += indx;
          result = sum;
          sum ^= b[indx & 7];
          result = sum;
        }
        else
          break;
      }
      else
      {
        sum += b[indx & 3];
        result = sum;
      }
    }
    return sum;
  }
  public static void main(String[] args)
  {
    int r = test1(box,0x80000100);
    if (result != 6)
    {
      System.out.println(result);
      System.exit(2);
    }
    else
    {
      System.out.println("WORKED");
    }
  }
}
====
/* Test for range check elimination with i <= limit */
/* Run with -Xcomp -XX:CompileOnly=rce7.test1
 -XX:MaxInlineSize=1 */
public class rce7
{
  static int[] box = {1,2,3,4,5,6,7,8,9,0x7fffffff};
  public static int result=0;
  public static int test1(int[] b)
  {
    int indx;
    int max = b[9];
    int sum = b[7];
    result = sum;
    for (indx = 0; indx < b.length; ++indx)
    {
      if (indx <= max)
      {
        sum += (indx ^ 15) + ((result != 0) ? 0 : sum);
        result = sum;
      }
      else
        throw new RuntimeException();
    }
    for (indx = -7; indx < b.length; ++indx)
    {
      if (indx <= 9)
      {
        sum += (sum ^ 15) + ((result != 0) ? 0 : sum);
        result = sum;
      }
      else
        throw new RuntimeException();
    }
    return sum;
  }
  public static void main(String[] args)
  {
    int r = test1(box);
    if (result != 14680079)
    {
      System.out.println(result);
      System.exit(2);
    }
    else
    {
      System.out.println("WORKED");
    }
  }
}
====
/* Test for range check elimination with i >= limit */
/* Run with -Xcomp -XX:CompileOnly=rce8.test1
 -XX:MaxInlineSize=1 */
public class rce8
{
  static int[] box = {-1,0,1,2,3,4,5,6,7,8,0x80000000};
  private static int result=0;
  public static int test1(int[] b)
  {
    int indx;
    int sum = b[5];
    int min = b[10];
    result = sum;
    for (indx = b.length-1; indx >= 0; --indx)
    {
      if (indx >= min)
      {
        sum += (sum ^ 9) + ((result != 0) ? 0 :sum);
        result = sum;
      }
      else
        throw new RuntimeException();
    }
    return sum;
  }
  public static void main(String[] args)
  {
    int r = test1(box);
    if (result != 16393)
    {
      System.out.println(result);
      System.exit(2);
    }
    else
    {
      System.out.println("WORKED");
    }
  }
}
====
/* Test for the bug of transforming indx <= MAXINT to indx < MAXINT+1 */
/* Run with -Xcomp -XX:CompileOnly=le1.test1 -XX:MaxInlineSize=1 */
public class le1
{
  public static int result = 0;
  public static int test1(int limit)
  {
    int indx;
    int sum = 0;
    for (indx = -500; indx <= limit; indx += 2)
    {
      sum += 3000 / indx;
      result = sum;
    }
    return sum;
  }
  public static void main(String[] args)
  {
    int r = 0;
    try
    {
      r = test1(0x7fffffff);
      System.out.println(result);
      System.out.println("FAILED");
      System.exit(1);
    }
    catch (ArithmeticException e1)
    {
      System.out.println("Expected exception caught");
      if (result != -9039)
      {
        System.out.println(result);
        System.out.println("FAILED");
        System.exit(1);
      }
    }
    System.out.println("WORKED");
  }
}
(Incident Review ID: 300690) 
======================================================================
Posted Date : 2005-10-10 15:28:31.0
Work Around
N/A
Evaluation
See Comments
Comments
  
  Include a link with my name & email   

Submitted On 09-JUL-2009
sairvin
I have what I believe is a similar problem:

public class ShortBug {

  static short shortIdentity() {
    short s = 0;
    for (short x = 1; x != 0; x++) {
      s += x;
    }
    return s;
  }

  public static void main(final String[] args) {
    for (int k = 0; k < 20; k++) {
      final long t = System.currentTimeMillis();
      final short s = shortIdentity();
      System.out.println("Time for iteration " + k + " was " + (System.currentTimeMillis() - t) + " ms, value was " + s);
    }
  }
}

With hotspot active this code can suddenly start returning the wrong result and run really slow.  I believe this is because it is forgetting about the shortness of the loop variable.

> java ShortBug
Time for iteration 0 was 1 ms, value was -32768
Time for iteration 1 was 0 ms, value was -32768
Time for iteration 2 was 2880 ms, value was 0
Time for iteration 3 was 2876 ms, value was 0
Time for iteration 4 was 3032 ms, value was 0
Time for iteration 5 was 2872 ms, value was 0
Time for iteration 6 was 2871 ms, value was 0
Time for iteration 7 was 2870 ms, value was 0
Time for iteration 8 was 2871 ms, value was 0
Time for iteration 9 was 2869 ms, value was 0
Time for iteration 10 was 2870 ms, value was 0
Time for iteration 11 was 2869 ms, value was 0
Time for iteration 12 was 2869 ms, value was 0
Time for iteration 13 was 2870 ms, value was 0
Time for iteration 14 was 2870 ms, value was 0
Time for iteration 15 was 2869 ms, value was 0
Time for iteration 16 was 2870 ms, value was 0
Time for iteration 17 was 2869 ms, value was 0
Time for iteration 18 was 2938 ms, value was 0
Time for iteration 19 was 2869 ms, value was 0

With -Xint it works fine

> java -Xint ShortBug
Time for iteration 0 was 1 ms, value was -32768
Time for iteration 1 was 1 ms, value was -32768
Time for iteration 2 was 1 ms, value was -32768
Time for iteration 3 was 1 ms, value was -32768
Time for iteration 4 was 1 ms, value was -32768
Time for iteration 5 was 1 ms, value was -32768
Time for iteration 6 was 1 ms, value was -32768
Time for iteration 7 was 1 ms, value was -32768
Time for iteration 8 was 1 ms, value was -32768
Time for iteration 9 was 1 ms, value was -32768
Time for iteration 10 was 1 ms, value was -32768
Time for iteration 11 was 1 ms, value was -32768
Time for iteration 12 was 1 ms, value was -32768
Time for iteration 13 was 1 ms, value was -32768
Time for iteration 14 was 1 ms, value was -32768
Time for iteration 15 was 1 ms, value was -32768
Time for iteration 16 was 1 ms, value was -32768
Time for iteration 17 was 1 ms, value was -32768
Time for iteration 18 was 1 ms, value was -32768
Time for iteration 19 was 1 ms, value was -32768




PLEASE NOTE: JDK6 is formerly known as Project Mustang