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.NodeCycles.CYCLES_8;
  28 
  29 import org.graalvm.compiler.core.common.type.StampFactory;
  30 import org.graalvm.compiler.graph.Node;
  31 import org.graalvm.compiler.graph.NodeClass;
  32 import org.graalvm.compiler.graph.spi.Canonicalizable;
  33 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  34 import org.graalvm.compiler.nodeinfo.InputType;
  35 import org.graalvm.compiler.nodeinfo.NodeCycles;
  36 import org.graalvm.compiler.nodeinfo.NodeInfo;
  37 import org.graalvm.compiler.nodes.DeoptimizeNode;
  38 import org.graalvm.compiler.nodes.FrameState;
  39 import org.graalvm.compiler.nodes.StateSplit;
  40 import org.graalvm.compiler.nodes.ValueNode;
  41 import org.graalvm.compiler.nodes.spi.Virtualizable;
  42 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  43 import org.graalvm.compiler.nodes.virtual.VirtualInstanceNode;
  44 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  45 
  46 import jdk.vm.ci.meta.DeoptimizationAction;
  47 import jdk.vm.ci.meta.DeoptimizationReason;
  48 import jdk.vm.ci.meta.ResolvedJavaField;
  49 
  50 /**
  51  * The {@code StoreFieldNode} represents a write to a static or instance field.
  52  */
  53 @NodeInfo(nameTemplate = "StoreField#{p#field/s}")
  54 public final class StoreFieldNode extends AccessFieldNode implements StateSplit, Virtualizable, Canonicalizable {
  55     public static final NodeClass<StoreFieldNode> TYPE = NodeClass.create(StoreFieldNode.class);
  56 
  57     @Input ValueNode value;
  58     @OptionalInput(InputType.State) FrameState stateAfter;
  59 
  60     @Override
  61     public FrameState stateAfter() {
  62         return stateAfter;
  63     }
  64 
  65     @Override
  66     public void setStateAfter(FrameState x) {
  67         assert x == null || x.isAlive() : "frame state must be in a graph";
  68         updateUsages(stateAfter, x);
  69         stateAfter = x;
  70     }
  71 
  72     @Override
  73     public boolean hasSideEffect() {
  74         return true;
  75     }
  76 
  77     public ValueNode value() {
  78         return value;
  79     }
  80 
  81     public StoreFieldNode(ValueNode object, ResolvedJavaField field, ValueNode value) {
  82         this(object, field, value, field.isVolatile());
  83     }
  84 
  85     public StoreFieldNode(ValueNode object, ResolvedJavaField field, ValueNode value, boolean volatileAccess) {
  86         super(TYPE, StampFactory.forVoid(), object, field, volatileAccess);
  87         this.value = value;
  88     }
  89 
  90     public StoreFieldNode(ValueNode object, ResolvedJavaField field, ValueNode value, FrameState stateAfter, boolean volatileAccess) {
  91         super(TYPE, StampFactory.forVoid(), object, field, volatileAccess);
  92         this.value = value;
  93         this.stateAfter = stateAfter;
  94     }
  95 
  96     @Override
  97     public void virtualize(VirtualizerTool tool) {
  98         ValueNode alias = tool.getAlias(object());
  99         if (alias instanceof VirtualObjectNode) {
 100             VirtualInstanceNode virtual = (VirtualInstanceNode) alias;
 101             int fieldIndex = virtual.fieldIndex(field());
 102             if (fieldIndex != -1) {
 103                 tool.setVirtualEntry(virtual, fieldIndex, value());
 104                 tool.delete();
 105             }
 106         }
 107     }
 108 
 109     public FrameState getState() {
 110         return stateAfter;
 111     }
 112 
 113     @Override
 114     public NodeCycles estimatedNodeCycles() {
 115         if (isVolatile()) {
 116             return CYCLES_8;
 117         }
 118         return super.estimatedNodeCycles();
 119     }
 120 
 121     @Override
 122     public Node canonical(CanonicalizerTool tool) {
 123         if (!field.isStatic() && object.isNullConstant()) {
 124             return new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.NullCheckException);
 125         }
 126         return this;
 127     }
 128 }