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.type.PrimitiveStamp;
  29 import org.graalvm.compiler.core.common.type.Stamp;
  30 import org.graalvm.compiler.core.common.type.StampFactory;
  31 import org.graalvm.compiler.graph.Node;
  32 import org.graalvm.compiler.graph.NodeClass;
  33 import org.graalvm.compiler.nodeinfo.NodeInfo;
  34 import org.graalvm.compiler.nodes.ValueNode;
  35 import org.graalvm.compiler.nodes.calc.ReinterpretNode;
  36 import org.graalvm.compiler.nodes.java.LoadFieldNode;
  37 import org.graalvm.compiler.nodes.spi.Lowerable;
  38 import org.graalvm.compiler.nodes.spi.LoweringTool;
  39 import org.graalvm.compiler.nodes.spi.Virtualizable;
  40 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  41 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  42 import org.graalvm.word.LocationIdentity;
  43 
  44 import jdk.vm.ci.meta.Assumptions;
  45 import jdk.vm.ci.meta.JavaKind;
  46 import jdk.vm.ci.meta.ResolvedJavaField;
  47 
  48 /**
  49  * Load of a value from a location specified as an offset relative to an object. No null check is
  50  * performed before the load.
  51  */
  52 @NodeInfo(cycles = CYCLES_2, size = SIZE_1)
  53 public class RawLoadNode extends UnsafeAccessNode implements Lowerable, Virtualizable {
  54     public static final NodeClass<RawLoadNode> TYPE = NodeClass.create(RawLoadNode.class);
  55 
  56     /**
  57      * This constructor exists for node intrinsics that need a stamp based on {@code accessKind}.
  58      */
  59     public RawLoadNode(ValueNode object, ValueNode offset, JavaKind accessKind, LocationIdentity locationIdentity) {
  60         this(object, offset, accessKind, locationIdentity, false);
  61     }
  62 
  63     public RawLoadNode(ValueNode object, ValueNode offset, JavaKind accessKind, LocationIdentity locationIdentity, boolean forceAnyLocation) {
  64         super(TYPE, StampFactory.forKind(accessKind.getStackKind()), object, offset, accessKind, locationIdentity, forceAnyLocation);
  65     }
  66 
  67     /**
  68      * This constructor exists for node intrinsics that need a stamp based on the return type of the
  69      * {@link org.graalvm.compiler.graph.Node.NodeIntrinsic} annotated method.
  70      */
  71     public RawLoadNode(@InjectedNodeParameter Stamp stamp, ValueNode object, ValueNode offset, LocationIdentity locationIdentity, JavaKind accessKind) {
  72         super(TYPE, stamp, object, offset, accessKind, locationIdentity, false);
  73     }
  74 
  75     public RawLoadNode(NodeClass<? extends RawLoadNode> c, ValueNode object, ValueNode offset, JavaKind accessKind, LocationIdentity locationIdentity) {
  76         super(c, StampFactory.forKind(accessKind.getStackKind()), object, offset, accessKind, locationIdentity, false);
  77     }
  78 
  79     @Override
  80     public void lower(LoweringTool tool) {
  81         tool.getLowerer().lower(this, tool);
  82     }
  83 
  84     @Override
  85     public void virtualize(VirtualizerTool tool) {
  86         ValueNode alias = tool.getAlias(object());
  87         if (alias instanceof VirtualObjectNode) {
  88             VirtualObjectNode virtual = (VirtualObjectNode) alias;
  89             ValueNode offsetValue = tool.getAlias(offset());
  90             if (offsetValue.isConstant()) {
  91                 long off = offsetValue.asJavaConstant().asLong();
  92                 int entryIndex = virtual.entryIndexForOffset(off, accessKind());
  93 
  94                 if (entryIndex != -1) {
  95                     ValueNode entry = tool.getEntry(virtual, entryIndex);
  96                     JavaKind entryKind = virtual.entryKind(entryIndex);
  97                     if (entry.getStackKind() == getStackKind() || entryKind == accessKind()) {
  98 
  99                         if (!(entry.stamp().isCompatible(stamp()))) {
 100                             if (entry.stamp() instanceof PrimitiveStamp && stamp instanceof PrimitiveStamp) {
 101                                 PrimitiveStamp p1 = (PrimitiveStamp) stamp;
 102                                 PrimitiveStamp p2 = (PrimitiveStamp) entry.stamp();
 103                                 int width1 = p1.getBits();
 104                                 int width2 = p2.getBits();
 105                                 if (width1 == width2) {
 106                                     Node replacement = new ReinterpretNode(p2, entry);
 107                                     tool.replaceWith((ValueNode) replacement);
 108                                     return;
 109                                 } else {
 110                                     // different bit width
 111                                     return;
 112                                 }
 113                             } else {
 114                                 // cannot reinterpret for arbitrary objects
 115                                 return;
 116                             }
 117                         }
 118                         tool.replaceWith(entry);
 119                     }
 120                 }
 121             }
 122         }
 123     }
 124 
 125     @Override
 126     protected ValueNode cloneAsFieldAccess(Assumptions assumptions, ResolvedJavaField field) {
 127         return LoadFieldNode.create(assumptions, object(), field);
 128     }
 129 
 130     @Override
 131     protected ValueNode cloneAsArrayAccess(ValueNode location, LocationIdentity identity) {
 132         return new RawLoadNode(object(), location, accessKind(), identity);
 133     }
 134 
 135     @NodeIntrinsic
 136     public static native Object load(Object object, long offset, @ConstantNodeParameter JavaKind kind, @ConstantNodeParameter LocationIdentity locationIdentity);
 137 }