use of org.graalvm.compiler.nodes.DeoptimizeNode in project graal by oracle.
the class StandardGraphBuilderPlugins method registerMethodHandleImplPlugins.
private static void registerMethodHandleImplPlugins(InvocationPlugins plugins, SnippetReflectionProvider snippetReflection, BytecodeProvider bytecodeProvider) {
Registration r = new Registration(plugins, "java.lang.invoke.MethodHandleImpl", bytecodeProvider);
r.register2("profileBoolean", boolean.class, int[].class, new InvocationPlugin() {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode result, ValueNode counters) {
if (result.isConstant()) {
b.push(JavaKind.Boolean, result);
return true;
}
if (counters.isConstant()) {
ValueNode newResult = result;
int[] ctrs = snippetReflection.asObject(int[].class, (JavaConstant) counters.asConstant());
if (ctrs != null && ctrs.length == 2) {
int falseCount = ctrs[0];
int trueCount = ctrs[1];
int totalCount = trueCount + falseCount;
if (totalCount == 0) {
b.add(new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.TransferToInterpreter));
} else if (falseCount == 0 || trueCount == 0) {
boolean expected = falseCount == 0;
LogicNode condition = b.addWithInputs(IntegerEqualsNode.create(b.getConstantReflection(), b.getMetaAccess(), b.getOptions(), null, result, b.add(ConstantNode.forBoolean(!expected)), NodeView.DEFAULT));
b.append(new FixedGuardNode(condition, DeoptimizationReason.UnreachedCode, DeoptimizationAction.InvalidateReprofile, true));
newResult = b.add(ConstantNode.forBoolean(expected));
} else {
// We cannot use BranchProbabilityNode here since there's no guarantee
// the result of MethodHandleImpl.profileBoolean() is used as the
// test in an `if` statement (as required by BranchProbabilityNode).
}
}
b.addPush(JavaKind.Boolean, newResult);
return true;
}
return false;
}
});
}
use of org.graalvm.compiler.nodes.DeoptimizeNode in project graal by oracle.
the class PEGraphDecoder method finishInlining.
@Override
protected void finishInlining(MethodScope is) {
PEMethodScope inlineScope = (PEMethodScope) is;
ResolvedJavaMethod inlineMethod = inlineScope.method;
PEMethodScope methodScope = inlineScope.caller;
LoopScope loopScope = inlineScope.callerLoopScope;
InvokeData invokeData = inlineScope.invokeData;
Invoke invoke = invokeData.invoke;
FixedNode invokeNode = invoke.asNode();
ValueNode exceptionValue = null;
int returnNodeCount = 0;
int unwindNodeCount = 0;
List<ControlSinkNode> returnAndUnwindNodes = inlineScope.returnAndUnwindNodes;
for (int i = 0; i < returnAndUnwindNodes.size(); i++) {
FixedNode fixedNode = returnAndUnwindNodes.get(i);
if (fixedNode instanceof ReturnNode) {
returnNodeCount++;
} else if (fixedNode.isAlive()) {
assert fixedNode instanceof UnwindNode;
unwindNodeCount++;
}
}
if (unwindNodeCount > 0) {
FixedNode unwindReplacement;
if (invoke instanceof InvokeWithExceptionNode) {
/* Decoding continues for the exception handler. */
unwindReplacement = makeStubNode(methodScope, loopScope, invokeData.exceptionNextOrderId);
} else {
/* No exception handler available, so the only thing we can do is deoptimize. */
unwindReplacement = graph.add(new DeoptimizeNode(DeoptimizationAction.InvalidateRecompile, DeoptimizationReason.NotCompiledExceptionHandler));
}
if (unwindNodeCount == 1) {
/* Only one UnwindNode, we can use the exception directly. */
UnwindNode unwindNode = getSingleMatchingNode(returnAndUnwindNodes, returnNodeCount > 0, UnwindNode.class);
exceptionValue = unwindNode.exception();
unwindNode.replaceAndDelete(unwindReplacement);
} else {
/*
* More than one UnwindNode. This can happen with the loop explosion strategy
* FULL_EXPLODE_UNTIL_RETURN, where we keep exploding after the loop and therefore
* also explode exception paths. Merge the exception in a similar way as multiple
* return values.
*/
MergeNode unwindMergeNode = graph.add(new MergeNode());
exceptionValue = InliningUtil.mergeValueProducers(unwindMergeNode, getMatchingNodes(returnAndUnwindNodes, returnNodeCount > 0, UnwindNode.class, unwindNodeCount), null, unwindNode -> unwindNode.exception());
unwindMergeNode.setNext(unwindReplacement);
ensureExceptionStateDecoded(inlineScope);
unwindMergeNode.setStateAfter(inlineScope.exceptionState.duplicateModified(JavaKind.Object, JavaKind.Object, exceptionValue));
}
}
assert invoke.next() == null;
assert !(invoke instanceof InvokeWithExceptionNode) || ((InvokeWithExceptionNode) invoke).exceptionEdge() == null;
ValueNode returnValue;
if (returnNodeCount == 0) {
returnValue = null;
} else if (returnNodeCount == 1) {
ReturnNode returnNode = getSingleMatchingNode(returnAndUnwindNodes, unwindNodeCount > 0, ReturnNode.class);
returnValue = returnNode.result();
FixedNode n = nodeAfterInvoke(methodScope, loopScope, invokeData, AbstractBeginNode.prevBegin(returnNode));
returnNode.replaceAndDelete(n);
} else {
AbstractMergeNode merge = graph.add(new MergeNode());
merge.setStateAfter((FrameState) ensureNodeCreated(methodScope, loopScope, invokeData.stateAfterOrderId));
returnValue = InliningUtil.mergeReturns(merge, getMatchingNodes(returnAndUnwindNodes, unwindNodeCount > 0, ReturnNode.class, returnNodeCount));
FixedNode n = nodeAfterInvoke(methodScope, loopScope, invokeData, merge);
merge.setNext(n);
}
invokeNode.replaceAtUsages(returnValue);
/*
* Usage the handles that we have on the return value and the exception to update the
* orderId->Node table.
*/
registerNode(loopScope, invokeData.invokeOrderId, returnValue, true, true);
if (invoke instanceof InvokeWithExceptionNode) {
registerNode(loopScope, invokeData.exceptionOrderId, exceptionValue, true, true);
}
if (inlineScope.exceptionPlaceholderNode != null) {
inlineScope.exceptionPlaceholderNode.replaceAtUsagesAndDelete(exceptionValue);
}
deleteInvoke(invoke);
for (InlineInvokePlugin plugin : inlineInvokePlugins) {
plugin.notifyAfterInline(inlineMethod);
}
}
use of org.graalvm.compiler.nodes.DeoptimizeNode in project graal by oracle.
the class PEGraphDecoder method doInline.
protected LoopScope doInline(PEMethodScope methodScope, LoopScope loopScope, InvokeData invokeData, InlineInfo inlineInfo, ValueNode[] arguments) {
ResolvedJavaMethod inlineMethod = inlineInfo.getMethodToInline();
EncodedGraph graphToInline = lookupEncodedGraph(inlineMethod, inlineInfo.getOriginalMethod(), inlineInfo.getIntrinsicBytecodeProvider(), graph.trackNodeSourcePosition());
if (graphToInline == null) {
return null;
}
assert !graph.trackNodeSourcePosition() || graphToInline.trackNodeSourcePosition() : graph + " " + graphToInline;
if (methodScope.inliningDepth > Options.InliningDepthError.getValue(options)) {
throw tooDeepInlining(methodScope);
}
for (InlineInvokePlugin plugin : inlineInvokePlugins) {
plugin.notifyBeforeInline(inlineMethod);
}
Invoke invoke = invokeData.invoke;
FixedNode invokeNode = invoke.asNode();
FixedWithNextNode predecessor = (FixedWithNextNode) invokeNode.predecessor();
invokeNode.replaceAtPredecessor(null);
PEMethodScope inlineScope = new PEMethodScope(graph, methodScope, loopScope, graphToInline, inlineMethod, invokeData, methodScope.inliningDepth + 1, loopExplosionPlugin, arguments);
if (!inlineMethod.isStatic()) {
if (StampTool.isPointerAlwaysNull(arguments[0])) {
/*
* The receiver is null, so we can unconditionally throw a NullPointerException
* instead of performing any inlining.
*/
DeoptimizeNode deoptimizeNode = graph.add(new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.NullCheckException));
predecessor.setNext(deoptimizeNode);
finishInlining(inlineScope);
/* Continue decoding in the caller. */
return loopScope;
} else if (!StampTool.isPointerNonNull(arguments[0])) {
/* The receiver might be null, so we need to insert a null check. */
PEAppendGraphBuilderContext graphBuilderContext = new PEAppendGraphBuilderContext(inlineScope, predecessor);
arguments[0] = graphBuilderContext.nullCheckedValue(arguments[0]);
predecessor = graphBuilderContext.lastInstr;
}
}
LoopScope inlineLoopScope = createInitialLoopScope(inlineScope, predecessor);
/*
* The GraphEncoder assigns parameters a nodeId immediately after the fixed nodes.
* Initializing createdNodes here avoid decoding and immediately replacing the
* ParameterNodes.
*/
int firstArgumentNodeId = inlineScope.maxFixedNodeOrderId + 1;
for (int i = 0; i < arguments.length; i++) {
inlineLoopScope.createdNodes[firstArgumentNodeId + i] = arguments[i];
}
// Copy assumptions from inlinee to caller
Assumptions assumptions = graph.getAssumptions();
Assumptions inlinedAssumptions = graphToInline.getAssumptions();
if (assumptions != null) {
if (inlinedAssumptions != null) {
assumptions.record(inlinedAssumptions);
}
} else {
assert inlinedAssumptions == null : String.format("cannot inline graph (%s) which makes assumptions into a graph (%s) that doesn't", inlineMethod, graph);
}
// Copy inlined methods from inlinee to caller
List<ResolvedJavaMethod> inlinedMethods = graphToInline.getInlinedMethods();
if (inlinedMethods != null) {
graph.getMethods().addAll(inlinedMethods);
}
if (graphToInline.getFields() != null) {
for (ResolvedJavaField field : graphToInline.getFields()) {
graph.recordField(field);
}
}
if (graphToInline.hasUnsafeAccess()) {
graph.markUnsafeAccess();
}
/*
* Do the actual inlining by returning the initial loop scope for the inlined method scope.
*/
return inlineLoopScope;
}
use of org.graalvm.compiler.nodes.DeoptimizeNode in project graal by oracle.
the class IntegerExactArithmeticSplitNode method lower.
static void lower(LoweringTool tool, IntegerExactArithmeticNode node) {
if (node.asNode().graph().getGuardsStage() == StructuredGraph.GuardsStage.FIXED_DEOPTS) {
FloatingNode floatingNode = (FloatingNode) node;
FixedWithNextNode previous = tool.lastFixedNode();
FixedNode next = previous.next();
previous.setNext(null);
DeoptimizeNode deopt = floatingNode.graph().add(new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.ArithmeticException));
AbstractBeginNode normalBegin = floatingNode.graph().add(new BeginNode());
normalBegin.setNext(next);
IntegerExactArithmeticSplitNode split = node.createSplit(normalBegin, BeginNode.begin(deopt));
previous.setNext(split);
floatingNode.replaceAndDelete(split);
}
}
use of org.graalvm.compiler.nodes.DeoptimizeNode in project graal by oracle.
the class TruffleGraphBuilderPlugins method registerOptimizedAssumptionPlugins.
public static void registerOptimizedAssumptionPlugins(InvocationPlugins plugins, MetaAccessProvider metaAccess, KnownTruffleTypes types) {
ResolvedJavaType optimizedAssumptionType = getRuntime().resolveType(metaAccess, "org.graalvm.compiler.truffle.runtime.OptimizedAssumption");
Registration r = new Registration(plugins, new ResolvedJavaSymbol(optimizedAssumptionType), null);
InvocationPlugin plugin = new InvocationPlugin() {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
if (receiver.isConstant() && b.getAssumptions() != null) {
JavaConstant assumption = (JavaConstant) receiver.get().asConstant();
if (b.getConstantReflection().readFieldValue(types.fieldOptimizedAssumptionIsValid, assumption).asBoolean()) {
if (targetMethod.getName().equals("isValid")) {
b.addPush(JavaKind.Boolean, ConstantNode.forBoolean(true));
} else {
assert targetMethod.getName().equals("check") : targetMethod;
}
b.getAssumptions().record(new TruffleAssumption(assumption));
} else {
if (targetMethod.getName().equals("isValid")) {
b.addPush(JavaKind.Boolean, ConstantNode.forBoolean(false));
} else {
assert targetMethod.getName().equals("check") : targetMethod;
b.add(new DeoptimizeNode(DeoptimizationAction.InvalidateRecompile, DeoptimizationReason.None));
}
}
return true;
} else {
return false;
}
}
};
r.register1("isValid", Receiver.class, plugin);
r.register1("check", Receiver.class, plugin);
}
Aggregations