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 |
|
46640
|
27 |
import static java.util.Collections.singletonList;
|
43972
|
28 |
import static org.graalvm.compiler.core.GraalCompiler.emitFrontEnd;
|
|
29 |
import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
|
59095
|
30 |
import static org.graalvm.compiler.core.common.GraalOptions.RegisterPressure;
|
46640
|
31 |
import static org.graalvm.compiler.debug.DebugContext.DEFAULT_LOG_STREAM;
|
|
32 |
import static org.graalvm.compiler.debug.DebugOptions.DebugStubsAndSnippets;
|
43972
|
33 |
import static org.graalvm.compiler.hotspot.HotSpotHostBackend.UNCOMMON_TRAP_HANDLER;
|
46344
|
34 |
import static org.graalvm.util.CollectionsUtil.allMatch;
|
43972
|
35 |
|
|
36 |
import java.util.ListIterator;
|
46640
|
37 |
import java.util.concurrent.atomic.AtomicInteger;
|
43972
|
38 |
|
49873
|
39 |
import jdk.internal.vm.compiler.collections.EconomicSet;
|
43972
|
40 |
import org.graalvm.compiler.code.CompilationResult;
|
|
41 |
import org.graalvm.compiler.core.common.CompilationIdentifier;
|
49873
|
42 |
import org.graalvm.compiler.core.common.GraalOptions;
|
43972
|
43 |
import org.graalvm.compiler.core.target.Backend;
|
46640
|
44 |
import org.graalvm.compiler.debug.DebugContext;
|
|
45 |
import org.graalvm.compiler.debug.DebugContext.Description;
|
43972
|
46 |
import org.graalvm.compiler.hotspot.HotSpotCompiledCodeBuilder;
|
|
47 |
import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
|
|
48 |
import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
|
|
49 |
import org.graalvm.compiler.hotspot.nodes.StubStartNode;
|
|
50 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
|
|
51 |
import org.graalvm.compiler.lir.phases.LIRPhase;
|
|
52 |
import org.graalvm.compiler.lir.phases.LIRSuites;
|
|
53 |
import org.graalvm.compiler.lir.phases.PostAllocationOptimizationPhase.PostAllocationOptimizationContext;
|
|
54 |
import org.graalvm.compiler.lir.profiling.MoveProfilingPhase;
|
|
55 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
46344
|
56 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
57 |
import org.graalvm.compiler.phases.OptimisticOptimizations;
|
|
58 |
import org.graalvm.compiler.phases.PhaseSuite;
|
|
59 |
import org.graalvm.compiler.phases.tiers.Suites;
|
46640
|
60 |
import org.graalvm.compiler.printer.GraalDebugHandlersFactory;
|
43972
|
61 |
|
52578
|
62 |
import jdk.vm.ci.code.CodeCacheProvider;
|
|
63 |
import jdk.vm.ci.code.InstalledCode;
|
|
64 |
import jdk.vm.ci.code.Register;
|
|
65 |
import jdk.vm.ci.code.RegisterConfig;
|
|
66 |
import jdk.vm.ci.code.site.Call;
|
|
67 |
import jdk.vm.ci.code.site.ConstantReference;
|
|
68 |
import jdk.vm.ci.code.site.DataPatch;
|
|
69 |
import jdk.vm.ci.code.site.Infopoint;
|
|
70 |
import jdk.vm.ci.hotspot.HotSpotCompiledCode;
|
|
71 |
import jdk.vm.ci.hotspot.HotSpotMetaspaceConstant;
|
|
72 |
import jdk.vm.ci.meta.DefaultProfilingInfo;
|
|
73 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
74 |
import jdk.vm.ci.meta.TriState;
|
|
75 |
|
43972
|
76 |
//JaCoCo Exclude
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Base class for implementing some low level code providing the out-of-line slow path for a snippet
|
59095
|
80 |
* and/or a callee saved call to a HotSpot C/C++ runtime function or even another compiled Java
|
43972
|
81 |
* method.
|
|
82 |
*/
|
|
83 |
public abstract class Stub {
|
|
84 |
|
|
85 |
/**
|
|
86 |
* The linkage information for a call to this stub from compiled code.
|
|
87 |
*/
|
|
88 |
protected final HotSpotForeignCallLinkage linkage;
|
|
89 |
|
|
90 |
/**
|
|
91 |
* The code installed for the stub.
|
|
92 |
*/
|
|
93 |
protected InstalledCode code;
|
|
94 |
|
|
95 |
/**
|
|
96 |
* The registers destroyed by this stub (from the caller's perspective).
|
|
97 |
*/
|
46344
|
98 |
private EconomicSet<Register> destroyedCallerRegisters;
|
43972
|
99 |
|
46344
|
100 |
private static boolean checkRegisterSetEquivalency(EconomicSet<Register> a, EconomicSet<Register> b) {
|
|
101 |
if (a == b) {
|
|
102 |
return true;
|
|
103 |
}
|
|
104 |
if (a.size() != b.size()) {
|
|
105 |
return false;
|
|
106 |
}
|
|
107 |
return allMatch(a, e -> b.contains(e));
|
|
108 |
}
|
|
109 |
|
|
110 |
public void initDestroyedCallerRegisters(EconomicSet<Register> registers) {
|
43972
|
111 |
assert registers != null;
|
46344
|
112 |
assert destroyedCallerRegisters == null || checkRegisterSetEquivalency(registers, destroyedCallerRegisters) : "cannot redefine";
|
43972
|
113 |
destroyedCallerRegisters = registers;
|
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Gets the registers destroyed by this stub from a caller's perspective. These are the
|
|
118 |
* temporaries of this stub and must thus be caller saved by a callers of this stub.
|
|
119 |
*/
|
46344
|
120 |
public EconomicSet<Register> getDestroyedCallerRegisters() {
|
43972
|
121 |
assert destroyedCallerRegisters != null : "not yet initialized";
|
|
122 |
return destroyedCallerRegisters;
|
|
123 |
}
|
|
124 |
|
58299
|
125 |
public boolean shouldSaveRegistersAroundCalls() {
|
|
126 |
return linkage.getEffect() == HotSpotForeignCallLinkage.RegisterEffect.COMPUTES_REGISTERS_KILLED;
|
43972
|
127 |
}
|
|
128 |
|
46344
|
129 |
protected final OptionValues options;
|
43972
|
130 |
protected final HotSpotProviders providers;
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Creates a new stub.
|
|
134 |
*
|
|
135 |
* @param linkage linkage details for a call to the stub
|
|
136 |
*/
|
46344
|
137 |
public Stub(OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) {
|
43972
|
138 |
this.linkage = linkage;
|
59095
|
139 |
// The RegisterPressure flag can be ignored by a compilation that runs out of registers, so
|
|
140 |
// the stub compilation must ignore the flag so that all allocatable registers are saved.
|
|
141 |
this.options = new OptionValues(options, GraalOptions.TraceInlining, GraalOptions.TraceInliningForStubsAndSnippets.getValue(options), RegisterPressure, null);
|
43972
|
142 |
this.providers = providers;
|
|
143 |
}
|
|
144 |
|
|
145 |
/**
|
|
146 |
* Gets the linkage for a call to this stub from compiled code.
|
|
147 |
*/
|
|
148 |
public HotSpotForeignCallLinkage getLinkage() {
|
|
149 |
return linkage;
|
|
150 |
}
|
|
151 |
|
|
152 |
public RegisterConfig getRegisterConfig() {
|
|
153 |
return null;
|
|
154 |
}
|
|
155 |
|
|
156 |
/**
|
|
157 |
* Gets the graph that from which the code for this stub will be compiled.
|
|
158 |
*
|
|
159 |
* @param compilationId unique compilation id for the stub
|
|
160 |
*/
|
46640
|
161 |
protected abstract StructuredGraph getGraph(DebugContext debug, CompilationIdentifier compilationId);
|
43972
|
162 |
|
|
163 |
@Override
|
|
164 |
public String toString() {
|
|
165 |
return "Stub<" + linkage.getDescriptor() + ">";
|
|
166 |
}
|
|
167 |
|
|
168 |
/**
|
|
169 |
* Gets the method the stub's code will be associated with once installed. This may be null.
|
|
170 |
*/
|
|
171 |
protected abstract ResolvedJavaMethod getInstalledCodeOwner();
|
|
172 |
|
|
173 |
/**
|
|
174 |
* Gets a context object for the debug scope created when producing the code for this stub.
|
|
175 |
*/
|
|
176 |
protected abstract Object debugScopeContext();
|
|
177 |
|
46640
|
178 |
private static final AtomicInteger nextStubId = new AtomicInteger();
|
|
179 |
|
|
180 |
private DebugContext openDebugContext(DebugContext outer) {
|
|
181 |
if (DebugStubsAndSnippets.getValue(options)) {
|
|
182 |
Description description = new Description(linkage, "Stub_" + nextStubId.incrementAndGet());
|
|
183 |
return DebugContext.create(options, description, outer.getGlobalMetrics(), DEFAULT_LOG_STREAM, singletonList(new GraalDebugHandlersFactory(providers.getSnippetReflection())));
|
|
184 |
}
|
54601
|
185 |
return DebugContext.disabled(options);
|
46640
|
186 |
}
|
|
187 |
|
43972
|
188 |
/**
|
|
189 |
* Gets the code for this stub, compiling it first if necessary.
|
|
190 |
*/
|
|
191 |
@SuppressWarnings("try")
|
|
192 |
public synchronized InstalledCode getCode(final Backend backend) {
|
|
193 |
if (code == null) {
|
46640
|
194 |
try (DebugContext debug = openDebugContext(DebugContext.forCurrentThread())) {
|
|
195 |
try (DebugContext.Scope d = debug.scope("CompilingStub", providers.getCodeCache(), debugScopeContext())) {
|
|
196 |
CodeCacheProvider codeCache = providers.getCodeCache();
|
|
197 |
CompilationResult compResult = buildCompilationResult(debug, backend);
|
|
198 |
try (DebugContext.Scope s = debug.scope("CodeInstall", compResult);
|
|
199 |
DebugContext.Activation a = debug.activate()) {
|
|
200 |
assert destroyedCallerRegisters != null;
|
|
201 |
// Add a GeneratePIC check here later, we don't want to install
|
|
202 |
// code if we don't have a corresponding VM global symbol.
|
52578
|
203 |
HotSpotCompiledCode compiledCode = HotSpotCompiledCodeBuilder.createCompiledCode(codeCache, null, null, compResult, options);
|
46640
|
204 |
code = codeCache.installCode(null, compiledCode, null, null, false);
|
|
205 |
} catch (Throwable e) {
|
|
206 |
throw debug.handle(e);
|
|
207 |
}
|
43972
|
208 |
} catch (Throwable e) {
|
46640
|
209 |
throw debug.handle(e);
|
43972
|
210 |
}
|
46640
|
211 |
assert code != null : "error installing stub " + this;
|
43972
|
212 |
}
|
|
213 |
}
|
|
214 |
|
|
215 |
return code;
|
|
216 |
}
|
|
217 |
|
|
218 |
@SuppressWarnings("try")
|
46640
|
219 |
private CompilationResult buildCompilationResult(DebugContext debug, final Backend backend) {
|
47084
|
220 |
CompilationIdentifier compilationId = getStubCompilationId();
|
|
221 |
final StructuredGraph graph = getGraph(debug, compilationId);
|
|
222 |
CompilationResult compResult = new CompilationResult(compilationId, toString(), GeneratePIC.getValue(options));
|
43972
|
223 |
|
|
224 |
// Stubs cannot be recompiled so they cannot be compiled with assumptions
|
|
225 |
assert graph.getAssumptions() == null;
|
|
226 |
|
|
227 |
if (!(graph.start() instanceof StubStartNode)) {
|
|
228 |
StubStartNode newStart = graph.add(new StubStartNode(Stub.this));
|
|
229 |
newStart.setStateAfter(graph.start().stateAfter());
|
|
230 |
graph.replaceFixed(graph.start(), newStart);
|
|
231 |
}
|
|
232 |
|
46640
|
233 |
try (DebugContext.Scope s0 = debug.scope("StubCompilation", graph, providers.getCodeCache())) {
|
43972
|
234 |
Suites suites = createSuites();
|
|
235 |
emitFrontEnd(providers, backend, graph, providers.getSuites().getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL, DefaultProfilingInfo.get(TriState.UNKNOWN), suites);
|
|
236 |
LIRSuites lirSuites = createLIRSuites();
|
54084
|
237 |
backend.emitBackEnd(graph, Stub.this, getInstalledCodeOwner(), compResult, CompilationResultBuilderFactory.Default, getRegisterConfig(), lirSuites);
|
43972
|
238 |
assert checkStubInvariants(compResult);
|
|
239 |
} catch (Throwable e) {
|
46640
|
240 |
throw debug.handle(e);
|
43972
|
241 |
}
|
|
242 |
return compResult;
|
|
243 |
}
|
|
244 |
|
|
245 |
/**
|
|
246 |
* Gets a {@link CompilationResult} that can be used for code generation. Required for AOT.
|
|
247 |
*/
|
|
248 |
@SuppressWarnings("try")
|
46640
|
249 |
public CompilationResult getCompilationResult(DebugContext debug, final Backend backend) {
|
|
250 |
try (DebugContext.Scope d = debug.scope("CompilingStub", providers.getCodeCache(), debugScopeContext())) {
|
|
251 |
return buildCompilationResult(debug, backend);
|
43972
|
252 |
} catch (Throwable e) {
|
46640
|
253 |
throw debug.handle(e);
|
43972
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
public CompilationIdentifier getStubCompilationId() {
|
|
258 |
return new StubCompilationIdentifier(this);
|
|
259 |
}
|
|
260 |
|
|
261 |
/**
|
|
262 |
* Checks the conditions a compilation must satisfy to be installed as a RuntimeStub.
|
|
263 |
*/
|
|
264 |
private boolean checkStubInvariants(CompilationResult compResult) {
|
|
265 |
assert compResult.getExceptionHandlers().isEmpty() : this;
|
|
266 |
|
|
267 |
// Stubs cannot be recompiled so they cannot be compiled with
|
|
268 |
// assumptions and there is no point in recording evol_method dependencies
|
|
269 |
assert compResult.getAssumptions() == null : "stubs should not use assumptions: " + this;
|
|
270 |
|
|
271 |
for (DataPatch data : compResult.getDataPatches()) {
|
|
272 |
if (data.reference instanceof ConstantReference) {
|
|
273 |
ConstantReference ref = (ConstantReference) data.reference;
|
|
274 |
if (ref.getConstant() instanceof HotSpotMetaspaceConstant) {
|
|
275 |
HotSpotMetaspaceConstant c = (HotSpotMetaspaceConstant) ref.getConstant();
|
|
276 |
if (c.asResolvedJavaType() != null && c.asResolvedJavaType().getName().equals("[I")) {
|
|
277 |
// special handling for NewArrayStub
|
|
278 |
// embedding the type '[I' is safe, since it is never unloaded
|
|
279 |
continue;
|
|
280 |
}
|
|
281 |
}
|
|
282 |
}
|
|
283 |
|
|
284 |
assert !(data.reference instanceof ConstantReference) : this + " cannot have embedded object or metadata constant: " + data.reference;
|
|
285 |
}
|
|
286 |
for (Infopoint infopoint : compResult.getInfopoints()) {
|
|
287 |
assert infopoint instanceof Call : this + " cannot have non-call infopoint: " + infopoint;
|
|
288 |
Call call = (Call) infopoint;
|
|
289 |
assert call.target instanceof HotSpotForeignCallLinkage : this + " cannot have non runtime call: " + call.target;
|
|
290 |
HotSpotForeignCallLinkage callLinkage = (HotSpotForeignCallLinkage) call.target;
|
|
291 |
assert !callLinkage.isCompiledStub() || callLinkage.getDescriptor().equals(UNCOMMON_TRAP_HANDLER) : this + " cannot call compiled stub " + callLinkage;
|
|
292 |
}
|
|
293 |
return true;
|
|
294 |
}
|
|
295 |
|
|
296 |
protected Suites createSuites() {
|
46344
|
297 |
Suites defaultSuites = providers.getSuites().getDefaultSuites(options);
|
43972
|
298 |
return new Suites(new PhaseSuite<>(), defaultSuites.getMidTier(), defaultSuites.getLowTier());
|
|
299 |
}
|
|
300 |
|
|
301 |
protected LIRSuites createLIRSuites() {
|
46344
|
302 |
LIRSuites lirSuites = new LIRSuites(providers.getSuites().getDefaultLIRSuites(options));
|
43972
|
303 |
ListIterator<LIRPhase<PostAllocationOptimizationContext>> moveProfiling = lirSuites.getPostAllocationOptimizationStage().findPhase(MoveProfilingPhase.class);
|
|
304 |
if (moveProfiling != null) {
|
|
305 |
moveProfiling.remove();
|
|
306 |
}
|
|
307 |
return lirSuites;
|
|
308 |
}
|
|
309 |
}
|