changeset 16036:4161a58e48cb

refactor/simplify Constant
author Lukas Stadler <lukas.stadler@oracle.com>
date Thu, 05 Jun 2014 13:19:59 +0200
parents 15f1580a37e7
children 3ad5e034ac1d
files graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/Constant.java graal/com.oracle.graal.java/src/com/oracle/graal/java/AbstractBytecodeParser.java
diffstat 2 files changed, 43 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/Constant.java	Thu Jun 05 13:19:59 2014 +0200
+++ b/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/Constant.java	Thu Jun 05 13:19:59 2014 +0200
@@ -32,37 +32,24 @@
 
     private static final long serialVersionUID = -6355452536852663986L;
 
-    private static final Constant[] INT_CONSTANT_CACHE = new Constant[100];
-    static {
-        for (int i = 0; i < INT_CONSTANT_CACHE.length; ++i) {
-            INT_CONSTANT_CACHE[i] = new PrimitiveConstant(Kind.Int, i);
-        }
-    }
-
+    /*
+     * Using a larger cache for integers leads to only a slight increase in cache hit ratio which is
+     * not enough to justify the impact on startup time.
+     */
     public static final Constant NULL_OBJECT = new NullConstant();
     public static final Constant INT_MINUS_1 = new PrimitiveConstant(Kind.Int, -1);
-    public static final Constant INT_0 = forInt(0);
-    public static final Constant INT_1 = forInt(1);
-    public static final Constant INT_2 = forInt(2);
-    public static final Constant INT_3 = forInt(3);
-    public static final Constant INT_4 = forInt(4);
-    public static final Constant INT_5 = forInt(5);
+    public static final Constant INT_0 = new PrimitiveConstant(Kind.Int, 0);
+    public static final Constant INT_1 = new PrimitiveConstant(Kind.Int, 1);
+    public static final Constant INT_2 = new PrimitiveConstant(Kind.Int, 2);
     public static final Constant LONG_0 = new PrimitiveConstant(Kind.Long, 0L);
     public static final Constant LONG_1 = new PrimitiveConstant(Kind.Long, 1L);
     public static final Constant FLOAT_0 = new PrimitiveConstant(Kind.Float, Float.floatToRawIntBits(0.0F));
     public static final Constant FLOAT_1 = new PrimitiveConstant(Kind.Float, Float.floatToRawIntBits(1.0F));
-    public static final Constant FLOAT_2 = new PrimitiveConstant(Kind.Float, Float.floatToRawIntBits(2.0F));
     public static final Constant DOUBLE_0 = new PrimitiveConstant(Kind.Double, Double.doubleToRawLongBits(0.0D));
     public static final Constant DOUBLE_1 = new PrimitiveConstant(Kind.Double, Double.doubleToRawLongBits(1.0D));
     public static final Constant TRUE = new PrimitiveConstant(Kind.Boolean, 1L);
     public static final Constant FALSE = new PrimitiveConstant(Kind.Boolean, 0L);
 
-    static {
-        assert FLOAT_0 != forFloat(-0.0F) : "Constant for 0.0f must be different from -0.0f";
-        assert DOUBLE_0 != forDouble(-0.0d) : "Constant for 0.0d must be different from -0.0d";
-        assert NULL_OBJECT.isNull();
-    }
-
     protected Constant(PlatformKind kind) {
         super(kind);
     }
@@ -183,9 +170,6 @@
         if (Float.compare(f, 1.0F) == 0) {
             return FLOAT_1;
         }
-        if (Float.compare(f, 2.0F) == 0) {
-            return FLOAT_2;
-        }
         return new PrimitiveConstant(Kind.Float, Float.floatToRawIntBits(f));
     }
 
@@ -196,7 +180,13 @@
      * @return a boxed copy of {@code value}
      */
     public static Constant forLong(long i) {
-        return i == 0 ? LONG_0 : i == 1 ? LONG_1 : new PrimitiveConstant(Kind.Long, i);
+        if (i == 0) {
+            return LONG_0;
+        } else if (i == 1) {
+            return LONG_1;
+        } else {
+            return new PrimitiveConstant(Kind.Long, i);
+        }
     }
 
     /**
@@ -206,13 +196,18 @@
      * @return a boxed copy of {@code value}
      */
     public static Constant forInt(int i) {
-        if (i == -1) {
-            return INT_MINUS_1;
+        switch (i) {
+            case -1:
+                return INT_MINUS_1;
+            case 0:
+                return INT_0;
+            case 1:
+                return INT_1;
+            case 2:
+                return INT_2;
+            default:
+                return new PrimitiveConstant(Kind.Int, i);
         }
-        if (i >= 0 && i < INT_CONSTANT_CACHE.length) {
-            return INT_CONSTANT_CACHE[i];
-        }
-        return new PrimitiveConstant(Kind.Int, i);
     }
 
     /**
@@ -346,6 +341,8 @@
      */
     public static Constant zero(Kind kind) {
         switch (kind) {
+            case Boolean:
+                return FALSE;
             case Byte:
                 return forByte((byte) 0);
             case Char:
@@ -370,6 +367,8 @@
      */
     public static Constant one(Kind kind) {
         switch (kind) {
+            case Boolean:
+                return TRUE;
             case Byte:
                 return forByte((byte) 1);
             case Char:
--- a/graal/com.oracle.graal.java/src/com/oracle/graal/java/AbstractBytecodeParser.java	Thu Jun 05 13:19:59 2014 +0200
+++ b/graal/com.oracle.graal.java/src/com/oracle/graal/java/AbstractBytecodeParser.java	Thu Jun 05 13:19:59 2014 +0200
@@ -907,20 +907,20 @@
     switch (opcode) {
         case NOP            : /* nothing to do */ break;
         case ACONST_NULL    : frameState.apush(appendConstant(Constant.NULL_OBJECT)); break;
-        case ICONST_M1      : frameState.ipush(appendConstant(Constant.INT_MINUS_1)); break;
-        case ICONST_0       : frameState.ipush(appendConstant(Constant.INT_0)); break;
-        case ICONST_1       : frameState.ipush(appendConstant(Constant.INT_1)); break;
-        case ICONST_2       : frameState.ipush(appendConstant(Constant.INT_2)); break;
-        case ICONST_3       : frameState.ipush(appendConstant(Constant.INT_3)); break;
-        case ICONST_4       : frameState.ipush(appendConstant(Constant.INT_4)); break;
-        case ICONST_5       : frameState.ipush(appendConstant(Constant.INT_5)); break;
-        case LCONST_0       : frameState.lpush(appendConstant(Constant.LONG_0)); break;
-        case LCONST_1       : frameState.lpush(appendConstant(Constant.LONG_1)); break;
-        case FCONST_0       : frameState.fpush(appendConstant(Constant.FLOAT_0)); break;
-        case FCONST_1       : frameState.fpush(appendConstant(Constant.FLOAT_1)); break;
-        case FCONST_2       : frameState.fpush(appendConstant(Constant.FLOAT_2)); break;
-        case DCONST_0       : frameState.dpush(appendConstant(Constant.DOUBLE_0)); break;
-        case DCONST_1       : frameState.dpush(appendConstant(Constant.DOUBLE_1)); break;
+        case ICONST_M1      : // fall through
+        case ICONST_0       : // fall through
+        case ICONST_1       : // fall through
+        case ICONST_2       : // fall through
+        case ICONST_3       : // fall through
+        case ICONST_4       : // fall through
+        case ICONST_5       : frameState.ipush(appendConstant(Constant.forInt(opcode - ICONST_0))); break;
+        case LCONST_0       : // fall through
+        case LCONST_1       : frameState.lpush(appendConstant(Constant.forLong(opcode - LCONST_0))); break;
+        case FCONST_0       : // fall through
+        case FCONST_1       : // fall through
+        case FCONST_2       : frameState.fpush(appendConstant(Constant.forFloat(opcode - FCONST_0))); break;
+        case DCONST_0       : // fall through
+        case DCONST_1       : frameState.dpush(appendConstant(Constant.forDouble(opcode - DCONST_0))); break;
         case BIPUSH         : frameState.ipush(appendConstant(Constant.forInt(stream.readByte()))); break;
         case SIPUSH         : frameState.ipush(appendConstant(Constant.forInt(stream.readShort()))); break;
         case LDC            : // fall through