1 /*
   2  * Copyright (c) 2011, 2016, 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.memory;
  24 
  25 import static org.graalvm.compiler.nodeinfo.InputType.Guard;
  26 
  27 import org.graalvm.compiler.core.common.LIRKind;
  28 import org.graalvm.compiler.core.common.LocationIdentity;
  29 import org.graalvm.compiler.debug.GraalError;
  30 import org.graalvm.compiler.graph.Node;
  31 import org.graalvm.compiler.graph.NodeClass;
  32 import org.graalvm.compiler.graph.spi.Simplifiable;
  33 import org.graalvm.compiler.graph.spi.SimplifierTool;
  34 import org.graalvm.compiler.nodeinfo.NodeInfo;
  35 import org.graalvm.compiler.nodes.PiNode;
  36 import org.graalvm.compiler.nodes.ValueNode;
  37 import org.graalvm.compiler.nodes.extended.GuardingNode;
  38 import org.graalvm.compiler.nodes.memory.address.AddressNode;
  39 import org.graalvm.compiler.nodes.memory.address.AddressNode.Address;
  40 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
  41 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  42 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  43 import org.graalvm.compiler.nodes.spi.Virtualizable;
  44 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  45 
  46 /**
  47  * Writes a given {@linkplain #value() value} a {@linkplain FixedAccessNode memory location}.
  48  */
  49 @NodeInfo(nameTemplate = "Write#{p#location/s}")
  50 public class WriteNode extends AbstractWriteNode implements LIRLowerable, Simplifiable, Virtualizable {
  51 
  52     public static final NodeClass<WriteNode> TYPE = NodeClass.create(WriteNode.class);
  53 
  54     @OptionalInput(Guard) protected GuardingNode storeCheckGuard;
  55 
  56     protected WriteNode(ValueNode address, LocationIdentity location, ValueNode value, BarrierType barrierType) {
  57         this((AddressNode) address, location, value, barrierType);
  58     }
  59 
  60     public WriteNode(AddressNode address, LocationIdentity location, ValueNode value, BarrierType barrierType) {
  61         super(TYPE, address, location, value, barrierType);
  62     }
  63 
  64     public WriteNode(AddressNode address, LocationIdentity location, ValueNode value, BarrierType barrierType, boolean initialization) {
  65         super(TYPE, address, location, value, barrierType, initialization);
  66     }
  67 
  68     public WriteNode(AddressNode address, LocationIdentity location, ValueNode value, BarrierType barrierType, GuardingNode guard, boolean initialization) {
  69         this(TYPE, address, location, value, barrierType, guard, initialization);
  70     }
  71 
  72     protected WriteNode(NodeClass<? extends WriteNode> c, AddressNode address, LocationIdentity location, ValueNode value, BarrierType barrierType, GuardingNode guard, boolean initialization) {
  73         super(c, address, location, value, barrierType, guard, initialization);
  74     }
  75 
  76     @Override
  77     public void generate(NodeLIRBuilderTool gen) {
  78         LIRKind writeKind = gen.getLIRGeneratorTool().getLIRKind(value().stamp());
  79         gen.getLIRGeneratorTool().getArithmetic().emitStore(writeKind, gen.operand(address), gen.operand(value()), gen.state(this));
  80     }
  81 
  82     @Override
  83     public void simplify(SimplifierTool tool) {
  84         if (getAddress() instanceof OffsetAddressNode) {
  85             OffsetAddressNode objAddress = (OffsetAddressNode) getAddress();
  86             if (objAddress.getBase() instanceof PiNode && ((PiNode) objAddress.getBase()).getGuard() == getGuard()) {
  87                 OffsetAddressNode newAddress = graph().unique(new OffsetAddressNode(((PiNode) objAddress.getBase()).getOriginalNode(), objAddress.getOffset()));
  88                 setAddress(newAddress);
  89                 tool.addToWorkList(newAddress);
  90             }
  91         }
  92     }
  93 
  94     @NodeIntrinsic
  95     public static native void writeMemory(Address address, @ConstantNodeParameter LocationIdentity location, Object value, @ConstantNodeParameter BarrierType barrierType);
  96 
  97     @Override
  98     public void virtualize(VirtualizerTool tool) {
  99         throw GraalError.shouldNotReachHere("unexpected WriteNode before PEA");
 100     }
 101 
 102     @Override
 103     public boolean canNullCheck() {
 104         return true;
 105     }
 106 
 107     public void setStoreCheckGuard(GuardingNode newStoreCheckGuard) {
 108         updateUsages((Node) this.storeCheckGuard, (Node) newStoreCheckGuard);
 109         this.storeCheckGuard = newStoreCheckGuard;
 110     }
 111 }