43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, 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.replacements.nodes;
|
|
24 |
|
46393
|
25 |
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_8;
|
|
26 |
|
43972
|
27 |
import java.util.Collections;
|
|
28 |
|
|
29 |
import org.graalvm.compiler.core.common.type.ObjectStamp;
|
|
30 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
31 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
|
32 |
import org.graalvm.compiler.core.common.type.StampPair;
|
|
33 |
import org.graalvm.compiler.graph.NodeClass;
|
47798
|
34 |
import org.graalvm.compiler.nodeinfo.NodeCycles;
|
43972
|
35 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
36 |
import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind;
|
48190
|
37 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
38 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
39 |
import org.graalvm.compiler.nodes.java.LoadFieldNode;
|
|
40 |
import org.graalvm.compiler.nodes.java.MonitorIdNode;
|
|
41 |
import org.graalvm.compiler.nodes.spi.ArrayLengthProvider;
|
|
42 |
import org.graalvm.compiler.nodes.spi.VirtualizableAllocation;
|
|
43 |
import org.graalvm.compiler.nodes.spi.VirtualizerTool;
|
|
44 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
|
45 |
import org.graalvm.compiler.nodes.virtual.VirtualInstanceNode;
|
|
46 |
import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
|
|
47 |
|
|
48 |
import jdk.vm.ci.meta.Assumptions;
|
|
49 |
import jdk.vm.ci.meta.ResolvedJavaField;
|
|
50 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
51 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
52 |
|
47798
|
53 |
@NodeInfo(cycles = NodeCycles.CYCLES_UNKNOWN, size = SIZE_8)
|
43972
|
54 |
public abstract class BasicObjectCloneNode extends MacroStateSplitNode implements VirtualizableAllocation, ArrayLengthProvider {
|
|
55 |
|
|
56 |
public static final NodeClass<BasicObjectCloneNode> TYPE = NodeClass.create(BasicObjectCloneNode.class);
|
|
57 |
|
|
58 |
public BasicObjectCloneNode(NodeClass<? extends MacroNode> c, InvokeKind invokeKind, ResolvedJavaMethod targetMethod, int bci, StampPair returnStamp, ValueNode... arguments) {
|
|
59 |
super(c, invokeKind, targetMethod, bci, returnStamp, arguments);
|
|
60 |
updateStamp(computeStamp(getObject()));
|
|
61 |
}
|
|
62 |
|
|
63 |
@Override
|
|
64 |
public boolean inferStamp() {
|
48861
|
65 |
return updateStamp(stamp.improveWith(computeStamp(getObject())));
|
43972
|
66 |
}
|
|
67 |
|
46509
|
68 |
protected Stamp computeStamp(ValueNode object) {
|
48190
|
69 |
Stamp objectStamp = object.stamp(NodeView.DEFAULT);
|
43972
|
70 |
if (objectStamp instanceof ObjectStamp) {
|
|
71 |
objectStamp = objectStamp.join(StampFactory.objectNonNull());
|
|
72 |
}
|
|
73 |
return objectStamp;
|
|
74 |
}
|
|
75 |
|
|
76 |
public ValueNode getObject() {
|
|
77 |
return arguments.get(0);
|
|
78 |
}
|
|
79 |
|
|
80 |
/*
|
|
81 |
* Looks at the given stamp and determines if it is an exact type (or can be assumed to be an
|
|
82 |
* exact type) and if it is a cloneable type.
|
|
83 |
*
|
|
84 |
* If yes, then the exact type is returned, otherwise it returns null.
|
|
85 |
*/
|
46509
|
86 |
protected ResolvedJavaType getConcreteType(Stamp forStamp) {
|
|
87 |
if (!(forStamp instanceof ObjectStamp)) {
|
43972
|
88 |
return null;
|
|
89 |
}
|
46509
|
90 |
ObjectStamp objectStamp = (ObjectStamp) forStamp;
|
43972
|
91 |
if (objectStamp.type() == null) {
|
|
92 |
return null;
|
|
93 |
} else if (objectStamp.isExactType()) {
|
|
94 |
return objectStamp.type().isCloneableWithAllocation() ? objectStamp.type() : null;
|
46509
|
95 |
} else if (objectStamp.type().isArray()) {
|
|
96 |
return objectStamp.type();
|
43972
|
97 |
}
|
|
98 |
return null;
|
|
99 |
}
|
|
100 |
|
|
101 |
protected LoadFieldNode genLoadFieldNode(Assumptions assumptions, ValueNode originalAlias, ResolvedJavaField field) {
|
|
102 |
return LoadFieldNode.create(assumptions, originalAlias, field);
|
|
103 |
}
|
|
104 |
|
|
105 |
@Override
|
|
106 |
public void virtualize(VirtualizerTool tool) {
|
|
107 |
ValueNode originalAlias = tool.getAlias(getObject());
|
|
108 |
if (originalAlias instanceof VirtualObjectNode) {
|
|
109 |
VirtualObjectNode originalVirtual = (VirtualObjectNode) originalAlias;
|
|
110 |
if (originalVirtual.type().isCloneableWithAllocation()) {
|
|
111 |
ValueNode[] newEntryState = new ValueNode[originalVirtual.entryCount()];
|
|
112 |
for (int i = 0; i < newEntryState.length; i++) {
|
|
113 |
newEntryState[i] = tool.getEntry(originalVirtual, i);
|
|
114 |
}
|
|
115 |
VirtualObjectNode newVirtual = originalVirtual.duplicate();
|
|
116 |
tool.createVirtualObject(newVirtual, newEntryState, Collections.<MonitorIdNode> emptyList(), false);
|
|
117 |
tool.replaceWithVirtual(newVirtual);
|
|
118 |
}
|
|
119 |
} else {
|
48190
|
120 |
ResolvedJavaType type = getConcreteType(originalAlias.stamp(NodeView.DEFAULT));
|
43972
|
121 |
if (type != null && !type.isArray()) {
|
|
122 |
VirtualInstanceNode newVirtual = createVirtualInstanceNode(type, true);
|
|
123 |
ResolvedJavaField[] fields = newVirtual.getFields();
|
|
124 |
|
|
125 |
ValueNode[] state = new ValueNode[fields.length];
|
|
126 |
final LoadFieldNode[] loads = new LoadFieldNode[fields.length];
|
|
127 |
for (int i = 0; i < fields.length; i++) {
|
|
128 |
state[i] = loads[i] = genLoadFieldNode(graph().getAssumptions(), originalAlias, fields[i]);
|
|
129 |
tool.addNode(loads[i]);
|
|
130 |
}
|
|
131 |
tool.createVirtualObject(newVirtual, state, Collections.<MonitorIdNode> emptyList(), false);
|
|
132 |
tool.replaceWithVirtual(newVirtual);
|
|
133 |
}
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
protected VirtualInstanceNode createVirtualInstanceNode(ResolvedJavaType type, boolean hasIdentity) {
|
|
138 |
return new VirtualInstanceNode(type, hasIdentity);
|
|
139 |
}
|
|
140 |
|
|
141 |
@Override
|
|
142 |
public ValueNode length() {
|
|
143 |
return GraphUtil.arrayLength(getObject());
|
|
144 |
}
|
|
145 |
}
|