1 /*
   2  * Copyright (c) 2009, 2019, 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 
  24 
  25 package org.graalvm.compiler.nodes.java;
  26 
  27 import static org.graalvm.compiler.nodeinfo.InputType.State;
  28 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_8;
  29 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_8;
  30 
  31 import org.graalvm.compiler.core.common.type.StampFactory;
  32 import org.graalvm.compiler.graph.Node;
  33 import org.graalvm.compiler.graph.NodeClass;
  34 import org.graalvm.compiler.graph.spi.Canonicalizable;
  35 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  36 import org.graalvm.compiler.nodeinfo.InputType;
  37 import org.graalvm.compiler.nodeinfo.NodeInfo;
  38 import org.graalvm.compiler.nodes.DeoptimizeNode;
  39 import org.graalvm.compiler.nodes.FrameState;
  40 import org.graalvm.compiler.nodes.StateSplit;
  41 import org.graalvm.compiler.nodes.ValueNode;
  42 import org.graalvm.compiler.nodes.extended.GuardingNode;
  43 import org.graalvm.compiler.nodes.spi.Lowerable;
  44 import org.graalvm.compiler.nodes.spi.Virtualizable;
  45 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  46 import org.graalvm.compiler.nodes.type.StampTool;
  47 import org.graalvm.compiler.nodes.virtual.VirtualArrayNode;
  48 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  49 
  50 import jdk.vm.ci.meta.DeoptimizationAction;
  51 import jdk.vm.ci.meta.DeoptimizationReason;
  52 import jdk.vm.ci.meta.JavaKind;
  53 import jdk.vm.ci.meta.ResolvedJavaType;
  54 
  55 /**
  56  * The {@code StoreIndexedNode} represents a write to an array element.
  57  */
  58 @NodeInfo(cycles = CYCLES_8, size = SIZE_8)
  59 public final class StoreIndexedNode extends AccessIndexedNode implements StateSplit, Lowerable, Virtualizable, Canonicalizable {
  60 
  61     public static final NodeClass<StoreIndexedNode> TYPE = NodeClass.create(StoreIndexedNode.class);
  62 
  63     @OptionalInput(InputType.Guard) private GuardingNode storeCheck;
  64     @Input ValueNode value;
  65     @OptionalInput(State) FrameState stateAfter;
  66 
  67     public GuardingNode getStoreCheck() {
  68         return storeCheck;
  69     }
  70 
  71     @Override
  72     public FrameState stateAfter() {
  73         return stateAfter;
  74     }
  75 
  76     @Override
  77     public void setStateAfter(FrameState x) {
  78         assert x == null || x.isAlive() : "frame state must be in a graph";
  79         updateUsages(stateAfter, x);
  80         stateAfter = x;
  81     }
  82 
  83     @Override
  84     public boolean hasSideEffect() {
  85         return true;
  86     }
  87 
  88     public ValueNode value() {
  89         return value;
  90     }
  91 
  92     public StoreIndexedNode(ValueNode array, ValueNode index, GuardingNode boundsCheck, GuardingNode storeCheck, JavaKind elementKind, ValueNode value) {
  93         super(TYPE, StampFactory.forVoid(), array, index, boundsCheck, elementKind);
  94         this.storeCheck = storeCheck;
  95         this.value = value;
  96     }
  97 
  98     @Override
  99     public void virtualize(VirtualizerTool tool) {
 100         ValueNode alias = tool.getAlias(array());
 101         if (alias instanceof VirtualObjectNode) {
 102             ValueNode indexValue = tool.getAlias(index());
 103             int idx = indexValue.isConstant() ? indexValue.asJavaConstant().asInt() : -1;
 104             VirtualArrayNode virtual = (VirtualArrayNode) alias;
 105             if (idx >= 0 && idx < virtual.entryCount()) {
 106                 ResolvedJavaType componentType = virtual.type().getComponentType();
 107                 if (componentType.isPrimitive() || StampTool.isPointerAlwaysNull(value) || componentType.isJavaLangObject() ||
 108                                 (StampTool.typeReferenceOrNull(value) != null && componentType.isAssignableFrom(StampTool.typeOrNull(value)))) {
 109                     tool.setVirtualEntry(virtual, idx, value());
 110                     tool.delete();
 111                 }
 112             }
 113         }
 114     }
 115 
 116     public FrameState getState() {
 117         return stateAfter;
 118     }
 119 
 120     @Override
 121     public Node canonical(CanonicalizerTool tool) {
 122         if (array().isNullConstant()) {
 123             return new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.NullCheckException);
 124         }
 125         return this;
 126     }
 127 }