1 /* 2 * Copyright (c) 2012, 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.hotspot.nodes; 24 25 import static org.graalvm.compiler.nodeinfo.InputType.Association; 26 import static org.graalvm.compiler.nodeinfo.InputType.Memory; 27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_30; 28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_8; 29 30 import org.graalvm.compiler.core.common.LocationIdentity; 31 import org.graalvm.compiler.graph.NodeClass; 32 import org.graalvm.compiler.hotspot.HotSpotNodeLIRBuilder; 33 import org.graalvm.compiler.nodeinfo.NodeInfo; 34 import org.graalvm.compiler.nodes.FixedWithNextNode; 35 import org.graalvm.compiler.nodes.StateSplit; 36 import org.graalvm.compiler.nodes.ValueNode; 37 import org.graalvm.compiler.nodes.java.CompareAndSwapNode; 38 import org.graalvm.compiler.nodes.memory.MemoryCheckpoint; 39 import org.graalvm.compiler.nodes.memory.address.AddressNode; 40 import org.graalvm.compiler.nodes.memory.address.AddressNode.Address; 41 import org.graalvm.compiler.nodes.spi.LIRLowerable; 42 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 43 import org.graalvm.compiler.word.Word; 44 45 /** 46 * A special purpose store node that differs from {@link CompareAndSwapNode} in that it is not a 47 * {@link StateSplit} and it {@linkplain #compareAndSwap(Address, Word, Word, LocationIdentity)} 48 * returns either the expected value or the compared against value instead of a boolean. 49 */ 50 @NodeInfo(allowedUsageTypes = Memory, cycles = CYCLES_30, size = SIZE_8) 51 public final class DirectCompareAndSwapNode extends FixedWithNextNode implements LIRLowerable, MemoryCheckpoint.Single { 52 53 public static final NodeClass<DirectCompareAndSwapNode> TYPE = NodeClass.create(DirectCompareAndSwapNode.class); 54 @Input(Association) AddressNode address; 55 @Input ValueNode expectedValue; 56 @Input ValueNode newValue; 57 58 protected final LocationIdentity locationIdentity; 59 60 public DirectCompareAndSwapNode(ValueNode address, ValueNode expected, ValueNode newValue, LocationIdentity locationIdentity) { 61 super(TYPE, expected.stamp()); 62 this.address = (AddressNode) address; 63 this.expectedValue = expected; 64 this.newValue = newValue; 65 this.locationIdentity = locationIdentity; 66 } 67 68 public AddressNode getAddress() { 69 return address; 70 } 71 72 public ValueNode expectedValue() { 73 return expectedValue; 74 } 75 76 public ValueNode newValue() { 77 return newValue; 78 } 79 80 @Override 81 public LocationIdentity getLocationIdentity() { 82 return locationIdentity; 83 } 84 85 @Override 86 public void generate(NodeLIRBuilderTool gen) { 87 ((HotSpotNodeLIRBuilder) gen).visitDirectCompareAndSwap(this); 88 } 89 90 /** 91 * Compares an expected value with the actual value in a location denoted by an address. Iff 92 * they are same, {@code newValue} is placed into the location and the {@code expectedValue} is 93 * returned. Otherwise, the actual value is returned. All of the above is performed in one 94 * atomic hardware transaction. 95 * 96 * @param address the address to be atomically tested and updated 97 * @param expectedValue if this value is currently in the field, perform the swap 98 * @param newValue the new value to put into the field 99 * @return either {@code expectedValue} or the actual value 100 */ 101 @NodeIntrinsic 102 public static native Word compareAndSwap(Address address, Word expectedValue, Word newValue, @ConstantNodeParameter LocationIdentity locationIdentity); 103 }