DeoptimizeDirectiveTest.java revision 12968:4d8a004e5c6d
1139825Simp/*
21541Srgrimes * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
31541Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41541Srgrimes *
51541Srgrimes * This code is free software; you can redistribute it and/or modify it
61541Srgrimes * under the terms of the GNU General Public License version 2 only, as
71541Srgrimes * published by the Free Software Foundation.
81541Srgrimes *
91541Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101541Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111541Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121541Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131541Srgrimes * accompanied this code).
141541Srgrimes *
151541Srgrimes * You should have received a copy of the GNU General Public License version
161541Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171541Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181541Srgrimes *
191541Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201541Srgrimes * or visit www.oracle.com if you need additional information or have any
211541Srgrimes * questions.
221541Srgrimes */
231541Srgrimespackage org.graalvm.compiler.api.directives.test;
241541Srgrimes
251541Srgrimesimport jdk.vm.ci.code.InstalledCode;
261541Srgrimesimport jdk.vm.ci.meta.ResolvedJavaMethod;
271541Srgrimes
281541Srgrimesimport org.junit.Assert;
2914488Shsuimport org.junit.Test;
3050477Speter
311541Srgrimesimport org.graalvm.compiler.api.directives.GraalDirectives;
321541Srgrimesimport org.graalvm.compiler.core.test.GraalCompilerTest;
331541Srgrimes
341541Srgrimespublic class DeoptimizeDirectiveTest extends GraalCompilerTest {
351541Srgrimes
36170407Srwatson    public static boolean inCompiledCode() {
37170407Srwatson        return GraalDirectives.inCompiledCode();
38219304Strasz    }
39219304Strasz
401541Srgrimes    @Test
411541Srgrimes    public void testInCompiledCode() {
4243429Sphk        ResolvedJavaMethod method = getResolvedJavaMethod("inCompiledCode");
43167572Srwatson
44167572Srwatson        Result interpreted = executeExpected(method, null);
451541Srgrimes        assertEquals(new Result(false, null), interpreted);
46102538Salfred
471541Srgrimes        Result compiled = executeActual(method, null);
4899092Sbde        assertEquals(new Result(true, null), compiled);
4984827Sjhb    }
5099092Sbde
5199092Sbde    public static boolean deoptimizeSnippet() {
5299092Sbde        GraalDirectives.deoptimize();
53194498Sbrooks        return GraalDirectives.inCompiledCode(); // should always return false
5499092Sbde    }
55147641Sdelphij
5690757Sjulian    public static boolean deoptimizeAndInvalidateSnippet() {
5790757Sjulian        GraalDirectives.deoptimizeAndInvalidate();
58112704Smaxim        return GraalDirectives.inCompiledCode(); // should always return false
59219304Strasz    }
60193255Srwatson
61195741Sjamie    @Test
62100986Srwatson    public void testDeoptimize() {
63122524Srwatson        test("deoptimizeSnippet");
64170407Srwatson    }
65194498Sbrooks
66194498Sbrooks    private boolean testDeoptimizeCheckValid(ResolvedJavaMethod method) {
671541Srgrimes        Result expected = executeExpected(method, null);
6898745Sbde
6998745Sbde        InstalledCode code = getCode(method);
70102538Salfred        Result actual;
711541Srgrimes        try {
72194498Sbrooks            actual = new Result(code.executeVarargs(), null);
73194498Sbrooks        } catch (Exception e) {
7472650Sgreen            actual = new Result(null, e);
75219129Srwatson        }
76219129Srwatson
77219129Srwatson        assertEquals(expected, actual);
78219129Srwatson        return code.isValid();
79219129Srwatson    }
8094020Sdd
8172650Sgreen    @Test
8272650Sgreen    public void testDeoptimizeAndInvalidate() {
8391354Sdd        ResolvedJavaMethod method = getResolvedJavaMethod("deoptimizeAndInvalidateSnippet");
8472650Sgreen        boolean valid = testDeoptimizeCheckValid(method);
8572650Sgreen        Assert.assertFalse("code should be invalidated", valid);
86194498Sbrooks    }
8772650Sgreen
8872650Sgreen    @Test
8991354Sdd    public void testDeoptimizeDontInvalidate() {
9072650Sgreen        ResolvedJavaMethod method = getResolvedJavaMethod("deoptimizeSnippet");
9194020Sdd        boolean valid = testDeoptimizeCheckValid(method);
9298745Sbde        Assert.assertTrue("code should still be valid", valid);
9394020Sdd    }
9455205Speter}
95194498Sbrooks