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