1 /* 2 * Copyright (c) 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 24 package org.graalvm.compiler.hotspot.test; 25 26 import java.util.Arrays; 27 28 import org.graalvm.compiler.bytecode.Bytecode; 29 import org.graalvm.compiler.bytecode.BytecodeDisassembler; 30 import org.graalvm.compiler.bytecode.BytecodeStream; 31 import org.graalvm.compiler.bytecode.ResolvedJavaMethodBytecode; 32 import org.graalvm.compiler.core.target.Backend; 33 import org.graalvm.compiler.core.test.GraalCompilerTest; 34 import org.graalvm.compiler.debug.DebugContext; 35 import org.graalvm.compiler.debug.GraalError; 36 import org.graalvm.compiler.debug.TTY; 37 import org.graalvm.compiler.hotspot.CompilationTask; 38 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 39 import org.graalvm.compiler.hotspot.HotSpotGraalCompiler; 40 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider; 41 import org.graalvm.compiler.java.BciBlockMapping; 42 import org.graalvm.compiler.java.BciBlockMapping.BciBlock; 43 import org.graalvm.compiler.nodes.StructuredGraph; 44 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 45 import org.graalvm.compiler.options.OptionValues; 46 import org.junit.Assert; 47 48 import jdk.vm.ci.code.Architecture; 49 import jdk.vm.ci.hotspot.HotSpotCompilationRequest; 50 import jdk.vm.ci.hotspot.HotSpotCompilationRequestResult; 51 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; 52 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider; 53 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 54 import jdk.vm.ci.meta.ResolvedJavaMethod; 55 56 public abstract class GraalOSRTestBase extends GraalCompilerTest { 57 58 protected void testOSR(OptionValues options, String methodName) { 59 testOSR(options, methodName, null); 60 } 61 62 protected void testOSR(OptionValues options, String methodName, Object receiver, Object... args) { 63 ResolvedJavaMethod method = getResolvedJavaMethod(methodName); 64 testOSR(options, method, receiver, args); 65 } 66 67 protected void testOSR(OptionValues options, ResolvedJavaMethod method, Object receiver, Object... args) { 68 // invalidate any existing compiled code 69 method.reprofile(); 70 compileOSR(options, method); 71 Result result = executeExpected(method, receiver, args); 72 checkResult(result); 73 } 74 75 protected static void compile(DebugContext debug, ResolvedJavaMethod method, int bci) { 76 HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime(); 77 long jvmciEnv = 0L; 78 HotSpotCompilationRequest request = new HotSpotCompilationRequest((HotSpotResolvedJavaMethod) method, bci, jvmciEnv); 79 HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) runtime.getCompiler(); 80 CompilationTask task = new CompilationTask(runtime, compiler, request, true, true, debug.getOptions()); 81 if (method instanceof HotSpotResolvedJavaMethod) { 82 HotSpotGraalRuntimeProvider graalRuntime = compiler.getGraalRuntime(); 83 GraalHotSpotVMConfig config = graalRuntime.getVMConfig(); 84 if (((HotSpotResolvedJavaMethod) method).hasCodeAtLevel(bci, config.compilationLevelFullOptimization)) { 85 return; 86 } 87 } 88 HotSpotCompilationRequestResult result = task.runCompilation(debug); 89 if (result.getFailure() != null) { 90 throw new GraalError(result.getFailureMessage()); 91 } 92 } 93 94 /** 95 * Returns the target BCI of the first bytecode backedge. This is where HotSpot triggers 96 * on-stack-replacement in case the backedge counter overflows. 97 */ 98 private static int getBackedgeBCI(DebugContext debug, ResolvedJavaMethod method) { 99 Bytecode code = new ResolvedJavaMethodBytecode(method); 100 BytecodeStream stream = new BytecodeStream(code.getCode()); 101 OptionValues options = debug.getOptions(); 102 BciBlockMapping bciBlockMapping = BciBlockMapping.create(stream, code, options, debug); 103 104 for (BciBlock block : bciBlockMapping.getBlocks()) { 105 if (block.startBci != -1) { 106 int bci = block.startBci; 107 for (BciBlock succ : block.getSuccessors()) { 108 if (succ.startBci != -1) { 109 int succBci = succ.startBci; 110 if (succBci < bci) { 111 // back edge 112 return succBci; 113 } 114 } 115 } 116 } 117 } 118 TTY.println("Cannot find loop back edge with bytecode loops at:%s", Arrays.toString(bciBlockMapping.getLoopHeaders())); 119 TTY.println(new BytecodeDisassembler().disassemble(code)); 120 return -1; 121 } 122 123 private static void checkResult(Result result) { 124 Assert.assertNull("Unexpected exception", result.exception); 125 Assert.assertNotNull(result.returnValue); 126 Assert.assertTrue(result.returnValue instanceof ReturnValue); 127 Assert.assertEquals(ReturnValue.SUCCESS, result.returnValue); 128 } 129 130 private void compileOSR(OptionValues options, ResolvedJavaMethod method) { 131 // ensure eager resolving 132 StructuredGraph graph = parseEager(method, AllowAssumptions.YES, options); 133 DebugContext debug = graph.getDebug(); 134 int bci = getBackedgeBCI(debug, method); 135 assert bci != -1; 136 compile(debug, method, bci); 137 } 138 139 protected enum ReturnValue { 140 SUCCESS, 141 FAILURE, 142 SIDE 143 } 144 145 public GraalOSRTestBase() { 146 super(); 147 } 148 149 public GraalOSRTestBase(Class<? extends Architecture> arch) { 150 super(arch); 151 } 152 153 public GraalOSRTestBase(Backend backend) { 154 super(backend); 155 } 156 157 }