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.extended;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
  27 
  28 import org.graalvm.compiler.core.common.LocationIdentity;
  29 import org.graalvm.compiler.core.common.type.StampFactory;
  30 import org.graalvm.compiler.graph.NodeClass;
  31 import org.graalvm.compiler.nodeinfo.NodeInfo;
  32 import org.graalvm.compiler.nodes.ValueNode;
  33 import org.graalvm.compiler.nodes.java.LoadFieldNode;
  34 import org.graalvm.compiler.nodes.spi.Lowerable;
  35 import org.graalvm.compiler.nodes.spi.LoweringTool;
  36 import org.graalvm.compiler.nodes.spi.Virtualizable;
  37 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  38 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  39 
  40 import jdk.vm.ci.meta.Assumptions;
  41 import jdk.vm.ci.meta.JavaKind;
  42 import jdk.vm.ci.meta.ResolvedJavaField;
  43 
  44 /**
  45  * Load of a value from a location specified as an offset relative to an object. No null check is
  46  * performed before the load.
  47  */
  48 @NodeInfo(cycles = CYCLES_2, size = SIZE_1)
  49 public class UnsafeLoadNode extends UnsafeAccessNode implements Lowerable, Virtualizable {
  50     public static final NodeClass<UnsafeLoadNode> TYPE = NodeClass.create(UnsafeLoadNode.class);
  51 
  52     public UnsafeLoadNode(ValueNode object, ValueNode offset, JavaKind accessKind, LocationIdentity locationIdentity) {
  53         super(TYPE, StampFactory.forKind(accessKind.getStackKind()), object, offset, accessKind, locationIdentity);
  54     }
  55 
  56     public UnsafeLoadNode(NodeClass<? extends UnsafeLoadNode> c, ValueNode object, ValueNode offset, JavaKind accessKind, LocationIdentity locationIdentity) {
  57         super(c, StampFactory.forKind(accessKind.getStackKind()), object, offset, accessKind, locationIdentity);
  58     }
  59 
  60     @Override
  61     public void lower(LoweringTool tool) {
  62         tool.getLowerer().lower(this, tool);
  63     }
  64 
  65     @Override
  66     public void virtualize(VirtualizerTool tool) {
  67         ValueNode alias = tool.getAlias(object());
  68         if (alias instanceof VirtualObjectNode) {
  69             VirtualObjectNode virtual = (VirtualObjectNode) alias;
  70             ValueNode offsetValue = tool.getAlias(offset());
  71             if (offsetValue.isConstant()) {
  72                 long off = offsetValue.asJavaConstant().asLong();
  73                 int entryIndex = virtual.entryIndexForOffset(off, accessKind());
  74 
  75                 if (entryIndex != -1) {
  76                     ValueNode entry = tool.getEntry(virtual, entryIndex);
  77                     JavaKind entryKind = virtual.entryKind(entryIndex);
  78                     if (entry.getStackKind() == getStackKind() || entryKind == accessKind()) {
  79                         tool.replaceWith(entry);
  80                     }
  81                 }
  82             }
  83         }
  84     }
  85 
  86     @Override
  87     protected ValueNode cloneAsFieldAccess(Assumptions assumptions, ResolvedJavaField field) {
  88         return LoadFieldNode.create(assumptions, object(), field);
  89     }
  90 
  91     @Override
  92     protected ValueNode cloneAsArrayAccess(ValueNode location, LocationIdentity identity) {
  93         return new UnsafeLoadNode(object(), location, accessKind(), identity);
  94     }
  95 
  96     @NodeIntrinsic
  97     public static native Object load(Object object, long offset, @ConstantNodeParameter JavaKind kind, @ConstantNodeParameter LocationIdentity locationIdentity);
  98 }