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.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.spi.Virtualizable; 37 import org.graalvm.compiler.nodes.spi.VirtualizerTool; 38 import org.graalvm.compiler.nodes.virtual.VirtualInstanceNode; 39 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 40 41 import jdk.vm.ci.meta.JavaConstant; 42 import jdk.vm.ci.meta.ResolvedJavaField; 43 44 /** 45 * The {@code StoreFieldNode} represents a write to a static or instance field. 46 */ 47 @NodeInfo(nameTemplate = "StoreField#{p#field/s}") 48 public final class StoreFieldNode extends AccessFieldNode implements StateSplit, Virtualizable { 49 public static final NodeClass<StoreFieldNode> TYPE = NodeClass.create(StoreFieldNode.class); 50 51 @Input ValueNode value; 52 @OptionalInput(InputType.State) FrameState stateAfter; 53 54 @Override 55 public FrameState stateAfter() { 56 return stateAfter; 57 } 58 59 @Override 60 public void setStateAfter(FrameState x) { 61 assert x == null || x.isAlive() : "frame state must be in a graph"; 62 updateUsages(stateAfter, x); 63 stateAfter = x; 64 } 65 66 @Override 67 public boolean hasSideEffect() { 68 return true; 69 } 70 71 public ValueNode value() { 72 return value; 73 } 74 75 public StoreFieldNode(ValueNode object, ResolvedJavaField field, ValueNode value) { 76 super(TYPE, StampFactory.forVoid(), object, field); 77 this.value = value; 78 } 79 80 public StoreFieldNode(ValueNode object, ResolvedJavaField field, ValueNode value, FrameState stateAfter) { 81 super(TYPE, StampFactory.forVoid(), object, field); 82 this.value = value; 83 this.stateAfter = stateAfter; 84 } 85 86 @Override 87 public void virtualize(VirtualizerTool tool) { 88 ValueNode alias = tool.getAlias(object()); 89 if (alias instanceof VirtualObjectNode) { 90 VirtualInstanceNode virtual = (VirtualInstanceNode) alias; 91 int fieldIndex = virtual.fieldIndex(field()); 92 if (fieldIndex != -1) { 93 ValueNode existing = tool.getEntry((VirtualObjectNode) alias, fieldIndex); 94 if (existing.stamp().isCompatible(value().stamp())) { 95 tool.setVirtualEntry(virtual, fieldIndex, value(), false); 96 tool.delete(); 97 } else { 98 // stamp of the value is not compatible with the value in the virtualizer 99 // can only happen with unsafe two slot writes on one slot fields 100 assert existing instanceof ConstantNode && ((ConstantNode) existing).asConstant().equals(JavaConstant.forIllegal()) : value.stamp() + " vs " + existing.stamp(); 101 } 102 } 103 } 104 } 105 106 public FrameState getState() { 107 return stateAfter; 108 } 109 110 @Override 111 public NodeCycles estimatedNodeCycles() { 112 if (field.isVolatile()) { 113 return CYCLES_8; 114 } 115 return super.estimatedNodeCycles(); 116 } 117 }