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