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