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.HotSpotGraalCompiler;

  39 import org.graalvm.compiler.java.BciBlockMapping;
  40 import org.graalvm.compiler.java.BciBlockMapping.BciBlock;
  41 import org.graalvm.compiler.nodes.StructuredGraph;
  42 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  43 import org.graalvm.compiler.options.OptionValues;
  44 import org.junit.Assert;
  45 
  46 import jdk.vm.ci.code.Architecture;
  47 import jdk.vm.ci.hotspot.HotSpotCompilationRequest;
  48 import jdk.vm.ci.hotspot.HotSpotCompilationRequestResult;
  49 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  50 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
  51 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  52 import jdk.vm.ci.meta.ResolvedJavaMethod;
  53 
  54 public abstract class GraalOSRTestBase extends GraalCompilerTest {
  55 
  56     protected void testOSR(OptionValues options, String methodName) {
  57         testOSR(options, methodName, null);
  58     }
  59 
  60     protected void testOSR(OptionValues options, String methodName, Object receiver, Object... args) {
  61         ResolvedJavaMethod method = getResolvedJavaMethod(methodName);
  62         testOSR(options, method, receiver, args);
  63     }
  64 
  65     protected void testOSR(OptionValues options, ResolvedJavaMethod method, Object receiver, Object... args) {
  66         // invalidate any existing compiled code
  67         method.reprofile();
  68         compileOSR(options, method);
  69         Result result = executeExpected(method, receiver, args);
  70         checkResult(result);
  71     }
  72 
  73     protected static void compile(DebugContext debug, ResolvedJavaMethod method, int bci) {
  74         HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime();
  75         long jvmciEnv = 0L;
  76         HotSpotCompilationRequest request = new HotSpotCompilationRequest((HotSpotResolvedJavaMethod) method, bci, jvmciEnv);
  77         HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) runtime.getCompiler();
  78         CompilationTask task = new CompilationTask(runtime, compiler, request, true, true, debug.getOptions());







  79         HotSpotCompilationRequestResult result = task.runCompilation(debug);
  80         if (result.getFailure() != null) {
  81             throw new GraalError(result.getFailureMessage());
  82         }
  83     }
  84 
  85     /**
  86      * Returns the target BCI of the first bytecode backedge. This is where HotSpot triggers
  87      * on-stack-replacement in case the backedge counter overflows.
  88      */
  89     private static int getBackedgeBCI(DebugContext debug, ResolvedJavaMethod method) {
  90         Bytecode code = new ResolvedJavaMethodBytecode(method);
  91         BytecodeStream stream = new BytecodeStream(code.getCode());
  92         OptionValues options = debug.getOptions();
  93         BciBlockMapping bciBlockMapping = BciBlockMapping.create(stream, code, options, debug);
  94 
  95         for (BciBlock block : bciBlockMapping.getBlocks()) {
  96             if (block.startBci != -1) {
  97                 int bci = block.startBci;
  98                 for (BciBlock succ : block.getSuccessors()) {
  99                     if (succ.startBci != -1) {
 100                         int succBci = succ.startBci;
 101                         if (succBci < bci) {
 102                             // back edge
 103                             return succBci;
 104                         }
 105                     }
 106                 }
 107             }
 108         }
 109         TTY.println("Cannot find loop back edge with bytecode loops at:%s", Arrays.toString(bciBlockMapping.getLoopHeaders()));
 110         TTY.println(new BytecodeDisassembler().disassemble(code));
 111         return -1;
 112     }
 113 
 114     private static void checkResult(Result result) {
 115         Assert.assertNull("Unexpected exception", result.exception);
 116         Assert.assertNotNull(result.returnValue);
 117         Assert.assertTrue(result.returnValue instanceof ReturnValue);
 118         Assert.assertEquals(ReturnValue.SUCCESS, result.returnValue);
 119     }
 120 
 121     private void compileOSR(OptionValues options, ResolvedJavaMethod method) {
 122         // ensure eager resolving
 123         StructuredGraph graph = parseEager(method, AllowAssumptions.YES, options);
 124         DebugContext debug = graph.getDebug();
 125         int bci = getBackedgeBCI(debug, method);
 126         assert bci != -1;
 127         compile(debug, method, bci);
 128     }
 129 
 130     protected enum ReturnValue {
 131         SUCCESS,
 132         FAILURE,
 133         SIDE
 134     }
 135 
 136     public GraalOSRTestBase() {
 137         super();
 138     }
 139 
 140     public GraalOSRTestBase(Class<? extends Architecture> arch) {
 141         super(arch);
 142     }
 143 
 144     public GraalOSRTestBase(Backend backend) {
 145         super(backend);
 146     }
 147 
 148 }
--- EOF ---