1 /*
   2  * Copyright (c) 2011, 2017, 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.extended;
  24 
  25 import static org.graalvm.compiler.nodeinfo.InputType.State;
  26 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
  27 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
  28 
  29 import org.graalvm.compiler.core.common.type.StampFactory;
  30 import org.graalvm.compiler.graph.NodeClass;
  31 import org.graalvm.compiler.nodeinfo.NodeInfo;
  32 import org.graalvm.compiler.nodes.FrameState;
  33 import org.graalvm.compiler.nodes.StateSplit;
  34 import org.graalvm.compiler.nodes.ValueNode;
  35 import org.graalvm.compiler.nodes.java.StoreFieldNode;
  36 import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
  37 import org.graalvm.compiler.nodes.spi.Lowerable;
  38 import org.graalvm.compiler.nodes.spi.LoweringTool;
  39 import org.graalvm.compiler.nodes.spi.Virtualizable;
  40 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  41 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  42 import org.graalvm.word.LocationIdentity;
  43 
  44 import jdk.vm.ci.meta.Assumptions;
  45 import jdk.vm.ci.meta.JavaKind;
  46 import jdk.vm.ci.meta.ResolvedJavaField;
  47 
  48 /**
  49  * Store of a value at a location specified as an offset relative to an object. No null check is
  50  * performed before the store.
  51  */
  52 @NodeInfo(cycles = CYCLES_2, size = SIZE_1)
  53 public final class RawStoreNode extends UnsafeAccessNode implements StateSplit, Lowerable, Virtualizable, MemoryCheckpoint.Single {
  54 
  55     public static final NodeClass<RawStoreNode> TYPE = NodeClass.create(RawStoreNode.class);
  56     @Input ValueNode value;
  57     @OptionalInput(State) FrameState stateAfter;
  58     private final boolean needsBarrier;
  59 
  60     public RawStoreNode(ValueNode object, ValueNode offset, ValueNode value, JavaKind accessKind, LocationIdentity locationIdentity) {
  61         this(object, offset, value, accessKind, locationIdentity, true);
  62     }
  63 
  64     public RawStoreNode(ValueNode object, ValueNode offset, ValueNode value, JavaKind accessKind, LocationIdentity locationIdentity, boolean needsBarrier) {
  65         this(object, offset, value, accessKind, locationIdentity, needsBarrier, null, false);
  66     }
  67 
  68     public RawStoreNode(ValueNode object, ValueNode offset, ValueNode value, JavaKind accessKind, LocationIdentity locationIdentity, boolean needsBarrier, FrameState stateAfter,
  69                     boolean forceAnyLocation) {
  70         super(TYPE, StampFactory.forVoid(), object, offset, accessKind, locationIdentity, forceAnyLocation);
  71         this.value = value;
  72         this.needsBarrier = needsBarrier;
  73         this.stateAfter = stateAfter;
  74         assert accessKind != JavaKind.Void && accessKind != JavaKind.Illegal;
  75     }
  76 
  77     @NodeIntrinsic
  78     public static native Object storeObject(Object object, long offset, Object value, @ConstantNodeParameter JavaKind kind, @ConstantNodeParameter LocationIdentity locationIdentity,
  79                     @ConstantNodeParameter boolean needsBarrier);
  80 
  81     @NodeIntrinsic
  82     public static native Object storeChar(Object object, long offset, char value, @ConstantNodeParameter JavaKind kind, @ConstantNodeParameter LocationIdentity locationIdentity);
  83 
  84     public boolean needsBarrier() {
  85         return needsBarrier;
  86     }
  87 
  88     @Override
  89     public FrameState stateAfter() {
  90         return stateAfter;
  91     }
  92 
  93     @Override
  94     public void setStateAfter(FrameState x) {
  95         assert x == null || x.isAlive() : "frame state must be in a graph";
  96         updateUsages(stateAfter, x);
  97         stateAfter = x;
  98     }
  99 
 100     @Override
 101     public boolean hasSideEffect() {
 102         return true;
 103     }
 104 
 105     public ValueNode value() {
 106         return value;
 107     }
 108 
 109     @Override
 110     public void lower(LoweringTool tool) {
 111         tool.getLowerer().lower(this, tool);
 112     }
 113 
 114     @Override
 115     public void virtualize(VirtualizerTool tool) {
 116         ValueNode alias = tool.getAlias(object());
 117         if (alias instanceof VirtualObjectNode) {
 118             VirtualObjectNode virtual = (VirtualObjectNode) alias;
 119             ValueNode indexValue = tool.getAlias(offset());
 120             if (indexValue.isConstant()) {
 121                 long off = indexValue.asJavaConstant().asLong();
 122                 int entryIndex = virtual.entryIndexForOffset(off, accessKind());
 123                 if (entryIndex != -1 && tool.setVirtualEntry(virtual, entryIndex, value(), accessKind(), off)) {
 124                     tool.delete();
 125                 }
 126             }
 127         }
 128     }
 129 
 130     @Override
 131     protected ValueNode cloneAsFieldAccess(Assumptions assumptions, ResolvedJavaField field) {
 132         return new StoreFieldNode(object(), field, value(), stateAfter());
 133     }
 134 
 135     @Override
 136     protected ValueNode cloneAsArrayAccess(ValueNode location, LocationIdentity identity) {
 137         return new RawStoreNode(object(), location, value, accessKind(), identity, needsBarrier, stateAfter(), isAnyLocationForced());
 138     }
 139 
 140     public FrameState getState() {
 141         return stateAfter;
 142     }
 143 }