43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2012, 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.stubs;
|
|
26 |
|
|
27 |
import org.graalvm.compiler.api.replacements.Snippet;
|
|
28 |
import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
|
46344
|
29 |
import org.graalvm.compiler.api.replacements.Snippet.NonNullParameter;
|
43972
|
30 |
import org.graalvm.compiler.core.common.CompilationIdentifier;
|
46344
|
31 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
46640
|
32 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
33 |
import org.graalvm.compiler.debug.GraalError;
|
|
34 |
import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
|
|
35 |
import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
|
48190
|
36 |
import org.graalvm.compiler.nodes.NodeView;
|
46344
|
37 |
import org.graalvm.compiler.nodes.ParameterNode;
|
43972
|
38 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
39 |
import org.graalvm.compiler.nodes.StructuredGraph.GuardsStage;
|
|
40 |
import org.graalvm.compiler.nodes.spi.LoweringTool;
|
46344
|
41 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
42 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
43 |
import org.graalvm.compiler.phases.common.LoweringPhase;
|
46344
|
44 |
import org.graalvm.compiler.phases.common.RemoveValueProxyPhase;
|
43972
|
45 |
import org.graalvm.compiler.replacements.SnippetTemplate;
|
|
46 |
import org.graalvm.compiler.replacements.Snippets;
|
|
47 |
|
|
48 |
import jdk.vm.ci.meta.Local;
|
|
49 |
import jdk.vm.ci.meta.LocalVariableTable;
|
|
50 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
51 |
|
|
52 |
/**
|
|
53 |
* Base class for a stub defined by a snippet.
|
|
54 |
*/
|
|
55 |
public abstract class SnippetStub extends Stub implements Snippets {
|
|
56 |
|
|
57 |
protected final ResolvedJavaMethod method;
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Creates a new snippet stub.
|
|
61 |
*
|
|
62 |
* @param snippetMethodName name of the single {@link Snippet} annotated method in the class of
|
|
63 |
* this object
|
|
64 |
* @param linkage linkage details for a call to the stub
|
|
65 |
*/
|
46344
|
66 |
public SnippetStub(String snippetMethodName, OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) {
|
|
67 |
this(null, snippetMethodName, options, providers, linkage);
|
43972
|
68 |
}
|
|
69 |
|
|
70 |
/**
|
|
71 |
* Creates a new snippet stub.
|
|
72 |
*
|
|
73 |
* @param snippetDeclaringClass this class in which the {@link Snippet} annotated method is
|
|
74 |
* declared. If {@code null}, this the class of this object is used.
|
|
75 |
* @param snippetMethodName name of the single {@link Snippet} annotated method in
|
|
76 |
* {@code snippetDeclaringClass}
|
|
77 |
* @param linkage linkage details for a call to the stub
|
|
78 |
*/
|
46344
|
79 |
public SnippetStub(Class<? extends Snippets> snippetDeclaringClass, String snippetMethodName, OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) {
|
|
80 |
super(options, providers, linkage);
|
52956
|
81 |
this.method = SnippetTemplate.AbstractTemplates.findMethod(providers.getMetaAccess(), snippetDeclaringClass == null ? getClass() : snippetDeclaringClass, snippetMethodName);
|
|
82 |
registerSnippet();
|
|
83 |
}
|
|
84 |
|
|
85 |
protected void registerSnippet() {
|
54601
|
86 |
providers.getReplacements().registerSnippet(method, null, null, false, options);
|
43972
|
87 |
}
|
|
88 |
|
|
89 |
@Override
|
|
90 |
@SuppressWarnings("try")
|
46640
|
91 |
protected StructuredGraph getGraph(DebugContext debug, CompilationIdentifier compilationId) {
|
43972
|
92 |
// Stubs cannot have optimistic assumptions since they have
|
|
93 |
// to be valid for the entire run of the VM.
|
52956
|
94 |
final StructuredGraph graph = buildInitialGraph(debug, compilationId, makeConstArgs());
|
46640
|
95 |
try (DebugContext.Scope outer = debug.scope("SnippetStub", graph)) {
|
46344
|
96 |
for (ParameterNode param : graph.getNodes(ParameterNode.TYPE)) {
|
|
97 |
int index = param.index();
|
|
98 |
if (method.getParameterAnnotation(NonNullParameter.class, index) != null) {
|
48190
|
99 |
param.setStamp(param.stamp(NodeView.DEFAULT).join(StampFactory.objectNonNull()));
|
43972
|
100 |
}
|
|
101 |
}
|
|
102 |
|
46344
|
103 |
new RemoveValueProxyPhase().apply(graph);
|
43972
|
104 |
graph.setGuardsStage(GuardsStage.FLOATING_GUARDS);
|
|
105 |
CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
|
55509
|
106 |
canonicalizer.apply(graph, providers);
|
|
107 |
new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, providers);
|
43972
|
108 |
} catch (Throwable e) {
|
46640
|
109 |
throw debug.handle(e);
|
43972
|
110 |
}
|
|
111 |
|
|
112 |
return graph;
|
|
113 |
}
|
|
114 |
|
52956
|
115 |
protected StructuredGraph buildInitialGraph(DebugContext debug, CompilationIdentifier compilationId, Object[] args) {
|
54601
|
116 |
return providers.getReplacements().getSnippet(method, null, args, false, null, options).copyWithIdentifier(compilationId, debug);
|
46344
|
117 |
}
|
|
118 |
|
43972
|
119 |
protected boolean checkConstArg(int index, String expectedName) {
|
|
120 |
assert method.getParameterAnnotation(ConstantParameter.class, index) != null : String.format("parameter %d of %s is expected to be constant", index, method.format("%H.%n(%p)"));
|
|
121 |
LocalVariableTable lvt = method.getLocalVariableTable();
|
|
122 |
if (lvt != null) {
|
|
123 |
Local local = lvt.getLocal(index, 0);
|
|
124 |
assert local != null;
|
|
125 |
String actualName = local.getName();
|
|
126 |
assert actualName.equals(expectedName) : String.format("parameter %d of %s is expected to be named %s, not %s", index, method.format("%H.%n(%p)"), expectedName, actualName);
|
|
127 |
}
|
|
128 |
return true;
|
|
129 |
}
|
|
130 |
|
|
131 |
protected Object[] makeConstArgs() {
|
|
132 |
int count = method.getSignature().getParameterCount(false);
|
|
133 |
Object[] args = new Object[count];
|
|
134 |
for (int i = 0; i < args.length; i++) {
|
|
135 |
if (method.getParameterAnnotation(ConstantParameter.class, i) != null) {
|
|
136 |
args[i] = getConstantParameterValue(i, null);
|
|
137 |
}
|
|
138 |
}
|
|
139 |
return args;
|
|
140 |
}
|
|
141 |
|
|
142 |
protected Object getConstantParameterValue(int index, String name) {
|
|
143 |
throw new GraalError("%s must override getConstantParameterValue() to provide a value for parameter %d%s", getClass().getName(), index, name == null ? "" : " (" + name + ")");
|
|
144 |
}
|
|
145 |
|
|
146 |
@Override
|
|
147 |
protected Object debugScopeContext() {
|
|
148 |
return getInstalledCodeOwner();
|
|
149 |
}
|
|
150 |
|
|
151 |
@Override
|
|
152 |
public ResolvedJavaMethod getInstalledCodeOwner() {
|
|
153 |
return method;
|
|
154 |
}
|
|
155 |
|
|
156 |
@Override
|
|
157 |
public String toString() {
|
|
158 |
return "Stub<" + getInstalledCodeOwner().format("%h.%n") + ">";
|
|
159 |
}
|
52956
|
160 |
|
|
161 |
public ResolvedJavaMethod getMethod() {
|
|
162 |
return method;
|
|
163 |
}
|
43972
|
164 |
}
|