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.amd64;
|
|
26 |
|
|
27 |
import static jdk.vm.ci.amd64.AMD64.r10;
|
|
28 |
import static jdk.vm.ci.amd64.AMD64.rax;
|
59095
|
29 |
import static jdk.vm.ci.amd64.AMD64.rbp;
|
43972
|
30 |
import static jdk.vm.ci.amd64.AMD64.rsp;
|
|
31 |
import static jdk.vm.ci.code.ValueUtil.asRegister;
|
46640
|
32 |
import static org.graalvm.compiler.core.common.GraalOptions.CanOmitFrame;
|
|
33 |
import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
|
|
34 |
import static org.graalvm.compiler.core.common.GraalOptions.ZapStackOnMethodEntry;
|
43972
|
35 |
|
49873
|
36 |
import jdk.internal.vm.compiler.collections.EconomicSet;
|
43972
|
37 |
import org.graalvm.compiler.asm.Assembler;
|
|
38 |
import org.graalvm.compiler.asm.Label;
|
|
39 |
import org.graalvm.compiler.asm.amd64.AMD64Address;
|
|
40 |
import org.graalvm.compiler.asm.amd64.AMD64Assembler.ConditionFlag;
|
|
41 |
import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
|
|
42 |
import org.graalvm.compiler.code.CompilationResult;
|
|
43 |
import org.graalvm.compiler.core.amd64.AMD64NodeMatchRules;
|
|
44 |
import org.graalvm.compiler.core.common.LIRKind;
|
|
45 |
import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig;
|
54084
|
46 |
import org.graalvm.compiler.core.gen.LIRGenerationProvider;
|
46640
|
47 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
48 |
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
|
|
49 |
import org.graalvm.compiler.hotspot.HotSpotDataBuilder;
|
|
50 |
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
|
|
51 |
import org.graalvm.compiler.hotspot.HotSpotHostBackend;
|
|
52 |
import org.graalvm.compiler.hotspot.HotSpotLIRGenerationResult;
|
|
53 |
import org.graalvm.compiler.hotspot.meta.HotSpotConstantLoadAction;
|
|
54 |
import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProvider;
|
|
55 |
import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
|
|
56 |
import org.graalvm.compiler.hotspot.stubs.Stub;
|
|
57 |
import org.graalvm.compiler.lir.LIR;
|
|
58 |
import org.graalvm.compiler.lir.amd64.AMD64Call;
|
|
59 |
import org.graalvm.compiler.lir.amd64.AMD64FrameMap;
|
|
60 |
import org.graalvm.compiler.lir.amd64.AMD64FrameMapBuilder;
|
|
61 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
|
|
62 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
|
|
63 |
import org.graalvm.compiler.lir.asm.DataBuilder;
|
|
64 |
import org.graalvm.compiler.lir.asm.FrameContext;
|
|
65 |
import org.graalvm.compiler.lir.framemap.FrameMap;
|
|
66 |
import org.graalvm.compiler.lir.framemap.FrameMapBuilder;
|
|
67 |
import org.graalvm.compiler.lir.gen.LIRGenerationResult;
|
|
68 |
import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
|
|
69 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
70 |
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
|
46344
|
71 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
72 |
|
|
73 |
import jdk.vm.ci.amd64.AMD64;
|
|
74 |
import jdk.vm.ci.amd64.AMD64Kind;
|
|
75 |
import jdk.vm.ci.code.CallingConvention;
|
|
76 |
import jdk.vm.ci.code.Register;
|
|
77 |
import jdk.vm.ci.code.RegisterConfig;
|
|
78 |
import jdk.vm.ci.code.StackSlot;
|
|
79 |
import jdk.vm.ci.hotspot.HotSpotCallingConventionType;
|
|
80 |
import jdk.vm.ci.hotspot.HotSpotSentinelConstant;
|
|
81 |
import jdk.vm.ci.meta.JavaKind;
|
|
82 |
import jdk.vm.ci.meta.JavaType;
|
|
83 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
84 |
|
|
85 |
/**
|
|
86 |
* HotSpot AMD64 specific backend.
|
|
87 |
*/
|
54084
|
88 |
public class AMD64HotSpotBackend extends HotSpotHostBackend implements LIRGenerationProvider {
|
43972
|
89 |
|
|
90 |
public AMD64HotSpotBackend(GraalHotSpotVMConfig config, HotSpotGraalRuntimeProvider runtime, HotSpotProviders providers) {
|
|
91 |
super(config, runtime, providers);
|
|
92 |
}
|
|
93 |
|
58299
|
94 |
@Override
|
|
95 |
protected FrameMapBuilder newFrameMapBuilder(RegisterConfig registerConfig) {
|
43972
|
96 |
RegisterConfig registerConfigNonNull = registerConfig == null ? getCodeCache().getRegisterConfig() : registerConfig;
|
59095
|
97 |
FrameMap frameMap = new AMD64FrameMap(getCodeCache(), registerConfigNonNull, this, config.preserveFramePointer);
|
54084
|
98 |
return new AMD64FrameMapBuilder(frameMap, getCodeCache(), registerConfigNonNull);
|
43972
|
99 |
}
|
|
100 |
|
|
101 |
@Override
|
|
102 |
public LIRGeneratorTool newLIRGenerator(LIRGenerationResult lirGenRes) {
|
|
103 |
return new AMD64HotSpotLIRGenerator(getProviders(), config, lirGenRes);
|
|
104 |
}
|
|
105 |
|
|
106 |
@Override
|
|
107 |
public NodeLIRBuilderTool newNodeLIRBuilder(StructuredGraph graph, LIRGeneratorTool lirGen) {
|
|
108 |
return new AMD64HotSpotNodeLIRBuilder(graph, lirGen, new AMD64NodeMatchRules(lirGen));
|
|
109 |
}
|
|
110 |
|
|
111 |
@Override
|
|
112 |
protected void bangStackWithOffset(CompilationResultBuilder crb, int bangOffset) {
|
|
113 |
AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;
|
|
114 |
int pos = asm.position();
|
|
115 |
asm.movl(new AMD64Address(rsp, -bangOffset), AMD64.rax);
|
|
116 |
assert asm.position() - pos >= PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE;
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* The size of the instruction used to patch the verified entry point of an nmethod when the
|
|
121 |
* nmethod is made non-entrant or a zombie (e.g. during deopt or class unloading). The first
|
|
122 |
* instruction emitted at an nmethod's verified entry point must be at least this length to
|
|
123 |
* ensure mt-safe patching.
|
|
124 |
*/
|
|
125 |
public static final int PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE = 5;
|
|
126 |
|
|
127 |
/**
|
|
128 |
* Emits code at the verified entry point and return point(s) of a method.
|
|
129 |
*/
|
|
130 |
class HotSpotFrameContext implements FrameContext {
|
|
131 |
|
|
132 |
final boolean isStub;
|
|
133 |
final boolean omitFrame;
|
59095
|
134 |
final boolean useStandardFrameProlog;
|
43972
|
135 |
|
59095
|
136 |
HotSpotFrameContext(boolean isStub, boolean omitFrame, boolean useStandardFrameProlog) {
|
43972
|
137 |
this.isStub = isStub;
|
|
138 |
this.omitFrame = omitFrame;
|
59095
|
139 |
this.useStandardFrameProlog = useStandardFrameProlog;
|
43972
|
140 |
}
|
|
141 |
|
|
142 |
@Override
|
|
143 |
public boolean hasFrame() {
|
|
144 |
return !omitFrame;
|
|
145 |
}
|
|
146 |
|
|
147 |
@Override
|
|
148 |
public void enter(CompilationResultBuilder crb) {
|
|
149 |
FrameMap frameMap = crb.frameMap;
|
|
150 |
int frameSize = frameMap.frameSize();
|
|
151 |
AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;
|
|
152 |
if (omitFrame) {
|
|
153 |
if (!isStub) {
|
|
154 |
asm.nop(PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE);
|
|
155 |
}
|
|
156 |
} else {
|
|
157 |
int verifiedEntryPointOffset = asm.position();
|
|
158 |
if (!isStub) {
|
|
159 |
emitStackOverflowCheck(crb);
|
|
160 |
// assert asm.position() - verifiedEntryPointOffset >=
|
|
161 |
// PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE;
|
|
162 |
}
|
59095
|
163 |
if (useStandardFrameProlog) {
|
|
164 |
// Stack-walking friendly instructions
|
|
165 |
asm.push(rbp);
|
|
166 |
asm.movq(rbp, rsp);
|
|
167 |
}
|
43972
|
168 |
if (!isStub && asm.position() == verifiedEntryPointOffset) {
|
|
169 |
asm.subqWide(rsp, frameSize);
|
|
170 |
assert asm.position() - verifiedEntryPointOffset >= PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE;
|
|
171 |
} else {
|
|
172 |
asm.decrementq(rsp, frameSize);
|
|
173 |
}
|
46344
|
174 |
if (ZapStackOnMethodEntry.getValue(crb.getOptions())) {
|
43972
|
175 |
final int intSize = 4;
|
|
176 |
for (int i = 0; i < frameSize / intSize; ++i) {
|
|
177 |
asm.movl(new AMD64Address(rsp, i * intSize), 0xC1C1C1C1);
|
|
178 |
}
|
|
179 |
}
|
|
180 |
assert frameMap.getRegisterConfig().getCalleeSaveRegisters() == null;
|
|
181 |
}
|
|
182 |
}
|
|
183 |
|
|
184 |
@Override
|
|
185 |
public void leave(CompilationResultBuilder crb) {
|
|
186 |
if (!omitFrame) {
|
|
187 |
AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;
|
|
188 |
assert crb.frameMap.getRegisterConfig().getCalleeSaveRegisters() == null;
|
|
189 |
|
|
190 |
int frameSize = crb.frameMap.frameSize();
|
59095
|
191 |
if (useStandardFrameProlog) {
|
|
192 |
asm.movq(rsp, rbp);
|
|
193 |
asm.pop(rbp);
|
|
194 |
} else {
|
|
195 |
asm.incrementq(rsp, frameSize);
|
|
196 |
}
|
43972
|
197 |
}
|
|
198 |
}
|
|
199 |
}
|
|
200 |
|
|
201 |
@Override
|
|
202 |
public CompilationResultBuilder newCompilationResultBuilder(LIRGenerationResult lirGenRen, FrameMap frameMap, CompilationResult compilationResult, CompilationResultBuilderFactory factory) {
|
|
203 |
// Omit the frame if the method:
|
|
204 |
// - has no spill slots or other slots allocated during register allocation
|
|
205 |
// - has no callee-saved registers
|
|
206 |
// - has no incoming arguments passed on the stack
|
|
207 |
// - has no deoptimization points
|
|
208 |
// - makes no foreign calls (which require an aligned stack)
|
|
209 |
HotSpotLIRGenerationResult gen = (HotSpotLIRGenerationResult) lirGenRen;
|
|
210 |
LIR lir = gen.getLIR();
|
|
211 |
assert gen.getDeoptimizationRescueSlot() == null || frameMap.frameNeedsAllocating() : "method that can deoptimize must have a frame";
|
46344
|
212 |
OptionValues options = lir.getOptions();
|
46640
|
213 |
DebugContext debug = lir.getDebug();
|
46344
|
214 |
boolean omitFrame = CanOmitFrame.getValue(options) && !frameMap.frameNeedsAllocating() && !lir.hasArgInCallerFrame() && !gen.hasForeignCall();
|
43972
|
215 |
|
|
216 |
Stub stub = gen.getStub();
|
54084
|
217 |
Assembler masm = new AMD64MacroAssembler(getTarget());
|
59095
|
218 |
HotSpotFrameContext frameContext = new HotSpotFrameContext(stub != null, omitFrame, config.preserveFramePointer);
|
43972
|
219 |
DataBuilder dataBuilder = new HotSpotDataBuilder(getCodeCache().getTarget());
|
50858
|
220 |
CompilationResultBuilder crb = factory.createBuilder(getCodeCache(), getForeignCalls(), frameMap, masm, dataBuilder, frameContext, options, debug, compilationResult, Register.None);
|
43972
|
221 |
crb.setTotalFrameSize(frameMap.totalFrameSize());
|
|
222 |
crb.setMaxInterpreterFrameSize(gen.getMaxInterpreterFrameSize());
|
|
223 |
StackSlot deoptimizationRescueSlot = gen.getDeoptimizationRescueSlot();
|
|
224 |
if (deoptimizationRescueSlot != null && stub == null) {
|
|
225 |
crb.compilationResult.setCustomStackAreaOffset(deoptimizationRescueSlot);
|
|
226 |
}
|
|
227 |
|
|
228 |
if (stub != null) {
|
58299
|
229 |
updateStub(stub, gen, frameMap);
|
43972
|
230 |
}
|
|
231 |
|
|
232 |
return crb;
|
|
233 |
}
|
|
234 |
|
|
235 |
@Override
|
|
236 |
public void emitCode(CompilationResultBuilder crb, LIR lir, ResolvedJavaMethod installedCodeOwner) {
|
|
237 |
AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;
|
|
238 |
FrameMap frameMap = crb.frameMap;
|
|
239 |
RegisterConfig regConfig = frameMap.getRegisterConfig();
|
|
240 |
Label verifiedEntry = new Label();
|
|
241 |
|
|
242 |
// Emit the prefix
|
|
243 |
emitCodePrefix(installedCodeOwner, crb, asm, regConfig, verifiedEntry);
|
|
244 |
|
|
245 |
// Emit code for the LIR
|
|
246 |
emitCodeBody(installedCodeOwner, crb, lir);
|
|
247 |
|
|
248 |
// Emit the suffix
|
|
249 |
emitCodeSuffix(installedCodeOwner, crb, asm, frameMap);
|
|
250 |
|
|
251 |
// Profile assembler instructions
|
|
252 |
profileInstructions(lir, crb);
|
|
253 |
}
|
|
254 |
|
|
255 |
/**
|
|
256 |
* Emits the code prior to the verified entry point.
|
|
257 |
*
|
54084
|
258 |
* @param installedCodeOwner see {@link LIRGenerationProvider#emitCode}
|
43972
|
259 |
*/
|
|
260 |
public void emitCodePrefix(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, AMD64MacroAssembler asm, RegisterConfig regConfig, Label verifiedEntry) {
|
|
261 |
HotSpotProviders providers = getProviders();
|
|
262 |
if (installedCodeOwner != null && !installedCodeOwner.isStatic()) {
|
|
263 |
crb.recordMark(config.MARKID_UNVERIFIED_ENTRY);
|
|
264 |
CallingConvention cc = regConfig.getCallingConvention(HotSpotCallingConventionType.JavaCallee, null, new JavaType[]{providers.getMetaAccess().lookupJavaType(Object.class)}, this);
|
|
265 |
Register inlineCacheKlass = rax; // see definition of IC_Klass in
|
|
266 |
// c1_LIRAssembler_x86.cpp
|
|
267 |
Register receiver = asRegister(cc.getArgument(0));
|
|
268 |
AMD64Address src = new AMD64Address(receiver, config.hubOffset);
|
|
269 |
|
|
270 |
if (config.useCompressedClassPointers) {
|
|
271 |
Register register = r10;
|
54914
|
272 |
Register heapBase = providers.getRegisters().getHeapBaseRegister();
|
|
273 |
AMD64HotSpotMove.decodeKlassPointer(crb, asm, register, heapBase, src, config);
|
46344
|
274 |
if (GeneratePIC.getValue(crb.getOptions())) {
|
54914
|
275 |
asm.movq(heapBase, asm.getPlaceholder(-1));
|
43972
|
276 |
crb.recordMark(config.MARKID_NARROW_OOP_BASE_ADDRESS);
|
|
277 |
} else {
|
|
278 |
if (config.narrowKlassBase != 0) {
|
|
279 |
// The heap base register was destroyed above, so restore it
|
54914
|
280 |
if (config.narrowOopBase == 0L) {
|
|
281 |
asm.xorq(heapBase, heapBase);
|
|
282 |
} else {
|
|
283 |
asm.movq(heapBase, config.narrowOopBase);
|
|
284 |
}
|
43972
|
285 |
}
|
|
286 |
}
|
|
287 |
asm.cmpq(inlineCacheKlass, register);
|
|
288 |
} else {
|
|
289 |
asm.cmpq(inlineCacheKlass, src);
|
|
290 |
}
|
|
291 |
AMD64Call.directConditionalJmp(crb, asm, getForeignCalls().lookupForeignCall(IC_MISS_HANDLER), ConditionFlag.NotEqual);
|
|
292 |
}
|
|
293 |
|
|
294 |
asm.align(config.codeEntryAlignment);
|
|
295 |
crb.recordMark(config.MARKID_OSR_ENTRY);
|
|
296 |
asm.bind(verifiedEntry);
|
|
297 |
crb.recordMark(config.MARKID_VERIFIED_ENTRY);
|
|
298 |
|
46344
|
299 |
if (GeneratePIC.getValue(crb.getOptions())) {
|
43972
|
300 |
// Check for method state
|
|
301 |
HotSpotFrameContext frameContext = (HotSpotFrameContext) crb.frameContext;
|
|
302 |
if (!frameContext.isStub) {
|
|
303 |
crb.recordInlineDataInCodeWithNote(new HotSpotSentinelConstant(LIRKind.value(AMD64Kind.QWORD), JavaKind.Long), HotSpotConstantLoadAction.MAKE_NOT_ENTRANT);
|
|
304 |
asm.movq(AMD64.rax, asm.getPlaceholder(-1));
|
|
305 |
asm.testq(AMD64.rax, AMD64.rax);
|
|
306 |
AMD64Call.directConditionalJmp(crb, asm, getForeignCalls().lookupForeignCall(WRONG_METHOD_HANDLER), ConditionFlag.NotZero);
|
|
307 |
}
|
|
308 |
}
|
|
309 |
}
|
|
310 |
|
|
311 |
/**
|
|
312 |
* Emits the code which starts at the verified entry point.
|
|
313 |
*
|
54084
|
314 |
* @param installedCodeOwner see {@link LIRGenerationProvider#emitCode}
|
43972
|
315 |
*/
|
|
316 |
public void emitCodeBody(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, LIR lir) {
|
|
317 |
crb.emit(lir);
|
|
318 |
}
|
|
319 |
|
|
320 |
/**
|
54084
|
321 |
* @param installedCodeOwner see {@link LIRGenerationProvider#emitCode}
|
43972
|
322 |
*/
|
|
323 |
public void emitCodeSuffix(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, AMD64MacroAssembler asm, FrameMap frameMap) {
|
|
324 |
HotSpotProviders providers = getProviders();
|
|
325 |
HotSpotFrameContext frameContext = (HotSpotFrameContext) crb.frameContext;
|
|
326 |
if (!frameContext.isStub) {
|
|
327 |
HotSpotForeignCallsProvider foreignCalls = providers.getForeignCalls();
|
|
328 |
crb.recordMark(config.MARKID_EXCEPTION_HANDLER_ENTRY);
|
|
329 |
AMD64Call.directCall(crb, asm, foreignCalls.lookupForeignCall(EXCEPTION_HANDLER), null, false, null);
|
|
330 |
crb.recordMark(config.MARKID_DEOPT_HANDLER_ENTRY);
|
|
331 |
AMD64Call.directCall(crb, asm, foreignCalls.lookupForeignCall(DEOPTIMIZATION_HANDLER), null, false, null);
|
|
332 |
} else {
|
|
333 |
// No need to emit the stubs for entries back into the method since
|
|
334 |
// it has no calls that can cause such "return" entries
|
|
335 |
|
|
336 |
if (frameContext.omitFrame) {
|
|
337 |
// Cannot access slots in caller's frame if my frame is omitted
|
|
338 |
assert !frameMap.accessesCallerFrame();
|
|
339 |
}
|
|
340 |
}
|
|
341 |
}
|
|
342 |
|
|
343 |
@Override
|
46344
|
344 |
public RegisterAllocationConfig newRegisterAllocationConfig(RegisterConfig registerConfig, String[] allocationRestrictedTo) {
|
43972
|
345 |
RegisterConfig registerConfigNonNull = registerConfig == null ? getCodeCache().getRegisterConfig() : registerConfig;
|
59095
|
346 |
return new AMD64HotSpotRegisterAllocationConfig(registerConfigNonNull, allocationRestrictedTo, config.preserveFramePointer);
|
43972
|
347 |
}
|
|
348 |
|
|
349 |
@Override
|
46344
|
350 |
public EconomicSet<Register> translateToCallerRegisters(EconomicSet<Register> calleeRegisters) {
|
43972
|
351 |
return calleeRegisters;
|
|
352 |
}
|
|
353 |
}
|