changeset 21424:fe76bf3867f3

Make CachedGraph generic. Clean up Graph#copy methods.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Tue, 19 May 2015 15:02:51 +0200
parents fdf9166e575f
children a9b3d1cfdd42
files graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/phases/GraphChangeMonitoringPhase.java graal/com.oracle.graal.graph/src/com/oracle/graal/graph/CachedGraph.java graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Graph.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/ObjectCloneNode.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/arraycopy/ArrayCopyNode.java graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/StructuredGraph.java graal/com.oracle.graal.phases.common/src/com/oracle/graal/phases/common/inlining/info/elem/InlineableGraph.java graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/SnippetTemplate.java graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/nodes/MacroNode.java
diffstat 9 files changed, 43 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/phases/GraphChangeMonitoringPhase.java	Tue May 19 14:24:47 2015 +0200
+++ b/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/phases/GraphChangeMonitoringPhase.java	Tue May 19 15:02:51 2015 +0200
@@ -66,7 +66,7 @@
          * having their inputs change are the main interesting differences.
          */
         HashSetNodeEventListener listener = new HashSetNodeEventListener().exclude(NodeEvent.NODE_ADDED);
-        StructuredGraph graphCopy = graph.copy();
+        StructuredGraph graphCopy = (StructuredGraph) graph.copy();
         try (NodeEventScope s = graphCopy.trackNodeEvents(listener)) {
             try (Scope s2 = Debug.sandbox("WithoutMonitoring", null)) {
                 super.run(graphCopy, context);
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/CachedGraph.java	Tue May 19 14:24:47 2015 +0200
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/CachedGraph.java	Tue May 19 15:02:51 2015 +0200
@@ -29,22 +29,32 @@
  * This class is a container of a graph that needs to be readonly and optionally a lazily created
  * mutable copy of the graph.
  */
-public class CachedGraph {
+public final class CachedGraph<G extends Graph> {
 
-    private final Graph readonlyCopy;
-    private Graph mutableCopy;
+    private final G readonlyCopy;
+    private G mutableCopy;
 
-    public CachedGraph(Graph readonlyCopy) {
+    private CachedGraph(G readonlyCopy, G mutableCopy) {
         this.readonlyCopy = readonlyCopy;
+        this.mutableCopy = mutableCopy;
     }
 
-    public Graph getReadonlyCopy() {
+    public static <G extends Graph> CachedGraph<G> fromReadonlyCopy(G graph) {
+        return new CachedGraph<>(graph, null);
+    }
+
+    public static <G extends Graph> CachedGraph<G> fromMutableCopy(G graph) {
+        return new CachedGraph<>(graph, graph);
+    }
+
+    public G getReadonlyCopy() {
         return readonlyCopy;
     }
 
-    public Graph getMutableCopy(Consumer<Map<Node, Node>> duplicationMapCallback) {
+    @SuppressWarnings("unchecked")
+    public G getMutableCopy(Consumer<Map<Node, Node>> duplicationMapCallback) {
         if (mutableCopy == null) {
-            mutableCopy = readonlyCopy.copy(duplicationMapCallback);
+            mutableCopy = (G) readonlyCopy.copy(duplicationMapCallback);
         }
         return mutableCopy;
     }
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Graph.java	Tue May 19 14:24:47 2015 +0200
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Graph.java	Tue May 19 15:02:51 2015 +0200
@@ -220,7 +220,7 @@
     /**
      * Creates a copy of this graph.
      */
-    public Graph copy() {
+    public final Graph copy() {
         return copy(name, null);
     }
 
@@ -229,7 +229,7 @@
      *
      * @param duplicationMapCallback consumer of the duplication map created during the copying
      */
-    public Graph copy(Consumer<Map<Node, Node>> duplicationMapCallback) {
+    public final Graph copy(Consumer<Map<Node, Node>> duplicationMapCallback) {
         return copy(name, duplicationMapCallback);
     }
 
@@ -238,7 +238,7 @@
      *
      * @param newName the name of the copy, used for debugging purposes (can be null)
      */
-    public Graph copy(String newName) {
+    public final Graph copy(String newName) {
         return copy(newName, null);
     }
 
@@ -248,7 +248,7 @@
      * @param newName the name of the copy, used for debugging purposes (can be null)
      * @param duplicationMapCallback consumer of the duplication map created during the copying
      */
-    public Graph copy(String newName, Consumer<Map<Node, Node>> duplicationMapCallback) {
+    protected Graph copy(String newName, Consumer<Map<Node, Node>> duplicationMapCallback) {
         Graph copy = new Graph(newName);
         Map<Node, Node> duplicates = copy.addDuplicates(getNodes(), this, this.getNodeCount(), (Map<Node, Node>) null);
         if (duplicationMapCallback != null) {
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/ObjectCloneNode.java	Tue May 19 14:24:47 2015 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/ObjectCloneNode.java	Tue May 19 15:02:51 2015 +0200
@@ -63,7 +63,7 @@
                     }
 
                     assert snippetGraph != null : "ObjectCloneSnippets should be installed";
-                    return lowerReplacement(snippetGraph.copy(), tool);
+                    return lowerReplacement((StructuredGraph) snippetGraph.copy(), tool);
                 }
                 assert false : "unhandled array type " + type.getComponentType().getKind();
             } else {
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/arraycopy/ArrayCopyNode.java	Tue May 19 14:24:47 2015 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/arraycopy/ArrayCopyNode.java	Tue May 19 15:02:51 2015 +0200
@@ -98,14 +98,14 @@
             }
             snippetGraph = null;
             try (Scope s = Debug.scope("ArrayCopySnippet", snippetMethod)) {
-                snippetGraph = replacements.getSnippet(snippetMethod, getTargetMethod(), null).copy();
+                snippetGraph = (StructuredGraph) replacements.getSnippet(snippetMethod, getTargetMethod(), null).copy();
             } catch (Throwable e) {
                 throw Debug.handle(e);
             }
             replaceSnippetInvokes(snippetGraph);
         } else {
             assert snippetGraph != null : "ArrayCopySnippets should be installed";
-            snippetGraph = snippetGraph.copy();
+            snippetGraph = (StructuredGraph) snippetGraph.copy();
             if (shouldUnroll()) {
                 final StructuredGraph copy = snippetGraph;
                 try (Scope s = Debug.scope("ArrayCopySnippetSpecialization", snippetGraph.method())) {
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/StructuredGraph.java	Tue May 19 14:24:47 2015 +0200
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/StructuredGraph.java	Tue May 19 15:02:51 2015 +0200
@@ -24,6 +24,7 @@
 
 import java.util.*;
 import java.util.concurrent.atomic.*;
+import java.util.function.*;
 
 import com.oracle.graal.api.meta.Assumptions.Assumption;
 import com.oracle.graal.api.meta.*;
@@ -217,17 +218,17 @@
         this.start = start;
     }
 
+    /**
+     * Creates a copy of this graph.
+     *
+     * @param newName the name of the copy, used for debugging purposes (can be null)
+     * @param duplicationMapCallback consumer of the duplication map created during the copying
+     */
     @Override
-    public StructuredGraph copy() {
-        return copy(name);
-    }
-
-    public StructuredGraph copy(String newName, ResolvedJavaMethod newMethod) {
-        return copy(newName, newMethod, AllowAssumptions.from(assumptions != null), isInlinedMethodRecordingEnabled());
-    }
-
-    public StructuredGraph copy(String newName, ResolvedJavaMethod newMethod, AllowAssumptions allowAssumptions, boolean enableInlinedMethodRecording) {
-        StructuredGraph copy = new StructuredGraph(newName, newMethod, graphId, entryBCI, allowAssumptions);
+    protected Graph copy(String newName, Consumer<Map<Node, Node>> duplicationMapCallback) {
+        AllowAssumptions allowAssumptions = AllowAssumptions.from(assumptions != null);
+        boolean enableInlinedMethodRecording = isInlinedMethodRecordingEnabled();
+        StructuredGraph copy = new StructuredGraph(newName, method, graphId, entryBCI, allowAssumptions);
         if (allowAssumptions == AllowAssumptions.YES && assumptions != null) {
             copy.assumptions.record(assumptions);
         }
@@ -239,15 +240,13 @@
         copy.hasValueProxies = hasValueProxies;
         Map<Node, Node> replacements = Node.newMap();
         replacements.put(start, copy.start);
-        copy.addDuplicates(getNodes(), this, this.getNodeCount(), replacements);
+        Map<Node, Node> duplicates = copy.addDuplicates(getNodes(), this, this.getNodeCount(), replacements);
+        if (duplicationMapCallback != null) {
+            duplicationMapCallback.accept(duplicates);
+        }
         return copy;
     }
 
-    @Override
-    public StructuredGraph copy(String newName) {
-        return copy(newName, method);
-    }
-
     public ParameterNode getParameter(int index) {
         for (ParameterNode param : getNodes(ParameterNode.TYPE)) {
             if (param.index() == index) {
--- a/graal/com.oracle.graal.phases.common/src/com/oracle/graal/phases/common/inlining/info/elem/InlineableGraph.java	Tue May 19 14:24:47 2015 +0200
+++ b/graal/com.oracle.graal.phases.common/src/com/oracle/graal/phases/common/inlining/info/elem/InlineableGraph.java	Tue May 19 15:02:51 2015 +0200
@@ -61,7 +61,7 @@
     public InlineableGraph(final ResolvedJavaMethod method, final Invoke invoke, final HighTierContext context, CanonicalizerPhase canonicalizer) {
         StructuredGraph original = getOriginalGraph(method, context, canonicalizer, invoke.asNode().graph(), invoke.bci());
         // TODO copying the graph is only necessary if it is modified or if it contains any invokes
-        this.graph = original.copy();
+        this.graph = (StructuredGraph) original.copy();
         specializeGraphToArguments(invoke, context, canonicalizer);
     }
 
--- a/graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/SnippetTemplate.java	Tue May 19 14:24:47 2015 +0200
+++ b/graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/SnippetTemplate.java	Tue May 19 15:02:51 2015 +0200
@@ -1299,7 +1299,7 @@
      * Gets a copy of the specialized graph.
      */
     public StructuredGraph copySpecializedGraph() {
-        return snippet.copy();
+        return (StructuredGraph) snippet.copy();
     }
 
     /**
--- a/graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/nodes/MacroNode.java	Tue May 19 14:24:47 2015 +0200
+++ b/graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/nodes/MacroNode.java	Tue May 19 15:02:51 2015 +0200
@@ -112,7 +112,7 @@
     protected StructuredGraph getLoweredSubstitutionGraph(LoweringTool tool) {
         StructuredGraph methodSubstitution = tool.getReplacements().getSubstitution(getTargetMethod(), true, bci);
         if (methodSubstitution != null) {
-            methodSubstitution = methodSubstitution.copy();
+            methodSubstitution = (StructuredGraph) methodSubstitution.copy();
             return lowerReplacement(methodSubstitution, tool);
         }
         return null;