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  */
  23 package org.graalvm.compiler.nodes.java;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_50;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_100;
  27 
  28 import org.graalvm.compiler.core.common.LocationIdentity;
  29 import org.graalvm.compiler.graph.IterableNodeType;
  30 import org.graalvm.compiler.graph.NodeClass;
  31 import org.graalvm.compiler.nodeinfo.NodeInfo;
  32 import org.graalvm.compiler.nodes.ValueNode;
  33 import org.graalvm.compiler.nodes.extended.MonitorExit;
  34 import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
  35 import org.graalvm.compiler.nodes.spi.Lowerable;
  36 import org.graalvm.compiler.nodes.spi.LoweringTool;
  37 import org.graalvm.compiler.nodes.spi.Virtualizable;
  38 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  39 import 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)
  47 public 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 }