43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2009, 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.core.target;
|
|
24 |
|
|
25 |
import org.graalvm.compiler.asm.Assembler;
|
|
26 |
import org.graalvm.compiler.code.CompilationResult;
|
|
27 |
import org.graalvm.compiler.core.common.CompilationIdentifier;
|
|
28 |
import org.graalvm.compiler.core.common.LIRKind;
|
|
29 |
import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig;
|
|
30 |
import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
|
|
31 |
import org.graalvm.compiler.core.common.spi.ForeignCallsProvider;
|
|
32 |
import org.graalvm.compiler.debug.Debug;
|
|
33 |
import org.graalvm.compiler.debug.Debug.Scope;
|
|
34 |
import org.graalvm.compiler.lir.LIR;
|
|
35 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
|
|
36 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
|
|
37 |
import org.graalvm.compiler.lir.framemap.FrameMap;
|
|
38 |
import org.graalvm.compiler.lir.framemap.FrameMapBuilder;
|
|
39 |
import org.graalvm.compiler.lir.gen.LIRGenerationResult;
|
|
40 |
import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
|
|
41 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
42 |
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
|
|
43 |
import org.graalvm.compiler.phases.tiers.SuitesProvider;
|
|
44 |
import org.graalvm.compiler.phases.tiers.TargetProvider;
|
|
45 |
import org.graalvm.compiler.phases.util.Providers;
|
46344
|
46 |
import org.graalvm.util.EconomicSet;
|
43972
|
47 |
|
|
48 |
import jdk.vm.ci.code.BailoutException;
|
|
49 |
import jdk.vm.ci.code.CodeCacheProvider;
|
|
50 |
import jdk.vm.ci.code.CompilationRequest;
|
|
51 |
import jdk.vm.ci.code.CompiledCode;
|
|
52 |
import jdk.vm.ci.code.InstalledCode;
|
|
53 |
import jdk.vm.ci.code.Register;
|
|
54 |
import jdk.vm.ci.code.RegisterConfig;
|
|
55 |
import jdk.vm.ci.code.TargetDescription;
|
|
56 |
import jdk.vm.ci.code.ValueKindFactory;
|
|
57 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
|
58 |
import jdk.vm.ci.meta.JavaKind;
|
|
59 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
60 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
61 |
import jdk.vm.ci.meta.SpeculationLog;
|
|
62 |
|
46551
|
63 |
import java.util.ArrayList;
|
|
64 |
|
43972
|
65 |
/**
|
|
66 |
* Represents a compiler backend for Graal.
|
|
67 |
*/
|
|
68 |
public abstract class Backend implements TargetProvider, ValueKindFactory<LIRKind> {
|
|
69 |
|
|
70 |
private final Providers providers;
|
46551
|
71 |
private final ArrayList<CodeInstallationTaskFactory> codeInstallationTaskFactories;
|
43972
|
72 |
|
|
73 |
public static final ForeignCallDescriptor ARITHMETIC_FREM = new ForeignCallDescriptor("arithmeticFrem", float.class, float.class, float.class);
|
|
74 |
public static final ForeignCallDescriptor ARITHMETIC_DREM = new ForeignCallDescriptor("arithmeticDrem", double.class, double.class, double.class);
|
|
75 |
|
|
76 |
protected Backend(Providers providers) {
|
|
77 |
this.providers = providers;
|
46551
|
78 |
this.codeInstallationTaskFactories = new ArrayList<>();
|
|
79 |
}
|
|
80 |
|
|
81 |
public synchronized void addCodeInstallationTask(CodeInstallationTaskFactory factory) {
|
|
82 |
this.codeInstallationTaskFactories.add(factory);
|
43972
|
83 |
}
|
|
84 |
|
|
85 |
public Providers getProviders() {
|
|
86 |
return providers;
|
|
87 |
}
|
|
88 |
|
|
89 |
public CodeCacheProvider getCodeCache() {
|
|
90 |
return providers.getCodeCache();
|
|
91 |
}
|
|
92 |
|
|
93 |
public MetaAccessProvider getMetaAccess() {
|
|
94 |
return providers.getMetaAccess();
|
|
95 |
}
|
|
96 |
|
|
97 |
public ConstantReflectionProvider getConstantReflection() {
|
|
98 |
return providers.getConstantReflection();
|
|
99 |
}
|
|
100 |
|
|
101 |
public ForeignCallsProvider getForeignCalls() {
|
|
102 |
return providers.getForeignCalls();
|
|
103 |
}
|
|
104 |
|
|
105 |
public abstract SuitesProvider getSuites();
|
|
106 |
|
|
107 |
@Override
|
|
108 |
public TargetDescription getTarget() {
|
|
109 |
return providers.getCodeCache().getTarget();
|
|
110 |
}
|
|
111 |
|
|
112 |
@Override
|
|
113 |
public LIRKind getValueKind(JavaKind javaKind) {
|
|
114 |
return LIRKind.fromJavaKind(getTarget().arch, javaKind);
|
|
115 |
}
|
|
116 |
|
|
117 |
/**
|
|
118 |
* The given registerConfig is optional, in case null is passed the default RegisterConfig from
|
|
119 |
* the CodeCacheProvider will be used.
|
|
120 |
*/
|
|
121 |
public abstract FrameMapBuilder newFrameMapBuilder(RegisterConfig registerConfig);
|
|
122 |
|
46344
|
123 |
/**
|
|
124 |
* Creates a new configuration for register allocation.
|
|
125 |
*
|
|
126 |
* @param allocationRestrictedTo if not {@code null}, register allocation will be restricted to
|
|
127 |
* registers whose names appear in this array
|
|
128 |
*/
|
|
129 |
public abstract RegisterAllocationConfig newRegisterAllocationConfig(RegisterConfig registerConfig, String[] allocationRestrictedTo);
|
43972
|
130 |
|
|
131 |
public abstract FrameMap newFrameMap(RegisterConfig registerConfig);
|
|
132 |
|
|
133 |
public abstract LIRGeneratorTool newLIRGenerator(LIRGenerationResult lirGenRes);
|
|
134 |
|
|
135 |
public abstract LIRGenerationResult newLIRGenerationResult(CompilationIdentifier compilationId, LIR lir, FrameMapBuilder frameMapBuilder, StructuredGraph graph,
|
|
136 |
Object stub);
|
|
137 |
|
|
138 |
public abstract NodeLIRBuilderTool newNodeLIRBuilder(StructuredGraph graph, LIRGeneratorTool lirGen);
|
|
139 |
|
|
140 |
/**
|
|
141 |
* Creates the assembler used to emit the machine code.
|
|
142 |
*/
|
|
143 |
protected abstract Assembler createAssembler(FrameMap frameMap);
|
|
144 |
|
|
145 |
/**
|
|
146 |
* Creates the object used to fill in the details of a given compilation result.
|
|
147 |
*/
|
|
148 |
public abstract CompilationResultBuilder newCompilationResultBuilder(LIRGenerationResult lirGenResult, FrameMap frameMap, CompilationResult compilationResult,
|
|
149 |
CompilationResultBuilderFactory factory);
|
|
150 |
|
|
151 |
/**
|
|
152 |
* Turns a Graal {@link CompilationResult} into a {@link CompiledCode} object that can be passed
|
|
153 |
* to the VM for code installation.
|
|
154 |
*/
|
|
155 |
protected abstract CompiledCode createCompiledCode(ResolvedJavaMethod method, CompilationRequest compilationRequest, CompilationResult compilationResult);
|
|
156 |
|
|
157 |
/**
|
|
158 |
* @see #createInstalledCode(ResolvedJavaMethod, CompilationRequest, CompilationResult,
|
|
159 |
* SpeculationLog, InstalledCode, boolean)
|
|
160 |
*/
|
|
161 |
public InstalledCode createInstalledCode(ResolvedJavaMethod method, CompilationResult compilationResult,
|
|
162 |
SpeculationLog speculationLog, InstalledCode predefinedInstalledCode, boolean isDefault) {
|
|
163 |
return createInstalledCode(method, null, compilationResult, speculationLog, predefinedInstalledCode, isDefault);
|
|
164 |
}
|
|
165 |
|
|
166 |
/**
|
46551
|
167 |
* @see #createInstalledCode(ResolvedJavaMethod, CompilationRequest, CompilationResult,
|
|
168 |
* SpeculationLog, InstalledCode, boolean, Object[])
|
|
169 |
*/
|
|
170 |
@SuppressWarnings("try")
|
|
171 |
public InstalledCode createInstalledCode(ResolvedJavaMethod method, CompilationRequest compilationRequest, CompilationResult compilationResult,
|
|
172 |
SpeculationLog speculationLog, InstalledCode predefinedInstalledCode, boolean isDefault) {
|
|
173 |
return createInstalledCode(method, compilationRequest, compilationResult, speculationLog, predefinedInstalledCode, isDefault, null);
|
|
174 |
}
|
|
175 |
|
|
176 |
/**
|
43972
|
177 |
* Installs code based on a given compilation result.
|
|
178 |
*
|
|
179 |
* @param method the method compiled to produce {@code compiledCode} or {@code null} if the
|
|
180 |
* input to {@code compResult} was not a {@link ResolvedJavaMethod}
|
|
181 |
* @param compilationRequest the compilation request or {@code null}
|
|
182 |
* @param compilationResult the code to be compiled
|
|
183 |
* @param predefinedInstalledCode a pre-allocated {@link InstalledCode} object to use as a
|
|
184 |
* reference to the installed code. If {@code null}, a new {@link InstalledCode}
|
|
185 |
* object will be created.
|
|
186 |
* @param speculationLog the speculation log to be used
|
|
187 |
* @param isDefault specifies if the installed code should be made the default implementation of
|
|
188 |
* {@code compRequest.getMethod()}. The default implementation for a method is the
|
|
189 |
* code executed for standard calls to the method. This argument is ignored if
|
|
190 |
* {@code compRequest == null}.
|
46551
|
191 |
* @param context a custom debug context to use for the code installation.
|
43972
|
192 |
* @return a reference to the compiled and ready-to-run installed code
|
|
193 |
* @throws BailoutException if the code installation failed
|
|
194 |
*/
|
|
195 |
@SuppressWarnings("try")
|
|
196 |
public InstalledCode createInstalledCode(ResolvedJavaMethod method, CompilationRequest compilationRequest, CompilationResult compilationResult,
|
46551
|
197 |
SpeculationLog speculationLog, InstalledCode predefinedInstalledCode, boolean isDefault, Object[] context) {
|
|
198 |
Object[] debugContext = context != null ? context : new Object[]{getProviders().getCodeCache(), method, compilationResult};
|
|
199 |
CodeInstallationTask[] tasks = new CodeInstallationTask[codeInstallationTaskFactories.size()];
|
|
200 |
for (int i = 0; i < codeInstallationTaskFactories.size(); i++) {
|
|
201 |
tasks[i] = codeInstallationTaskFactories.get(i).create();
|
|
202 |
}
|
|
203 |
try (Scope s = Debug.scope("CodeInstall", debugContext)) {
|
|
204 |
for (CodeInstallationTask task : tasks) {
|
|
205 |
task.preProcess(compilationResult);
|
|
206 |
}
|
|
207 |
|
43972
|
208 |
CompiledCode compiledCode = createCompiledCode(method, compilationRequest, compilationResult);
|
46551
|
209 |
InstalledCode installedCode = getProviders().getCodeCache().installCode(method, compiledCode, predefinedInstalledCode, speculationLog, isDefault);
|
|
210 |
|
|
211 |
// Run post-code installation tasks.
|
|
212 |
try {
|
|
213 |
for (CodeInstallationTask task : tasks) {
|
|
214 |
task.postProcess(installedCode);
|
|
215 |
}
|
|
216 |
for (CodeInstallationTask task : tasks) {
|
|
217 |
task.releaseInstallation(installedCode);
|
|
218 |
}
|
|
219 |
} catch (Throwable t) {
|
|
220 |
installedCode.invalidate();
|
|
221 |
throw t;
|
|
222 |
}
|
|
223 |
return installedCode;
|
43972
|
224 |
} catch (Throwable e) {
|
|
225 |
throw Debug.handle(e);
|
|
226 |
}
|
|
227 |
}
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Installs code based on a given compilation result.
|
|
231 |
*
|
|
232 |
* @param method the method compiled to produce {@code compiledCode} or {@code null} if the
|
|
233 |
* input to {@code compResult} was not a {@link ResolvedJavaMethod}
|
|
234 |
* @param compilationRequest the request or {@code null}
|
|
235 |
* @param compilationResult the code to be compiled
|
|
236 |
* @return a reference to the compiled and ready-to-run installed code
|
|
237 |
* @throws BailoutException if the code installation failed
|
|
238 |
*/
|
|
239 |
public InstalledCode addInstalledCode(ResolvedJavaMethod method, CompilationRequest compilationRequest, CompilationResult compilationResult) {
|
|
240 |
return createInstalledCode(method, compilationRequest, compilationResult, null, null, false);
|
|
241 |
}
|
|
242 |
|
|
243 |
/**
|
|
244 |
* Installs code based on a given compilation result and sets it as the default code to be used
|
|
245 |
* when {@code method} is invoked.
|
|
246 |
*
|
|
247 |
* @param method the method compiled to produce {@code compiledCode} or {@code null} if the
|
|
248 |
* input to {@code compResult} was not a {@link ResolvedJavaMethod}
|
|
249 |
* @param compilationResult the code to be compiled
|
|
250 |
* @return a reference to the compiled and ready-to-run installed code
|
|
251 |
* @throws BailoutException if the code installation failed
|
|
252 |
*/
|
|
253 |
public InstalledCode createDefaultInstalledCode(ResolvedJavaMethod method, CompilationResult compilationResult) {
|
|
254 |
return createInstalledCode(method, compilationResult, null, null, true);
|
|
255 |
}
|
|
256 |
|
|
257 |
/**
|
|
258 |
* Emits the code for a given graph.
|
|
259 |
*
|
|
260 |
* @param installedCodeOwner the method the compiled code will be associated with once
|
|
261 |
* installed. This argument can be null.
|
|
262 |
*/
|
|
263 |
public abstract void emitCode(CompilationResultBuilder crb, LIR lir, ResolvedJavaMethod installedCodeOwner);
|
|
264 |
|
|
265 |
/**
|
|
266 |
* Translates a set of registers from the callee's perspective to the caller's perspective. This
|
|
267 |
* is needed for architectures where input/output registers are renamed during a call (e.g.
|
|
268 |
* register windows on SPARC). Registers which are not visible by the caller are removed.
|
|
269 |
*/
|
46344
|
270 |
public abstract EconomicSet<Register> translateToCallerRegisters(EconomicSet<Register> calleeRegisters);
|
43972
|
271 |
|
|
272 |
/**
|
|
273 |
* Gets the compilation id for a given {@link ResolvedJavaMethod}. Returns
|
|
274 |
* {@code CompilationIdentifier#INVALID_COMPILATION_ID} in case there is no such id.
|
|
275 |
*
|
|
276 |
* @param resolvedJavaMethod
|
|
277 |
*/
|
|
278 |
public CompilationIdentifier getCompilationIdentifier(ResolvedJavaMethod resolvedJavaMethod) {
|
|
279 |
return CompilationIdentifier.INVALID_COMPILATION_ID;
|
|
280 |
}
|
46551
|
281 |
|
|
282 |
/**
|
|
283 |
* Encapsulates custom tasks done before and after code installation.
|
|
284 |
*/
|
|
285 |
public abstract static class CodeInstallationTask {
|
|
286 |
/**
|
|
287 |
* Task to run before code installation.
|
|
288 |
*/
|
|
289 |
@SuppressWarnings("unused")
|
|
290 |
public void preProcess(CompilationResult compilationResult) {
|
|
291 |
}
|
|
292 |
|
|
293 |
/**
|
|
294 |
* Task to run after the code is installed.
|
|
295 |
*/
|
|
296 |
@SuppressWarnings("unused")
|
|
297 |
public void postProcess(InstalledCode installedCode) {
|
|
298 |
}
|
|
299 |
|
|
300 |
/**
|
|
301 |
* Task to run after all the post-code installation tasks are complete, used to release the
|
|
302 |
* installed code.
|
|
303 |
*/
|
|
304 |
@SuppressWarnings("unused")
|
|
305 |
public void releaseInstallation(InstalledCode installedCode) {
|
|
306 |
}
|
|
307 |
}
|
|
308 |
|
|
309 |
/**
|
|
310 |
* Creates code installation tasks.
|
|
311 |
*/
|
|
312 |
public abstract static class CodeInstallationTaskFactory {
|
|
313 |
public abstract CodeInstallationTask create();
|
|
314 |
}
|
43972
|
315 |
}
|