changeset 5466:af07e798947d

lifted fast subtype check into checkcast snippets introduced ExplodeLoopNode for use in snippets to denote a loop that must be completely unrolled
author Doug Simon <doug.simon@oracle.com>
date Fri, 01 Jun 2012 11:10:49 +0200
parents 215981c9fd77
children 174eb2b7f6ba 785eeaaf340e
files graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotVMConfig.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/ri/HotSpotRuntime.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/ri/HotSpotTypeResolvedImpl.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/CheckCastSnippets.java graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/IndexedLocationNode.java graal/com.oracle.graal.snippets/src/com/oracle/graal/snippets/SnippetTemplate.java graal/com.oracle.graal.snippets/src/com/oracle/graal/snippets/nodes/ExplodeLoopNode.java graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/CheckCastTest.java src/share/vm/graal/graalCompiler.cpp src/share/vm/graal/graalCompilerToVM.cpp src/share/vm/graal/graalJavaAccess.hpp
diffstat 11 files changed, 484 insertions(+), 109 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotVMConfig.java	Fri Jun 01 11:08:44 2012 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotVMConfig.java	Fri Jun 01 11:10:49 2012 +0200
@@ -47,6 +47,9 @@
     public int vmPageSize;
     public int stackShadowPages;
     public int hubOffset;
+    public int superCheckOffsetOffset;
+    public int secondarySuperCacheOffset;
+    public int secondarySupersOffset;
     public int arrayLengthOffset;
     public int klassStateOffset;
     public int klassStateFullyInitialized;
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/ri/HotSpotRuntime.java	Fri Jun 01 11:08:44 2012 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/ri/HotSpotRuntime.java	Fri Jun 01 11:10:49 2012 +0200
@@ -28,16 +28,13 @@
 import java.util.*;
 
 import com.oracle.graal.compiler.*;
-import com.oracle.graal.compiler.phases.*;
 import com.oracle.graal.compiler.target.*;
 import com.oracle.graal.cri.*;
-import com.oracle.graal.debug.*;
 import com.oracle.graal.graph.*;
 import com.oracle.graal.hotspot.*;
 import com.oracle.graal.hotspot.Compiler;
 import com.oracle.graal.hotspot.nodes.*;
 import com.oracle.graal.hotspot.snippets.*;
-import com.oracle.graal.hotspot.snippets.CheckCastSnippets.Counter;
 import com.oracle.graal.hotspot.target.amd64.*;
 import com.oracle.graal.nodes.*;
 import com.oracle.graal.nodes.calc.*;
@@ -357,15 +354,13 @@
         } else if (n instanceof UnsafeLoadNode) {
             UnsafeLoadNode load = (UnsafeLoadNode) n;
             assert load.kind() != CiKind.Illegal;
-            IndexedLocationNode location = IndexedLocationNode.create(LocationNode.ANY_LOCATION, load.loadKind(), load.displacement(), load.offset(), graph);
-            location.setIndexScalingEnabled(false);
+            IndexedLocationNode location = IndexedLocationNode.create(LocationNode.ANY_LOCATION, load.loadKind(), load.displacement(), load.offset(), graph, false);
             ReadNode memoryRead = graph.add(new ReadNode(load.object(), location, load.stamp()));
             memoryRead.dependencies().add(tool.createNullCheckGuard(load.object(), StructuredGraph.INVALID_GRAPH_ID));
             graph.replaceFixedWithFixed(load, memoryRead);
         } else if (n instanceof UnsafeStoreNode) {
             UnsafeStoreNode store = (UnsafeStoreNode) n;
-            IndexedLocationNode location = IndexedLocationNode.create(LocationNode.ANY_LOCATION, store.storeKind(), store.displacement(), store.offset(), graph);
-            location.setIndexScalingEnabled(false);
+            IndexedLocationNode location = IndexedLocationNode.create(LocationNode.ANY_LOCATION, store.storeKind(), store.displacement(), store.offset(), graph, false);
             WriteNode write = graph.add(new WriteNode(store.object(), store.value(), location));
             write.setStateAfter(store.stateAfter());
             graph.replaceFixedWithFixed(store, write);
@@ -381,31 +376,7 @@
             graph.replaceFixed(objectClassNode, memoryRead);
         } else if (n instanceof CheckCastNode) {
             if (shouldLowerCheckcast(graph)) {
-                CheckCastNode checkcast = (CheckCastNode) n;
-                ValueNode hub = checkcast.targetClassInstruction();
-                ValueNode object = checkcast.object();
-                TypeCheckHints hints = new TypeCheckHints(checkcast.targetClass(), checkcast.profile(), tool.assumptions(), GraalOptions.CheckcastMinHintHitProbability, GraalOptions.CheckcastMaxHints);
-                HotSpotKlassOop[] hintHubs = new HotSpotKlassOop[hints.types.length];
-                for (int i = 0; i < hintHubs.length; i++) {
-                    hintHubs[i] = ((HotSpotType) hints.types[i]).klassOop();
-                }
-                Debug.log("Lowering checkcast in %s: node=%s, hintsHubs=%s, exact=%b", graph, checkcast, Arrays.toString(hints.types), hints.exact);
-
-                final Counter noHintsCounter;
-                if (GraalOptions.CheckcastCounters) {
-                    if (checkcast.targetClass() == null) {
-                        noHintsCounter = Counter.noHints_unknown;
-                    } else if (checkcast.targetClass().isInterface()) {
-                        noHintsCounter = Counter.noHints_iface;
-                    } else {
-                        noHintsCounter = Counter.noHints_class;
-                    }
-                } else {
-                    noHintsCounter = null;
-                }
-                boolean checkNull = !object.stamp().nonNull();
-                checkcasts.get(hintHubs.length, hints.exact, checkNull, noHintsCounter).instantiate(this, checkcast, checkcast, hub, object, hintHubs, noHintsCounter);
-                new DeadCodeEliminationPhase().apply(graph);
+                checkcasts.lower((CheckCastNode) n, tool);
             }
         } else {
             assert false : "Node implementing Lowerable not handled: " + n;
@@ -425,7 +396,7 @@
     }
 
     private IndexedLocationNode createArrayLocation(Graph graph, CiKind elementKind, ValueNode index) {
-        return IndexedLocationNode.create(LocationNode.getArrayLocation(elementKind), elementKind, config.getArrayOffset(elementKind), index, graph);
+        return IndexedLocationNode.create(LocationNode.getArrayLocation(elementKind), elementKind, config.getArrayOffset(elementKind), index, graph, true);
     }
 
     private SafeReadNode safeReadArrayLength(ValueNode array, long leafGraphId) {
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/ri/HotSpotTypeResolvedImpl.java	Fri Jun 01 11:08:44 2012 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/ri/HotSpotTypeResolvedImpl.java	Fri Jun 01 11:10:49 2012 +0200
@@ -26,6 +26,7 @@
 import java.lang.reflect.*;
 import java.util.*;
 
+import com.oracle.graal.hotspot.*;
 import com.oracle.max.cri.ci.*;
 import com.oracle.max.cri.ri.*;
 
@@ -42,6 +43,7 @@
     private boolean hasFinalizer;
     private boolean hasSubclass;
     private boolean hasFinalizableSubclass;
+    private int superCheckOffset;
     private boolean isArrayClass;
     private boolean isInstanceClass;
     private boolean isInterface;
@@ -264,7 +266,7 @@
         return this;
     }
 
-    // (dnsimon) this value may require identity semantics
+    // this value may require identity semantics so cache it
     private HotSpotKlassOop klassOopCache;
 
     @Override
@@ -274,4 +276,14 @@
         }
         return klassOopCache;
     }
+
+    private static final int SECONDARY_SUPER_CACHE_OFFSET = CompilerImpl.getInstance().getConfig().secondarySuperCacheOffset;
+
+    public boolean isPrimaryType() {
+        return SECONDARY_SUPER_CACHE_OFFSET != superCheckOffset;
+    }
+
+    public int superCheckOffset() {
+        return superCheckOffset;
+    }
 }
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/CheckCastSnippets.java	Fri Jun 01 11:08:44 2012 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/CheckCastSnippets.java	Fri Jun 01 11:10:49 2012 +0200
@@ -21,7 +21,9 @@
  * questions.
  */
 package com.oracle.graal.hotspot.snippets;
+import static com.oracle.graal.hotspot.snippets.ArrayCopySnippets.*;
 import static com.oracle.graal.hotspot.snippets.CheckCastSnippets.Counter.*;
+import static com.oracle.graal.hotspot.snippets.CheckCastSnippets.TemplateFlag.*;
 import static com.oracle.graal.snippets.SnippetTemplate.*;
 
 import java.io.*;
@@ -31,53 +33,173 @@
 import sun.misc.*;
 
 import com.oracle.graal.compiler.*;
+import com.oracle.graal.compiler.phases.*;
+import com.oracle.graal.cri.*;
+import com.oracle.graal.debug.*;
 import com.oracle.graal.graph.*;
 import com.oracle.graal.graph.Node.Fold;
 import com.oracle.graal.hotspot.*;
-import com.oracle.graal.hotspot.nodes.*;
 import com.oracle.graal.hotspot.ri.*;
 import com.oracle.graal.nodes.*;
 import com.oracle.graal.nodes.extended.*;
 import com.oracle.graal.nodes.java.*;
 import com.oracle.graal.snippets.*;
+import com.oracle.graal.snippets.nodes.*;
 import com.oracle.max.cri.ci.*;
 import com.oracle.max.cri.ri.*;
+import com.oracle.max.criutils.*;
 
 /**
- * Snippets used for lowering {@link CheckCastNode}s.
+ * Snippets used for implementing the type test of a checkcast instruction.
+ *
+ * The test first checks against the profiled types (if any) and then implements the
+ * checks described in paper <a href="http://dl.acm.org/citation.cfm?id=583821">
+ * Fast subtype checking in the HotSpot JVM</a> by Cliff Click and John Rose.
  */
 public class CheckCastSnippets implements SnippetsInterface {
 
     /**
-     * Checks that a given object is null or is a subtype of a given type.
-     *
-     * @param hub the hub of the type being checked against
-     * @param object the object whose type is being checked against {@code hub}
-     * @param hintHubs the hubs of objects that have been profiled during previous executions
-     * @param hintsAreExact specifies if {@code hintHubs} contains all subtypes of {@code hub}
-     * @param checkNull specifies if {@code object} may be null
-     * @return {@code object} if the type check succeeds
-     * @throws ClassCastException if the type check fails
+     * Type test used when the type being tested against is a restricted primary type.
      */
     @Snippet
-    public static Object checkcast(Object hub, Object object, Object[] hintHubs, boolean hintsAreExact, boolean checkNull, @SuppressWarnings("unused") Counter ignore) {
-        if (object == null) {
+    public static Object checkcastPrimary(Object hub, Object object, Object[] hintHubs, boolean hintsAreExact, boolean checkNull, int superCheckOffset) {
+        if (checkNull && object == null) {
             return object;
         }
-        Object objectHub = UnsafeLoadNode.load(object, 0, hubOffset(), CiKind.Object);
+        Object objectHub = UnsafeLoadNode.loadObject(object, 0, hubOffset(), true);
         // if we get an exact match: succeed immediately
+        ExplodeLoopNode.explodeLoop();
+        for (int i = 0; i < hintHubs.length; i++) {
+            Object hintHub = hintHubs[i];
+            if (hintHub == objectHub) {
+                return object;
+            }
+        }
+        if (hintsAreExact) {
+            DeoptimizeNode.deopt(RiDeoptAction.InvalidateReprofile, RiDeoptReason.ClassCastException);
+        } else {
+            if (UnsafeLoadNode.loadObject(objectHub, 0, superCheckOffset, true) != hub) {
+                DeoptimizeNode.deopt(RiDeoptAction.InvalidateReprofile, RiDeoptReason.ClassCastException);
+            }
+        }
+        return object;
+    }
+
+    /**
+     * Type test used when the type being tested against is a restricted secondary type.
+     */
+    @Snippet
+    public static Object checkcastSecondary(Object hub, Object object, Object[] hintHubs, boolean hintsAreExact, boolean checkNull) {
+        if (checkNull && object == null) {
+            return object;
+        }
+        Object objectHub = UnsafeLoadNode.loadObject(object, 0, hubOffset(), true);
+        // if we get an exact match: succeed immediately
+        ExplodeLoopNode.explodeLoop();
         for (int i = 0; i < hintHubs.length; i++) {
             Object hintHub = hintHubs[i];
             if (hintHub == objectHub) {
                 return object;
             }
         }
-        if (hintsAreExact || !TypeCheckSlowPath.check(objectHub, hub)) {
+        if (hintsAreExact) {
             DeoptimizeNode.deopt(RiDeoptAction.InvalidateReprofile, RiDeoptReason.ClassCastException);
+        } else {
+            if (!checkSecondarySubType(hub, objectHub)) {
+                DeoptimizeNode.deopt(RiDeoptAction.InvalidateReprofile, RiDeoptReason.ClassCastException);
+            }
+        }
+        return object;
+    }
+
+    /**
+     * Type test used when the type being tested against is not known at compile time (e.g. the type test
+     * in an object array store check).
+     */
+    @Snippet
+    public static Object checkcastUnknown(Object hub, Object object, Object[] hintHubs, boolean hintsAreExact, boolean checkNull) {
+        if (checkNull && object == null) {
+            return object;
+        }
+        Object objectHub = UnsafeLoadNode.loadObject(object, 0, hubOffset(), true);
+        // if we get an exact match: succeed immediately
+        ExplodeLoopNode.explodeLoop();
+        for (int i = 0; i < hintHubs.length; i++) {
+            Object hintHub = hintHubs[i];
+            if (hintHub == objectHub) {
+                return object;
+            }
+        }
+        if (hintsAreExact) {
+            DeoptimizeNode.deopt(RiDeoptAction.InvalidateReprofile, RiDeoptReason.ClassCastException);
+        } else {
+            if (!checkUnknownSubType(hub, objectHub)) {
+                DeoptimizeNode.deopt(RiDeoptAction.InvalidateReprofile, RiDeoptReason.ClassCastException);
+            }
         }
         return object;
     }
 
+    //This is used instead of a Java array read to avoid the array bounds check.
+    static Object loadNonNullObjectElement(Object array, int index) {
+        return UnsafeLoadNode.loadObject(array, arrayBaseOffset(CiKind.Object), index * arrayIndexScale(CiKind.Object), true);
+    }
+
+    static boolean checkSecondarySubType(Object t, Object s) {
+        // if (S.cache == T) return true
+        if (UnsafeLoadNode.loadObject(s, 0, secondarySuperCacheOffset(), true) == t) {
+            return true;
+        }
+
+        // if (T == S) return true
+        if (s == t) {
+            return true;
+        }
+
+        // if (S.scan_s_s_array(T)) { S.cache = T; return true; }
+        Object[] secondarySupers = UnsafeCastNode.cast(UnsafeLoadNode.loadObject(s, 0, secondarySupersOffset(), true), Object[].class);
+
+        for (int i = 0; i < secondarySupers.length; i++) {
+            if (t == loadNonNullObjectElement(secondarySupers, i)) {
+                DirectObjectStoreNode.store(s, secondarySuperCacheOffset(), 0, t);
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    static boolean checkUnknownSubType(Object t, Object s) {
+        // int off = T.offset
+        int superCheckOffset = UnsafeLoadNode.load(t, 0, superCheckOffsetOffset(), CiKind.Int);
+
+        // if (T = S[off]) return true
+        if (UnsafeLoadNode.loadObject(s, 0, superCheckOffset, true) == t) {
+            return true;
+        }
+
+        // if (off != &cache) return false
+        if (superCheckOffset != secondarySuperCacheOffset()) {
+            return false;
+        }
+
+        // if (T == S) return true
+        if (s == t) {
+            return true;
+        }
+
+        // if (S.scan_s_s_array(T)) { S.cache = T; return true; }
+        Object[] secondarySupers = UnsafeCastNode.cast(UnsafeLoadNode.loadObject(s, 0, secondarySupersOffset(), true), Object[].class);
+        for (int i = 0; i < secondarySupers.length; i++) {
+            if (t == loadNonNullObjectElement(secondarySupers, i)) {
+                DirectObjectStoreNode.store(s, secondarySuperCacheOffset(), 0, t);
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     /**
      * Counters for the various code paths through a type check.
      */
@@ -85,9 +207,7 @@
         hintsHit("hit a hint type"),
         hintsMissed("missed the hint types"),
         exact("tested type is (statically) final"),
-        noHints_class("profile information is not used (test type is a class)"),
-        noHints_iface("profile information is not used (test type is an interface)"),
-        noHints_unknown("test type is not a compile-time constant"),
+        noHints("profile information is not used"),
         isNull("object tested is null"),
         exception("type test failed with a ClassCastException");
 
@@ -124,21 +244,25 @@
         static final boolean ENABLED = GraalOptions.CheckcastCounters;
     }
 
+    /**
+     * Type test used when {@link GraalOptions#CheckcastCounters} is enabled.
+     */
     @Snippet
-    public static Object checkcastWithCounters(Object hub, Object object, Object[] hintHubs, boolean hintsAreExact, @SuppressWarnings("unused") boolean checkNull, Counter noHintsCounter) {
+    public static Object checkcastCounters(Object hub, Object object, Object[] hintHubs, boolean hintsAreExact) {
         if (object == null) {
             isNull.inc();
             return object;
         }
-        Object objectHub = UnsafeLoadNode.load(object, 0, hubOffset(), CiKind.Object);
+        Object objectHub = UnsafeLoadNode.loadObject(object, 0, hubOffset(), true);
         if (hintHubs.length == 0) {
-            noHintsCounter.inc();
-            if (!TypeCheckSlowPath.check(objectHub, hub)) {
+            noHints.inc();
+            if (!checkUnknownSubType(hub, objectHub)) {
                 exception.inc();
                 DeoptimizeNode.deopt(RiDeoptAction.InvalidateReprofile, RiDeoptReason.ClassCastException);
             }
         } else {
             // if we get an exact match: succeed immediately
+            ExplodeLoopNode.explodeLoop();
             for (int i = 0; i < hintHubs.length; i++) {
                 Object hintHub = hintHubs[i];
                 if (hintHub == objectHub) {
@@ -151,7 +275,7 @@
                 }
             }
             if (!hintsAreExact) {
-                if (!TypeCheckSlowPath.check(objectHub, hub)) {
+                if (!checkUnknownSubType(hub, objectHub)) {
                     exception.inc();
                     DeoptimizeNode.deopt(RiDeoptAction.InvalidateReprofile, RiDeoptReason.ClassCastException);
                 } else {
@@ -166,6 +290,21 @@
     }
 
     @Fold
+    private static int superCheckOffsetOffset() {
+        return CompilerImpl.getInstance().getConfig().superCheckOffsetOffset;
+    }
+
+    @Fold
+    private static int secondarySuperCacheOffset() {
+        return CompilerImpl.getInstance().getConfig().secondarySuperCacheOffset;
+    }
+
+    @Fold
+    private static int secondarySupersOffset() {
+        return CompilerImpl.getInstance().getConfig().secondarySupersOffset;
+    }
+
+    @Fold
     private static int hubOffset() {
         return CompilerImpl.getInstance().getConfig().hubOffset;
     }
@@ -205,54 +344,148 @@
         }
     }
 
+    public enum TemplateFlag {
+        CHECK_NULL,
+        EXACT_HINTS,
+        COUNTERS,
+        PRIMARY_SUPER,
+        SECONDARY_SUPER,
+        UNKNOWN_SUPER;
+
+        public int bit(boolean value) {
+            if (value) {
+                return bit();
+            }
+            return 0;
+        }
+
+        public boolean bool(int flags) {
+            return (flags & bit()) != 0;
+        }
+
+        static final int FLAGS_BITS = values().length;
+        static final int FLAGS_MASK = (1 << FLAGS_BITS) - 1;
+
+        static final int NHINTS_SHIFT = FLAGS_BITS;
+        static final int NHINTS_BITS = 3;
+        static final int SUPER_CHECK_OFFSET_SHIFT = NHINTS_SHIFT + NHINTS_BITS;
+
+        public int bit() {
+            return 1 << ordinal();
+        }
+    }
+
     /**
      * Templates for partially specialized checkcast snippet graphs.
      */
     public static class Templates {
 
         private final ConcurrentHashMap<Integer, SnippetTemplate> templates;
-        private final RiResolvedMethod method;
+        private final RiResolvedMethod counters;
+        private final RiResolvedMethod primary;
+        private final RiResolvedMethod secondary;
+        private final RiResolvedMethod unknown;
         private final RiRuntime runtime;
 
         public Templates(RiRuntime runtime) {
             this.runtime = runtime;
             this.templates = new ConcurrentHashMap<>();
             try {
-                Class[] parameterTypes = {Object.class, Object.class, Object[].class, boolean.class, boolean.class, Counter.class};
-                String name = GraalOptions.CheckcastCounters ? "checkcastWithCounters" : "checkcast";
-                method = runtime.getRiMethod(CheckCastSnippets.class.getDeclaredMethod(name, parameterTypes));
+                primary = runtime.getRiMethod(CheckCastSnippets.class.getDeclaredMethod("checkcastPrimary", Object.class, Object.class, Object[].class, boolean.class, boolean.class, int.class));
+                secondary = runtime.getRiMethod(CheckCastSnippets.class.getDeclaredMethod("checkcastSecondary", Object.class, Object.class, Object[].class, boolean.class, boolean.class));
+                unknown = runtime.getRiMethod(CheckCastSnippets.class.getDeclaredMethod("checkcastUnknown", Object.class, Object.class, Object[].class, boolean.class, boolean.class));
+                counters = runtime.getRiMethod(CheckCastSnippets.class.getDeclaredMethod("checkcastCounters", Object.class, Object.class, Object[].class, boolean.class));
             } catch (NoSuchMethodException e) {
                 throw new GraalInternalError(e);
             }
         }
 
         /**
-         * Gets a checkcast snippet specialized for a given set of inputs.
+         * Interface for lazily creating a snippet template.
          */
-        public SnippetTemplate get(int nHints, boolean isExact, boolean checkNull, Counter noHintsCounter) {
-            Integer key = key(nHints, isExact, checkNull);
+        abstract static class Factory {
+            abstract SnippetTemplate create(HotSpotKlassOop[] hints, int flags);
+        }
+
+        /**
+         * Gets a template from the template cache, creating and installing it first if necessary.
+         */
+        private SnippetTemplate getTemplate(int nHints, int flags, int superCheckOffset, Factory factory) {
+            assert (flags & ~FLAGS_MASK) == 0;
+            assert nHints >= 0 && nHints < (1 << NHINTS_BITS) - 1 : "nHints out of range";
+            assert superCheckOffset >= 0 && superCheckOffset == ((superCheckOffset << SUPER_CHECK_OFFSET_SHIFT) >>> SUPER_CHECK_OFFSET_SHIFT) : "superCheckOffset out of range";
+            Integer key = superCheckOffset << SUPER_CHECK_OFFSET_SHIFT | nHints << NHINTS_SHIFT | flags;
             SnippetTemplate result = templates.get(key);
             if (result == null) {
                 HotSpotKlassOop[] hints = new HotSpotKlassOop[nHints];
-                Arrays.fill(hints, new HotSpotKlassOop(null, Templates.class));
-                result = SnippetTemplate.create(runtime, method, _, _, hints, isExact, checkNull, noHintsCounter);
+                for (int i = 0; i < hints.length; i++) {
+                    hints[i] = new HotSpotKlassOop(null, Templates.class);
+                }
+                result = factory.create(hints, flags);
+                //System.err.println(result);
                 templates.put(key, result);
             }
             return result;
         }
 
         /**
-         * Creates a canonical key for a combination of specialization parameters.
+         * Lowers a checkcast node.
          */
-        private static Integer key(int nHints, boolean isExact, boolean checkNull) {
-            int key = nHints << 2;
-            if (isExact) {
-                key |= 2;
+        public void lower(CheckCastNode checkcast, CiLoweringTool tool) {
+            StructuredGraph graph = (StructuredGraph) checkcast.graph();
+            ValueNode hub = checkcast.targetClassInstruction();
+            ValueNode object = checkcast.object();
+            TypeCheckHints hints = new TypeCheckHints(checkcast.targetClass(), checkcast.profile(), tool.assumptions(), GraalOptions.CheckcastMinHintHitProbability, GraalOptions.CheckcastMaxHints);
+            HotSpotKlassOop[] hintHubs = new HotSpotKlassOop[hints.types.length];
+            for (int i = 0; i < hintHubs.length; i++) {
+                hintHubs[i] = ((HotSpotType) hints.types[i]).klassOop();
             }
-            if (checkNull) {
-                key |= 1;
+            Debug.log("Lowering checkcast in %s: node=%s, hintsHubs=%s, exact=%b", graph, checkcast, Arrays.toString(hints.types), hints.exact);
+
+            final HotSpotTypeResolvedImpl target = (HotSpotTypeResolvedImpl) checkcast.targetClass();
+            int flags = EXACT_HINTS.bit(hints.exact) | CHECK_NULL.bit(!object.stamp().nonNull());
+            if (GraalOptions.CheckcastCounters) {
+                SnippetTemplate template = getTemplate(hintHubs.length, flags | COUNTERS.bit(), 0, new Factory() {
+                    @SuppressWarnings("hiding")
+                    @Override
+                    SnippetTemplate create(HotSpotKlassOop[] hints, int flags) {
+                        // checkcastCounters(Object hub, Object object, Object[] hintHubs, boolean hintsAreExact)
+                        return SnippetTemplate.create(runtime, counters, _, _, hints, EXACT_HINTS.bool(flags));
+                    }
+                });
+                template.instantiate(runtime, checkcast, checkcast, hub, object, hintHubs, hints.exact);
+            } else if (target == null) {
+                SnippetTemplate template = getTemplate(hintHubs.length, flags | UNKNOWN_SUPER.bit(), 0, new Factory() {
+                    @SuppressWarnings("hiding")
+                    @Override
+                    SnippetTemplate create(HotSpotKlassOop[] hints, int flags) {
+                        // checkcastUnknown(Object hub, Object object, Object[] hintHubs, boolean hintsAreExact, boolean checkNull)
+                        return SnippetTemplate.create(runtime, unknown, _, _, hints, EXACT_HINTS.bool(flags), CHECK_NULL.bool(flags));
+                    }
+                });
+                template.instantiate(runtime, checkcast, checkcast, hub, object, hintHubs);
+            } else if (target.isPrimaryType()) {
+                SnippetTemplate template = getTemplate(hintHubs.length, flags | PRIMARY_SUPER.bit(), target.superCheckOffset(), new Factory() {
+                    @SuppressWarnings("hiding")
+                    @Override
+                    SnippetTemplate create(HotSpotKlassOop[] hints, int flags) {
+                        // checkcastPrimary(Object hub, Object object, Object[] hintHubs, boolean hintsAreExact, boolean checkNull, int superCheckOffset)
+                        return SnippetTemplate.create(runtime, primary, _, _, hints, EXACT_HINTS.bool(flags), CHECK_NULL.bool(flags), target.superCheckOffset());
+                    }
+                });
+                template.instantiate(runtime, checkcast, checkcast, hub, object, hintHubs);
+            } else {
+                SnippetTemplate template = getTemplate(hintHubs.length, flags | SECONDARY_SUPER.bit(), 0, new Factory() {
+                    @SuppressWarnings("hiding")
+                    @Override
+                    SnippetTemplate create(HotSpotKlassOop[] hints, int flags) {
+                        // checkcastSecondary(Object hub, Object object, Object[] hintHubs, boolean hintsAreExact, boolean checkNull)
+                        return SnippetTemplate.create(runtime, secondary, _, _, hints, EXACT_HINTS.bool(flags), CHECK_NULL.bool(flags));
+                    }
+                });
+                template.instantiate(runtime, checkcast, checkcast, hub, object, hintHubs);
             }
-            return key;
+            new DeadCodeEliminationPhase().apply(graph);
         }
     }
 }
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/IndexedLocationNode.java	Fri Jun 01 11:08:44 2012 +0200
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/IndexedLocationNode.java	Fri Jun 01 11:10:49 2012 +0200
@@ -27,10 +27,20 @@
 import com.oracle.graal.nodes.*;
 import com.oracle.graal.nodes.spi.*;
 
+/**
+ * Extension of a {@linkplain LocationNode location} to include a scaled index or an additional offset.
+ */
 public final class IndexedLocationNode extends LocationNode implements LIRLowerable, Canonicalizable {
+
+    /**
+     * An offset or index depending on whether {@link #indexScalingEnabled} is true or false respectively.
+     */
     @Input private ValueNode index;
-    private boolean indexScalingEnabled;
+    private final boolean indexScalingEnabled;
 
+    /**
+     * Gets the index or offset of this location.
+     */
     public ValueNode index() {
         return index;
     }
@@ -46,17 +56,6 @@
         return indexScalingEnabled;
     }
 
-    /**
-     * Enables or disables scaling of the index by the value kind's size. Has no effect if the index input is not used.
-     */
-    public void setIndexScalingEnabled(boolean enable) {
-        this.indexScalingEnabled = enable;
-    }
-
-    public static IndexedLocationNode create(Object identity, CiKind kind, int displacement, ValueNode index, Graph graph) {
-        return create(identity, kind, displacement, index, graph, true);
-    }
-
     public static IndexedLocationNode create(Object identity, CiKind kind, int displacement, ValueNode index, Graph graph, boolean indexScalingEnabled) {
         return graph.unique(new IndexedLocationNode(identity, kind, index, displacement, indexScalingEnabled));
     }
--- a/graal/com.oracle.graal.snippets/src/com/oracle/graal/snippets/SnippetTemplate.java	Fri Jun 01 11:08:44 2012 +0200
+++ b/graal/com.oracle.graal.snippets/src/com/oracle/graal/snippets/SnippetTemplate.java	Fri Jun 01 11:10:49 2012 +0200
@@ -35,6 +35,7 @@
 import com.oracle.graal.lir.cfg.*;
 import com.oracle.graal.nodes.*;
 import com.oracle.graal.nodes.util.*;
+import com.oracle.graal.snippets.nodes.*;
 import com.oracle.max.cri.ci.*;
 import com.oracle.max.cri.ri.*;
 
@@ -115,24 +116,41 @@
                     new CanonicalizerPhase(null, runtime, null, 0, immutabilityPredicate).apply(snippetCopy);
                 }
 
-                // Explode all loops in the snippet
-                if (snippetCopy.hasLoops()) {
-                    ControlFlowGraph cfg = ControlFlowGraph.compute(snippetCopy, true, true, false, false);
-                    for (Loop loop : cfg.getLoops()) {
-                        LoopBeginNode loopBegin = loop.loopBegin();
-                        SuperBlock wholeLoop = LoopTransformUtil.wholeLoop(loop);
-                        Debug.dump(snippetCopy, "Before exploding loop %s", loopBegin);
-                        int peel = 0;
-                        while (!loopBegin.isDeleted()) {
-                            int mark = snippetCopy.getMark();
-                            LoopTransformUtil.peel(loop, wholeLoop);
-                            Debug.dump(snippetCopy, "After peel %d", peel);
-                            new CanonicalizerPhase(null, runtime, null, mark, immutabilityPredicate).apply(snippetCopy);
-                            peel++;
+                boolean exploded = false;
+                do {
+                    exploded = false;
+                    for (Node node : snippetCopy.getNodes()) {
+                        if (node instanceof ExplodeLoopNode) {
+                            final ExplodeLoopNode explodeLoop = (ExplodeLoopNode) node;
+                            LoopBeginNode loopBegin = explodeLoop.findLoopBegin();
+                            ControlFlowGraph cfg = ControlFlowGraph.compute(snippetCopy, true, true, false, false);
+                            for (Loop loop : cfg.getLoops()) {
+                                if (loop.loopBegin() == loopBegin) {
+                                    SuperBlock wholeLoop = LoopTransformUtil.wholeLoop(loop);
+                                    Debug.dump(snippetCopy, "Before exploding loop %s", loopBegin);
+                                    int peel = 0;
+                                    while (!loopBegin.isDeleted()) {
+                                        int mark = snippetCopy.getMark();
+                                        LoopTransformUtil.peel(loop, wholeLoop);
+                                        Debug.dump(snippetCopy, "After peel %d", peel);
+                                        new CanonicalizerPhase(null, runtime, null, mark, immutabilityPredicate).apply(snippetCopy);
+                                        peel++;
+                                    }
+                                    Debug.dump(snippetCopy, "After exploding loop %s", loopBegin);
+                                    exploded = true;
+                                    break;
+                                }
+                            }
+
+                            FixedNode explodeLoopNext = explodeLoop.next();
+                            explodeLoop.clearSuccessors();
+                            explodeLoop.replaceAtPredecessors(explodeLoopNext);
+                            explodeLoop.replaceAtUsages(null);
+                            GraphUtil.killCFG(explodeLoop);
+                            break;
                         }
-                        Debug.dump(snippetCopy, "After exploding loop %s", loopBegin);
                     }
-                }
+                } while (exploded);
 
                 // Remove all frame states from inlined snippet graph. Snippets must be atomic (i.e. free
                 // of side-effects that prevent deoptimizing to a point before the snippet).
@@ -245,8 +263,13 @@
             } else if (arg instanceof ValueNode) {
                 assert param instanceof LocalNode;
                 replacements.put((LocalNode) param, (ValueNode) arg);
+            } else if (param instanceof ConstantNode) {
+                replacements.put((ConstantNode) param, ConstantNode.forObject(arg, runtime, replaceeGraph));
             } else {
-                replacements.put((ConstantNode) param, ConstantNode.forObject(arg, runtime, replaceeGraph));
+                if (!param.equals(arg)) {
+                    System.exit(1);
+                }
+                assert param.equals(arg) : param + " != " + arg;
             }
         }
         return replacements;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.snippets/src/com/oracle/graal/snippets/nodes/ExplodeLoopNode.java	Fri Jun 01 11:10:49 2012 +0200
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.graal.snippets.nodes;
+
+import java.util.*;
+
+import com.oracle.graal.graph.*;
+import com.oracle.graal.nodes.*;
+import com.oracle.graal.nodes.type.*;
+
+/**
+ * Placeholder node to denote to snippet preparation that the following loop
+ * must be completely unrolled.
+ */
+public final class ExplodeLoopNode extends FixedWithNextNode {
+
+    public ExplodeLoopNode() {
+        super(StampFactory.forVoid());
+    }
+
+    public LoopBeginNode findLoopBegin() {
+        Node next = next();
+        while (!(next instanceof LoopBeginNode)) {
+            assert next != null : "cannot find loop after " + this;
+            Iterator< ? extends Node> successors = next.cfgSuccessors().iterator();
+            assert successors.hasNext() : "cannot find loop after " + this;
+            next = successors.next();
+            assert !successors.hasNext() : "should only be straight line code after " + this;
+        }
+        return (LoopBeginNode) next;
+    }
+
+    /**
+     * A call to this method must be placed immediately prior to the loop that is to be exploded.
+     */
+    @NodeIntrinsic
+    public static void explodeLoop() {
+        throw new UnsupportedOperationException("This method may only be compiled with the Graal compiler");
+    }
+}
--- a/graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/CheckCastTest.java	Fri Jun 01 11:08:44 2012 +0200
+++ b/graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/CheckCastTest.java	Fri Jun 01 11:10:49 2012 +0200
@@ -34,13 +34,27 @@
  */
 public class CheckCastTest extends TypeCheckTest {
 
+    /**
+     * Enables making the target type "unknown" at compile time.
+     */
+    boolean unknown;
+
     @Override
     protected void replaceProfile(StructuredGraph graph, RiTypeProfile profile) {
         CheckCastNode ccn = graph.getNodes(CheckCastNode.class).first();
         if (ccn != null) {
-            CheckCastNode ccnNew = graph.add(new CheckCastNode(ccn.targetClassInstruction(), ccn.targetClass(), ccn.object(), profile));
+            RiResolvedType targetClass = unknown ? null : ccn.targetClass();
+            CheckCastNode ccnNew = graph.add(new CheckCastNode(ccn.targetClassInstruction(), targetClass, ccn.object(), profile));
             graph.replaceFixedWithFixed(ccn, ccnNew);
         }
+        unknown = false;
+    }
+
+    @Override
+    protected void test(String name, RiTypeProfile profile, Object... args) {
+        super.test(name, profile, args);
+        unknown = true;
+        super.test(name, profile, args);
     }
 
     @Test
@@ -89,6 +103,19 @@
         test("asStringExt", profile(String.class), 111);
     }
 
+    @Test
+    public void test7() {
+        Throwable throwable = new Exception();
+        test("asThrowable",   profile(),                             throwable);
+        test("asThrowable",   profile(Throwable.class),              throwable);
+        test("asThrowable",   profile(Exception.class, Error.class), throwable);
+    }
+
+    @Test
+    public void test8() {
+        test("arrayFill", profile(), new Object[100], "111");
+    }
+
     public static Number asNumber(Object o) {
         return (Number) o;
     }
@@ -97,6 +124,14 @@
         return (String) o;
     }
 
+    public static Throwable asThrowable(Object o) {
+        return (Throwable) o;
+    }
+
+    public static ValueNode asValueNode(Object o) {
+        return (ValueNode) o;
+    }
+
     public static Number asNumberExt(Object o) {
         Number n = (Number) o;
         return n.intValue() + 10;
@@ -107,15 +142,48 @@
         return "#" + s;
     }
 
-    @Test
-    public void test7() {
-        test("arrayFill", profile(), new Object[100], "111");
-    }
-
     public static Object[] arrayFill(Object[] arr, Object value) {
         for (int i = 0; i < arr.length; i++) {
             arr[i] = value;
         }
         return arr;
     }
+
+    static class Depth1 implements Cloneable {}
+    static class Depth2 extends Depth1 {}
+    static class Depth3 extends Depth2 {}
+    static class Depth4 extends Depth3 {}
+    static class Depth5 extends Depth4 {}
+    static class Depth6 extends Depth5 {}
+    static class Depth7 extends Depth6 {}
+    static class Depth8 extends Depth7 {}
+    static class Depth9 extends Depth8 {}
+    static class Depth10 extends Depth9 {}
+    static class Depth11 extends Depth10 {}
+    static class Depth12 extends Depth11 {}
+    static class Depth13 extends Depth12 {}
+
+    public static Depth12 asDepth12(Object o) {
+        return (Depth12) o;
+    }
+
+    public static Depth12[][] asDepth12Arr(Object o) {
+        return (Depth12[][]) o;
+    }
+
+    public static Cloneable asCloneable(Object o) {
+        return (Cloneable) o;
+    }
+
+    @Test
+    public void test9() {
+        Object o = new Depth13();
+        test("asDepth12",   profile(), o);
+    }
+
+    @Test
+    public void test10() {
+        Object o = new Depth13[3][];
+        test("asDepth12Arr",   profile(), o);
+    }
 }
--- a/src/share/vm/graal/graalCompiler.cpp	Fri Jun 01 11:08:44 2012 +0200
+++ b/src/share/vm/graal/graalCompiler.cpp	Fri Jun 01 11:10:49 2012 +0200
@@ -275,6 +275,7 @@
   HotSpotTypeResolved::set_simpleName(obj, name());
   HotSpotTypeResolved::set_accessFlags(obj, klass->access_flags().as_int());
   HotSpotTypeResolved::set_isInterface(obj, klass->is_interface());
+  HotSpotTypeResolved::set_superCheckOffset(obj, klass->super_check_offset());
   HotSpotTypeResolved::set_isInstanceClass(obj, klass->oop_is_instance());
 
   if (klass->oop_is_javaArray()) {
--- a/src/share/vm/graal/graalCompilerToVM.cpp	Fri Jun 01 11:08:44 2012 +0200
+++ b/src/share/vm/graal/graalCompilerToVM.cpp	Fri Jun 01 11:10:49 2012 +0200
@@ -789,6 +789,9 @@
   set_int(env, config, "vmPageSize", os::vm_page_size());
   set_int(env, config, "stackShadowPages", StackShadowPages);
   set_int(env, config, "hubOffset", oopDesc::klass_offset_in_bytes());
+  set_int(env, config, "superCheckOffsetOffset", in_bytes(Klass::super_check_offset_offset()));
+  set_int(env, config, "secondarySuperCacheOffset", in_bytes(Klass::secondary_super_cache_offset()));
+  set_int(env, config, "secondarySupersOffset", in_bytes(Klass::secondary_supers_offset()));
   set_int(env, config, "arrayLengthOffset", arrayOopDesc::length_offset_in_bytes());
   set_int(env, config, "klassStateOffset", in_bytes(instanceKlass::init_state_offset()));
   set_int(env, config, "klassStateFullyInitialized", (int)instanceKlass::fully_initialized);
@@ -1021,6 +1024,7 @@
   if (nm == NULL || !nm->is_alive()) {
     THROW_0(vmSymbols::MethodInvalidatedException());
   }
+
   JavaCalls::call(&result, mh, nm, &jca, CHECK_NULL);
 
   if (jap.get_ret_type() == T_VOID) {
--- a/src/share/vm/graal/graalJavaAccess.hpp	Fri Jun 01 11:08:44 2012 +0200
+++ b/src/share/vm/graal/graalJavaAccess.hpp	Fri Jun 01 11:10:49 2012 +0200
@@ -53,6 +53,7 @@
     boolean_field(HotSpotTypeResolved, hasFinalizer)                                    \
     boolean_field(HotSpotTypeResolved, hasSubclass)                                     \
     boolean_field(HotSpotTypeResolved, hasFinalizableSubclass)                          \
+    int_field(HotSpotTypeResolved, superCheckOffset)                                    \
     boolean_field(HotSpotTypeResolved, isArrayClass)                                    \
     boolean_field(HotSpotTypeResolved, isInstanceClass)                                 \
     boolean_field(HotSpotTypeResolved, isInterface)                                     \