001/*
002 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.nodes.java;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.meta.*;
027
028import com.oracle.graal.graph.*;
029import com.oracle.graal.graph.spi.*;
030import com.oracle.graal.nodeinfo.*;
031import com.oracle.graal.nodes.*;
032import com.oracle.graal.nodes.extended.*;
033import com.oracle.graal.nodes.memory.*;
034import com.oracle.graal.nodes.spi.*;
035import com.oracle.graal.nodes.virtual.*;
036
037/**
038 * The {@code MonitorExitNode} represents a monitor release. If it is the release of the monitor of
039 * a synchronized method, then the return value of the method will be referenced, so that it will be
040 * materialized before releasing the monitor.
041 */
042@NodeInfo
043public final class MonitorExitNode extends AccessMonitorNode implements Virtualizable, Simplifiable, Lowerable, IterableNodeType, MonitorExit, MemoryCheckpoint.Single {
044
045    public static final NodeClass<MonitorExitNode> TYPE = NodeClass.create(MonitorExitNode.class);
046    @OptionalInput ValueNode escapedReturnValue;
047
048    public MonitorExitNode(ValueNode object, MonitorIdNode monitorId, ValueNode escapedReturnValue) {
049        super(TYPE, object, monitorId);
050        this.escapedReturnValue = escapedReturnValue;
051    }
052
053    public ValueNode getEscapedReturnValue() {
054        return escapedReturnValue;
055    }
056
057    public void setEscapedReturnValue(ValueNode x) {
058        updateUsages(escapedReturnValue, x);
059        this.escapedReturnValue = x;
060    }
061
062    @Override
063    public LocationIdentity getLocationIdentity() {
064        return LocationIdentity.any();
065    }
066
067    @Override
068    public void simplify(SimplifierTool tool) {
069        if (escapedReturnValue != null && stateAfter() != null && stateAfter().bci != BytecodeFrame.AFTER_BCI) {
070            ValueNode returnValue = escapedReturnValue;
071            setEscapedReturnValue(null);
072            tool.removeIfUnused(returnValue);
073        }
074    }
075
076    @Override
077    public void lower(LoweringTool tool) {
078        tool.getLowerer().lower(this, tool);
079    }
080
081    @Override
082    public void virtualize(VirtualizerTool tool) {
083        // the monitor exit for a synchronized method should never be virtualized
084        ValueNode alias = tool.getAlias(object());
085        if (alias instanceof VirtualObjectNode) {
086            VirtualObjectNode virtual = (VirtualObjectNode) alias;
087            if (virtual.hasIdentity()) {
088                MonitorIdNode removedLock = tool.removeLock(virtual);
089                assert removedLock == getMonitorId() : "mismatch at " + this + ": " + removedLock + " vs. " + getMonitorId();
090                tool.delete();
091            }
092        }
093    }
094}