use of org.graalvm.compiler.nodes.ValueNode in project graal by oracle.
the class TruffleGraphBuilderPlugins method registerFrameMethods.
private static void registerFrameMethods(Registration r) {
r.register1("getArguments", Receiver.class, new InvocationPlugin() {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver frame) {
if (frame.get(false) instanceof NewFrameNode) {
b.push(JavaKind.Object, ((NewFrameNode) frame.get()).getArguments());
return true;
}
return false;
}
});
r.register1("getFrameDescriptor", Receiver.class, new InvocationPlugin() {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver frame) {
if (frame.get(false) instanceof NewFrameNode) {
b.push(JavaKind.Object, ((NewFrameNode) frame.get()).getDescriptor());
return true;
}
return false;
}
});
r.register1("materialize", Receiver.class, new InvocationPlugin() {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
ValueNode frame = receiver.get();
if (TruffleCompilerOptions.getValue(Options.TruffleIntrinsifyFrameAccess) && frame instanceof NewFrameNode && ((NewFrameNode) frame).getIntrinsifyAccessors()) {
JavaConstant speculation = b.getGraph().getSpeculationLog().speculate(((NewFrameNode) frame).getIntrinsifyAccessorsSpeculation());
b.add(new DeoptimizeNode(DeoptimizationAction.InvalidateRecompile, DeoptimizationReason.RuntimeConstraint, speculation));
return true;
}
b.addPush(JavaKind.Object, new AllowMaterializeNode(frame));
return true;
}
});
}
use of org.graalvm.compiler.nodes.ValueNode in project graal by oracle.
the class TruffleGraphBuilderPlugins method registerFrameAccessors.
/**
* We intrinsify the getXxx, setXxx, and isXxx methods for all type tags. The intrinsic nodes
* are lightweight fixed nodes without a {@link FrameState}. No {@link FrameState} is important
* for partial evaluation performance, because creating and later on discarding FrameStates for
* the setXxx methods have a high compile time cost.
*
* Intrinsification requires the following conditions: (1) the accessed frame is directly the
* {@link NewFrameNode}, (2) the accessed FrameSlot is a constant, and (3) the FrameDescriptor
* was never materialized before. All three conditions together guarantee that the escape
* analysis can virtualize the access. The condition (3) is necessary because a possible
* materialization of the frame can prevent escape analysis - so in that case a FrameState for
* setXxx methods is actually necessary since they stores can be state-changing memory
* operations.
*
* Note that we do not register an intrinsification for {@code FrameWithoutBoxing.getValue()}.
* It is a complicated method to intrinsify, and it is not used frequently enough to justify the
* complexity of an intrinsification.
*/
private static void registerFrameAccessors(Registration r, JavaKind accessKind, ConstantReflectionProvider constantReflection, KnownTruffleTypes types) {
TruffleCompilerRuntime runtime = TruffleCompilerRuntime.getRuntime();
int accessTag = runtime.getFrameSlotKindTagForJavaKind(accessKind);
String nameSuffix = accessKind.name();
ResolvedJavaSymbol frameSlotType = new ResolvedJavaSymbol(types.classFrameSlot);
r.register2("get" + nameSuffix, Receiver.class, frameSlotType, new InvocationPlugin() {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver frameNode, ValueNode frameSlotNode) {
int frameSlotIndex = maybeGetConstantFrameSlotIndex(frameNode, frameSlotNode, constantReflection, types);
if (frameSlotIndex >= 0) {
b.addPush(accessKind, new VirtualFrameGetNode(frameNode, frameSlotIndex, accessKind, accessTag));
return true;
}
return false;
}
});
r.register3("set" + nameSuffix, Receiver.class, frameSlotType, accessKind == JavaKind.Object ? Object.class : accessKind.toJavaClass(), new InvocationPlugin() {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver frameNode, ValueNode frameSlotNode, ValueNode value) {
int frameSlotIndex = maybeGetConstantFrameSlotIndex(frameNode, frameSlotNode, constantReflection, types);
if (frameSlotIndex >= 0) {
b.add(new VirtualFrameSetNode(frameNode, frameSlotIndex, accessTag, value));
return true;
}
return false;
}
});
r.register2("is" + nameSuffix, Receiver.class, frameSlotType, new InvocationPlugin() {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver frameNode, ValueNode frameSlotNode) {
int frameSlotIndex = maybeGetConstantFrameSlotIndex(frameNode, frameSlotNode, constantReflection, types);
if (frameSlotIndex >= 0) {
b.addPush(JavaKind.Boolean, new VirtualFrameIsNode(frameNode, frameSlotIndex, accessTag));
return true;
}
return false;
}
});
}
use of org.graalvm.compiler.nodes.ValueNode in project graal by oracle.
the class NewFrameNode method virtualize.
@Override
public void virtualize(VirtualizerTool tool) {
ResolvedJavaType frameType = stamp(NodeView.DEFAULT).javaType(tool.getMetaAccessProvider());
ResolvedJavaField[] frameFields = frameType.getInstanceFields(true);
ResolvedJavaField descriptorField = findField(frameFields, "descriptor");
ResolvedJavaField argumentsField = findField(frameFields, "arguments");
ResolvedJavaField localsField = findField(frameFields, "locals");
ResolvedJavaField primitiveLocalsField = findField(frameFields, "primitiveLocals");
ResolvedJavaField tagsField = findField(frameFields, "tags");
ValueNode[] objectArrayEntryState = new ValueNode[frameSize];
ValueNode[] primitiveArrayEntryState = new ValueNode[frameSize];
ValueNode[] tagArrayEntryState = new ValueNode[frameSize];
if (frameSize > 0) {
Arrays.fill(objectArrayEntryState, frameDefaultValue);
if (virtualFrameTagArray != null) {
Arrays.fill(tagArrayEntryState, smallIntConstants.get(0));
}
if (virtualFramePrimitiveArray != null) {
for (int i = 0; i < frameSize; i++) {
JavaKind kind = frameSlotKinds[i];
if (kind == null) {
kind = JavaKind.Int;
}
primitiveArrayEntryState[i] = ConstantNode.defaultForKind(kind, graph());
}
}
}
tool.createVirtualObject(virtualFrameObjectArray, objectArrayEntryState, Collections.<MonitorIdNode>emptyList(), false);
if (virtualFramePrimitiveArray != null) {
tool.createVirtualObject(virtualFramePrimitiveArray, primitiveArrayEntryState, Collections.<MonitorIdNode>emptyList(), false);
}
if (virtualFrameTagArray != null) {
tool.createVirtualObject(virtualFrameTagArray, tagArrayEntryState, Collections.<MonitorIdNode>emptyList(), false);
}
assert frameFields.length == 5 || frameFields.length == 3;
ValueNode[] frameEntryState = new ValueNode[frameFields.length];
List<ResolvedJavaField> frameFieldList = Arrays.asList(frameFields);
frameEntryState[frameFieldList.indexOf(descriptorField)] = getDescriptor();
frameEntryState[frameFieldList.indexOf(argumentsField)] = getArguments();
frameEntryState[frameFieldList.indexOf(localsField)] = virtualFrameObjectArray;
if (primitiveLocalsField != null) {
frameEntryState[frameFieldList.indexOf(primitiveLocalsField)] = virtualFramePrimitiveArray;
}
if (tagsField != null) {
frameEntryState[frameFieldList.indexOf(tagsField)] = virtualFrameTagArray;
}
/*
* The new frame is created with "ensureVirtualized" enabled, so that it cannot be
* materialized. This can only be lifted by a AllowMaterializeNode, which corresponds to a
* frame.materialize() call.
*/
tool.createVirtualObject(virtualFrame, frameEntryState, Collections.<MonitorIdNode>emptyList(), true);
tool.replaceWithVirtual(virtualFrame);
}
use of org.graalvm.compiler.nodes.ValueNode in project graal by oracle.
the class LoadJavaMirrorWithKlassPhase method getClassConstantReplacement.
private ValueNode getClassConstantReplacement(StructuredGraph graph, PhaseContext context, JavaConstant constant) {
if (constant instanceof HotSpotObjectConstant) {
ConstantReflectionProvider constantReflection = context.getConstantReflection();
ResolvedJavaType type = constantReflection.asJavaType(constant);
if (type != null) {
MetaAccessProvider metaAccess = context.getMetaAccess();
Stamp stamp = StampFactory.objectNonNull(TypeReference.createExactTrusted(metaAccess.lookupJavaType(Class.class)));
if (type instanceof HotSpotResolvedObjectType) {
ConstantNode klass = ConstantNode.forConstant(KlassPointerStamp.klassNonNull(), ((HotSpotResolvedObjectType) type).klass(), metaAccess, graph);
ValueNode getClass = graph.unique(new HubGetClassNode(metaAccess, klass));
if (((HotSpotObjectConstant) constant).isCompressed()) {
return HotSpotCompressionNode.compress(getClass, oopEncoding);
} else {
return getClass;
}
} else {
/*
* Primitive classes are more difficult since they don't have a corresponding
* Klass* so get them from Class.TYPE for the java box type.
*/
HotSpotResolvedPrimitiveType primitive = (HotSpotResolvedPrimitiveType) type;
ResolvedJavaType boxingClass = metaAccess.lookupJavaType(primitive.getJavaKind().toBoxedJavaClass());
ConstantNode clazz = ConstantNode.forConstant(context.getConstantReflection().asJavaClass(boxingClass), metaAccess, graph);
HotSpotResolvedJavaField[] a = (HotSpotResolvedJavaField[]) boxingClass.getStaticFields();
HotSpotResolvedJavaField typeField = null;
for (HotSpotResolvedJavaField f : a) {
if (f.getName().equals("TYPE")) {
typeField = f;
break;
}
}
if (typeField == null) {
throw new GraalError("Can't find TYPE field in class");
}
if (oopEncoding != null) {
stamp = HotSpotNarrowOopStamp.compressed((AbstractObjectStamp) stamp, oopEncoding);
}
AddressNode address = graph.unique(new OffsetAddressNode(clazz, ConstantNode.forLong(typeField.offset(), graph)));
ValueNode read = graph.unique(new FloatingReadNode(address, FINAL_LOCATION, null, stamp));
if (oopEncoding == null || ((HotSpotObjectConstant) constant).isCompressed()) {
return read;
} else {
return HotSpotCompressionNode.uncompress(read, oopEncoding);
}
}
}
}
return null;
}
use of org.graalvm.compiler.nodes.ValueNode in project graal by oracle.
the class OnStackReplacementPhase method run.
@Override
@SuppressWarnings("try")
protected void run(StructuredGraph graph) {
DebugContext debug = graph.getDebug();
if (graph.getEntryBCI() == JVMCICompiler.INVOCATION_ENTRY_BCI) {
// used.
assert graph.getNodes(EntryMarkerNode.TYPE).isEmpty();
return;
}
debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement initial at bci %d", graph.getEntryBCI());
EntryMarkerNode osr;
int maxIterations = -1;
int iterations = 0;
final EntryMarkerNode originalOSRNode = getEntryMarker(graph);
final LoopBeginNode originalOSRLoop = osrLoop(originalOSRNode);
final boolean currentOSRWithLocks = osrWithLocks(originalOSRNode);
if (originalOSRLoop == null) {
/*
* OSR with Locks: We do not have an OSR loop for the original OSR bci. Therefore we
* cannot decide where to deopt and which framestate will be used. In the worst case the
* framestate of the OSR entry would be used.
*/
throw new PermanentBailoutException("OSR compilation without OSR entry loop.");
}
if (!supportOSRWithLocks(graph.getOptions()) && currentOSRWithLocks) {
throw new PermanentBailoutException("OSR with locks disabled.");
}
do {
osr = getEntryMarker(graph);
LoopsData loops = new LoopsData(graph);
// Find the loop that contains the EntryMarker
Loop<Block> l = loops.getCFG().getNodeToBlock().get(osr).getLoop();
if (l == null) {
break;
}
iterations++;
if (maxIterations == -1) {
maxIterations = l.getDepth();
} else if (iterations > maxIterations) {
throw GraalError.shouldNotReachHere();
}
// Peel the outermost loop first
while (l.getParent() != null) {
l = l.getParent();
}
LoopTransformations.peel(loops.loop(l));
osr.replaceAtUsages(InputType.Guard, AbstractBeginNode.prevBegin((FixedNode) osr.predecessor()));
for (Node usage : osr.usages().snapshot()) {
EntryProxyNode proxy = (EntryProxyNode) usage;
proxy.replaceAndDelete(proxy.value());
}
GraphUtil.removeFixedWithUnusedInputs(osr);
debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement loop peeling result");
} while (true);
StartNode start = graph.start();
FrameState osrState = osr.stateAfter();
OSRStartNode osrStart;
try (DebugCloseable context = osr.withNodeSourcePosition()) {
osr.setStateAfter(null);
osrStart = graph.add(new OSRStartNode());
FixedNode next = osr.next();
osr.setNext(null);
osrStart.setNext(next);
graph.setStart(osrStart);
osrStart.setStateAfter(osrState);
debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement after setting OSR start");
final int localsSize = osrState.localsSize();
final int locksSize = osrState.locksSize();
for (int i = 0; i < localsSize + locksSize; i++) {
ValueNode value = null;
if (i >= localsSize) {
value = osrState.lockAt(i - localsSize);
} else {
value = osrState.localAt(i);
}
if (value instanceof EntryProxyNode) {
EntryProxyNode proxy = (EntryProxyNode) value;
/*
* We need to drop the stamp since the types we see during OSR may be too
* precise (if a branch was not parsed for example). In cases when this is
* possible, we insert a guard and narrow the OSRLocal stamp at its usages.
*/
Stamp narrowedStamp = proxy.value().stamp(NodeView.DEFAULT);
Stamp unrestrictedStamp = proxy.stamp(NodeView.DEFAULT).unrestricted();
ValueNode osrLocal;
if (i >= localsSize) {
osrLocal = graph.addOrUnique(new OSRLockNode(i - localsSize, unrestrictedStamp));
} else {
osrLocal = graph.addOrUnique(new OSRLocalNode(i, unrestrictedStamp));
}
// Speculate on the OSRLocal stamps that could be more precise.
OSRLocalSpeculationReason reason = new OSRLocalSpeculationReason(osrState.bci, narrowedStamp, i);
if (graph.getSpeculationLog().maySpeculate(reason) && osrLocal instanceof OSRLocalNode && value.getStackKind().equals(JavaKind.Object) && !narrowedStamp.isUnrestricted()) {
// Add guard.
LogicNode check = graph.addOrUniqueWithInputs(InstanceOfNode.createHelper((ObjectStamp) narrowedStamp, osrLocal, null, null));
JavaConstant constant = graph.getSpeculationLog().speculate(reason);
FixedGuardNode guard = graph.add(new FixedGuardNode(check, DeoptimizationReason.OptimizedTypeCheckViolated, DeoptimizationAction.InvalidateRecompile, constant, false));
graph.addAfterFixed(osrStart, guard);
// Replace with a more specific type at usages.
// We know that we are at the root,
// so we need to replace the proxy in the state.
proxy.replaceAtMatchingUsages(osrLocal, n -> n == osrState);
osrLocal = graph.addOrUnique(new PiNode(osrLocal, narrowedStamp, guard));
}
proxy.replaceAndDelete(osrLocal);
} else {
assert value == null || value instanceof OSRLocalNode;
}
}
osr.replaceAtUsages(InputType.Guard, osrStart);
}
debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement after replacing entry proxies");
GraphUtil.killCFG(start);
debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement result");
new DeadCodeEliminationPhase(Required).apply(graph);
if (currentOSRWithLocks) {
OsrWithLocksCount.increment(debug);
try (DebugCloseable context = osrStart.withNodeSourcePosition()) {
for (int i = osrState.monitorIdCount() - 1; i >= 0; --i) {
MonitorIdNode id = osrState.monitorIdAt(i);
ValueNode lockedObject = osrState.lockAt(i);
OSRMonitorEnterNode osrMonitorEnter = graph.add(new OSRMonitorEnterNode(lockedObject, id));
for (Node usage : id.usages()) {
if (usage instanceof AccessMonitorNode) {
AccessMonitorNode access = (AccessMonitorNode) usage;
access.setObject(lockedObject);
}
}
FixedNode oldNext = osrStart.next();
oldNext.replaceAtPredecessor(null);
osrMonitorEnter.setNext(oldNext);
osrStart.setNext(osrMonitorEnter);
}
}
debug.dump(DebugContext.DETAILED_LEVEL, graph, "After inserting OSR monitor enters");
/*
* Ensure balanced monitorenter - monitorexit
*
* Ensure that there is no monitor exit without a monitor enter in the graph. If there
* is one this can only be done by bytecode as we have the monitor enter before the OSR
* loop but the exit in a path of the loop that must be under a condition, else it will
* throw an IllegalStateException anyway in the 2.iteration
*/
for (MonitorExitNode exit : graph.getNodes(MonitorExitNode.TYPE)) {
MonitorIdNode id = exit.getMonitorId();
if (id.usages().filter(MonitorEnterNode.class).count() != 1) {
throw new PermanentBailoutException("Unbalanced monitor enter-exit in OSR compilation with locks. Object is locked before the loop but released inside the loop.");
}
}
}
debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement result");
new DeadCodeEliminationPhase(Required).apply(graph);
/*
* There must not be any parameter nodes left after OSR compilation.
*/
assert graph.getNodes(ParameterNode.TYPE).count() == 0 : "OSR Compilation contains references to parameters.";
}
Aggregations