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.ConstantNode;
  33 import org.graalvm.compiler.nodes.FrameState;
  34 import org.graalvm.compiler.nodes.StateSplit;
  35 import org.graalvm.compiler.nodes.ValueNode;
  36 import org.graalvm.compiler.nodes.java.StoreFieldNode;
  37 import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
  38 import org.graalvm.compiler.nodes.spi.Lowerable;
  39 import org.graalvm.compiler.nodes.spi.LoweringTool;
  40 import org.graalvm.compiler.nodes.spi.Virtualizable;
  41 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  42 import org.graalvm.compiler.nodes.type.StampTool;
  43 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  44 import org.graalvm.word.LocationIdentity;
  45 
  46 import jdk.vm.ci.meta.Assumptions;
  47 import jdk.vm.ci.meta.JavaConstant;
  48 import jdk.vm.ci.meta.JavaKind;
  49 import jdk.vm.ci.meta.ResolvedJavaField;
  50 
  51 /**
  52  * Store of a value at a location specified as an offset relative to an object. No null check is
  53  * performed before the store.
  54  */
  55 @NodeInfo(cycles = CYCLES_2, size = SIZE_1)
  56 public final class RawStoreNode extends UnsafeAccessNode implements StateSplit, Lowerable, Virtualizable, MemoryCheckpoint.Single {
  57 
  58     public static final NodeClass<RawStoreNode> TYPE = NodeClass.create(RawStoreNode.class);
  59     @Input ValueNode value;
  60     @OptionalInput(State) FrameState stateAfter;
  61     private final boolean needsBarrier;
  62 
  63     public RawStoreNode(ValueNode object, ValueNode offset, ValueNode value, JavaKind accessKind, LocationIdentity locationIdentity) {
  64         this(object, offset, value, accessKind, locationIdentity, true);
  65     }
  66 
  67     public RawStoreNode(ValueNode object, ValueNode offset, ValueNode value, JavaKind accessKind, LocationIdentity locationIdentity, boolean needsBarrier) {
  68         this(object, offset, value, accessKind, locationIdentity, needsBarrier, null, false);
  69     }
  70 
  71     public RawStoreNode(ValueNode object, ValueNode offset, ValueNode value, JavaKind accessKind, LocationIdentity locationIdentity, boolean needsBarrier, FrameState stateAfter,
  72                     boolean forceAnyLocation) {
  73         super(TYPE, StampFactory.forVoid(), object, offset, accessKind, locationIdentity, forceAnyLocation);
  74         this.value = value;
  75         this.needsBarrier = needsBarrier;
  76         this.stateAfter = stateAfter;
  77         assert accessKind != JavaKind.Void && accessKind != JavaKind.Illegal;
  78     }
  79 
  80     @NodeIntrinsic
  81     public static native Object storeObject(Object object, long offset, Object value, @ConstantNodeParameter JavaKind kind, @ConstantNodeParameter LocationIdentity locationIdentity,
  82                     @ConstantNodeParameter boolean needsBarrier);
  83 
  84     @NodeIntrinsic
  85     public static native Object storeChar(Object object, long offset, char value, @ConstantNodeParameter JavaKind kind, @ConstantNodeParameter LocationIdentity locationIdentity);
  86 
  87     public boolean needsBarrier() {
  88         return needsBarrier;
  89     }
  90 
  91     @Override
  92     public FrameState stateAfter() {
  93         return stateAfter;
  94     }
  95 
  96     @Override
  97     public void setStateAfter(FrameState x) {
  98         assert x == null || x.isAlive() : "frame state must be in a graph";
  99         updateUsages(stateAfter, x);
 100         stateAfter = x;
 101     }
 102 
 103     @Override
 104     public boolean hasSideEffect() {
 105         return true;
 106     }
 107 
 108     public ValueNode value() {
 109         return value;
 110     }
 111 
 112     @Override
 113     public void lower(LoweringTool tool) {
 114         tool.getLowerer().lower(this, tool);
 115     }
 116 
 117     @Override
 118     public void virtualize(VirtualizerTool tool) {
 119         ValueNode alias = tool.getAlias(object());
 120         if (alias instanceof VirtualObjectNode) {
 121             VirtualObjectNode virtual = (VirtualObjectNode) alias;
 122             ValueNode indexValue = tool.getAlias(offset());
 123             if (indexValue.isConstant()) {
 124                 long off = indexValue.asJavaConstant().asLong();
 125                 int entryIndex = virtual.entryIndexForOffset(off, accessKind());
 126                 if (entryIndex != -1) {
 127                     JavaKind entryKind = virtual.entryKind(entryIndex);
 128                     boolean canVirtualize = entryKind == accessKind() || (entryKind == accessKind().getStackKind() && !StampTool.typeOrNull(object()).isArray());
 129                     if (!canVirtualize) {
 130                         /*
 131                          * Special case: If the entryKind is long, allow arbitrary kinds as long as
 132                          * a value of the same kind is already there. This can only happen if some
 133                          * other node initialized the entry with a value of a different kind. One
 134                          * example where this happens is the Truffle NewFrameNode.
 135                          */
 136                         ValueNode entry = tool.getEntry(virtual, entryIndex);
 137                         if (entryKind == JavaKind.Long && entry.getStackKind() == value.getStackKind()) {
 138                             canVirtualize = true;
 139                         }
 140                     }
 141                     if (canVirtualize) {
 142                         tool.setVirtualEntry(virtual, entryIndex, value(), true);
 143                         tool.delete();
 144                     } else {
 145                         /*
 146                          * Special case: Allow storing a single long or double value into two
 147                          * consecutive int slots.
 148                          */
 149                         if ((accessKind() == JavaKind.Long || accessKind() == JavaKind.Double) && entryKind == JavaKind.Int) {
 150                             int nextIndex = virtual.entryIndexForOffset(off + 4, entryKind);
 151                             if (nextIndex != -1) {
 152                                 JavaKind nextKind = virtual.entryKind(nextIndex);
 153                                 if (nextKind == JavaKind.Int) {
 154                                     tool.setVirtualEntry(virtual, entryIndex, value(), true);
 155                                     tool.setVirtualEntry(virtual, nextIndex, ConstantNode.forConstant(JavaConstant.forIllegal(), tool.getMetaAccessProvider(), graph()), true);
 156                                     tool.delete();
 157                                 }
 158                             }
 159                         }
 160                     }
 161                 }
 162             }
 163         }
 164     }
 165 
 166     @Override
 167     protected ValueNode cloneAsFieldAccess(Assumptions assumptions, ResolvedJavaField field) {
 168         return new StoreFieldNode(object(), field, value(), stateAfter());
 169     }
 170 
 171     @Override
 172     protected ValueNode cloneAsArrayAccess(ValueNode location, LocationIdentity identity) {
 173         return new RawStoreNode(object(), location, value, accessKind(), identity, needsBarrier, stateAfter(), isAnyLocationForced());
 174     }
 175 
 176     public FrameState getState() {
 177         return stateAfter;
 178     }
 179 }