changeset 16193:48f86e56eb75

simplified Debug verification API
author Doug Simon <doug.simon@oracle.com>
date Thu, 19 Jun 2014 17:13:10 +0200
parents 8588f460fa9b
children 7fb828b4c32b
files graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugVerifyHandler.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java graal/com.oracle.graal.phases/src/com/oracle/graal/phases/BasePhase.java graal/com.oracle.graal.printer/src/com/oracle/graal/printer/NoDeadCodeVerifyHandler.java
diffstat 5 files changed, 66 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java	Thu Jun 19 15:35:24 2014 +0200
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java	Thu Jun 19 17:13:10 2014 +0200
@@ -90,7 +90,7 @@
      * Determines if verification is enabled in the current method, regardless of the
      * {@linkplain Debug#currentScope() current debug scope}.
      *
-     * @see Debug#verify(Object, String)
+     * @see Debug#verify(Object, Object)
      */
     public static boolean isVerifyEnabledForMethod() {
         if (!ENABLED) {
@@ -107,7 +107,7 @@
      * Determines if verification is enabled in the {@linkplain Debug#currentScope() current debug
      * scope}.
      *
-     * @see Debug#verify(Object, String)
+     * @see Debug#verify(Object, Object)
      */
     public static boolean isVerifyEnabled() {
         return ENABLED && DebugScope.getInstance().isVerifyEnabled();
@@ -490,12 +490,14 @@
      * Calls all {@link DebugVerifyHandler}s in the current {@linkplain DebugScope#getConfig()
      * config} to perform verification on a given object.
      *
-     * @param object the object to be verified
-     * @param msg denoting context of verification
+     * @param object object to verify
+     * @param context object describing the context of verification
+     *
+     * @see DebugVerifyHandler#verify(Object, Object...)
      */
-    public static void verify(Object object, String msg) {
+    public static void verify(Object object, Object context) {
         if (ENABLED && DebugScope.getInstance().isVerifyEnabled()) {
-            DebugScope.getInstance().verify(object, msg);
+            DebugScope.getInstance().verify(object, context);
         }
     }
 
@@ -503,45 +505,29 @@
      * Calls all {@link DebugVerifyHandler}s in the current {@linkplain DebugScope#getConfig()
      * config} to perform verification on a given object.
      *
-     * @param object the object to be verified
-     * @param format format string for message denoting context of verification
-     * @param arg argument to format string
+     * @param object object to verify
+     * @param context1 first object describing the context of verification
+     * @param context2 second object describing the context of verification
+     *
+     * @see DebugVerifyHandler#verify(Object, Object...)
      */
-    public static void verify(Object object, String format, Object arg) {
+    public static void verify(Object object, Object context1, Object context2) {
         if (ENABLED && DebugScope.getInstance().isVerifyEnabled()) {
-            DebugScope.getInstance().verify(object, format, arg);
-        }
-    }
-
-    /**
-     * @see Debug#verify(Object, String, Object)
-     */
-    public static void verify(Object object, String format, Object arg1, Object arg2) {
-        if (ENABLED && DebugScope.getInstance().isVerifyEnabled()) {
-            DebugScope.getInstance().verify(object, format, arg1, arg2);
+            DebugScope.getInstance().verify(object, context1, context2);
         }
     }
 
     /**
-     * @see Debug#verify(Object, String, Object)
-     */
-    public static void verify(Object object, String format, Object arg1, Object arg2, Object arg3) {
-        if (ENABLED && DebugScope.getInstance().isVerifyEnabled()) {
-            DebugScope.getInstance().verify(object, format, arg1, arg2, arg3);
-        }
-    }
-
-    /**
-     * This override exists to catch cases when {@link #verify(Object, String, Object)} is called
-     * with one argument bound to a varargs method parameter. It will bind to this method instead of
-     * the single arg variant and produce a deprecation warning instead of silently wrapping the
+     * This override exists to catch cases when {@link #verify(Object, Object)} is called with one
+     * argument bound to a varargs method parameter. It will bind to this method instead of the
+     * single arg variant and produce a deprecation warning instead of silently wrapping the
      * Object[] inside of another Object[].
      */
     @Deprecated
-    public static void verify(Object object, String format, Object[] args) {
+    public static void verify(Object object, Object[] args) {
         assert false : "shouldn't use this";
         if (ENABLED && DebugScope.getInstance().isVerifyEnabled()) {
-            DebugScope.getInstance().verify(object, format, args);
+            DebugScope.getInstance().verify(object, args);
         }
     }
 
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugVerifyHandler.java	Thu Jun 19 15:35:24 2014 +0200
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugVerifyHandler.java	Thu Jun 19 17:13:10 2014 +0200
@@ -29,6 +29,27 @@
 
     /**
      * Verifies that a given object satisfies some invariants.
+     *
+     * @param object object to verify
+     * @param context object(s) describing the context of verification
      */
-    void verify(Object object, String message);
+    void verify(Object object, Object... context);
+
+    /**
+     * Extracts the first object of a given type from a verification input object.
+     */
+    default <T> T extract(Class<T> type, Object input) {
+        if (type.isInstance(input)) {
+            return type.cast(input);
+        }
+        if (input instanceof Object[]) {
+            for (Object nestedContext : (Object[]) input) {
+                T object = extract(type, nestedContext);
+                if (object != null) {
+                    return object;
+                }
+            }
+        }
+        return null;
+    }
 }
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java	Thu Jun 19 15:35:24 2014 +0200
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java	Thu Jun 19 17:13:10 2014 +0200
@@ -218,15 +218,14 @@
     }
 
     /**
-     * @see Debug#verify(Object, String, Object)
+     * @see Debug#verify(Object, Object)
      */
-    public void verify(Object object, String formatString, Object... args) {
+    public void verify(Object object, Object... ctx) {
         if (isVerifyEnabled()) {
             DebugConfig config = getConfig();
             if (config != null) {
-                String message = String.format(formatString, args);
                 for (DebugVerifyHandler handler : config.verifyHandlers()) {
-                    handler.verify(object, message);
+                    handler.verify(object, ctx);
                 }
             }
         }
--- a/graal/com.oracle.graal.phases/src/com/oracle/graal/phases/BasePhase.java	Thu Jun 19 15:35:24 2014 +0200
+++ b/graal/com.oracle.graal.phases/src/com/oracle/graal/phases/BasePhase.java	Thu Jun 19 17:13:10 2014 +0200
@@ -94,14 +94,14 @@
 
     public final void apply(final StructuredGraph graph, final C context, final boolean dumpGraph) {
         try (TimerCloseable a = timer.start(); Scope s = Debug.scope(getClass(), this); Closeable c = memUseTracker.start()) {
-            BasePhase.this.run(graph, context);
+            this.run(graph, context);
             executionCount.increment();
             inputNodesCount.add(graph.getNodeCount());
             if (dumpGraph && Debug.isDumpEnabled()) {
                 Debug.dump(graph, "After phase %s", getName());
             }
             if (Debug.isVerifyEnabled()) {
-                Debug.verify(graph, "After phase %s", getName());
+                Debug.verify(graph, this, "After phase " + getName());
             }
             assert graph.verify();
         } catch (Throwable t) {
--- a/graal/com.oracle.graal.printer/src/com/oracle/graal/printer/NoDeadCodeVerifyHandler.java	Thu Jun 19 15:35:24 2014 +0200
+++ b/graal/com.oracle.graal.printer/src/com/oracle/graal/printer/NoDeadCodeVerifyHandler.java	Thu Jun 19 17:13:10 2014 +0200
@@ -28,6 +28,7 @@
 import com.oracle.graal.debug.*;
 import com.oracle.graal.graph.*;
 import com.oracle.graal.nodes.*;
+import com.oracle.graal.phases.*;
 import com.oracle.graal.phases.common.*;
 
 /**
@@ -35,13 +36,27 @@
  */
 public class NoDeadCodeVerifyHandler implements DebugVerifyHandler {
 
-    public void verify(Object object, String message) {
-        if (object instanceof StructuredGraph) {
-            StructuredGraph graph = (StructuredGraph) object;
+    private static final Collection<Class<? extends Phase>> excludedPhases = Arrays.asList(FloatingReadPhase.class);
+
+    private static boolean isExcluded(Phase phase) {
+        for (Class<? extends Phase> c : excludedPhases) {
+            if (c.isAssignableFrom(phase.getClass())) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public void verify(Object object, Object... context) {
+        StructuredGraph graph = extract(StructuredGraph.class, object);
+        Phase phase = extract(Phase.class, context);
+        String message = extract(String.class, context);
+        if (graph != null && (phase == null || !isExcluded(phase))) {
             List<Node> removed = new ArrayList<>();
             new DeadCodeEliminationPhase(removed).run(graph);
             if (!removed.isEmpty()) {
-                throw new GraalInternalError("%s: found dead nodes in %s: %s", message, graph, removed);
+                String prefix = message == null ? "" : message + ": ";
+                throw new GraalInternalError("%sfound dead nodes in %s: %s", prefix, graph, removed);
             }
         }
     }