1 /*
   2  * Copyright (c) 2011, 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.InputType.Memory;
  26 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_30;
  27 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_8;
  28 
  29 import org.graalvm.compiler.core.common.LocationIdentity;
  30 import org.graalvm.compiler.core.common.type.StampFactory;
  31 import org.graalvm.compiler.graph.NodeClass;
  32 import org.graalvm.compiler.nodeinfo.NodeInfo;
  33 import org.graalvm.compiler.nodes.ValueNode;
  34 import org.graalvm.compiler.nodes.memory.AbstractMemoryCheckpoint;
  35 import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
  36 import org.graalvm.compiler.nodes.spi.Lowerable;
  37 import org.graalvm.compiler.nodes.spi.LoweringTool;
  38 
  39 import jdk.vm.ci.meta.JavaKind;
  40 
  41 /**
  42  * Represents an atomic compare-and-swap operation The result is a boolean that contains whether the
  43  * value matched the expected value.
  44  */
  45 @NodeInfo(allowedUsageTypes = Memory, cycles = CYCLES_30, size = SIZE_8)
  46 public final class CompareAndSwapNode extends AbstractMemoryCheckpoint implements Lowerable, MemoryCheckpoint.Single {
  47 
  48     public static final NodeClass<CompareAndSwapNode> TYPE = NodeClass.create(CompareAndSwapNode.class);
  49     @Input ValueNode object;
  50     @Input ValueNode offset;
  51     @Input ValueNode expected;
  52     @Input ValueNode newValue;
  53 
  54     protected final JavaKind valueKind;
  55     protected final LocationIdentity locationIdentity;
  56 
  57     public CompareAndSwapNode(ValueNode object, ValueNode offset, ValueNode expected, ValueNode newValue, JavaKind valueKind, LocationIdentity locationIdentity) {
  58         super(TYPE, StampFactory.forKind(JavaKind.Boolean.getStackKind()));
  59         assert expected.stamp().isCompatible(newValue.stamp());
  60         this.object = object;
  61         this.offset = offset;
  62         this.expected = expected;
  63         this.newValue = newValue;
  64         this.valueKind = valueKind;
  65         this.locationIdentity = locationIdentity;
  66     }
  67 
  68     public ValueNode object() {
  69         return object;
  70     }
  71 
  72     public ValueNode offset() {
  73         return offset;
  74     }
  75 
  76     public ValueNode expected() {
  77         return expected;
  78     }
  79 
  80     public ValueNode newValue() {
  81         return newValue;
  82     }
  83 
  84     public JavaKind getValueKind() {
  85         return valueKind;
  86     }
  87 
  88     @Override
  89     public LocationIdentity getLocationIdentity() {
  90         return locationIdentity;
  91     }
  92 
  93     @Override
  94     public void lower(LoweringTool tool) {
  95         tool.getLowerer().lower(this, tool);
  96     }
  97 }