43972
|
1 |
/*
|
58877
|
2 |
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.nodes.extended;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
|
|
28 |
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
|
|
29 |
|
58877
|
30 |
import java.nio.ByteOrder;
|
|
31 |
|
43972
|
32 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
33 |
import org.graalvm.compiler.graph.Node;
|
|
34 |
import org.graalvm.compiler.graph.NodeClass;
|
|
35 |
import org.graalvm.compiler.graph.spi.Canonicalizable;
|
|
36 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
|
37 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
38 |
import org.graalvm.compiler.nodes.FixedWithNextNode;
|
|
39 |
import org.graalvm.compiler.nodes.NamedLocationIdentity;
|
|
40 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
41 |
import org.graalvm.compiler.nodes.type.StampTool;
|
49873
|
42 |
import jdk.internal.vm.compiler.word.LocationIdentity;
|
43972
|
43 |
|
|
44 |
import jdk.vm.ci.meta.Assumptions;
|
58877
|
45 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
|
46 |
import jdk.vm.ci.meta.JavaConstant;
|
43972
|
47 |
import jdk.vm.ci.meta.JavaKind;
|
|
48 |
import jdk.vm.ci.meta.ResolvedJavaField;
|
|
49 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
50 |
|
|
51 |
@NodeInfo(cycles = CYCLES_2, size = SIZE_1)
|
|
52 |
public abstract class UnsafeAccessNode extends FixedWithNextNode implements Canonicalizable {
|
|
53 |
|
|
54 |
public static final NodeClass<UnsafeAccessNode> TYPE = NodeClass.create(UnsafeAccessNode.class);
|
|
55 |
@Input ValueNode object;
|
|
56 |
@Input ValueNode offset;
|
|
57 |
protected final JavaKind accessKind;
|
|
58 |
protected final LocationIdentity locationIdentity;
|
46344
|
59 |
protected final boolean forceAnyLocation;
|
43972
|
60 |
|
46344
|
61 |
protected UnsafeAccessNode(NodeClass<? extends UnsafeAccessNode> c, Stamp stamp, ValueNode object, ValueNode offset, JavaKind accessKind, LocationIdentity locationIdentity,
|
|
62 |
boolean forceAnyLocation) {
|
43972
|
63 |
super(c, stamp);
|
46344
|
64 |
this.forceAnyLocation = forceAnyLocation;
|
43972
|
65 |
assert accessKind != null;
|
|
66 |
assert locationIdentity != null;
|
|
67 |
this.object = object;
|
|
68 |
this.offset = offset;
|
|
69 |
this.accessKind = accessKind;
|
|
70 |
this.locationIdentity = locationIdentity;
|
|
71 |
}
|
|
72 |
|
|
73 |
public LocationIdentity getLocationIdentity() {
|
|
74 |
return locationIdentity;
|
|
75 |
}
|
|
76 |
|
46344
|
77 |
public boolean isAnyLocationForced() {
|
|
78 |
return forceAnyLocation;
|
|
79 |
}
|
|
80 |
|
43972
|
81 |
public ValueNode object() {
|
|
82 |
return object;
|
|
83 |
}
|
|
84 |
|
|
85 |
public ValueNode offset() {
|
|
86 |
return offset;
|
|
87 |
}
|
|
88 |
|
|
89 |
public JavaKind accessKind() {
|
|
90 |
return accessKind;
|
|
91 |
}
|
|
92 |
|
|
93 |
@Override
|
|
94 |
public Node canonical(CanonicalizerTool tool) {
|
46344
|
95 |
if (!isAnyLocationForced() && getLocationIdentity().isAny()) {
|
|
96 |
if (offset().isConstant()) {
|
|
97 |
long constantOffset = offset().asJavaConstant().asLong();
|
43972
|
98 |
|
46344
|
99 |
// Try to canonicalize to a field access.
|
|
100 |
ResolvedJavaType receiverType = StampTool.typeOrNull(object());
|
|
101 |
if (receiverType != null) {
|
58877
|
102 |
ResolvedJavaField field = getStaticFieldUnsafeAccess(tool.getConstantReflection());
|
|
103 |
if (field == null) {
|
|
104 |
field = receiverType.findInstanceFieldWithOffset(constantOffset, accessKind());
|
|
105 |
}
|
|
106 |
|
|
107 |
// No need for checking that the receiver is non-null. The field access
|
|
108 |
// includes the null check and if a field is found, the offset is so small that
|
|
109 |
// this is never a valid access of an arbitrary address.
|
46344
|
110 |
if (field != null && field.getJavaKind() == this.accessKind()) {
|
|
111 |
assert !graph().isAfterFloatingReadPhase() : "cannot add more precise memory location after floating read phase";
|
50609
|
112 |
// Unsafe accesses never have volatile semantics.
|
|
113 |
// Memory barriers are placed around such an unsafe access at construction
|
|
114 |
// time if necessary, unlike AccessFieldNodes which encapsulate their
|
|
115 |
// potential volatile semantics.
|
|
116 |
return cloneAsFieldAccess(graph().getAssumptions(), field, false);
|
46344
|
117 |
}
|
43972
|
118 |
}
|
|
119 |
}
|
|
120 |
ResolvedJavaType receiverType = StampTool.typeOrNull(object());
|
|
121 |
// Try to build a better location identity.
|
|
122 |
if (receiverType != null && receiverType.isArray()) {
|
|
123 |
LocationIdentity identity = NamedLocationIdentity.getArrayLocation(receiverType.getComponentType().getJavaKind());
|
|
124 |
assert !graph().isAfterFloatingReadPhase() : "cannot add more precise memory location after floating read phase";
|
|
125 |
return cloneAsArrayAccess(offset(), identity);
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
return this;
|
|
130 |
}
|
|
131 |
|
50609
|
132 |
protected ValueNode cloneAsFieldAccess(Assumptions assumptions, ResolvedJavaField field) {
|
|
133 |
return cloneAsFieldAccess(assumptions, field, field.isVolatile());
|
|
134 |
}
|
|
135 |
|
|
136 |
protected abstract ValueNode cloneAsFieldAccess(Assumptions assumptions, ResolvedJavaField field, boolean volatileAccess);
|
43972
|
137 |
|
|
138 |
protected abstract ValueNode cloneAsArrayAccess(ValueNode location, LocationIdentity identity);
|
58877
|
139 |
|
|
140 |
/**
|
|
141 |
* In this method we check if the unsafe access is to a static field. This is the case when
|
|
142 |
* {@code object} is a constant of type {@link Class} (static field's declaring class) and
|
|
143 |
* {@code offset} is a constant (HotSpot-specific field offset from the declaring class).
|
|
144 |
*
|
|
145 |
* @return the static field, if any, that this node is reading
|
|
146 |
*/
|
|
147 |
private ResolvedJavaField getStaticFieldUnsafeAccess(ConstantReflectionProvider constantReflection) {
|
|
148 |
if (!object().isJavaConstant() || !offset().isJavaConstant() ||
|
|
149 |
object().isNullConstant() || offset().isNullConstant()) {
|
|
150 |
return null;
|
|
151 |
}
|
|
152 |
JavaConstant objectConstant = object().asJavaConstant();
|
|
153 |
JavaConstant offsetConstant = offset().asJavaConstant();
|
|
154 |
assert objectConstant != null && offsetConstant != null : "Verified by the check at the beginning.";
|
|
155 |
ResolvedJavaType staticReceiverType = constantReflection.asJavaType(objectConstant);
|
|
156 |
if (staticReceiverType == null) {
|
|
157 |
// object is not of type Class so it is not a static field
|
|
158 |
return null;
|
|
159 |
}
|
|
160 |
return findStaticFieldWithOffset(staticReceiverType, offsetConstant.asLong(), accessKind);
|
|
161 |
}
|
|
162 |
|
|
163 |
private static ResolvedJavaField findStaticFieldWithOffset(ResolvedJavaType type, long offset, JavaKind expectedEntryKind) {
|
|
164 |
try {
|
|
165 |
ResolvedJavaField[] declaredFields = type.getStaticFields();
|
|
166 |
return findFieldWithOffset(offset, expectedEntryKind, declaredFields);
|
|
167 |
} catch (UnsupportedOperationException e) {
|
|
168 |
return null;
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* NOTE GR-18873: this is a copy-paste implementation derived from
|
|
174 |
* {@code jdk.vm.ci.hotspot.HotSpotResolvedObjectTypeImpl#findStaticFieldWithOffset}.
|
|
175 |
*/
|
|
176 |
private static ResolvedJavaField findFieldWithOffset(long offset, JavaKind expectedEntryKind, ResolvedJavaField[] declaredFields) {
|
|
177 |
for (ResolvedJavaField field : declaredFields) {
|
|
178 |
long resolvedFieldOffset = field.getOffset();
|
|
179 |
if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN &&
|
|
180 |
expectedEntryKind.isPrimitive() &&
|
|
181 |
!expectedEntryKind.equals(JavaKind.Void) &&
|
|
182 |
field.getJavaKind().isPrimitive()) {
|
|
183 |
resolvedFieldOffset += field.getJavaKind().getByteCount() -
|
|
184 |
Math.min(field.getJavaKind().getByteCount(), 4 + expectedEntryKind.getByteCount());
|
|
185 |
}
|
|
186 |
if (resolvedFieldOffset == offset) {
|
|
187 |
return field;
|
|
188 |
}
|
|
189 |
}
|
|
190 |
return null;
|
|
191 |
}
|
|
192 |
|
43972
|
193 |
}
|