GraalOSRTestBase.java revision 12651:6ef01bd40ce2
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
24package org.graalvm.compiler.hotspot.test;
25
26import org.junit.Assert;
27
28import org.graalvm.compiler.bytecode.Bytecode;
29import org.graalvm.compiler.bytecode.BytecodeStream;
30import org.graalvm.compiler.bytecode.ResolvedJavaMethodBytecode;
31import org.graalvm.compiler.core.target.Backend;
32import org.graalvm.compiler.core.test.GraalCompilerTest;
33import org.graalvm.compiler.debug.GraalError;
34import org.graalvm.compiler.hotspot.CompilationTask;
35import org.graalvm.compiler.hotspot.HotSpotGraalCompiler;
36import org.graalvm.compiler.java.BciBlockMapping;
37import org.graalvm.compiler.java.BciBlockMapping.BciBlock;
38import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
39
40import jdk.vm.ci.code.Architecture;
41import jdk.vm.ci.hotspot.HotSpotCompilationRequest;
42import jdk.vm.ci.hotspot.HotSpotCompilationRequestResult;
43import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
44import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
45import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
46import jdk.vm.ci.meta.ResolvedJavaMethod;
47
48public abstract class GraalOSRTestBase extends GraalCompilerTest {
49
50    protected void testOSR(String methodName) {
51        ResolvedJavaMethod method = getResolvedJavaMethod(methodName);
52        testOSR(method);
53    }
54
55    protected void testOSR(ResolvedJavaMethod method) {
56        // invalidate any existing compiled code
57        method.reprofile();
58        compileOSR(method);
59        Result result = executeExpected(method, null);
60        checkResult(result);
61    }
62
63    private static void compile(ResolvedJavaMethod method, int bci) {
64        HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime();
65        long jvmciEnv = 0L;
66        HotSpotCompilationRequest request = new HotSpotCompilationRequest((HotSpotResolvedJavaMethod) method, bci, jvmciEnv);
67        HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) runtime.getCompiler();
68        CompilationTask task = new CompilationTask(runtime, compiler, request, true, true);
69        HotSpotCompilationRequestResult result = task.runCompilation();
70        if (result.getFailure() != null) {
71            throw new GraalError(result.getFailureMessage());
72        }
73    }
74
75    /**
76     * Returns the target BCI of the first bytecode backedge. This is where HotSpot triggers
77     * on-stack-replacement in case the backedge counter overflows.
78     */
79    private static int getBackedgeBCI(ResolvedJavaMethod method) {
80        Bytecode code = new ResolvedJavaMethodBytecode(method);
81        BytecodeStream stream = new BytecodeStream(code.getCode());
82        BciBlockMapping bciBlockMapping = BciBlockMapping.create(stream, code);
83        assert bciBlockMapping.getLoopCount() == 1 : "Expected exactly one loop " + method;
84
85        for (BciBlock block : bciBlockMapping.getBlocks()) {
86            int bci = block.startBci;
87            for (BciBlock succ : block.getSuccessors()) {
88                int succBci = succ.startBci;
89                if (succBci < bci) {
90                    // back edge
91                    return succBci;
92                }
93            }
94        }
95        return -1;
96    }
97
98    private static void checkResult(Result result) {
99        Assert.assertNull("Unexpected exception", result.exception);
100        Assert.assertNotNull(result.returnValue);
101        Assert.assertTrue(result.returnValue instanceof ReturnValue);
102        Assert.assertEquals(ReturnValue.SUCCESS, result.returnValue);
103    }
104
105    private void compileOSR(ResolvedJavaMethod method) {
106        int bci = getBackedgeBCI(method);
107        assert bci != -1;
108        // ensure eager resolving
109        parseEager(method, AllowAssumptions.YES);
110        compile(method, bci);
111    }
112
113    protected enum ReturnValue {
114        SUCCESS,
115        FAILURE
116    }
117
118    public GraalOSRTestBase() {
119        super();
120    }
121
122    public GraalOSRTestBase(Class<? extends Architecture> arch) {
123        super(arch);
124    }
125
126    public GraalOSRTestBase(Backend backend) {
127        super(backend);
128    }
129
130}
131