1 /* 2 * Copyright (c) 2012, 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.hotspot; 24 25 import static org.graalvm.compiler.hotspot.stubs.StubUtil.newDescriptor; 26 27 import java.util.EnumSet; 28 import java.util.HashSet; 29 import java.util.Map; 30 import java.util.Set; 31 32 import org.graalvm.compiler.code.CompilationResult; 33 import org.graalvm.compiler.core.common.CompilationIdentifier; 34 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; 35 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor; 36 import org.graalvm.compiler.core.common.spi.ForeignCallLinkage; 37 import org.graalvm.compiler.core.target.Backend; 38 import org.graalvm.compiler.graph.Node.ConstantNodeParameter; 39 import org.graalvm.compiler.graph.Node.NodeIntrinsic; 40 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 41 import org.graalvm.compiler.hotspot.nodes.DeoptimizationFetchUnrollInfoCallNode; 42 import org.graalvm.compiler.hotspot.nodes.UncommonTrapCallNode; 43 import org.graalvm.compiler.hotspot.nodes.VMErrorNode; 44 import org.graalvm.compiler.hotspot.nodes.aot.ResolveConstantStubCall; 45 import org.graalvm.compiler.hotspot.replacements.AESCryptSubstitutions; 46 import org.graalvm.compiler.hotspot.replacements.BigIntegerSubstitutions; 47 import org.graalvm.compiler.hotspot.replacements.CipherBlockChainingSubstitutions; 48 import org.graalvm.compiler.hotspot.replacements.SHA2Substitutions; 49 import org.graalvm.compiler.hotspot.replacements.SHA5Substitutions; 50 import org.graalvm.compiler.hotspot.replacements.SHASubstitutions; 51 import org.graalvm.compiler.hotspot.stubs.DeoptimizationStub; 52 import org.graalvm.compiler.hotspot.stubs.ExceptionHandlerStub; 53 import org.graalvm.compiler.hotspot.stubs.Stub; 54 import org.graalvm.compiler.hotspot.stubs.UnwindExceptionToCallerStub; 55 import org.graalvm.compiler.hotspot.word.KlassPointer; 56 import org.graalvm.compiler.lir.LIR; 57 import org.graalvm.compiler.lir.LIRFrameState; 58 import org.graalvm.compiler.lir.LIRInstruction; 59 import org.graalvm.compiler.lir.LIRInstruction.OperandFlag; 60 import org.graalvm.compiler.lir.LIRInstruction.OperandMode; 61 import org.graalvm.compiler.lir.StandardOp.LabelOp; 62 import org.graalvm.compiler.lir.StandardOp.SaveRegistersOp; 63 import org.graalvm.compiler.lir.ValueConsumer; 64 import org.graalvm.compiler.lir.asm.CompilationResultBuilder; 65 import org.graalvm.compiler.lir.framemap.FrameMap; 66 import org.graalvm.compiler.nodes.UnwindNode; 67 import org.graalvm.compiler.nodes.extended.ForeignCallNode; 68 import org.graalvm.compiler.options.Option; 69 import org.graalvm.compiler.options.OptionType; 70 import org.graalvm.compiler.options.OptionValue; 71 import org.graalvm.compiler.phases.tiers.SuitesProvider; 72 import org.graalvm.compiler.word.Pointer; 73 import org.graalvm.compiler.word.Word; 74 75 import jdk.vm.ci.code.CompilationRequest; 76 import jdk.vm.ci.code.CompiledCode; 77 import jdk.vm.ci.code.DebugInfo; 78 import jdk.vm.ci.code.Register; 79 import jdk.vm.ci.code.RegisterSaveLayout; 80 import jdk.vm.ci.code.StackSlot; 81 import jdk.vm.ci.code.ValueUtil; 82 import jdk.vm.ci.hotspot.HotSpotCompilationRequest; 83 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; 84 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 85 import jdk.vm.ci.meta.ResolvedJavaMethod; 86 import jdk.vm.ci.meta.Value; 87 import jdk.vm.ci.runtime.JVMCICompiler; 88 89 /** 90 * HotSpot specific backend. 91 */ 92 public abstract class HotSpotBackend extends Backend implements FrameMap.ReferenceMapBuilderFactory { 93 94 public static class Options { 95 // @formatter:off 96 @Option(help = "Use Graal stubs instead of HotSpot stubs where possible") 97 public static final OptionValue<Boolean> PreferGraalStubs = new OptionValue<>(false); 98 @Option(help = "Use Graal arithmetic stubs instead of HotSpot stubs where possible") 99 public static final OptionValue<Boolean> GraalArithmeticStubs = new OptionValue<>(true); 100 @Option(help = "Enables instruction profiling on assembler level. Valid values are a comma separated list of supported instructions." + 101 " Compare with subclasses of Assembler.InstructionCounter.", type = OptionType.Debug) 102 public static final OptionValue<String> ASMInstructionProfiling = new OptionValue<>(null); 103 // @formatter:on 104 } 105 106 /** 107 * Descriptor for {@link ExceptionHandlerStub}. This stub is called by the 108 * {@linkplain GraalHotSpotVMConfig#MARKID_EXCEPTION_HANDLER_ENTRY exception handler} in a 109 * compiled method. 110 */ 111 public static final ForeignCallDescriptor EXCEPTION_HANDLER = new ForeignCallDescriptor("exceptionHandler", void.class, Object.class, Word.class); 112 113 /** 114 * Descriptor for SharedRuntime::get_ic_miss_stub(). 115 */ 116 public static final ForeignCallDescriptor IC_MISS_HANDLER = new ForeignCallDescriptor("icMissHandler", void.class); 117 118 /** 119 * Descriptor for SharedRuntime::get_handle_wrong_method_stub(). 120 */ 121 public static final ForeignCallDescriptor WRONG_METHOD_HANDLER = new ForeignCallDescriptor("wrongMethodHandler", void.class); 122 123 /** 124 * Descriptor for {@link UnwindExceptionToCallerStub}. This stub is called by code generated 125 * from {@link UnwindNode}. 126 */ 127 public static final ForeignCallDescriptor UNWIND_EXCEPTION_TO_CALLER = new ForeignCallDescriptor("unwindExceptionToCaller", void.class, Object.class, Word.class); 128 129 /** 130 * Descriptor for the arguments when unwinding to an exception handler in a caller. 131 */ 132 public static final ForeignCallDescriptor EXCEPTION_HANDLER_IN_CALLER = new ForeignCallDescriptor("exceptionHandlerInCaller", void.class, Object.class, Word.class); 133 134 private final HotSpotGraalRuntimeProvider runtime; 135 136 /** 137 * @see DeoptimizationFetchUnrollInfoCallNode 138 */ 139 public static final ForeignCallDescriptor FETCH_UNROLL_INFO = new ForeignCallDescriptor("fetchUnrollInfo", Word.class, long.class, int.class); 140 141 /** 142 * @see DeoptimizationStub#unpackFrames(ForeignCallDescriptor, Word, int) 143 */ 144 public static final ForeignCallDescriptor UNPACK_FRAMES = newDescriptor(DeoptimizationStub.class, "unpackFrames", int.class, Word.class, int.class); 145 146 /** 147 * @see AESCryptSubstitutions#encryptBlockStub(ForeignCallDescriptor, Word, Word, Pointer) 148 */ 149 public static final ForeignCallDescriptor ENCRYPT_BLOCK = new ForeignCallDescriptor("encrypt_block", void.class, Word.class, Word.class, Pointer.class); 150 151 /** 152 * @see AESCryptSubstitutions#decryptBlockStub(ForeignCallDescriptor, Word, Word, Pointer) 153 */ 154 public static final ForeignCallDescriptor DECRYPT_BLOCK = new ForeignCallDescriptor("decrypt_block", void.class, Word.class, Word.class, Pointer.class); 155 156 /** 157 * @see AESCryptSubstitutions#decryptBlockStub(ForeignCallDescriptor, Word, Word, Pointer) 158 */ 159 public static final ForeignCallDescriptor DECRYPT_BLOCK_WITH_ORIGINAL_KEY = new ForeignCallDescriptor("decrypt_block_with_original_key", void.class, Word.class, Word.class, Pointer.class, 160 Pointer.class); 161 162 /** 163 * @see CipherBlockChainingSubstitutions#crypt 164 */ 165 public static final ForeignCallDescriptor ENCRYPT = new ForeignCallDescriptor("encrypt", void.class, Word.class, Word.class, Pointer.class, Pointer.class, int.class); 166 167 /** 168 * @see CipherBlockChainingSubstitutions#crypt 169 */ 170 public static final ForeignCallDescriptor DECRYPT = new ForeignCallDescriptor("decrypt", void.class, Word.class, Word.class, Pointer.class, Pointer.class, int.class); 171 172 /** 173 * @see CipherBlockChainingSubstitutions#crypt 174 */ 175 public static final ForeignCallDescriptor DECRYPT_WITH_ORIGINAL_KEY = new ForeignCallDescriptor("decrypt_with_original_key", void.class, Word.class, Word.class, Pointer.class, Pointer.class, 176 int.class, Pointer.class); 177 178 /** 179 * @see BigIntegerSubstitutions#multiplyToLen 180 */ 181 public static final ForeignCallDescriptor MULTIPLY_TO_LEN = new ForeignCallDescriptor("multiplyToLen", void.class, Word.class, int.class, Word.class, int.class, Word.class, int.class); 182 183 public static void multiplyToLenStub(Word xAddr, int xlen, Word yAddr, int ylen, Word zAddr, int zLen) { 184 multiplyToLenStub(HotSpotBackend.MULTIPLY_TO_LEN, xAddr, xlen, yAddr, ylen, zAddr, zLen); 185 } 186 187 @NodeIntrinsic(ForeignCallNode.class) 188 private static native void multiplyToLenStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word xIn, int xLen, Word yIn, int yLen, Word zIn, int zLen); 189 190 /** 191 * @see BigIntegerSubstitutions#mulAdd 192 */ 193 public static final ForeignCallDescriptor MUL_ADD = new ForeignCallDescriptor("mulAdd", int.class, Word.class, Word.class, int.class, int.class, int.class); 194 195 public static int mulAddStub(Word inAddr, Word outAddr, int newOffset, int len, int k) { 196 return mulAddStub(HotSpotBackend.MUL_ADD, inAddr, outAddr, newOffset, len, k); 197 } 198 199 @NodeIntrinsic(ForeignCallNode.class) 200 private static native int mulAddStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word inAddr, Word outAddr, int newOffset, int len, int k); 201 202 /** 203 * @see BigIntegerSubstitutions#implMontgomeryMultiply 204 */ 205 public static final ForeignCallDescriptor MONTGOMERY_MULTIPLY = new ForeignCallDescriptor("implMontgomeryMultiply", void.class, Word.class, Word.class, Word.class, int.class, long.class, 206 Word.class); 207 208 public static void implMontgomeryMultiply(Word aAddr, Word bAddr, Word nAddr, int len, long inv, Word productAddr) { 209 implMontgomeryMultiply(HotSpotBackend.MONTGOMERY_MULTIPLY, aAddr, bAddr, nAddr, len, inv, productAddr); 210 } 211 212 @NodeIntrinsic(ForeignCallNode.class) 213 private static native void implMontgomeryMultiply(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word aAddr, Word bAddr, Word nAddr, int len, long inv, Word productAddr); 214 215 /** 216 * @see BigIntegerSubstitutions#implMontgomerySquare 217 */ 218 public static final ForeignCallDescriptor MONTGOMERY_SQUARE = new ForeignCallDescriptor("implMontgomerySquare", void.class, Word.class, Word.class, int.class, long.class, Word.class); 219 220 public static void implMontgomerySquare(Word aAddr, Word nAddr, int len, long inv, Word productAddr) { 221 implMontgomerySquare(HotSpotBackend.MONTGOMERY_SQUARE, aAddr, nAddr, len, inv, productAddr); 222 } 223 224 @NodeIntrinsic(ForeignCallNode.class) 225 private static native void implMontgomerySquare(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word aAddr, Word nAddr, int len, long inv, Word productAddr); 226 227 /** 228 * @see BigIntegerSubstitutions#implSquareToLen 229 */ 230 public static final ForeignCallDescriptor SQUARE_TO_LEN = new ForeignCallDescriptor("implSquareToLen", void.class, Word.class, int.class, Word.class, int.class); 231 232 public static void implSquareToLen(Word xAddr, int len, Word zAddr, int zLen) { 233 implSquareToLen(SQUARE_TO_LEN, xAddr, len, zAddr, zLen); 234 } 235 236 @NodeIntrinsic(ForeignCallNode.class) 237 private static native void implSquareToLen(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word xAddr, int len, Word zAddr, int zLen); 238 239 /** 240 * @see SHASubstitutions#implCompress0 241 */ 242 public static final ForeignCallDescriptor SHA_IMPL_COMPRESS = new ForeignCallDescriptor("shaImplCompress", void.class, Word.class, Object.class); 243 244 public static void shaImplCompressStub(Word bufAddr, Object state) { 245 shaImplCompressStub(HotSpotBackend.SHA_IMPL_COMPRESS, bufAddr, state); 246 } 247 248 @NodeIntrinsic(ForeignCallNode.class) 249 private static native void shaImplCompressStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word bufAddr, Object state); 250 251 /** 252 * @see SHA2Substitutions#implCompress0 253 */ 254 public static final ForeignCallDescriptor SHA2_IMPL_COMPRESS = new ForeignCallDescriptor("sha2ImplCompress", void.class, Word.class, Object.class); 255 256 public static void sha2ImplCompressStub(Word bufAddr, Object state) { 257 sha2ImplCompressStub(HotSpotBackend.SHA2_IMPL_COMPRESS, bufAddr, state); 258 } 259 260 @NodeIntrinsic(ForeignCallNode.class) 261 private static native void sha2ImplCompressStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word bufAddr, Object state); 262 263 /** 264 * @see SHA5Substitutions#implCompress0 265 */ 266 public static final ForeignCallDescriptor SHA5_IMPL_COMPRESS = new ForeignCallDescriptor("sha5ImplCompress", void.class, Word.class, Object.class); 267 268 public static void sha5ImplCompressStub(Word bufAddr, Object state) { 269 sha5ImplCompressStub(HotSpotBackend.SHA5_IMPL_COMPRESS, bufAddr, state); 270 } 271 272 @NodeIntrinsic(ForeignCallNode.class) 273 private static native void sha5ImplCompressStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word bufAddr, Object state); 274 275 /** 276 * @see VMErrorNode 277 */ 278 public static final ForeignCallDescriptor VM_ERROR = new ForeignCallDescriptor("vm_error", void.class, Object.class, Object.class, long.class); 279 280 /** 281 * New multi array stub call. 282 */ 283 public static final ForeignCallDescriptor NEW_MULTI_ARRAY = new ForeignCallDescriptor("new_multi_array", Object.class, KlassPointer.class, int.class, Word.class); 284 285 /** 286 * New array stub. 287 */ 288 public static final ForeignCallDescriptor NEW_ARRAY = new ForeignCallDescriptor("new_array", Object.class, KlassPointer.class, int.class, boolean.class); 289 290 /** 291 * New instance stub. 292 */ 293 public static final ForeignCallDescriptor NEW_INSTANCE = new ForeignCallDescriptor("new_instance", Object.class, KlassPointer.class); 294 295 /** 296 * @see ResolveConstantStubCall 297 */ 298 public static final ForeignCallDescriptor RESOLVE_STRING_BY_SYMBOL = new ForeignCallDescriptor("resolve_string_by_symbol", Object.class, Word.class, Word.class); 299 300 /** 301 * @see ResolveConstantStubCall 302 */ 303 public static final ForeignCallDescriptor RESOLVE_KLASS_BY_SYMBOL = new ForeignCallDescriptor("resolve_klass_by_symbol", Word.class, Word.class, Word.class); 304 305 /** 306 * @see ResolveConstantStubCall 307 */ 308 public static final ForeignCallDescriptor INITIALIZE_KLASS_BY_SYMBOL = new ForeignCallDescriptor("initialize_klass_by_symbol", Word.class, Word.class, Word.class); 309 310 /** 311 * @see ResolveConstantStubCall 312 */ 313 public static final ForeignCallDescriptor RESOLVE_METHOD_BY_SYMBOL_AND_LOAD_COUNTERS = new ForeignCallDescriptor("resolve_method_by_symbol_and_load_counters", Word.class, Word.class, Word.class, 314 Word.class); 315 316 /** 317 * Tiered support. 318 */ 319 public static final ForeignCallDescriptor INVOCATION_EVENT = new ForeignCallDescriptor("invocation_event", void.class, Word.class); 320 public static final ForeignCallDescriptor BACKEDGE_EVENT = new ForeignCallDescriptor("backedge_event", void.class, Word.class, int.class, int.class); 321 322 /** 323 * @see UncommonTrapCallNode 324 */ 325 public static final ForeignCallDescriptor UNCOMMON_TRAP = new ForeignCallDescriptor("uncommonTrap", Word.class, Word.class, int.class, int.class); 326 327 public HotSpotBackend(HotSpotGraalRuntimeProvider runtime, HotSpotProviders providers) { 328 super(providers); 329 this.runtime = runtime; 330 } 331 332 public HotSpotGraalRuntimeProvider getRuntime() { 333 return runtime; 334 } 335 336 /** 337 * Performs any remaining initialization that was deferred until the {@linkplain #getRuntime() 338 * runtime} object was initialized and this backend was registered with it. 339 * 340 * @param jvmciRuntime 341 */ 342 public void completeInitialization(HotSpotJVMCIRuntime jvmciRuntime) { 343 } 344 345 /** 346 * Finds all the registers that are defined by some given LIR. 347 * 348 * @param lir the LIR to examine 349 * @return the registers that are defined by or used as temps for any instruction in {@code lir} 350 */ 351 protected final Set<Register> gatherDestroyedCallerRegisters(LIR lir) { 352 final Set<Register> destroyedRegisters = new HashSet<>(); 353 ValueConsumer defConsumer = new ValueConsumer() { 354 355 @Override 356 public void visitValue(Value value, OperandMode mode, EnumSet<OperandFlag> flags) { 357 if (ValueUtil.isRegister(value)) { 358 final Register reg = ValueUtil.asRegister(value); 359 destroyedRegisters.add(reg); 360 } 361 } 362 }; 363 for (AbstractBlockBase<?> block : lir.codeEmittingOrder()) { 364 if (block == null) { 365 continue; 366 } 367 for (LIRInstruction op : lir.getLIRforBlock(block)) { 368 if (op instanceof LabelOp) { 369 // Don't consider this as a definition 370 } else { 371 op.visitEachTemp(defConsumer); 372 op.visitEachOutput(defConsumer); 373 } 374 } 375 } 376 return translateToCallerRegisters(destroyedRegisters); 377 } 378 379 /** 380 * Updates a given stub with respect to the registers it destroys. 381 * <p> 382 * Any entry in {@code calleeSaveInfo} that {@linkplain SaveRegistersOp#supportsRemove() 383 * supports} pruning will have {@code destroyedRegisters} 384 * {@linkplain SaveRegistersOp#remove(Set) removed} as these registers are declared as 385 * temporaries in the stub's {@linkplain ForeignCallLinkage linkage} (and thus will be saved by 386 * the stub's caller). 387 * 388 * @param stub the stub to update 389 * @param destroyedRegisters the registers destroyed by the stub 390 * @param calleeSaveInfo a map from debug infos to the operations that provide their 391 * {@linkplain RegisterSaveLayout callee-save information} 392 * @param frameMap used to {@linkplain FrameMap#offsetForStackSlot(StackSlot) convert} a virtual 393 * slot to a frame slot index 394 */ 395 protected void updateStub(Stub stub, Set<Register> destroyedRegisters, Map<LIRFrameState, SaveRegistersOp> calleeSaveInfo, FrameMap frameMap) { 396 stub.initDestroyedCallerRegisters(destroyedRegisters); 397 398 for (Map.Entry<LIRFrameState, SaveRegistersOp> e : calleeSaveInfo.entrySet()) { 399 SaveRegistersOp save = e.getValue(); 400 if (save.supportsRemove()) { 401 save.remove(destroyedRegisters); 402 } 403 DebugInfo info = e.getKey() == null ? null : e.getKey().debugInfo(); 404 if (info != null) { 405 info.setCalleeSaveInfo(save.getMap(frameMap)); 406 } 407 } 408 } 409 410 @Override 411 public HotSpotProviders getProviders() { 412 return (HotSpotProviders) super.getProviders(); 413 } 414 415 @Override 416 public SuitesProvider getSuites() { 417 return getProviders().getSuites(); 418 } 419 420 protected void profileInstructions(LIR lir, CompilationResultBuilder crb) { 421 if (HotSpotBackend.Options.ASMInstructionProfiling.getValue() != null) { 422 HotSpotInstructionProfiling.countInstructions(lir, crb.asm); 423 } 424 } 425 426 @Override 427 public CompiledCode createCompiledCode(ResolvedJavaMethod method, CompilationRequest compilationRequest, CompilationResult compResult) { 428 HotSpotCompilationRequest compRequest = compilationRequest instanceof HotSpotCompilationRequest ? (HotSpotCompilationRequest) compilationRequest : null; 429 return HotSpotCompiledCodeBuilder.createCompiledCode(method, compRequest, compResult); 430 } 431 432 @Override 433 public CompilationIdentifier getCompilationIdentifier(ResolvedJavaMethod resolvedJavaMethod) { 434 if (resolvedJavaMethod instanceof HotSpotResolvedJavaMethod) { 435 HotSpotCompilationRequest request = new HotSpotCompilationRequest((HotSpotResolvedJavaMethod) resolvedJavaMethod, JVMCICompiler.INVOCATION_ENTRY_BCI, 0L); 436 return new HotSpotCompilationIdentifier(request); 437 } 438 return super.getCompilationIdentifier(resolvedJavaMethod); 439 } 440 }