43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 2014, 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;
|
|
24 |
|
|
25 |
import static org.graalvm.compiler.nodes.calc.CompareNode.createCompareNode;
|
|
26 |
|
|
27 |
import java.util.List;
|
|
28 |
|
|
29 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
|
48861
|
30 |
import org.graalvm.compiler.core.common.calc.CanonicalCondition;
|
46640
|
31 |
import org.graalvm.compiler.debug.DebugHandlersFactory;
|
43972
|
32 |
import org.graalvm.compiler.graph.Node;
|
|
33 |
import org.graalvm.compiler.nodes.ConditionAnchorNode;
|
|
34 |
import org.graalvm.compiler.nodes.ConstantNode;
|
|
35 |
import org.graalvm.compiler.nodes.FixedGuardNode;
|
|
36 |
import org.graalvm.compiler.nodes.IfNode;
|
|
37 |
import org.graalvm.compiler.nodes.LogicConstantNode;
|
|
38 |
import org.graalvm.compiler.nodes.LogicNode;
|
48190
|
39 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
40 |
import org.graalvm.compiler.nodes.PhiNode;
|
|
41 |
import org.graalvm.compiler.nodes.ShortCircuitOrNode;
|
|
42 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
43 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
44 |
import org.graalvm.compiler.nodes.calc.CompareNode;
|
|
45 |
import org.graalvm.compiler.nodes.calc.ConditionalNode;
|
|
46 |
import org.graalvm.compiler.nodes.calc.FloatingNode;
|
|
47 |
import org.graalvm.compiler.nodes.java.ClassIsAssignableFromNode;
|
|
48 |
import org.graalvm.compiler.nodes.java.InstanceOfDynamicNode;
|
|
49 |
import org.graalvm.compiler.nodes.java.InstanceOfNode;
|
|
50 |
import org.graalvm.compiler.nodes.spi.LoweringTool;
|
|
51 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
46344
|
52 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
53 |
import org.graalvm.compiler.phases.util.Providers;
|
|
54 |
import org.graalvm.compiler.replacements.SnippetTemplate.AbstractTemplates;
|
|
55 |
import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
|
|
56 |
import org.graalvm.compiler.replacements.SnippetTemplate.UsageReplacer;
|
|
57 |
|
|
58 |
import jdk.vm.ci.code.TargetDescription;
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Helper class for lowering {@link InstanceOfNode}s with snippets. The majority of the complexity
|
|
62 |
* in such a lowering derives from the fact that {@link InstanceOfNode} is a floating node. A
|
|
63 |
* snippet used to lower an {@link InstanceOfNode} will almost always incorporate control flow and
|
|
64 |
* replacing a floating node with control flow is not trivial.
|
|
65 |
* <p>
|
|
66 |
* The mechanism implemented in this class ensures that the graph for an instanceof snippet is
|
|
67 |
* instantiated once per {@link InstanceOfNode} being lowered. The result produced is then re-used
|
|
68 |
* by all usages of the node. Additionally, if there is a single usage that is an {@link IfNode},
|
|
69 |
* the control flow in the snippet is connected directly to the true and false successors of the
|
|
70 |
* {@link IfNode}. This avoids materializing the instanceof test as a boolean which is then retested
|
|
71 |
* by the {@link IfNode}.
|
|
72 |
*/
|
|
73 |
public abstract class InstanceOfSnippetsTemplates extends AbstractTemplates {
|
|
74 |
|
46640
|
75 |
public InstanceOfSnippetsTemplates(OptionValues options, Iterable<DebugHandlersFactory> factories, Providers providers, SnippetReflectionProvider snippetReflection, TargetDescription target) {
|
|
76 |
super(options, factories, providers, snippetReflection, target);
|
43972
|
77 |
}
|
|
78 |
|
|
79 |
/**
|
|
80 |
* Gets the arguments used to retrieve and instantiate an instanceof snippet template.
|
|
81 |
*/
|
|
82 |
protected abstract Arguments makeArguments(InstanceOfUsageReplacer replacer, LoweringTool tool);
|
|
83 |
|
|
84 |
public void lower(FloatingNode instanceOf, LoweringTool tool) {
|
|
85 |
assert instanceOf instanceof InstanceOfNode || instanceOf instanceof InstanceOfDynamicNode || instanceOf instanceof ClassIsAssignableFromNode;
|
|
86 |
List<Node> usages = instanceOf.usages().snapshot();
|
|
87 |
|
|
88 |
Instantiation instantiation = new Instantiation();
|
|
89 |
for (Node usage : usages) {
|
|
90 |
final StructuredGraph graph = (StructuredGraph) usage.graph();
|
|
91 |
|
|
92 |
InstanceOfUsageReplacer replacer = createReplacer(instanceOf, instantiation, usage, graph);
|
|
93 |
|
|
94 |
if (instantiation.isInitialized()) {
|
|
95 |
// No need to re-instantiate the snippet - just re-use its result
|
|
96 |
replacer.replaceUsingInstantiation();
|
|
97 |
} else {
|
|
98 |
Arguments args = makeArguments(replacer, tool);
|
46640
|
99 |
template(instanceOf.getDebug(), args).instantiate(providers.getMetaAccess(), instanceOf, replacer, tool, args);
|
43972
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
assert instanceOf.hasNoUsages();
|
|
104 |
if (!instanceOf.isDeleted()) {
|
|
105 |
GraphUtil.killWithUnusedFloatingInputs(instanceOf);
|
|
106 |
}
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Gets the specific replacer object used to replace the usage of an instanceof node with the
|
|
111 |
* result of an instantiated instanceof snippet.
|
|
112 |
*/
|
|
113 |
protected InstanceOfUsageReplacer createReplacer(FloatingNode instanceOf, Instantiation instantiation, Node usage, final StructuredGraph graph) {
|
|
114 |
InstanceOfUsageReplacer replacer;
|
|
115 |
if (!canMaterialize(usage)) {
|
|
116 |
ValueNode trueValue = ConstantNode.forInt(1, graph);
|
|
117 |
ValueNode falseValue = ConstantNode.forInt(0, graph);
|
|
118 |
if (instantiation.isInitialized() && (trueValue != instantiation.trueValue || falseValue != instantiation.falseValue)) {
|
|
119 |
/*
|
|
120 |
* This code doesn't really care what values are used so adopt the values from the
|
|
121 |
* previous instantiation.
|
|
122 |
*/
|
|
123 |
trueValue = instantiation.trueValue;
|
|
124 |
falseValue = instantiation.falseValue;
|
|
125 |
}
|
|
126 |
replacer = new NonMaterializationUsageReplacer(instantiation, trueValue, falseValue, instanceOf, usage);
|
|
127 |
} else {
|
|
128 |
assert usage instanceof ConditionalNode : "unexpected usage of " + instanceOf + ": " + usage;
|
|
129 |
ConditionalNode c = (ConditionalNode) usage;
|
|
130 |
replacer = new MaterializationUsageReplacer(instantiation, c.trueValue(), c.falseValue(), instanceOf, c);
|
|
131 |
}
|
|
132 |
return replacer;
|
|
133 |
}
|
|
134 |
|
|
135 |
/**
|
|
136 |
* Determines if an {@code instanceof} usage can be materialized.
|
|
137 |
*/
|
|
138 |
protected boolean canMaterialize(Node usage) {
|
|
139 |
if (usage instanceof ConditionalNode) {
|
|
140 |
ConditionalNode cn = (ConditionalNode) usage;
|
|
141 |
return cn.trueValue().isConstant() && cn.falseValue().isConstant();
|
|
142 |
}
|
|
143 |
if (usage instanceof IfNode || usage instanceof FixedGuardNode || usage instanceof ShortCircuitOrNode || usage instanceof ConditionAnchorNode) {
|
|
144 |
return false;
|
|
145 |
}
|
|
146 |
return true;
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* The result of instantiating an instanceof snippet. This enables a snippet instantiation to be
|
|
151 |
* re-used which reduces compile time and produces better code.
|
|
152 |
*/
|
|
153 |
public static final class Instantiation {
|
|
154 |
|
|
155 |
private ValueNode result;
|
|
156 |
private LogicNode condition;
|
|
157 |
private ValueNode trueValue;
|
|
158 |
private ValueNode falseValue;
|
|
159 |
|
|
160 |
/**
|
|
161 |
* Determines if the instantiation has occurred.
|
|
162 |
*/
|
|
163 |
boolean isInitialized() {
|
|
164 |
return result != null;
|
|
165 |
}
|
|
166 |
|
|
167 |
void initialize(ValueNode r, ValueNode t, ValueNode f) {
|
|
168 |
assert !isInitialized();
|
|
169 |
this.result = r;
|
|
170 |
this.trueValue = t;
|
|
171 |
this.falseValue = f;
|
|
172 |
}
|
|
173 |
|
|
174 |
/**
|
|
175 |
* Gets the result of this instantiation as a condition.
|
|
176 |
*
|
|
177 |
* @param testValue the returned condition is true if the result is equal to this value
|
|
178 |
*/
|
|
179 |
LogicNode asCondition(ValueNode testValue) {
|
|
180 |
assert isInitialized();
|
|
181 |
if (result.isConstant()) {
|
|
182 |
assert testValue.isConstant();
|
|
183 |
return LogicConstantNode.forBoolean(result.asConstant().equals(testValue.asConstant()), result.graph());
|
|
184 |
}
|
|
185 |
if (condition == null || (!(condition instanceof CompareNode)) || ((CompareNode) condition).getY() != testValue) {
|
|
186 |
// Re-use previously generated condition if the trueValue for the test is the same
|
48861
|
187 |
condition = createCompareNode(result.graph(), CanonicalCondition.EQ, result, testValue, null, NodeView.DEFAULT);
|
43972
|
188 |
}
|
|
189 |
return condition;
|
|
190 |
}
|
|
191 |
|
|
192 |
/**
|
|
193 |
* Gets the result of the instantiation as a materialized value.
|
|
194 |
*
|
|
195 |
* @param t the true value for the materialization
|
|
196 |
* @param f the false value for the materialization
|
|
197 |
*/
|
|
198 |
ValueNode asMaterialization(StructuredGraph graph, ValueNode t, ValueNode f) {
|
|
199 |
assert isInitialized();
|
|
200 |
if (t == this.trueValue && f == this.falseValue) {
|
|
201 |
// Can simply use the phi result if the same materialized values are expected.
|
|
202 |
return result;
|
|
203 |
} else {
|
|
204 |
return graph.unique(new ConditionalNode(asCondition(trueValue), t, f));
|
|
205 |
}
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
/**
|
|
210 |
* Replaces a usage of an {@link InstanceOfNode} or {@link InstanceOfDynamicNode}.
|
|
211 |
*/
|
|
212 |
public abstract static class InstanceOfUsageReplacer implements UsageReplacer {
|
|
213 |
|
|
214 |
public final Instantiation instantiation;
|
|
215 |
public final FloatingNode instanceOf;
|
|
216 |
public final ValueNode trueValue;
|
|
217 |
public final ValueNode falseValue;
|
|
218 |
|
|
219 |
public InstanceOfUsageReplacer(Instantiation instantiation, FloatingNode instanceOf, ValueNode trueValue, ValueNode falseValue) {
|
|
220 |
assert instanceOf instanceof InstanceOfNode || instanceOf instanceof InstanceOfDynamicNode || instanceOf instanceof ClassIsAssignableFromNode;
|
|
221 |
this.instantiation = instantiation;
|
|
222 |
this.instanceOf = instanceOf;
|
|
223 |
this.trueValue = trueValue;
|
|
224 |
this.falseValue = falseValue;
|
|
225 |
}
|
|
226 |
|
|
227 |
/**
|
|
228 |
* Does the replacement based on a previously snippet instantiation.
|
|
229 |
*/
|
|
230 |
public abstract void replaceUsingInstantiation();
|
|
231 |
}
|
|
232 |
|
|
233 |
/**
|
|
234 |
* Replaces the usage of an {@link InstanceOfNode} or {@link InstanceOfDynamicNode} that does
|
|
235 |
* not materialize the result of the type test.
|
|
236 |
*/
|
|
237 |
public static class NonMaterializationUsageReplacer extends InstanceOfUsageReplacer {
|
|
238 |
|
|
239 |
private final Node usage;
|
|
240 |
|
|
241 |
public NonMaterializationUsageReplacer(Instantiation instantiation, ValueNode trueValue, ValueNode falseValue, FloatingNode instanceOf, Node usage) {
|
|
242 |
super(instantiation, instanceOf, trueValue, falseValue);
|
|
243 |
this.usage = usage;
|
|
244 |
}
|
|
245 |
|
|
246 |
@Override
|
|
247 |
public void replaceUsingInstantiation() {
|
|
248 |
usage.replaceFirstInput(instanceOf, instantiation.asCondition(trueValue));
|
|
249 |
}
|
|
250 |
|
|
251 |
@Override
|
|
252 |
public void replace(ValueNode oldNode, ValueNode newNode) {
|
|
253 |
assert newNode instanceof PhiNode;
|
|
254 |
assert oldNode == instanceOf;
|
|
255 |
newNode.inferStamp();
|
|
256 |
instantiation.initialize(newNode, trueValue, falseValue);
|
|
257 |
usage.replaceFirstInput(oldNode, instantiation.asCondition(trueValue));
|
|
258 |
}
|
|
259 |
}
|
|
260 |
|
|
261 |
/**
|
|
262 |
* Replaces the usage of an {@link InstanceOfNode} or {@link InstanceOfDynamicNode} that does
|
|
263 |
* materializes the result of the type test.
|
|
264 |
*/
|
|
265 |
public static class MaterializationUsageReplacer extends InstanceOfUsageReplacer {
|
|
266 |
|
|
267 |
public final ConditionalNode usage;
|
|
268 |
|
|
269 |
public MaterializationUsageReplacer(Instantiation instantiation, ValueNode trueValue, ValueNode falseValue, FloatingNode instanceOf, ConditionalNode usage) {
|
|
270 |
super(instantiation, instanceOf, trueValue, falseValue);
|
|
271 |
this.usage = usage;
|
|
272 |
}
|
|
273 |
|
|
274 |
@Override
|
|
275 |
public void replaceUsingInstantiation() {
|
|
276 |
ValueNode newValue = instantiation.asMaterialization(usage.graph(), trueValue, falseValue);
|
|
277 |
usage.replaceAtUsages(newValue);
|
|
278 |
assert usage.hasNoUsages();
|
|
279 |
GraphUtil.killWithUnusedFloatingInputs(usage);
|
|
280 |
}
|
|
281 |
|
|
282 |
@Override
|
|
283 |
public void replace(ValueNode oldNode, ValueNode newNode) {
|
|
284 |
assert newNode instanceof PhiNode;
|
|
285 |
assert oldNode == instanceOf;
|
|
286 |
newNode.inferStamp();
|
|
287 |
instantiation.initialize(newNode, trueValue, falseValue);
|
|
288 |
usage.replaceAtUsages(newNode);
|
|
289 |
assert usage.hasNoUsages();
|
|
290 |
GraphUtil.killWithUnusedFloatingInputs(usage);
|
|
291 |
}
|
|
292 |
}
|
|
293 |
}
|