--- /dev/null 2016-05-31 09:42:47.975716356 -0700 +++ new/src/jdk.vm.compiler/share/classes/org.graalvm.compiler.nodes/src/org/graalvm/compiler/nodes/graphbuilderconf/GraphBuilderContext.java 2016-12-09 00:55:39.724640654 -0800 @@ -0,0 +1,240 @@ +/* + * Copyright (c) 2015, 2016, 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 org.graalvm.compiler.nodes.graphbuilderconf; + +import static org.graalvm.compiler.core.common.type.StampFactory.objectNonNull; +import static jdk.vm.ci.meta.DeoptimizationAction.InvalidateReprofile; +import static jdk.vm.ci.meta.DeoptimizationReason.NullCheckException; + +import org.graalvm.compiler.bytecode.Bytecode; +import org.graalvm.compiler.bytecode.BytecodeProvider; +import org.graalvm.compiler.core.common.type.ObjectStamp; +import org.graalvm.compiler.core.common.type.Stamp; +import org.graalvm.compiler.core.common.type.StampFactory; +import org.graalvm.compiler.core.common.type.StampPair; +import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind; +import org.graalvm.compiler.nodes.FixedGuardNode; +import org.graalvm.compiler.nodes.LogicNode; +import org.graalvm.compiler.nodes.PiNode; +import org.graalvm.compiler.nodes.StateSplit; +import org.graalvm.compiler.nodes.ValueNode; +import org.graalvm.compiler.nodes.calc.IsNullNode; +import org.graalvm.compiler.nodes.type.StampTool; + +import jdk.vm.ci.code.BailoutException; +import jdk.vm.ci.meta.Assumptions; +import jdk.vm.ci.meta.JavaKind; +import jdk.vm.ci.meta.JavaType; +import jdk.vm.ci.meta.ResolvedJavaMethod; + +/** + * Used by a {@link GraphBuilderPlugin} to interface with an object that parses the bytecode of a + * single {@linkplain #getMethod() method} as part of building a {@linkplain #getGraph() graph} . + */ +public interface GraphBuilderContext extends GraphBuilderTool { + + /** + * Pushes a given value to the frame state stack using an explicit kind. This should be used + * when {@code value.getJavaKind()} is different from the kind that the bytecode instruction + * currently being parsed pushes to the stack. + * + * @param kind the kind to use when type checking this operation + * @param value the value to push to the stack. The value must already have been + * {@linkplain #append(ValueNode) appended}. + */ + void push(JavaKind kind, ValueNode value); + + /** + * Adds a node to the graph. If the returned node is a {@link StateSplit} with a null + * {@linkplain StateSplit#stateAfter() frame state}, the frame state is initialized. + * + * @param value the value to add to the graph and push to the stack. The + * {@code value.getJavaKind()} kind is used when type checking this operation. + * @return a node equivalent to {@code value} in the graph + */ + default T add(T value) { + if (value.graph() != null) { + assert !(value instanceof StateSplit) || ((StateSplit) value).stateAfter() != null; + return value; + } + T equivalentValue = append(value); + if (equivalentValue instanceof StateSplit) { + StateSplit stateSplit = (StateSplit) equivalentValue; + if (stateSplit.stateAfter() == null && stateSplit.hasSideEffect()) { + setStateAfter(stateSplit); + } + } + return equivalentValue; + } + + /** + * Adds a node with a non-void kind to the graph, pushes it to the stack. If the returned node + * is a {@link StateSplit} with a null {@linkplain StateSplit#stateAfter() frame state}, the + * frame state is initialized. + * + * @param kind the kind to use when type checking this operation + * @param value the value to add to the graph and push to the stack + * @return a node equivalent to {@code value} in the graph + */ + default T addPush(JavaKind kind, T value) { + T equivalentValue = value.graph() != null ? value : append(value); + push(kind, equivalentValue); + if (equivalentValue instanceof StateSplit) { + StateSplit stateSplit = (StateSplit) equivalentValue; + if (stateSplit.stateAfter() == null && stateSplit.hasSideEffect()) { + setStateAfter(stateSplit); + } + } + return equivalentValue; + } + + /** + * Handles an invocation that a plugin determines can replace the original invocation (i.e., the + * one for which the plugin was applied). This applies all standard graph builder processing to + * the replaced invocation including applying any relevant plugins. + * + * @param invokeKind the kind of the replacement invocation + * @param targetMethod the target of the replacement invocation + * @param args the arguments to the replacement invocation + * @param forceInlineEverything specifies if all invocations encountered in the scope of + * handling the replaced invoke are to be force inlined + */ + void handleReplacedInvoke(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, ValueNode[] args, boolean forceInlineEverything); + + /** + * Intrinsifies an invocation of a given method by inlining the bytecodes of a given + * substitution method. + * + * @param bytecodeProvider used to get the bytecodes to parse for the substitution method + * @param targetMethod the method being intrinsified + * @param substitute the intrinsic implementation + * @param receiver the receiver, or null for static methods + * @param argsIncludingReceiver the arguments with which to inline the invocation + * + * @return whether the intrinsification was successful + */ + boolean intrinsify(BytecodeProvider bytecodeProvider, ResolvedJavaMethod targetMethod, ResolvedJavaMethod substitute, InvocationPlugin.Receiver receiver, ValueNode[] argsIncludingReceiver); + + /** + * Creates a snap shot of the current frame state with the BCI of the instruction after the one + * currently being parsed and assigns it to a given {@linkplain StateSplit#hasSideEffect() side + * effect} node. + * + * @param sideEffect a side effect node just appended to the graph + */ + void setStateAfter(StateSplit sideEffect); + + /** + * Gets the parsing context for the method that inlines the method being parsed by this context. + */ + GraphBuilderContext getParent(); + + /** + * Gets the first ancestor parsing context that is not parsing a {@linkplain #parsingIntrinsic() + * intrinsic}. + */ + default GraphBuilderContext getNonIntrinsicAncestor() { + GraphBuilderContext ancestor = getParent(); + while (ancestor != null && ancestor.parsingIntrinsic()) { + ancestor = ancestor.getParent(); + } + return ancestor; + } + + /** + * Gets the code being parsed. + */ + Bytecode getCode(); + + /** + * Gets the method being parsed by this context. + */ + ResolvedJavaMethod getMethod(); + + /** + * Gets the index of the bytecode instruction currently being parsed. + */ + int bci(); + + /** + * Gets the kind of invocation currently being parsed. + */ + InvokeKind getInvokeKind(); + + /** + * Gets the return type of the invocation currently being parsed. + */ + JavaType getInvokeReturnType(); + + default StampPair getInvokeReturnStamp(Assumptions assumptions) { + JavaType returnType = getInvokeReturnType(); + return StampFactory.forDeclaredType(assumptions, returnType, false); + } + + /** + * Gets the inline depth of this context. A return value of 0 implies that this is the context + * for the parse root. + */ + default int getDepth() { + GraphBuilderContext parent = getParent(); + return parent == null ? 0 : 1 + parent.getDepth(); + } + + /** + * Determines if this parsing context is within the bytecode of an intrinsic or a method inlined + * by an intrinsic. + */ + @Override + default boolean parsingIntrinsic() { + return getIntrinsic() != null; + } + + /** + * Gets the intrinsic of the current parsing context or {@code null} if not + * {@link #parsingIntrinsic() parsing an intrinsic}. + */ + IntrinsicContext getIntrinsic(); + + BailoutException bailout(String string); + + /** + * Gets a version of a given value that has a {@linkplain StampTool#isPointerNonNull(ValueNode) + * non-null} stamp. + */ + default ValueNode nullCheckedValue(ValueNode value) { + if (!StampTool.isPointerNonNull(value.stamp())) { + LogicNode condition = getGraph().unique(IsNullNode.create(value)); + ObjectStamp receiverStamp = (ObjectStamp) value.stamp(); + Stamp stamp = receiverStamp.join(objectNonNull()); + FixedGuardNode fixedGuard = append(new FixedGuardNode(condition, NullCheckException, InvalidateReprofile, true)); + PiNode nonNullReceiver = getGraph().unique(new PiNode(value, stamp, fixedGuard)); + // TODO: Propogating the non-null into the frame state would + // remove subsequent null-checks on the same value. However, + // it currently causes an assertion failure when merging states. + // + // frameState.replace(value, nonNullReceiver); + return nonNullReceiver; + } + return value; + } +}