use of org.graalvm.compiler.nodes.ValueNode in project graal by oracle.
the class InliningUtil method nonNullReceiver.
/**
* Gets the receiver for an invoke, adding a guard if necessary to ensure it is non-null, and
* ensuring that the resulting type is compatible with the method being invoked.
*/
@SuppressWarnings("try")
public static ValueNode nonNullReceiver(Invoke invoke) {
try (DebugCloseable position = invoke.asNode().withNodeSourcePosition()) {
MethodCallTargetNode callTarget = (MethodCallTargetNode) invoke.callTarget();
assert !callTarget.isStatic() : callTarget.targetMethod();
StructuredGraph graph = callTarget.graph();
ValueNode oldReceiver = callTarget.arguments().get(0);
ValueNode newReceiver = oldReceiver;
if (newReceiver.getStackKind() == JavaKind.Object) {
if (invoke.getInvokeKind() == InvokeKind.Special) {
Stamp paramStamp = newReceiver.stamp(NodeView.DEFAULT);
Stamp stamp = paramStamp.join(StampFactory.object(TypeReference.create(graph.getAssumptions(), callTarget.targetMethod().getDeclaringClass())));
if (!stamp.equals(paramStamp)) {
// The verifier and previous optimizations guarantee unconditionally that
// the
// receiver is at least of the type of the method holder for a special
// invoke.
newReceiver = graph.unique(new PiNode(newReceiver, stamp));
}
}
if (!StampTool.isPointerNonNull(newReceiver)) {
LogicNode condition = graph.unique(IsNullNode.create(newReceiver));
FixedGuardNode fixedGuard = graph.add(new FixedGuardNode(condition, NullCheckException, InvalidateReprofile, true));
PiNode nonNullReceiver = graph.unique(new PiNode(newReceiver, StampFactory.objectNonNull(), fixedGuard));
graph.addBeforeFixed(invoke.asNode(), fixedGuard);
newReceiver = nonNullReceiver;
}
}
if (newReceiver != oldReceiver) {
callTarget.replaceFirstInput(oldReceiver, newReceiver);
}
return newReceiver;
}
}
use of org.graalvm.compiler.nodes.ValueNode in project graal by oracle.
the class MultiTypeGuardInlineInfo method inlineMultipleMethods.
private EconomicSet<Node> inlineMultipleMethods(StructuredGraph graph, Providers providers) {
int numberOfMethods = concretes.size();
FixedNode continuation = invoke.next();
// setup merge and phi nodes for results and exceptions
AbstractMergeNode returnMerge = graph.add(new MergeNode());
returnMerge.setStateAfter(invoke.stateAfter());
PhiNode returnValuePhi = null;
if (invoke.asNode().getStackKind() != JavaKind.Void) {
returnValuePhi = graph.addWithoutUnique(new ValuePhiNode(invoke.asNode().stamp(NodeView.DEFAULT).unrestricted(), returnMerge));
}
AbstractMergeNode exceptionMerge = null;
PhiNode exceptionObjectPhi = null;
if (invoke instanceof InvokeWithExceptionNode) {
InvokeWithExceptionNode invokeWithException = (InvokeWithExceptionNode) invoke;
ExceptionObjectNode exceptionEdge = (ExceptionObjectNode) invokeWithException.exceptionEdge();
exceptionMerge = graph.add(new MergeNode());
FixedNode exceptionSux = exceptionEdge.next();
graph.addBeforeFixed(exceptionSux, exceptionMerge);
exceptionObjectPhi = graph.addWithoutUnique(new ValuePhiNode(StampFactory.forKind(JavaKind.Object), exceptionMerge));
exceptionMerge.setStateAfter(exceptionEdge.stateAfter().duplicateModified(invoke.stateAfter().bci, true, JavaKind.Object, new JavaKind[] { JavaKind.Object }, new ValueNode[] { exceptionObjectPhi }));
}
// create one separate block for each invoked method
AbstractBeginNode[] successors = new AbstractBeginNode[numberOfMethods + 1];
for (int i = 0; i < numberOfMethods; i++) {
successors[i] = createInvocationBlock(graph, invoke, returnMerge, returnValuePhi, exceptionMerge, exceptionObjectPhi, true);
}
// create the successor for an unknown type
FixedNode unknownTypeSux;
if (shouldFallbackToInvoke()) {
unknownTypeSux = createInvocationBlock(graph, invoke, returnMerge, returnValuePhi, exceptionMerge, exceptionObjectPhi, false);
} else {
unknownTypeSux = graph.add(new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.TypeCheckedInliningViolated));
}
successors[successors.length - 1] = BeginNode.begin(unknownTypeSux);
// replace the invoke exception edge
if (invoke instanceof InvokeWithExceptionNode) {
InvokeWithExceptionNode invokeWithExceptionNode = (InvokeWithExceptionNode) invoke;
ExceptionObjectNode exceptionEdge = (ExceptionObjectNode) invokeWithExceptionNode.exceptionEdge();
exceptionEdge.replaceAtUsages(exceptionObjectPhi);
exceptionEdge.setNext(null);
GraphUtil.killCFG(invokeWithExceptionNode.exceptionEdge());
}
assert invoke.asNode().isAlive();
// replace the invoke with a switch on the type of the actual receiver
boolean methodDispatch = createDispatchOnTypeBeforeInvoke(graph, successors, false, providers.getStampProvider(), providers.getConstantReflection());
assert invoke.next() == continuation;
invoke.setNext(null);
returnMerge.setNext(continuation);
if (returnValuePhi != null) {
invoke.asNode().replaceAtUsages(returnValuePhi);
}
invoke.asNode().safeDelete();
ArrayList<PiNode> replacementNodes = new ArrayList<>();
// prepare the anchors for the invokes
for (int i = 0; i < numberOfMethods; i++) {
AbstractBeginNode node = successors[i];
Invoke invokeForInlining = (Invoke) node.next();
ResolvedJavaType commonType;
if (methodDispatch) {
commonType = concretes.get(i).getDeclaringClass();
} else {
commonType = getLeastCommonType(i);
}
ValueNode receiver = ((MethodCallTargetNode) invokeForInlining.callTarget()).receiver();
boolean exact = (getTypeCount(i) == 1 && !methodDispatch);
PiNode anchoredReceiver = InliningUtil.createAnchoredReceiver(graph, node, commonType, receiver, exact);
invokeForInlining.callTarget().replaceFirstInput(receiver, anchoredReceiver);
assert !anchoredReceiver.isDeleted() : anchoredReceiver;
replacementNodes.add(anchoredReceiver);
}
if (shouldFallbackToInvoke()) {
replacementNodes.add(null);
}
EconomicSet<Node> canonicalizeNodes = EconomicSet.create(Equivalence.DEFAULT);
// do the actual inlining for every invoke
for (int i = 0; i < numberOfMethods; i++) {
Invoke invokeForInlining = (Invoke) successors[i].next();
canonicalizeNodes.addAll(doInline(i, invokeForInlining));
}
if (returnValuePhi != null) {
canonicalizeNodes.add(returnValuePhi);
}
return canonicalizeNodes;
}
use of org.graalvm.compiler.nodes.ValueNode in project graal by oracle.
the class MultiTypeGuardInlineInfo method createDispatchOnTypeBeforeInvoke.
private boolean createDispatchOnTypeBeforeInvoke(StructuredGraph graph, AbstractBeginNode[] successors, boolean invokeIsOnlySuccessor, StampProvider stampProvider, ConstantReflectionProvider constantReflection) {
assert ptypes.size() >= 1;
ValueNode nonNullReceiver = InliningUtil.nonNullReceiver(invoke);
LoadHubNode hub = graph.unique(new LoadHubNode(stampProvider, nonNullReceiver));
graph.getDebug().log("Type switch with %d types", concretes.size());
ResolvedJavaType[] keys = new ResolvedJavaType[ptypes.size()];
double[] keyProbabilities = new double[ptypes.size() + 1];
int[] keySuccessors = new int[ptypes.size() + 1];
double totalProbability = notRecordedTypeProbability;
for (int i = 0; i < ptypes.size(); i++) {
keys[i] = ptypes.get(i).getType();
keyProbabilities[i] = ptypes.get(i).getProbability();
totalProbability += keyProbabilities[i];
keySuccessors[i] = invokeIsOnlySuccessor ? 0 : typesToConcretes.get(i);
assert keySuccessors[i] < successors.length - 1 : "last successor is the unknownTypeSux";
}
keyProbabilities[keyProbabilities.length - 1] = notRecordedTypeProbability;
keySuccessors[keySuccessors.length - 1] = successors.length - 1;
// Normalize the probabilities.
for (int i = 0; i < keyProbabilities.length; i++) {
keyProbabilities[i] /= totalProbability;
}
TypeSwitchNode typeSwitch = graph.add(new TypeSwitchNode(hub, successors, keys, keyProbabilities, keySuccessors, constantReflection));
FixedWithNextNode pred = (FixedWithNextNode) invoke.asNode().predecessor();
pred.setNext(typeSwitch);
return false;
}
use of org.graalvm.compiler.nodes.ValueNode in project graal by oracle.
the class StampTool method meetOrNull.
/**
* Meet a collection of {@link ValueNode}s optionally excluding {@code selfValue}. If no values
* are encountered then return {@code null}.
*/
public static Stamp meetOrNull(Iterable<? extends ValueNode> values, ValueNode selfValue) {
Iterator<? extends ValueNode> iterator = values.iterator();
Stamp stamp = null;
while (iterator.hasNext()) {
ValueNode nextValue = iterator.next();
if (nextValue != selfValue) {
if (stamp == null) {
stamp = nextValue.stamp(NodeView.DEFAULT);
} else {
stamp = stamp.meet(nextValue.stamp(NodeView.DEFAULT));
}
}
}
return stamp;
}
use of org.graalvm.compiler.nodes.ValueNode in project graal by oracle.
the class GraphUtil method checkRedundantPhi.
public static void checkRedundantPhi(PhiNode phiNode) {
if (phiNode.isDeleted() || phiNode.valueCount() == 1) {
return;
}
ValueNode singleValue = phiNode.singleValueOrThis();
if (singleValue != phiNode) {
Collection<PhiNode> phiUsages = phiNode.usages().filter(PhiNode.class).snapshot();
Collection<ProxyNode> proxyUsages = phiNode.usages().filter(ProxyNode.class).snapshot();
phiNode.replaceAtUsagesAndDelete(singleValue);
for (PhiNode phi : phiUsages) {
checkRedundantPhi(phi);
}
for (ProxyNode proxy : proxyUsages) {
checkRedundantProxy(proxy);
}
}
}
Aggregations