use of org.graalvm.compiler.nodes.java.MonitorIdNode 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.";
}
use of org.graalvm.compiler.nodes.java.MonitorIdNode in project graal by oracle.
the class DefaultHotSpotLoweringProvider method lowerOSRStartNode.
private void lowerOSRStartNode(OSRStartNode osrStart) {
StructuredGraph graph = osrStart.graph();
if (graph.getGuardsStage() == StructuredGraph.GuardsStage.FIXED_DEOPTS) {
StartNode newStart = graph.add(new StartNode());
ParameterNode buffer = graph.addWithoutUnique(new ParameterNode(0, StampPair.createSingle(StampFactory.forKind(runtime.getTarget().wordJavaKind))));
ForeignCallNode migrationEnd = graph.add(new ForeignCallNode(foreignCalls, OSR_MIGRATION_END, buffer));
migrationEnd.setStateAfter(osrStart.stateAfter());
newStart.setNext(migrationEnd);
FixedNode next = osrStart.next();
osrStart.setNext(null);
migrationEnd.setNext(next);
graph.setStart(newStart);
final int wordSize = target.wordSize;
// @formatter:off
// taken from c2 locals_addr = osr_buf + (max_locals-1)*wordSize)
// @formatter:on
int localsOffset = (graph.method().getMaxLocals() - 1) * wordSize;
for (OSRLocalNode osrLocal : graph.getNodes(OSRLocalNode.TYPE)) {
int size = osrLocal.getStackKind().getSlotCount();
int offset = localsOffset - (osrLocal.index() + size - 1) * wordSize;
AddressNode address = createOffsetAddress(graph, buffer, offset);
ReadNode load = graph.add(new ReadNode(address, any(), osrLocal.stamp(NodeView.DEFAULT), BarrierType.NONE));
osrLocal.replaceAndDelete(load);
graph.addBeforeFixed(migrationEnd, load);
}
// @formatter:off
// taken from c2 monitors_addr = osr_buf + (max_locals+mcnt*2-1)*wordSize);
// @formatter:on
final int lockCount = osrStart.stateAfter().locksSize();
final int locksOffset = (graph.method().getMaxLocals() + lockCount * 2 - 1) * wordSize;
// buffer
for (OSRMonitorEnterNode osrMonitorEnter : graph.getNodes(OSRMonitorEnterNode.TYPE)) {
MonitorIdNode monitorID = osrMonitorEnter.getMonitorId();
OSRLockNode lock = (OSRLockNode) osrMonitorEnter.object();
final int index = lock.index();
final int offsetDisplacedHeader = locksOffset - ((index * 2) + 1) * wordSize;
final int offsetLockObject = locksOffset - index * 2 * wordSize;
// load the displaced mark from the osr buffer
AddressNode addressDisplacedHeader = createOffsetAddress(graph, buffer, offsetDisplacedHeader);
ReadNode loadDisplacedHeader = graph.add(new ReadNode(addressDisplacedHeader, any(), lock.stamp(NodeView.DEFAULT), BarrierType.NONE));
graph.addBeforeFixed(migrationEnd, loadDisplacedHeader);
// we need to initialize the stack slot for the lock
BeginLockScopeNode beginLockScope = graph.add(new BeginLockScopeNode(lock.getStackKind(), monitorID.getLockDepth()));
graph.addBeforeFixed(migrationEnd, beginLockScope);
// write the displaced mark to the correct stack slot
AddressNode addressDisplacedMark = createOffsetAddress(graph, beginLockScope, runtime.getVMConfig().basicLockDisplacedHeaderOffset);
WriteNode writeStackSlot = graph.add(new WriteNode(addressDisplacedMark, DISPLACED_MARK_WORD_LOCATION, loadDisplacedHeader, BarrierType.NONE));
graph.addBeforeFixed(migrationEnd, writeStackSlot);
// load the lock object from the osr buffer
AddressNode addressLockObject = createOffsetAddress(graph, buffer, offsetLockObject);
ReadNode loadObject = graph.add(new ReadNode(addressLockObject, any(), lock.stamp(NodeView.DEFAULT), BarrierType.NONE));
lock.replaceAndDelete(loadObject);
graph.addBeforeFixed(migrationEnd, loadObject);
}
osrStart.replaceAtUsagesAndDelete(newStart);
}
}
use of org.graalvm.compiler.nodes.java.MonitorIdNode in project graal by oracle.
the class BytecodeParser method createStateAfterStartOfReplacementGraph.
/**
* Creates the frame state after the start node of a graph for an {@link IntrinsicContext
* intrinsic} that is the parse root (either for root compiling or for post-parse inlining).
*/
private FrameState createStateAfterStartOfReplacementGraph() {
assert parent == null;
assert frameState.getMethod().equals(intrinsicContext.getIntrinsicMethod());
assert bci() == 0;
assert frameState.stackSize() == 0;
FrameState stateAfterStart;
if (intrinsicContext.isPostParseInlined()) {
stateAfterStart = graph.add(new FrameState(BytecodeFrame.BEFORE_BCI));
} else {
ResolvedJavaMethod original = intrinsicContext.getOriginalMethod();
ValueNode[] locals;
if (original.getMaxLocals() == frameState.localsSize() || original.isNative()) {
locals = new ValueNode[original.getMaxLocals()];
for (int i = 0; i < locals.length; i++) {
ValueNode node = frameState.locals[i];
if (node == FrameState.TWO_SLOT_MARKER) {
node = null;
}
locals[i] = node;
}
} else {
locals = new ValueNode[original.getMaxLocals()];
int parameterCount = original.getSignature().getParameterCount(!original.isStatic());
for (int i = 0; i < parameterCount; i++) {
ValueNode param = frameState.locals[i];
if (param == FrameState.TWO_SLOT_MARKER) {
param = null;
}
locals[i] = param;
assert param == null || param instanceof ParameterNode || param.isConstant();
}
}
ValueNode[] stack = {};
int stackSize = 0;
ValueNode[] locks = {};
List<MonitorIdNode> monitorIds = Collections.emptyList();
stateAfterStart = graph.add(new FrameState(null, new ResolvedJavaMethodBytecode(original), 0, locals, stack, stackSize, locks, monitorIds, false, false));
}
return stateAfterStart;
}
use of org.graalvm.compiler.nodes.java.MonitorIdNode in project graal by oracle.
the class DefaultJavaLoweringProvider method finishAllocatedObjects.
public void finishAllocatedObjects(LoweringTool tool, CommitAllocationNode commit, ValueNode[] allocations) {
StructuredGraph graph = commit.graph();
for (int objIndex = 0; objIndex < commit.getVirtualObjects().size(); objIndex++) {
FixedValueAnchorNode anchor = graph.add(new FixedValueAnchorNode(allocations[objIndex]));
allocations[objIndex] = anchor;
graph.addBeforeFixed(commit, anchor);
}
/*
* Note that the FrameState that is assigned to these MonitorEnterNodes isn't the correct
* state. It will be the state from before the allocation occurred instead of a valid state
* after the locking is performed. In practice this should be fine since these are newly
* allocated objects. The bytecodes themselves permit allocating an object, doing a
* monitorenter and then dropping all references to the object which would produce the same
* state, though that would normally produce an IllegalMonitorStateException. In HotSpot
* some form of fast path locking should always occur so the FrameState should never
* actually be used.
*/
ArrayList<MonitorEnterNode> enters = null;
for (int objIndex = 0; objIndex < commit.getVirtualObjects().size(); objIndex++) {
List<MonitorIdNode> locks = commit.getLocks(objIndex);
if (locks.size() > 1) {
// Ensure that the lock operations are performed in lock depth order
ArrayList<MonitorIdNode> newList = new ArrayList<>(locks);
newList.sort((a, b) -> Integer.compare(a.getLockDepth(), b.getLockDepth()));
locks = newList;
}
int lastDepth = -1;
for (MonitorIdNode monitorId : locks) {
assert lastDepth < monitorId.getLockDepth();
lastDepth = monitorId.getLockDepth();
MonitorEnterNode enter = graph.add(new MonitorEnterNode(allocations[objIndex], monitorId));
graph.addBeforeFixed(commit, enter);
if (enters == null) {
enters = new ArrayList<>();
}
enters.add(enter);
}
}
for (Node usage : commit.usages().snapshot()) {
if (usage instanceof AllocatedObjectNode) {
AllocatedObjectNode addObject = (AllocatedObjectNode) usage;
int index = commit.getVirtualObjects().indexOf(addObject.getVirtualObject());
addObject.replaceAtUsagesAndDelete(allocations[index]);
} else {
assert enters != null;
commit.replaceAtUsages(InputType.Memory, enters.get(enters.size() - 1));
}
}
if (enters != null) {
for (MonitorEnterNode enter : enters) {
enter.lower(tool);
}
}
assert commit.hasNoUsages();
insertAllocationBarrier(commit, graph);
}
use of org.graalvm.compiler.nodes.java.MonitorIdNode in project graal by oracle.
the class FrameState method verify.
@Override
public boolean verify() {
if (virtualObjectMappingCount() > 0) {
for (EscapeObjectState state : virtualObjectMappings()) {
assertTrue(state != null, "must be non-null");
}
}
/*
* The outermost FrameState should have a method that matches StructuredGraph.method except
* when it's a substitution or it's null.
*/
assertTrue(outerFrameState != null || graph() == null || graph().method() == null || code == null || Objects.equals(graph().method(), code.getMethod()) || graph().method().getAnnotation(MethodSubstitution.class) != null, "wrong outerFrameState %s != %s", code == null ? "null" : code.getMethod(), graph().method());
if (monitorIds() != null && monitorIds().size() > 0) {
int depth = outerLockDepth();
for (MonitorIdNode monitor : monitorIds()) {
assertTrue(monitor.getLockDepth() == depth++, "wrong depth");
}
}
assertTrue(locksSize() == monitorIdCount(), "mismatch in number of locks");
for (ValueNode value : values) {
assertTrue(value == null || !value.isDeleted(), "frame state must not contain deleted nodes: %s", value);
assertTrue(value == null || value instanceof VirtualObjectNode || (value.getStackKind() != JavaKind.Void), "unexpected value: %s", value);
}
verifyAfterExceptionState();
return super.verify();
}
Aggregations