1 /*
   2  * Copyright (c) 2009, 2015, 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.State;
  26 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_8;
  27 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_8;
  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.spi.Lowerable;
  36 import org.graalvm.compiler.nodes.spi.Virtualizable;
  37 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  38 import org.graalvm.compiler.nodes.type.StampTool;
  39 import org.graalvm.compiler.nodes.virtual.VirtualArrayNode;
  40 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  41 
  42 import jdk.vm.ci.meta.JavaKind;
  43 import jdk.vm.ci.meta.ResolvedJavaType;
  44 
  45 /**
  46  * The {@code StoreIndexedNode} represents a write to an array element.
  47  */
  48 @NodeInfo(cycles = CYCLES_8, size = SIZE_8)
  49 public final class StoreIndexedNode extends AccessIndexedNode implements StateSplit, Lowerable, Virtualizable {
  50 
  51     public static final NodeClass<StoreIndexedNode> TYPE = NodeClass.create(StoreIndexedNode.class);
  52     @Input ValueNode value;
  53     @OptionalInput(State) FrameState stateAfter;
  54 
  55     @Override
  56     public FrameState stateAfter() {
  57         return stateAfter;
  58     }
  59 
  60     @Override
  61     public void setStateAfter(FrameState x) {
  62         assert x == null || x.isAlive() : "frame state must be in a graph";
  63         updateUsages(stateAfter, x);
  64         stateAfter = x;
  65     }
  66 
  67     @Override
  68     public boolean hasSideEffect() {
  69         return true;
  70     }
  71 
  72     public ValueNode value() {
  73         return value;
  74     }
  75 
  76     public StoreIndexedNode(ValueNode array, ValueNode index, JavaKind elementKind, ValueNode value) {
  77         super(TYPE, StampFactory.forVoid(), array, index, elementKind);
  78         this.value = value;
  79     }
  80 
  81     @Override
  82     public void virtualize(VirtualizerTool tool) {
  83         ValueNode alias = tool.getAlias(array());
  84         if (alias instanceof VirtualObjectNode) {
  85             ValueNode indexValue = tool.getAlias(index());
  86             int idx = indexValue.isConstant() ? indexValue.asJavaConstant().asInt() : -1;
  87             VirtualArrayNode virtual = (VirtualArrayNode) alias;
  88             if (idx >= 0 && idx < virtual.entryCount()) {
  89                 ResolvedJavaType componentType = virtual.type().getComponentType();
  90                 if (componentType.isPrimitive() || StampTool.isPointerAlwaysNull(value) || componentType.getSuperclass() == null ||
  91                                 (StampTool.typeReferenceOrNull(value) != null && componentType.isAssignableFrom(StampTool.typeOrNull(value)))) {
  92                     tool.setVirtualEntry(virtual, idx, value(), false);
  93                     tool.delete();
  94                 }
  95             }
  96         }
  97     }
  98 
  99     public FrameState getState() {
 100         return stateAfter;
 101     }
 102 }