43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2016, 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.aot;
|
|
26 |
|
46536
|
27 |
import static org.graalvm.compiler.core.common.cfg.AbstractControlFlowGraph.strictlyDominates;
|
43972
|
28 |
import static org.graalvm.compiler.hotspot.nodes.aot.LoadMethodCountersNode.getLoadMethodCountersNodes;
|
|
29 |
import static org.graalvm.compiler.nodes.ConstantNode.getConstantNodes;
|
|
30 |
|
|
31 |
import java.util.HashSet;
|
46536
|
32 |
import java.util.List;
|
43972
|
33 |
|
49873
|
34 |
import jdk.internal.vm.compiler.collections.EconomicMap;
|
58299
|
35 |
import jdk.internal.vm.compiler.collections.Equivalence;
|
46536
|
36 |
import org.graalvm.compiler.core.common.cfg.BlockMap;
|
43972
|
37 |
import org.graalvm.compiler.core.common.type.ObjectStamp;
|
|
38 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
39 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
|
40 |
import org.graalvm.compiler.debug.GraalError;
|
|
41 |
import org.graalvm.compiler.graph.Node;
|
46536
|
42 |
import org.graalvm.compiler.graph.NodeMap;
|
43972
|
43 |
import org.graalvm.compiler.hotspot.meta.HotSpotConstantLoadAction;
|
|
44 |
import org.graalvm.compiler.hotspot.nodes.aot.InitializeKlassNode;
|
|
45 |
import org.graalvm.compiler.hotspot.nodes.aot.LoadConstantIndirectlyFixedNode;
|
|
46 |
import org.graalvm.compiler.hotspot.nodes.aot.LoadConstantIndirectlyNode;
|
|
47 |
import org.graalvm.compiler.hotspot.nodes.aot.LoadMethodCountersNode;
|
|
48 |
import org.graalvm.compiler.hotspot.nodes.aot.ResolveConstantNode;
|
47667
|
49 |
import org.graalvm.compiler.hotspot.nodes.aot.ResolveDynamicConstantNode;
|
43972
|
50 |
import org.graalvm.compiler.hotspot.nodes.aot.ResolveMethodAndLoadCountersNode;
|
46640
|
51 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
|
52 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
43972
|
53 |
import org.graalvm.compiler.nodes.ConstantNode;
|
46640
|
54 |
import org.graalvm.compiler.nodes.FixedNode;
|
46536
|
55 |
import org.graalvm.compiler.nodes.FixedWithNextNode;
|
46640
|
56 |
import org.graalvm.compiler.nodes.FrameState;
|
|
57 |
import org.graalvm.compiler.nodes.LoopBeginNode;
|
|
58 |
import org.graalvm.compiler.nodes.LoopExitNode;
|
|
59 |
import org.graalvm.compiler.nodes.StateSplit;
|
43972
|
60 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
46536
|
61 |
import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
|
43972
|
62 |
import org.graalvm.compiler.nodes.ValueNode;
|
46640
|
63 |
import org.graalvm.compiler.nodes.calc.FloatingNode;
|
46536
|
64 |
import org.graalvm.compiler.nodes.cfg.Block;
|
55509
|
65 |
import org.graalvm.compiler.nodes.spi.CoreProviders;
|
43972
|
66 |
import org.graalvm.compiler.phases.BasePhase;
|
46640
|
67 |
import org.graalvm.compiler.phases.graph.ReentrantNodeIterator;
|
|
68 |
import org.graalvm.compiler.phases.graph.ReentrantNodeIterator.NodeIteratorClosure;
|
46536
|
69 |
import org.graalvm.compiler.phases.schedule.SchedulePhase;
|
|
70 |
import org.graalvm.compiler.phases.schedule.SchedulePhase.SchedulingStrategy;
|
43972
|
71 |
|
46640
|
72 |
import jdk.vm.ci.code.BytecodeFrame;
|
|
73 |
import jdk.vm.ci.hotspot.HotSpotMetaspaceConstant;
|
|
74 |
import jdk.vm.ci.hotspot.HotSpotObjectConstant;
|
|
75 |
import jdk.vm.ci.hotspot.HotSpotResolvedJavaType;
|
|
76 |
import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
|
|
77 |
import jdk.vm.ci.meta.Constant;
|
|
78 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
50858
|
79 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
46640
|
80 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
81 |
|
55509
|
82 |
public class ReplaceConstantNodesPhase extends BasePhase<CoreProviders> {
|
43972
|
83 |
|
46536
|
84 |
private final boolean verifyFingerprints;
|
43972
|
85 |
|
50858
|
86 |
static Class<?> characterCacheClass = Character.class.getDeclaredClasses()[0];
|
|
87 |
static Class<?> byteCacheClass = Byte.class.getDeclaredClasses()[0];
|
|
88 |
static Class<?> shortCacheClass = Short.class.getDeclaredClasses()[0];
|
|
89 |
static Class<?> integerCacheClass = Integer.class.getDeclaredClasses()[0];
|
|
90 |
static Class<?> longCacheClass = Long.class.getDeclaredClasses()[0];
|
43972
|
91 |
|
50858
|
92 |
static class ClassInfo {
|
43972
|
93 |
|
50858
|
94 |
private ResolvedJavaType stringType;
|
|
95 |
private final HashSet<ResolvedJavaType> builtIns = new HashSet<>();
|
|
96 |
|
|
97 |
ClassInfo(MetaAccessProvider metaAccessProvider) {
|
|
98 |
builtIns.add(metaAccessProvider.lookupJavaType(Boolean.class));
|
43972
|
99 |
|
50858
|
100 |
assert "java.lang.Character$CharacterCache".equals(characterCacheClass.getName());
|
|
101 |
builtIns.add(metaAccessProvider.lookupJavaType(characterCacheClass));
|
|
102 |
|
|
103 |
assert "java.lang.Byte$ByteCache".equals(byteCacheClass.getName());
|
|
104 |
builtIns.add(metaAccessProvider.lookupJavaType(byteCacheClass));
|
|
105 |
|
|
106 |
assert "java.lang.Short$ShortCache".equals(shortCacheClass.getName());
|
|
107 |
builtIns.add(metaAccessProvider.lookupJavaType(shortCacheClass));
|
43972
|
108 |
|
50858
|
109 |
assert "java.lang.Integer$IntegerCache".equals(integerCacheClass.getName());
|
|
110 |
builtIns.add(metaAccessProvider.lookupJavaType(integerCacheClass));
|
43972
|
111 |
|
50858
|
112 |
assert "java.lang.Long$LongCache".equals(longCacheClass.getName());
|
|
113 |
builtIns.add(metaAccessProvider.lookupJavaType(longCacheClass));
|
|
114 |
|
|
115 |
stringType = metaAccessProvider.lookupJavaType(String.class);
|
|
116 |
}
|
43972
|
117 |
}
|
|
118 |
|
|
119 |
private static boolean isReplacementNode(Node n) {
|
|
120 |
// @formatter:off
|
|
121 |
return n instanceof LoadConstantIndirectlyNode ||
|
50858
|
122 |
n instanceof LoadConstantIndirectlyFixedNode ||
|
|
123 |
n instanceof ResolveDynamicConstantNode ||
|
|
124 |
n instanceof ResolveConstantNode ||
|
|
125 |
n instanceof InitializeKlassNode;
|
43972
|
126 |
// @formatter:on
|
|
127 |
}
|
|
128 |
|
46536
|
129 |
private static boolean anyUsagesNeedReplacement(ConstantNode node) {
|
|
130 |
return node.usages().filter(n -> !isReplacementNode(n)).isNotEmpty();
|
|
131 |
}
|
|
132 |
|
|
133 |
private static boolean anyUsagesNeedReplacement(LoadMethodCountersNode node) {
|
|
134 |
return node.usages().filter(n -> !(n instanceof ResolveMethodAndLoadCountersNode)).isNotEmpty();
|
|
135 |
}
|
|
136 |
|
43972
|
137 |
private static boolean checkForBadFingerprint(HotSpotResolvedJavaType type) {
|
|
138 |
if (type.isArray()) {
|
|
139 |
if (type.getElementalType().isPrimitive()) {
|
|
140 |
return false;
|
|
141 |
}
|
46640
|
142 |
return ((HotSpotResolvedObjectType) (type.getElementalType())).getFingerprint() == 0;
|
43972
|
143 |
}
|
46640
|
144 |
return ((HotSpotResolvedObjectType) type).getFingerprint() == 0;
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Insert the replacement node into the graph. We may need to insert it into a place different
|
|
149 |
* than the original {@link FloatingNode} since we need to make sure that replacement will have
|
|
150 |
* a valid state assigned.
|
|
151 |
*
|
|
152 |
* @param graph
|
|
153 |
* @param stateMapper
|
|
154 |
* @param node
|
|
155 |
* @param replacement
|
|
156 |
*/
|
|
157 |
private static void insertReplacement(StructuredGraph graph, FrameStateMapperClosure stateMapper, FloatingNode node, FixedWithNextNode replacement) {
|
|
158 |
FixedWithNextNode insertionPoint = findInsertionPoint(graph, stateMapper, node);
|
|
159 |
graph.addAfterFixed(insertionPoint, replacement);
|
|
160 |
stateMapper.addState(replacement, stateMapper.getState(insertionPoint));
|
43972
|
161 |
}
|
|
162 |
|
46536
|
163 |
/**
|
46640
|
164 |
* Find a good place to insert a stateful fixed node that is above the given node. A good
|
|
165 |
* insertion point should have a valid FrameState reaching it.
|
46536
|
166 |
*
|
|
167 |
* @param graph
|
46640
|
168 |
* @param stateMapper
|
|
169 |
* @param node start search from this node up
|
|
170 |
* @return an insertion point
|
46536
|
171 |
*/
|
46640
|
172 |
private static FixedWithNextNode findInsertionPoint(StructuredGraph graph, FrameStateMapperClosure stateMapper, FloatingNode node) {
|
|
173 |
FixedWithNextNode fixed = findFixedBeforeFloating(graph, node);
|
|
174 |
FixedWithNextNode result = findFixedWithValidState(graph, stateMapper, fixed);
|
|
175 |
return result;
|
43972
|
176 |
}
|
|
177 |
|
46536
|
178 |
/**
|
46640
|
179 |
* Find the first {@link FixedWithNextNode} that is currently scheduled before the given
|
|
180 |
* floating node.
|
46536
|
181 |
*
|
|
182 |
* @param graph
|
46640
|
183 |
* @param node start search from this node up
|
|
184 |
* @return the first {@link FixedWithNextNode}
|
46536
|
185 |
*/
|
46640
|
186 |
private static FixedWithNextNode findFixedBeforeFloating(StructuredGraph graph, FloatingNode node) {
|
46536
|
187 |
ScheduleResult schedule = graph.getLastSchedule();
|
|
188 |
NodeMap<Block> nodeToBlock = schedule.getNodeToBlockMap();
|
46640
|
189 |
Block block = nodeToBlock.get(node);
|
46536
|
190 |
BlockMap<List<Node>> blockToNodes = schedule.getBlockToNodesMap();
|
|
191 |
FixedWithNextNode result = null;
|
|
192 |
for (Node n : blockToNodes.get(block)) {
|
46640
|
193 |
if (n.equals(node)) {
|
|
194 |
break;
|
|
195 |
}
|
46536
|
196 |
if (n instanceof FixedWithNextNode) {
|
|
197 |
result = (FixedWithNextNode) n;
|
|
198 |
}
|
|
199 |
}
|
|
200 |
assert result != null;
|
|
201 |
return result;
|
|
202 |
}
|
|
203 |
|
|
204 |
/**
|
46640
|
205 |
* Find first dominating {@link FixedWithNextNode} that has a valid state reaching it starting
|
|
206 |
* from the given node.
|
|
207 |
*
|
|
208 |
* @param graph
|
|
209 |
* @param stateMapper
|
|
210 |
* @param node
|
|
211 |
* @return {@link FixedWithNextNode} that we can use as an insertion point
|
|
212 |
*/
|
|
213 |
private static FixedWithNextNode findFixedWithValidState(StructuredGraph graph, FrameStateMapperClosure stateMapper, FixedWithNextNode node) {
|
|
214 |
ScheduleResult schedule = graph.getLastSchedule();
|
|
215 |
NodeMap<Block> nodeToBlock = schedule.getNodeToBlockMap();
|
|
216 |
Block block = nodeToBlock.get(node);
|
|
217 |
|
|
218 |
Node n = node;
|
|
219 |
do {
|
|
220 |
if (isFixedWithValidState(stateMapper, n)) {
|
|
221 |
return (FixedWithNextNode) n;
|
|
222 |
}
|
|
223 |
while (n != block.getBeginNode()) {
|
|
224 |
n = n.predecessor();
|
|
225 |
if (isFixedWithValidState(stateMapper, n)) {
|
|
226 |
return (FixedWithNextNode) n;
|
|
227 |
}
|
|
228 |
}
|
|
229 |
block = block.getDominator();
|
|
230 |
if (block != null) {
|
|
231 |
n = block.getEndNode();
|
|
232 |
}
|
|
233 |
} while (block != null);
|
|
234 |
|
|
235 |
return graph.start();
|
|
236 |
}
|
|
237 |
|
|
238 |
private static boolean isFixedWithValidState(FrameStateMapperClosure stateMapper, Node n) {
|
|
239 |
if (n instanceof FixedWithNextNode) {
|
|
240 |
FixedWithNextNode fixed = (FixedWithNextNode) n;
|
|
241 |
assert stateMapper.getState(fixed) != null;
|
|
242 |
if (!BytecodeFrame.isPlaceholderBci(stateMapper.getState(fixed).bci)) {
|
|
243 |
return true;
|
|
244 |
}
|
|
245 |
}
|
|
246 |
return false;
|
|
247 |
}
|
|
248 |
|
|
249 |
/**
|
|
250 |
* Compute frame states for all fixed nodes in the graph.
|
|
251 |
*/
|
|
252 |
private static class FrameStateMapperClosure extends NodeIteratorClosure<FrameState> {
|
|
253 |
private NodeMap<FrameState> reachingStates;
|
|
254 |
|
|
255 |
@Override
|
|
256 |
protected FrameState processNode(FixedNode node, FrameState previousState) {
|
|
257 |
FrameState currentState = previousState;
|
|
258 |
if (node instanceof StateSplit) {
|
|
259 |
StateSplit stateSplit = (StateSplit) node;
|
|
260 |
FrameState stateAfter = stateSplit.stateAfter();
|
|
261 |
if (stateAfter != null) {
|
|
262 |
currentState = stateAfter;
|
|
263 |
}
|
|
264 |
}
|
|
265 |
reachingStates.put(node, currentState);
|
|
266 |
return currentState;
|
|
267 |
}
|
|
268 |
|
|
269 |
@Override
|
|
270 |
protected FrameState merge(AbstractMergeNode merge, List<FrameState> states) {
|
|
271 |
FrameState singleFrameState = singleFrameState(states);
|
|
272 |
FrameState currentState = singleFrameState == null ? merge.stateAfter() : singleFrameState;
|
|
273 |
reachingStates.put(merge, currentState);
|
|
274 |
return currentState;
|
|
275 |
}
|
|
276 |
|
|
277 |
@Override
|
|
278 |
protected FrameState afterSplit(AbstractBeginNode node, FrameState oldState) {
|
|
279 |
return oldState;
|
|
280 |
}
|
|
281 |
|
|
282 |
@Override
|
|
283 |
protected EconomicMap<LoopExitNode, FrameState> processLoop(LoopBeginNode loop, FrameState initialState) {
|
|
284 |
return ReentrantNodeIterator.processLoop(this, loop, initialState).exitStates;
|
|
285 |
}
|
|
286 |
|
|
287 |
private static FrameState singleFrameState(List<FrameState> states) {
|
|
288 |
FrameState singleState = states.get(0);
|
|
289 |
for (int i = 1; i < states.size(); ++i) {
|
|
290 |
if (states.get(i) != singleState) {
|
|
291 |
return null;
|
|
292 |
}
|
|
293 |
}
|
|
294 |
return singleState;
|
|
295 |
}
|
|
296 |
|
|
297 |
FrameStateMapperClosure(StructuredGraph graph) {
|
|
298 |
reachingStates = new NodeMap<>(graph);
|
|
299 |
}
|
|
300 |
|
|
301 |
public FrameState getState(Node n) {
|
|
302 |
return reachingStates.get(n);
|
|
303 |
}
|
|
304 |
|
|
305 |
public void addState(Node n, FrameState s) {
|
|
306 |
reachingStates.setAndGrow(n, s);
|
|
307 |
}
|
|
308 |
}
|
|
309 |
|
|
310 |
/**
|
46566
|
311 |
* Try to find dominating node doing the resolution that can be reused.
|
46536
|
312 |
*
|
|
313 |
* @param graph
|
|
314 |
* @param node {@link ConstantNode} containing a {@link HotSpotResolvedJavaType} that needs
|
|
315 |
* resolution.
|
|
316 |
*/
|
46566
|
317 |
private static void tryToReplaceWithExisting(StructuredGraph graph, ConstantNode node) {
|
46536
|
318 |
ScheduleResult schedule = graph.getLastSchedule();
|
|
319 |
NodeMap<Block> nodeToBlock = schedule.getNodeToBlockMap();
|
|
320 |
BlockMap<List<Node>> blockToNodes = schedule.getBlockToNodesMap();
|
|
321 |
|
58299
|
322 |
EconomicMap<Block, Node> blockToExisting = EconomicMap.create(Equivalence.IDENTITY);
|
46566
|
323 |
for (Node n : node.usages().filter(n -> isReplacementNode(n))) {
|
|
324 |
blockToExisting.put(nodeToBlock.get(n), n);
|
46536
|
325 |
}
|
|
326 |
for (Node use : node.usages().filter(n -> !isReplacementNode(n)).snapshot()) {
|
|
327 |
boolean replaced = false;
|
|
328 |
Block b = nodeToBlock.get(use);
|
46566
|
329 |
Node e = blockToExisting.get(b);
|
|
330 |
if (e != null) {
|
|
331 |
// There is an initialization or resolution in the same block as the use, look if
|
|
332 |
// the use is scheduled after it.
|
46536
|
333 |
for (Node n : blockToNodes.get(b)) {
|
|
334 |
if (n.equals(use)) {
|
|
335 |
// Usage is before initialization, can't use it
|
|
336 |
break;
|
|
337 |
}
|
46566
|
338 |
if (n.equals(e)) {
|
|
339 |
use.replaceFirstInput(node, e);
|
46536
|
340 |
replaced = true;
|
|
341 |
break;
|
|
342 |
}
|
|
343 |
}
|
|
344 |
}
|
|
345 |
if (!replaced) {
|
46566
|
346 |
// Look for dominating blocks that have existing nodes
|
|
347 |
for (Block d : blockToExisting.getKeys()) {
|
46536
|
348 |
if (strictlyDominates(d, b)) {
|
46566
|
349 |
use.replaceFirstInput(node, blockToExisting.get(d));
|
46536
|
350 |
break;
|
|
351 |
}
|
|
352 |
}
|
|
353 |
}
|
|
354 |
}
|
|
355 |
}
|
|
356 |
|
|
357 |
/**
|
|
358 |
* Replace the uses of a constant with either {@link LoadConstantIndirectlyNode} or
|
|
359 |
* {@link ResolveConstantNode}.
|
|
360 |
*
|
|
361 |
* @param graph
|
46640
|
362 |
* @param stateMapper
|
46536
|
363 |
* @param node {@link ConstantNode} containing a {@link HotSpotResolvedJavaType} that needs
|
|
364 |
* resolution.
|
|
365 |
*/
|
50858
|
366 |
private static void replaceWithResolution(StructuredGraph graph, FrameStateMapperClosure stateMapper, ConstantNode node, ClassInfo classInfo) {
|
46536
|
367 |
HotSpotMetaspaceConstant metaspaceConstant = (HotSpotMetaspaceConstant) node.asConstant();
|
|
368 |
HotSpotResolvedJavaType type = (HotSpotResolvedJavaType) metaspaceConstant.asResolvedJavaType();
|
|
369 |
ResolvedJavaType topMethodHolder = graph.method().getDeclaringClass();
|
|
370 |
ValueNode replacement;
|
|
371 |
|
|
372 |
if (type.isArray() && type.getComponentType().isPrimitive()) {
|
|
373 |
// Special case for primitive arrays. The AOT runtime pre-resolves them, so we may
|
|
374 |
// omit the resolution call.
|
|
375 |
replacement = graph.addOrUnique(new LoadConstantIndirectlyNode(node));
|
|
376 |
} else if (type.equals(topMethodHolder) || (type.isAssignableFrom(topMethodHolder) && !type.isInterface())) {
|
|
377 |
// If it's a supertype of or the same class that declares the top method, we are
|
|
378 |
// guaranteed to have it resolved already. If it's an interface, we just test for
|
|
379 |
// equality.
|
|
380 |
replacement = graph.addOrUnique(new LoadConstantIndirectlyNode(node));
|
|
381 |
} else {
|
|
382 |
FixedWithNextNode fixedReplacement;
|
50858
|
383 |
if (classInfo.builtIns.contains(type)) {
|
46536
|
384 |
// Special case of klass constants that come from {@link BoxingSnippets}.
|
|
385 |
fixedReplacement = graph.add(new ResolveConstantNode(node, HotSpotConstantLoadAction.INITIALIZE));
|
|
386 |
} else {
|
|
387 |
fixedReplacement = graph.add(new ResolveConstantNode(node));
|
|
388 |
}
|
46640
|
389 |
insertReplacement(graph, stateMapper, node, fixedReplacement);
|
46536
|
390 |
replacement = fixedReplacement;
|
|
391 |
}
|
|
392 |
node.replaceAtUsages(replacement, n -> !isReplacementNode(n));
|
|
393 |
}
|
|
394 |
|
|
395 |
/**
|
46640
|
396 |
* Replace {@link ConstantNode} containing a {@link HotSpotResolvedJavaType} with indirection.
|
|
397 |
*
|
|
398 |
* @param graph
|
|
399 |
* @param stateMapper
|
|
400 |
* @param node {@link ConstantNode} containing a {@link HotSpotResolvedJavaType} that needs
|
|
401 |
* resolution.
|
|
402 |
*/
|
50858
|
403 |
private void handleHotSpotMetaspaceConstant(StructuredGraph graph, FrameStateMapperClosure stateMapper, ConstantNode node, ClassInfo classInfo) {
|
46640
|
404 |
HotSpotMetaspaceConstant metaspaceConstant = (HotSpotMetaspaceConstant) node.asConstant();
|
|
405 |
HotSpotResolvedJavaType type = (HotSpotResolvedJavaType) metaspaceConstant.asResolvedJavaType();
|
|
406 |
|
|
407 |
if (type != null) {
|
|
408 |
if (verifyFingerprints && checkForBadFingerprint(type)) {
|
|
409 |
throw new GraalError("Type with bad fingerprint: " + type);
|
|
410 |
}
|
|
411 |
assert !metaspaceConstant.isCompressed() : "No support for replacing compressed metaspace constants";
|
|
412 |
tryToReplaceWithExisting(graph, node);
|
|
413 |
if (anyUsagesNeedReplacement(node)) {
|
50858
|
414 |
replaceWithResolution(graph, stateMapper, node, classInfo);
|
46640
|
415 |
}
|
|
416 |
} else {
|
|
417 |
throw new GraalError("Unsupported metaspace constant type: " + type);
|
|
418 |
}
|
|
419 |
}
|
|
420 |
|
|
421 |
/**
|
46536
|
422 |
* Replace an object constant with an indirect load {@link ResolveConstantNode}. Currently we
|
|
423 |
* support only strings.
|
|
424 |
*
|
|
425 |
* @param graph
|
46640
|
426 |
* @param stateMapper
|
46536
|
427 |
* @param node {@link ConstantNode} containing a {@link HotSpotObjectConstant} that needs
|
|
428 |
* resolution.
|
|
429 |
*/
|
50858
|
430 |
private static void handleHotSpotObjectConstant(StructuredGraph graph, FrameStateMapperClosure stateMapper, ConstantNode node, ClassInfo classInfo) {
|
43972
|
431 |
HotSpotObjectConstant constant = (HotSpotObjectConstant) node.asJavaConstant();
|
|
432 |
HotSpotResolvedJavaType type = (HotSpotResolvedJavaType) constant.getType();
|
50858
|
433 |
if (type.equals(classInfo.stringType)) {
|
43972
|
434 |
assert !constant.isCompressed() : "No support for replacing compressed oop constants";
|
46536
|
435 |
FixedWithNextNode replacement = graph.add(new ResolveConstantNode(node));
|
46640
|
436 |
insertReplacement(graph, stateMapper, node, replacement);
|
43972
|
437 |
node.replaceAtUsages(replacement, n -> !(n instanceof ResolveConstantNode));
|
|
438 |
} else {
|
|
439 |
throw new GraalError("Unsupported object constant type: " + type);
|
|
440 |
}
|
|
441 |
}
|
|
442 |
|
46536
|
443 |
/**
|
|
444 |
* Replace {@link LoadMethodCountersNode} with indirect load
|
|
445 |
* {@link ResolveMethodAndLoadCountersNode}, expose a klass constant of the holder.
|
|
446 |
*
|
|
447 |
* @param graph
|
46640
|
448 |
* @param stateMapper
|
46536
|
449 |
* @param node
|
|
450 |
* @param context
|
|
451 |
*/
|
55509
|
452 |
private static void handleLoadMethodCounters(StructuredGraph graph, FrameStateMapperClosure stateMapper, LoadMethodCountersNode node, CoreProviders context) {
|
43972
|
453 |
ResolvedJavaType type = node.getMethod().getDeclaringClass();
|
|
454 |
Stamp hubStamp = context.getStampProvider().createHubStamp((ObjectStamp) StampFactory.objectNonNull());
|
|
455 |
ConstantReflectionProvider constantReflection = context.getConstantReflection();
|
|
456 |
ConstantNode klassHint = ConstantNode.forConstant(hubStamp, constantReflection.asObjectHub(type), context.getMetaAccess(), graph);
|
46536
|
457 |
FixedWithNextNode replacement = graph.add(new ResolveMethodAndLoadCountersNode(node.getMethod(), klassHint));
|
46640
|
458 |
insertReplacement(graph, stateMapper, node, replacement);
|
43972
|
459 |
node.replaceAtUsages(replacement, n -> !(n instanceof ResolveMethodAndLoadCountersNode));
|
|
460 |
}
|
|
461 |
|
46536
|
462 |
/**
|
|
463 |
* Replace {@link LoadMethodCountersNode} with {@link ResolveMethodAndLoadCountersNode}, expose
|
|
464 |
* klass constants.
|
|
465 |
*
|
|
466 |
* @param graph
|
46640
|
467 |
* @param stateMapper
|
46536
|
468 |
* @param context
|
|
469 |
*/
|
55509
|
470 |
private static void replaceLoadMethodCounters(StructuredGraph graph, FrameStateMapperClosure stateMapper, CoreProviders context) {
|
46536
|
471 |
new SchedulePhase(SchedulingStrategy.LATEST_OUT_OF_LOOPS, true).apply(graph, false);
|
46640
|
472 |
|
46536
|
473 |
for (LoadMethodCountersNode node : getLoadMethodCountersNodes(graph)) {
|
|
474 |
if (anyUsagesNeedReplacement(node)) {
|
46640
|
475 |
handleLoadMethodCounters(graph, stateMapper, node, context);
|
46536
|
476 |
}
|
|
477 |
}
|
|
478 |
}
|
|
479 |
|
|
480 |
/**
|
|
481 |
* Replace object and klass constants with resolution nodes or reuse preceding initializations.
|
|
482 |
*
|
|
483 |
* @param graph
|
46640
|
484 |
* @param stateMapper
|
46536
|
485 |
*/
|
50858
|
486 |
private void replaceKlassesAndObjects(StructuredGraph graph, FrameStateMapperClosure stateMapper, ClassInfo classInfo) {
|
46536
|
487 |
new SchedulePhase(SchedulingStrategy.LATEST_OUT_OF_LOOPS, true).apply(graph, false);
|
|
488 |
|
|
489 |
for (ConstantNode node : getConstantNodes(graph)) {
|
|
490 |
Constant constant = node.asConstant();
|
|
491 |
if (constant instanceof HotSpotMetaspaceConstant && anyUsagesNeedReplacement(node)) {
|
50858
|
492 |
handleHotSpotMetaspaceConstant(graph, stateMapper, node, classInfo);
|
46536
|
493 |
} else if (constant instanceof HotSpotObjectConstant && anyUsagesNeedReplacement(node)) {
|
50858
|
494 |
handleHotSpotObjectConstant(graph, stateMapper, node, classInfo);
|
46536
|
495 |
}
|
|
496 |
}
|
|
497 |
}
|
|
498 |
|
43972
|
499 |
@Override
|
55509
|
500 |
protected void run(StructuredGraph graph, CoreProviders context) {
|
46640
|
501 |
FrameStateMapperClosure stateMapper = new FrameStateMapperClosure(graph);
|
|
502 |
ReentrantNodeIterator.apply(stateMapper, graph.start(), null);
|
|
503 |
|
43972
|
504 |
// Replace LoadMethodCountersNode with ResolveMethodAndLoadCountersNode, expose klass
|
|
505 |
// constants.
|
46640
|
506 |
replaceLoadMethodCounters(graph, stateMapper, context);
|
43972
|
507 |
|
|
508 |
// Replace object and klass constants (including the ones added in the previous pass) with
|
|
509 |
// resolution nodes.
|
50858
|
510 |
replaceKlassesAndObjects(graph, stateMapper, new ClassInfo(context.getMetaAccess()));
|
43972
|
511 |
}
|
|
512 |
|
|
513 |
@Override
|
|
514 |
public boolean checkContract() {
|
|
515 |
return false;
|
|
516 |
}
|
|
517 |
|
46536
|
518 |
public ReplaceConstantNodesPhase() {
|
|
519 |
this(true);
|
|
520 |
}
|
|
521 |
|
|
522 |
public ReplaceConstantNodesPhase(boolean verifyFingerprints) {
|
|
523 |
this.verifyFingerprints = verifyFingerprints;
|
|
524 |
}
|
43972
|
525 |
}
|