# HG changeset patch # User Christian Wimmer # Date 1393549464 28800 # Node ID 6db511bddb84f48235ad16ec6a63e6739c045586 # Parent 2d222e87d962c31e2e9e52ab150fd08e42b765e0 Move GraphKit out of HotSpot-specific project diff -r 2d222e87d962 -r 6db511bddb84 graal/com.oracle.graal.hotspot.ptx/src/com/oracle/graal/hotspot/ptx/PTXWrapperBuilder.java --- a/graal/com.oracle.graal.hotspot.ptx/src/com/oracle/graal/hotspot/ptx/PTXWrapperBuilder.java Thu Feb 27 12:05:52 2014 -0800 +++ b/graal/com.oracle.graal.hotspot.ptx/src/com/oracle/graal/hotspot/ptx/PTXWrapperBuilder.java Thu Feb 27 17:04:24 2014 -0800 @@ -40,7 +40,6 @@ import com.oracle.graal.graph.*; import com.oracle.graal.hotspot.meta.*; import com.oracle.graal.hotspot.nodes.*; -import com.oracle.graal.hotspot.stubs.*; import com.oracle.graal.java.*; import com.oracle.graal.lir.ptx.*; import com.oracle.graal.nodes.*; diff -r 2d222e87d962 -r 6db511bddb84 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/stubs/ForeignCallStub.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/stubs/ForeignCallStub.java Thu Feb 27 12:05:52 2014 -0800 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/stubs/ForeignCallStub.java Thu Feb 27 17:04:24 2014 -0800 @@ -35,6 +35,7 @@ import com.oracle.graal.hotspot.replacements.*; import com.oracle.graal.nodes.*; import com.oracle.graal.nodes.type.*; +import com.oracle.graal.replacements.*; import com.oracle.graal.replacements.nodes.*; import com.oracle.graal.word.*; diff -r 2d222e87d962 -r 6db511bddb84 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/stubs/GraphKit.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/stubs/GraphKit.java Thu Feb 27 12:05:52 2014 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,165 +0,0 @@ -/* - * Copyright (c) 2014, 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.hotspot.stubs; - -import java.lang.reflect.*; - -import com.oracle.graal.api.code.*; -import com.oracle.graal.api.meta.*; -import com.oracle.graal.graph.*; -import com.oracle.graal.java.*; -import com.oracle.graal.nodes.*; -import com.oracle.graal.nodes.calc.*; -import com.oracle.graal.nodes.java.*; -import com.oracle.graal.nodes.java.MethodCallTargetNode.*; -import com.oracle.graal.phases.common.*; -import com.oracle.graal.phases.util.*; -import com.oracle.graal.replacements.*; -import com.oracle.graal.replacements.ReplacementsImpl.*; -import com.oracle.graal.word.phases.*; - -/** - * A utility for manually creating a graph. This will be expanded as necessary to support all - * subsystems that employ manual graph creation (as opposed to {@linkplain GraphBuilderPhase - * bytecode parsing} based graph creation). - */ -public class GraphKit { - - private final Providers providers; - private final StructuredGraph graph; - private FixedWithNextNode lastFixedNode; - - public GraphKit(StructuredGraph graph, Providers providers) { - this.providers = providers; - this.graph = graph; - this.lastFixedNode = graph.start(); - } - - public StructuredGraph getGraph() { - return graph; - } - - /** - * Ensures a floating node is added to or already present in the graph via {@link Graph#unique}. - * - * @return a node similar to {@code node} if one exists, otherwise {@code node} - */ - protected T unique(T node) { - return graph.unique(node); - } - - /** - * Appends a fixed node to the graph. - */ - protected T append(T node) { - T result = graph.add(node); - assert lastFixedNode != null; - assert result.predecessor() == null; - graph.addAfterFixed(lastFixedNode, result); - if (result instanceof FixedWithNextNode) { - lastFixedNode = (FixedWithNextNode) result; - } else { - lastFixedNode = null; - } - return result; - } - - /** - * Creates and appends an {@link InvokeNode} for a call to a given method with a given set of - * arguments. - * - * @param declaringClass the class declaring the invoked method - * @param name the name of the invoked method - * @param args the arguments to the invocation - */ - public InvokeNode createInvoke(Class declaringClass, String name, ValueNode... args) { - ResolvedJavaMethod method = null; - for (Method m : declaringClass.getDeclaredMethods()) { - if (Modifier.isStatic(m.getModifiers()) && m.getName().equals(name)) { - assert method == null : "found more than one method in " + declaringClass + " named " + name; - method = providers.getMetaAccess().lookupJavaMethod(m); - } - } - assert method != null : "did not find method in " + declaringClass + " named " + name; - Signature signature = method.getSignature(); - JavaType returnType = signature.getReturnType(null); - assert checkArgs(method, args); - MethodCallTargetNode callTarget = graph.add(new MethodCallTargetNode(InvokeKind.Static, method, args, returnType)); - InvokeNode invoke = append(new InvokeNode(callTarget, FrameState.UNKNOWN_BCI)); - return invoke; - } - - /** - * Determines if a given set of arguments is compatible with the signature of a given method. - * - * @return true if {@code args} are compatible with the signature if {@code method} - * @throws AssertionError if {@code args} are not compatible with the signature if - * {@code method} - */ - public boolean checkArgs(ResolvedJavaMethod method, ValueNode... args) { - Signature signature = method.getSignature(); - if (signature.getParameterCount(false) != args.length) { - throw new AssertionError(graph + ": wrong number of arguments to " + method); - } - for (int i = 0; i != args.length; i++) { - Kind expected = signature.getParameterKind(i).getStackKind(); - Kind actual = args[i].stamp().getStackKind(); - if (expected != actual) { - throw new AssertionError(graph + ": wrong kind of value for argument " + i + " of calls to " + method + " [" + actual + " != " + expected + "]"); - } - } - return true; - } - - /** - * Rewrite all word types in the graph. - */ - public void rewriteWordTypes() { - new WordTypeRewriterPhase(providers.getMetaAccess(), providers.getCodeCache().getTarget().wordKind).apply(graph); - } - - /** - * {@linkplain #inline(InvokeNode) Inlines} all invocations currently in the graph. - */ - public void inlineInvokes() { - for (InvokeNode invoke : graph.getNodes().filter(InvokeNode.class).snapshot()) { - inline(invoke); - } - - // Clean up all code that is now dead after inlining. - new DeadCodeEliminationPhase().apply(graph); - assert graph.getNodes().filter(InvokeNode.class).isEmpty(); - } - - /** - * Inlines a given invocation to a method. The graph of the inlined method is - * {@linkplain ReplacementsImpl#makeGraph processed} in the same manner as for snippets and - * method substitutions. - */ - public void inline(InvokeNode invoke) { - ResolvedJavaMethod method = ((MethodCallTargetNode) invoke.callTarget()).targetMethod(); - ReplacementsImpl repl = new ReplacementsImpl(providers, new Assumptions(false), providers.getCodeCache().getTarget()); - StructuredGraph calleeGraph = repl.makeGraph(method, null, method, null, FrameStateProcessing.CollapseFrameForSingleSideEffect); - InliningUtil.inline(invoke, calleeGraph, false); - } -} diff -r 2d222e87d962 -r 6db511bddb84 graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/GraphKit.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/GraphKit.java Thu Feb 27 17:04:24 2014 -0800 @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2014, 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.replacements; + +import java.lang.reflect.*; + +import com.oracle.graal.api.code.*; +import com.oracle.graal.api.meta.*; +import com.oracle.graal.graph.*; +import com.oracle.graal.java.*; +import com.oracle.graal.nodes.*; +import com.oracle.graal.nodes.calc.*; +import com.oracle.graal.nodes.java.*; +import com.oracle.graal.nodes.java.MethodCallTargetNode.InvokeKind; +import com.oracle.graal.phases.common.*; +import com.oracle.graal.phases.util.*; +import com.oracle.graal.replacements.ReplacementsImpl.FrameStateProcessing; +import com.oracle.graal.word.phases.*; + +/** + * A utility for manually creating a graph. This will be expanded as necessary to support all + * subsystems that employ manual graph creation (as opposed to {@linkplain GraphBuilderPhase + * bytecode parsing} based graph creation). + */ +public class GraphKit { + + private final Providers providers; + private final StructuredGraph graph; + private FixedWithNextNode lastFixedNode; + + public GraphKit(StructuredGraph graph, Providers providers) { + this.providers = providers; + this.graph = graph; + this.lastFixedNode = graph.start(); + } + + public StructuredGraph getGraph() { + return graph; + } + + /** + * Ensures a floating node is added to or already present in the graph via {@link Graph#unique}. + * + * @return a node similar to {@code node} if one exists, otherwise {@code node} + */ + public T unique(T node) { + return graph.unique(node); + } + + /** + * Appends a fixed node to the graph. + */ + public T append(T node) { + T result = graph.add(node); + assert lastFixedNode != null; + assert result.predecessor() == null; + graph.addAfterFixed(lastFixedNode, result); + if (result instanceof FixedWithNextNode) { + lastFixedNode = (FixedWithNextNode) result; + } else { + lastFixedNode = null; + } + return result; + } + + /** + * Creates and appends an {@link InvokeNode} for a call to a given method with a given set of + * arguments. + * + * @param declaringClass the class declaring the invoked method + * @param name the name of the invoked method + * @param args the arguments to the invocation + */ + public InvokeNode createInvoke(Class declaringClass, String name, ValueNode... args) { + ResolvedJavaMethod method = null; + for (Method m : declaringClass.getDeclaredMethods()) { + if (Modifier.isStatic(m.getModifiers()) && m.getName().equals(name)) { + assert method == null : "found more than one method in " + declaringClass + " named " + name; + method = providers.getMetaAccess().lookupJavaMethod(m); + } + } + assert method != null : "did not find method in " + declaringClass + " named " + name; + Signature signature = method.getSignature(); + JavaType returnType = signature.getReturnType(null); + assert checkArgs(method, args); + MethodCallTargetNode callTarget = graph.add(new MethodCallTargetNode(InvokeKind.Static, method, args, returnType)); + InvokeNode invoke = append(new InvokeNode(callTarget, FrameState.UNKNOWN_BCI)); + return invoke; + } + + /** + * Determines if a given set of arguments is compatible with the signature of a given method. + * + * @return true if {@code args} are compatible with the signature if {@code method} + * @throws AssertionError if {@code args} are not compatible with the signature if + * {@code method} + */ + public boolean checkArgs(ResolvedJavaMethod method, ValueNode... args) { + Signature signature = method.getSignature(); + if (signature.getParameterCount(false) != args.length) { + throw new AssertionError(graph + ": wrong number of arguments to " + method); + } + for (int i = 0; i != args.length; i++) { + Kind expected = signature.getParameterKind(i).getStackKind(); + Kind actual = args[i].stamp().getStackKind(); + if (expected != actual) { + throw new AssertionError(graph + ": wrong kind of value for argument " + i + " of calls to " + method + " [" + actual + " != " + expected + "]"); + } + } + return true; + } + + /** + * Rewrite all word types in the graph. + */ + public void rewriteWordTypes() { + new WordTypeRewriterPhase(providers.getMetaAccess(), providers.getCodeCache().getTarget().wordKind).apply(graph); + } + + /** + * {@linkplain #inline(InvokeNode) Inlines} all invocations currently in the graph. + */ + public void inlineInvokes() { + for (InvokeNode invoke : graph.getNodes().filter(InvokeNode.class).snapshot()) { + inline(invoke); + } + + // Clean up all code that is now dead after inlining. + new DeadCodeEliminationPhase().apply(graph); + assert graph.getNodes().filter(InvokeNode.class).isEmpty(); + } + + /** + * Inlines a given invocation to a method. The graph of the inlined method is + * {@linkplain ReplacementsImpl#makeGraph processed} in the same manner as for snippets and + * method substitutions. + */ + public void inline(InvokeNode invoke) { + ResolvedJavaMethod method = ((MethodCallTargetNode) invoke.callTarget()).targetMethod(); + ReplacementsImpl repl = new ReplacementsImpl(providers, new Assumptions(false), providers.getCodeCache().getTarget()); + StructuredGraph calleeGraph = repl.makeGraph(method, null, method, null, FrameStateProcessing.CollapseFrameForSingleSideEffect); + InliningUtil.inline(invoke, calleeGraph, false); + } +}