1 /* 2 * Copyright (c) 2012, 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.Stamp; 30 import org.graalvm.compiler.graph.Node; 31 import org.graalvm.compiler.graph.NodeClass; 32 import org.graalvm.compiler.graph.spi.Canonicalizable; 33 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 34 import org.graalvm.compiler.nodeinfo.NodeInfo; 35 import org.graalvm.compiler.nodes.FixedWithNextNode; 36 import org.graalvm.compiler.nodes.NamedLocationIdentity; 37 import org.graalvm.compiler.nodes.ValueNode; 38 import org.graalvm.compiler.nodes.type.StampTool; 39 40 import jdk.vm.ci.meta.Assumptions; 41 import jdk.vm.ci.meta.JavaKind; 42 import jdk.vm.ci.meta.ResolvedJavaField; 43 import jdk.vm.ci.meta.ResolvedJavaType; 44 45 @NodeInfo(cycles = CYCLES_2, size = SIZE_1) 46 public abstract class UnsafeAccessNode extends FixedWithNextNode implements Canonicalizable { 47 48 public static final NodeClass<UnsafeAccessNode> TYPE = NodeClass.create(UnsafeAccessNode.class); 49 @Input ValueNode object; 50 @Input ValueNode offset; 51 protected final JavaKind accessKind; 52 protected final LocationIdentity locationIdentity; 53 54 protected UnsafeAccessNode(NodeClass<? extends UnsafeAccessNode> c, Stamp stamp, ValueNode object, ValueNode offset, JavaKind accessKind, LocationIdentity locationIdentity) { 55 super(c, stamp); 56 assert accessKind != null; 57 assert locationIdentity != null; 58 this.object = object; 59 this.offset = offset; 60 this.accessKind = accessKind; 61 this.locationIdentity = locationIdentity; 62 } 63 64 public LocationIdentity getLocationIdentity() { 65 return locationIdentity; 66 } 67 68 public ValueNode object() { 69 return object; 70 } 71 72 public ValueNode offset() { 73 return offset; 74 } 75 76 public JavaKind accessKind() { 77 return accessKind; 78 } 79 80 @Override 81 public Node canonical(CanonicalizerTool tool) { 82 if (this.getLocationIdentity().isAny() && offset().isConstant()) { 83 long constantOffset = offset().asJavaConstant().asLong(); 84 85 // Try to canonicalize to a field access. 86 ResolvedJavaType receiverType = StampTool.typeOrNull(object()); 87 if (receiverType != null) { 88 ResolvedJavaField field = receiverType.findInstanceFieldWithOffset(constantOffset, accessKind()); 89 // No need for checking that the receiver is non-null. The field access includes 90 // the null check and if a field is found, the offset is so small that this is 91 // never a valid access of an arbitrary address. 92 if (field != null && field.getJavaKind() == this.accessKind()) { 93 assert !graph().isAfterFloatingReadPhase() : "cannot add more precise memory location after floating read phase"; 94 return cloneAsFieldAccess(graph().getAssumptions(), field); 95 } 96 } 97 } 98 if (this.getLocationIdentity().isAny()) { 99 ResolvedJavaType receiverType = StampTool.typeOrNull(object()); 100 // Try to build a better location identity. 101 if (receiverType != null && receiverType.isArray()) { 102 LocationIdentity identity = NamedLocationIdentity.getArrayLocation(receiverType.getComponentType().getJavaKind()); 103 assert !graph().isAfterFloatingReadPhase() : "cannot add more precise memory location after floating read phase"; 104 return cloneAsArrayAccess(offset(), identity); 105 } 106 } 107 108 return this; 109 } 110 111 protected abstract ValueNode cloneAsFieldAccess(Assumptions assumptions, ResolvedJavaField field); 112 113 protected abstract ValueNode cloneAsArrayAccess(ValueNode location, LocationIdentity identity); 114 }