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.graph.spi.Canonicalizable; 34 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 35 import org.graalvm.compiler.nodeinfo.NodeInfo; 36 import org.graalvm.compiler.nodes.ConstantNode; 37 import org.graalvm.compiler.nodes.ValueNode; 38 import org.graalvm.compiler.nodes.calc.ReinterpretNode; 39 import org.graalvm.compiler.nodes.java.LoadFieldNode; 40 import org.graalvm.compiler.nodes.spi.Lowerable; 41 import org.graalvm.compiler.nodes.spi.LoweringTool; 42 import org.graalvm.compiler.nodes.spi.Virtualizable; 43 import org.graalvm.compiler.nodes.spi.VirtualizerTool; 44 import org.graalvm.compiler.nodes.type.StampTool; 45 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 46 import org.graalvm.word.LocationIdentity; 47 48 import jdk.vm.ci.meta.Assumptions; 49 import jdk.vm.ci.meta.Constant; 50 import jdk.vm.ci.meta.JavaConstant; 51 import jdk.vm.ci.meta.JavaKind; 52 import jdk.vm.ci.meta.ResolvedJavaField; 53 import jdk.vm.ci.meta.ResolvedJavaType; 54 55 /** 56 * Load of a value from a location specified as an offset relative to an object. No null check is 57 * performed before the load. 58 */ 59 @NodeInfo(cycles = CYCLES_2, size = SIZE_1) 60 public class RawLoadNode extends UnsafeAccessNode implements Lowerable, Virtualizable, Canonicalizable { 61 public static final NodeClass<RawLoadNode> TYPE = NodeClass.create(RawLoadNode.class); 62 63 /** 64 * This constructor exists for node intrinsics that need a stamp based on {@code accessKind}. 65 */ 66 public RawLoadNode(ValueNode object, ValueNode offset, JavaKind accessKind, LocationIdentity locationIdentity) { 67 this(object, offset, accessKind, locationIdentity, false); 68 } 69 70 public RawLoadNode(ValueNode object, ValueNode offset, JavaKind accessKind, LocationIdentity locationIdentity, boolean forceAnyLocation) { 71 super(TYPE, StampFactory.forKind(accessKind.getStackKind()), object, offset, accessKind, locationIdentity, forceAnyLocation); 72 } 73 74 /** 75 * This constructor exists for node intrinsics that need a stamp based on the return type of the 76 * {@link org.graalvm.compiler.graph.Node.NodeIntrinsic} annotated method. 77 */ 78 public RawLoadNode(@InjectedNodeParameter Stamp stamp, ValueNode object, ValueNode offset, LocationIdentity locationIdentity, JavaKind accessKind) { 79 super(TYPE, stamp, object, offset, accessKind, locationIdentity, false); 80 } 81 82 protected RawLoadNode(NodeClass<? extends RawLoadNode> c, ValueNode object, ValueNode offset, JavaKind accessKind, LocationIdentity locationIdentity) { 83 super(c, StampFactory.forKind(accessKind.getStackKind()), object, offset, accessKind, locationIdentity, false); 84 } 85 86 @Override 87 public void lower(LoweringTool tool) { 88 tool.getLowerer().lower(this, tool); 89 } 90 91 @Override 92 public void virtualize(VirtualizerTool tool) { 93 ValueNode alias = tool.getAlias(object()); 94 if (alias instanceof VirtualObjectNode) { 95 VirtualObjectNode virtual = (VirtualObjectNode) alias; 96 ValueNode offsetValue = tool.getAlias(offset()); 97 if (offsetValue.isConstant()) { 98 long off = offsetValue.asJavaConstant().asLong(); 99 int entryIndex = virtual.entryIndexForOffset(off, accessKind()); 100 101 if (entryIndex != -1) { 102 ValueNode entry = tool.getEntry(virtual, entryIndex); 103 JavaKind entryKind = virtual.entryKind(entryIndex); 104 if (entry.getStackKind() == getStackKind() || entryKind == accessKind()) { 105 106 if (!(entry.stamp().isCompatible(stamp()))) { 107 if (entry.stamp() instanceof PrimitiveStamp && stamp instanceof PrimitiveStamp) { 108 PrimitiveStamp p1 = (PrimitiveStamp) stamp; 109 PrimitiveStamp p2 = (PrimitiveStamp) entry.stamp(); 110 int width1 = p1.getBits(); 111 int width2 = p2.getBits(); 112 if (width1 == width2) { 113 Node replacement = new ReinterpretNode(p2, entry); 114 tool.replaceWith((ValueNode) replacement); 115 return; 116 } else { 117 // different bit width 118 return; 119 } 120 } else { 121 // cannot reinterpret for arbitrary objects 122 return; 123 } 124 } 125 tool.replaceWith(entry); 126 } 127 } 128 } 129 } 130 } 131 132 @Override 133 public Node canonical(CanonicalizerTool tool) { 134 if (!isAnyLocationForced() && getLocationIdentity().isAny()) { 135 ValueNode targetObject = object(); 136 if (offset().isConstant() && targetObject.isConstant() && !targetObject.isNullConstant()) { 137 ConstantNode objectConstant = (ConstantNode) targetObject; 138 ResolvedJavaType type = StampTool.typeOrNull(objectConstant); 139 if (type != null && type.isArray()) { 140 JavaConstant arrayConstant = objectConstant.asJavaConstant(); 141 if (arrayConstant != null) { 142 int stableDimension = objectConstant.getStableDimension(); 143 if (stableDimension > 0) { 144 long constantOffset = offset().asJavaConstant().asLong(); 145 Constant constant = stamp().readConstant(tool.getConstantReflection().getMemoryAccessProvider(), arrayConstant, constantOffset); 146 boolean isDefaultStable = objectConstant.isDefaultStable(); 147 if (constant != null && (isDefaultStable || !constant.isDefaultForKind())) { 148 return ConstantNode.forConstant(stamp(), constant, stableDimension - 1, isDefaultStable, tool.getMetaAccess()); 149 } 150 } 151 } 152 } 153 } 154 } 155 return super.canonical(tool); 156 } 157 158 @Override 159 protected ValueNode cloneAsFieldAccess(Assumptions assumptions, ResolvedJavaField field) { 160 return LoadFieldNode.create(assumptions, object(), field); 161 } 162 163 @Override 164 protected ValueNode cloneAsArrayAccess(ValueNode location, LocationIdentity identity) { 165 return new RawLoadNode(object(), location, accessKind(), identity); 166 } 167 168 @NodeIntrinsic 169 public static native Object load(Object object, long offset, @ConstantNodeParameter JavaKind kind, @ConstantNodeParameter LocationIdentity locationIdentity); 170 }