43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 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.hotspot.replacements;
|
|
24 |
|
46393
|
25 |
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
|
43972
|
26 |
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
|
|
27 |
|
48861
|
28 |
import org.graalvm.compiler.core.common.calc.CanonicalCondition;
|
46371
|
29 |
import org.graalvm.compiler.core.common.type.Stamp;
|
43972
|
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.hotspot.nodes.type.KlassPointerStamp;
|
|
35 |
import org.graalvm.compiler.hotspot.word.KlassPointer;
|
|
36 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
37 |
import org.graalvm.compiler.nodes.ConstantNode;
|
48190
|
38 |
import org.graalvm.compiler.nodes.NodeView;
|
46344
|
39 |
import org.graalvm.compiler.nodes.PiNode;
|
43972
|
40 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
41 |
import org.graalvm.compiler.nodes.calc.ConvertNode;
|
46344
|
42 |
import org.graalvm.compiler.nodes.calc.FloatingNode;
|
43972
|
43 |
import org.graalvm.compiler.nodes.extended.GetClassNode;
|
|
44 |
import org.graalvm.compiler.nodes.extended.GuardingNode;
|
|
45 |
import org.graalvm.compiler.nodes.extended.LoadHubNode;
|
46371
|
46 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
|
43972
|
47 |
import org.graalvm.compiler.nodes.memory.ReadNode;
|
|
48 |
import org.graalvm.compiler.nodes.memory.address.AddressNode;
|
|
49 |
import org.graalvm.compiler.nodes.spi.Lowerable;
|
|
50 |
import org.graalvm.compiler.nodes.spi.LoweringTool;
|
46551
|
51 |
import org.graalvm.word.LocationIdentity;
|
43972
|
52 |
|
|
53 |
import jdk.vm.ci.meta.Constant;
|
|
54 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
|
55 |
import jdk.vm.ci.meta.JavaConstant;
|
48861
|
56 |
import jdk.vm.ci.meta.JavaKind;
|
43972
|
57 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
48861
|
58 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
43972
|
59 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
60 |
|
|
61 |
/**
|
|
62 |
* Read {@code Class::_klass} to get the hub for a {@link java.lang.Class}. This node mostly exists
|
|
63 |
* to replace {@code _klass._java_mirror._klass} with {@code _klass}. The constant folding could be
|
|
64 |
* handled by
|
|
65 |
* {@link ReadNode#canonicalizeRead(ValueNode, AddressNode, LocationIdentity, CanonicalizerTool)}.
|
|
66 |
*/
|
46393
|
67 |
@NodeInfo(cycles = CYCLES_1, size = SIZE_1)
|
46344
|
68 |
public final class ClassGetHubNode extends FloatingNode implements Lowerable, Canonicalizable, ConvertNode {
|
43972
|
69 |
public static final NodeClass<ClassGetHubNode> TYPE = NodeClass.create(ClassGetHubNode.class);
|
|
70 |
@Input protected ValueNode clazz;
|
|
71 |
|
|
72 |
public ClassGetHubNode(ValueNode clazz) {
|
46344
|
73 |
super(TYPE, KlassPointerStamp.klass());
|
43972
|
74 |
this.clazz = clazz;
|
|
75 |
}
|
|
76 |
|
46371
|
77 |
public static ValueNode create(ValueNode clazz, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, boolean allUsagesAvailable) {
|
|
78 |
return canonical(null, metaAccess, constantReflection, allUsagesAvailable, KlassPointerStamp.klass(), clazz);
|
|
79 |
}
|
|
80 |
|
|
81 |
@SuppressWarnings("unused")
|
|
82 |
public static boolean intrinsify(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode clazz) {
|
|
83 |
ValueNode clazzValue = create(clazz, b.getMetaAccess(), b.getConstantReflection(), false);
|
46393
|
84 |
b.push(JavaKind.Object, b.append(clazzValue));
|
46371
|
85 |
return true;
|
|
86 |
}
|
|
87 |
|
|
88 |
public static ValueNode canonical(ClassGetHubNode classGetHubNode, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, boolean allUsagesAvailable, Stamp stamp,
|
|
89 |
ValueNode clazz) {
|
|
90 |
ClassGetHubNode self = classGetHubNode;
|
|
91 |
if (allUsagesAvailable && self != null && self.hasNoUsages()) {
|
43972
|
92 |
return null;
|
|
93 |
} else {
|
|
94 |
if (clazz.isConstant()) {
|
|
95 |
if (metaAccess != null) {
|
46371
|
96 |
ResolvedJavaType exactType = constantReflection.asJavaType(clazz.asJavaConstant());
|
43972
|
97 |
if (exactType.isPrimitive()) {
|
46371
|
98 |
return ConstantNode.forConstant(stamp, JavaConstant.NULL_POINTER, metaAccess);
|
43972
|
99 |
} else {
|
46371
|
100 |
return ConstantNode.forConstant(stamp, constantReflection.asObjectHub(exactType), metaAccess);
|
43972
|
101 |
}
|
|
102 |
}
|
|
103 |
}
|
|
104 |
if (clazz instanceof GetClassNode) {
|
|
105 |
GetClassNode getClass = (GetClassNode) clazz;
|
|
106 |
return new LoadHubNode(KlassPointerStamp.klassNonNull(), getClass.getObject());
|
|
107 |
}
|
|
108 |
if (clazz instanceof HubGetClassNode) {
|
46371
|
109 |
// Replace: _klass._java_mirror._klass -> _klass
|
43972
|
110 |
return ((HubGetClassNode) clazz).getHub();
|
|
111 |
}
|
46371
|
112 |
if (self == null) {
|
|
113 |
self = new ClassGetHubNode(clazz);
|
|
114 |
}
|
|
115 |
return self;
|
43972
|
116 |
}
|
|
117 |
}
|
|
118 |
|
|
119 |
@Override
|
46371
|
120 |
public Node canonical(CanonicalizerTool tool) {
|
48190
|
121 |
return canonical(this, tool.getMetaAccess(), tool.getConstantReflection(), tool.allUsagesAvailable(), stamp(NodeView.DEFAULT), clazz);
|
46371
|
122 |
}
|
|
123 |
|
|
124 |
@Override
|
43972
|
125 |
public void lower(LoweringTool tool) {
|
|
126 |
tool.getLowerer().lower(this, tool);
|
|
127 |
}
|
|
128 |
|
|
129 |
@NodeIntrinsic
|
46344
|
130 |
public static native KlassPointer readClass(Class<?> clazzNonNull);
|
43972
|
131 |
|
46344
|
132 |
@NodeIntrinsic(PiNode.class)
|
|
133 |
public static native KlassPointer piCastNonNull(Object object, GuardingNode anchor);
|
43972
|
134 |
|
|
135 |
@Override
|
|
136 |
public ValueNode getValue() {
|
|
137 |
return clazz;
|
|
138 |
}
|
|
139 |
|
|
140 |
@Override
|
|
141 |
public Constant convert(Constant c, ConstantReflectionProvider constantReflection) {
|
|
142 |
ResolvedJavaType exactType = constantReflection.asJavaType(c);
|
|
143 |
if (exactType.isPrimitive()) {
|
|
144 |
return JavaConstant.NULL_POINTER;
|
|
145 |
} else {
|
|
146 |
return constantReflection.asObjectHub(exactType);
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
@Override
|
|
151 |
public Constant reverse(Constant c, ConstantReflectionProvider constantReflection) {
|
|
152 |
assert !c.equals(JavaConstant.NULL_POINTER);
|
|
153 |
ResolvedJavaType objectType = constantReflection.asJavaType(c);
|
|
154 |
return constantReflection.asJavaClass(objectType);
|
|
155 |
}
|
|
156 |
|
|
157 |
@Override
|
|
158 |
public boolean isLossless() {
|
|
159 |
return false;
|
|
160 |
}
|
|
161 |
|
46344
|
162 |
/**
|
|
163 |
* There is more than one {@link java.lang.Class} value that has a NULL hub.
|
|
164 |
*/
|
|
165 |
@Override
|
|
166 |
public boolean mayNullCheckSkipConversion() {
|
|
167 |
return false;
|
|
168 |
}
|
|
169 |
|
43972
|
170 |
@Override
|
48861
|
171 |
public boolean preservesOrder(CanonicalCondition op, Constant value, ConstantReflectionProvider constantReflection) {
|
|
172 |
assert op == CanonicalCondition.EQ;
|
43972
|
173 |
ResolvedJavaType exactType = constantReflection.asJavaType(value);
|
|
174 |
return !exactType.isPrimitive();
|
|
175 |
}
|
|
176 |
|
|
177 |
}
|