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