43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2013, 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.hotspot.phases;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.nodes.ConstantNode.getConstantNodes;
|
|
28 |
import static org.graalvm.compiler.nodes.NamedLocationIdentity.FINAL_LOCATION;
|
|
29 |
|
46344
|
30 |
import org.graalvm.compiler.core.common.CompressEncoding;
|
43972
|
31 |
import org.graalvm.compiler.core.common.type.AbstractObjectStamp;
|
|
32 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
33 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
|
34 |
import org.graalvm.compiler.core.common.type.TypeReference;
|
|
35 |
import org.graalvm.compiler.debug.GraalError;
|
46371
|
36 |
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
|
46459
|
37 |
import org.graalvm.compiler.hotspot.nodes.HotSpotCompressionNode;
|
|
38 |
import org.graalvm.compiler.hotspot.nodes.type.HotSpotNarrowOopStamp;
|
43972
|
39 |
import org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp;
|
46371
|
40 |
import org.graalvm.compiler.hotspot.replacements.HubGetClassNode;
|
43972
|
41 |
import org.graalvm.compiler.nodes.ConstantNode;
|
|
42 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
43 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
44 |
import org.graalvm.compiler.nodes.memory.FloatingReadNode;
|
|
45 |
import org.graalvm.compiler.nodes.memory.address.AddressNode;
|
|
46 |
import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
|
55509
|
47 |
import org.graalvm.compiler.nodes.spi.CoreProviders;
|
43972
|
48 |
import org.graalvm.compiler.phases.BasePhase;
|
|
49 |
import org.graalvm.compiler.phases.common.LoweringPhase;
|
|
50 |
|
|
51 |
import jdk.vm.ci.hotspot.HotSpotObjectConstant;
|
|
52 |
import jdk.vm.ci.hotspot.HotSpotResolvedJavaField;
|
|
53 |
import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
|
|
54 |
import jdk.vm.ci.hotspot.HotSpotResolvedPrimitiveType;
|
|
55 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
|
56 |
import jdk.vm.ci.meta.JavaConstant;
|
|
57 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
58 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
59 |
|
|
60 |
/**
|
|
61 |
* For AOT compilation we aren't allowed to use a {@link Class} reference ({@code javaMirror})
|
|
62 |
* directly. Instead the {@link Class} reference should be obtained from the {@code Klass} object.
|
|
63 |
* The reason for this is, that in Class Data Sharing (CDS) a {@code Klass} object is mapped to a
|
|
64 |
* fixed address in memory, but the {@code javaMirror} is not (which lives in the Java heap).
|
|
65 |
*
|
|
66 |
* Lowering can introduce new {@link ConstantNode}s containing a {@link Class} reference, thus this
|
|
67 |
* phase must be applied after {@link LoweringPhase}.
|
|
68 |
*
|
|
69 |
* @see AheadOfTimeVerificationPhase
|
|
70 |
*/
|
55509
|
71 |
public class LoadJavaMirrorWithKlassPhase extends BasePhase<CoreProviders> {
|
43972
|
72 |
|
|
73 |
private final CompressEncoding oopEncoding;
|
|
74 |
|
46371
|
75 |
public LoadJavaMirrorWithKlassPhase(GraalHotSpotVMConfig config) {
|
|
76 |
this.oopEncoding = config.useCompressedOops ? config.getOopEncoding() : null;
|
43972
|
77 |
}
|
|
78 |
|
55509
|
79 |
private ValueNode getClassConstantReplacement(StructuredGraph graph, CoreProviders context, JavaConstant constant) {
|
43972
|
80 |
if (constant instanceof HotSpotObjectConstant) {
|
|
81 |
ConstantReflectionProvider constantReflection = context.getConstantReflection();
|
|
82 |
ResolvedJavaType type = constantReflection.asJavaType(constant);
|
|
83 |
if (type != null) {
|
|
84 |
MetaAccessProvider metaAccess = context.getMetaAccess();
|
|
85 |
Stamp stamp = StampFactory.objectNonNull(TypeReference.createExactTrusted(metaAccess.lookupJavaType(Class.class)));
|
|
86 |
|
|
87 |
if (type instanceof HotSpotResolvedObjectType) {
|
|
88 |
ConstantNode klass = ConstantNode.forConstant(KlassPointerStamp.klassNonNull(), ((HotSpotResolvedObjectType) type).klass(), metaAccess, graph);
|
46371
|
89 |
ValueNode getClass = graph.unique(new HubGetClassNode(metaAccess, klass));
|
43972
|
90 |
|
|
91 |
if (((HotSpotObjectConstant) constant).isCompressed()) {
|
46459
|
92 |
return HotSpotCompressionNode.compress(getClass, oopEncoding);
|
43972
|
93 |
} else {
|
46371
|
94 |
return getClass;
|
43972
|
95 |
}
|
|
96 |
} else {
|
|
97 |
/*
|
|
98 |
* Primitive classes are more difficult since they don't have a corresponding
|
|
99 |
* Klass* so get them from Class.TYPE for the java box type.
|
|
100 |
*/
|
|
101 |
HotSpotResolvedPrimitiveType primitive = (HotSpotResolvedPrimitiveType) type;
|
|
102 |
ResolvedJavaType boxingClass = metaAccess.lookupJavaType(primitive.getJavaKind().toBoxedJavaClass());
|
|
103 |
ConstantNode clazz = ConstantNode.forConstant(context.getConstantReflection().asJavaClass(boxingClass), metaAccess, graph);
|
|
104 |
HotSpotResolvedJavaField[] a = (HotSpotResolvedJavaField[]) boxingClass.getStaticFields();
|
|
105 |
HotSpotResolvedJavaField typeField = null;
|
|
106 |
for (HotSpotResolvedJavaField f : a) {
|
|
107 |
if (f.getName().equals("TYPE")) {
|
|
108 |
typeField = f;
|
|
109 |
break;
|
|
110 |
}
|
|
111 |
}
|
|
112 |
if (typeField == null) {
|
|
113 |
throw new GraalError("Can't find TYPE field in class");
|
|
114 |
}
|
|
115 |
|
|
116 |
if (oopEncoding != null) {
|
46459
|
117 |
stamp = HotSpotNarrowOopStamp.compressed((AbstractObjectStamp) stamp, oopEncoding);
|
43972
|
118 |
}
|
50858
|
119 |
AddressNode address = graph.unique(new OffsetAddressNode(clazz, ConstantNode.forLong(typeField.getOffset(), graph)));
|
43972
|
120 |
ValueNode read = graph.unique(new FloatingReadNode(address, FINAL_LOCATION, null, stamp));
|
|
121 |
|
|
122 |
if (oopEncoding == null || ((HotSpotObjectConstant) constant).isCompressed()) {
|
|
123 |
return read;
|
|
124 |
} else {
|
46459
|
125 |
return HotSpotCompressionNode.uncompress(read, oopEncoding);
|
43972
|
126 |
}
|
|
127 |
}
|
|
128 |
}
|
|
129 |
}
|
|
130 |
return null;
|
|
131 |
}
|
|
132 |
|
|
133 |
@Override
|
55509
|
134 |
protected void run(StructuredGraph graph, CoreProviders context) {
|
43972
|
135 |
for (ConstantNode node : getConstantNodes(graph)) {
|
|
136 |
JavaConstant constant = node.asJavaConstant();
|
|
137 |
ValueNode freadNode = getClassConstantReplacement(graph, context, constant);
|
|
138 |
if (freadNode != null) {
|
|
139 |
node.replace(graph, freadNode);
|
|
140 |
}
|
|
141 |
}
|
|
142 |
}
|
|
143 |
|
|
144 |
@Override
|
|
145 |
public float codeSizeIncrease() {
|
|
146 |
return 2.5f;
|
|
147 |
}
|
|
148 |
}
|