MonitorExitNode.java revision 12657:6ef01bd40ce2
1/*
2 * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.nodes.java;
24
25import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_50;
26import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_100;
27
28import org.graalvm.compiler.core.common.LocationIdentity;
29import org.graalvm.compiler.graph.IterableNodeType;
30import org.graalvm.compiler.graph.NodeClass;
31import org.graalvm.compiler.nodeinfo.NodeInfo;
32import org.graalvm.compiler.nodes.ValueNode;
33import org.graalvm.compiler.nodes.extended.MonitorExit;
34import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
35import org.graalvm.compiler.nodes.spi.Lowerable;
36import org.graalvm.compiler.nodes.spi.LoweringTool;
37import org.graalvm.compiler.nodes.spi.Virtualizable;
38import org.graalvm.compiler.nodes.spi.VirtualizerTool;
39import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
40
41/**
42 * The {@code MonitorExitNode} represents a monitor release. If it is the release of the monitor of
43 * a synchronized method, then the return value of the method will be referenced via the edge
44 * {@link #escapedReturnValue}, so that it will be materialized before releasing the monitor.
45 */
46@NodeInfo(cycles = CYCLES_50, size = SIZE_100)
47public final class MonitorExitNode extends AccessMonitorNode implements Virtualizable, Lowerable, IterableNodeType, MonitorExit, MemoryCheckpoint.Single {
48
49    public static final NodeClass<MonitorExitNode> TYPE = NodeClass.create(MonitorExitNode.class);
50
51    /**
52     * Non-null for the monitor exit introduced due to a synchronized root method and null in all
53     * other cases.
54     */
55    @OptionalInput ValueNode escapedReturnValue;
56
57    public MonitorExitNode(ValueNode object, MonitorIdNode monitorId, ValueNode escapedReturnValue) {
58        super(TYPE, object, monitorId);
59        this.escapedReturnValue = escapedReturnValue;
60    }
61
62    /**
63     * Return value is cleared when a synchronized method graph is inlined.
64     */
65    public void clearEscapedReturnValue() {
66        updateUsages(escapedReturnValue, null);
67        this.escapedReturnValue = null;
68    }
69
70    @Override
71    public LocationIdentity getLocationIdentity() {
72        return LocationIdentity.any();
73    }
74
75    @Override
76    public void lower(LoweringTool tool) {
77        tool.getLowerer().lower(this, tool);
78    }
79
80    @Override
81    public void virtualize(VirtualizerTool tool) {
82        ValueNode alias = tool.getAlias(object());
83        if (alias instanceof VirtualObjectNode) {
84            VirtualObjectNode virtual = (VirtualObjectNode) alias;
85            if (virtual.hasIdentity()) {
86                MonitorIdNode removedLock = tool.removeLock(virtual);
87                assert removedLock == getMonitorId() : "mismatch at " + this + ": " + removedLock + " vs. " + getMonitorId();
88                tool.delete();
89            }
90        }
91    }
92}
93