001/*
002 * Copyright (c) 2012, 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.hotspot.nodes;
024
025import jdk.internal.jvmci.meta.*;
026
027import com.oracle.graal.graph.*;
028import com.oracle.graal.hotspot.*;
029import com.oracle.graal.nodeinfo.*;
030import com.oracle.graal.nodes.*;
031import com.oracle.graal.nodes.java.*;
032import com.oracle.graal.nodes.memory.*;
033import com.oracle.graal.nodes.memory.address.*;
034import com.oracle.graal.nodes.memory.address.AddressNode.Address;
035import com.oracle.graal.nodes.spi.*;
036import com.oracle.graal.word.*;
037
038/**
039 * A special purpose store node that differs from {@link CompareAndSwapNode} in that it is not a
040 * {@link StateSplit} and it {@linkplain #compareAndSwap(Address, Word, Word, LocationIdentity)}
041 * returns either the expected value or the compared against value instead of a boolean.
042 */
043@NodeInfo(allowedUsageTypes = {InputType.Memory})
044public final class DirectCompareAndSwapNode extends FixedWithNextNode implements LIRLowerable, MemoryCheckpoint.Single {
045
046    public static final NodeClass<DirectCompareAndSwapNode> TYPE = NodeClass.create(DirectCompareAndSwapNode.class);
047    @Input(InputType.Association) AddressNode address;
048    @Input ValueNode expectedValue;
049    @Input ValueNode newValue;
050
051    protected final LocationIdentity locationIdentity;
052
053    public DirectCompareAndSwapNode(ValueNode address, ValueNode expected, ValueNode newValue, LocationIdentity locationIdentity) {
054        super(TYPE, expected.stamp());
055        this.address = (AddressNode) address;
056        this.expectedValue = expected;
057        this.newValue = newValue;
058        this.locationIdentity = locationIdentity;
059    }
060
061    public AddressNode getAddress() {
062        return address;
063    }
064
065    public ValueNode expectedValue() {
066        return expectedValue;
067    }
068
069    public ValueNode newValue() {
070        return newValue;
071    }
072
073    @Override
074    public LocationIdentity getLocationIdentity() {
075        return locationIdentity;
076    }
077
078    @Override
079    public void generate(NodeLIRBuilderTool gen) {
080        ((HotSpotNodeLIRBuilder) gen).visitDirectCompareAndSwap(this);
081    }
082
083    /**
084     * Compares an expected value with the actual value in a location denoted by an address. Iff
085     * they are same, {@code newValue} is placed into the location and the {@code expectedValue} is
086     * returned. Otherwise, the actual value is returned. All of the above is performed in one
087     * atomic hardware transaction.
088     *
089     * @param address the address to be atomically tested and updated
090     * @param expectedValue if this value is currently in the field, perform the swap
091     * @param newValue the new value to put into the field
092     * @return either {@code expectedValue} or the actual value
093     */
094    @NodeIntrinsic
095    public static native Word compareAndSwap(Address address, Word expectedValue, Word newValue, @ConstantNodeParameter LocationIdentity locationIdentity);
096}