001/*
002 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.api.directives.test;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.meta.*;
027
028import org.junit.*;
029
030import com.oracle.graal.api.directives.*;
031import com.oracle.graal.compiler.test.*;
032
033public class DeoptimizeDirectiveTest extends GraalCompilerTest {
034
035    public static boolean inCompiledCode() {
036        return GraalDirectives.inCompiledCode();
037    }
038
039    @Test
040    public void testInCompiledCode() {
041        ResolvedJavaMethod method = getResolvedJavaMethod("inCompiledCode");
042
043        Result interpreted = executeExpected(method, null);
044        assertEquals(new Result(false, null), interpreted);
045
046        Result compiled = executeActual(method, null);
047        assertEquals(new Result(true, null), compiled);
048    }
049
050    public static boolean deoptimizeSnippet() {
051        GraalDirectives.deoptimize();
052        return GraalDirectives.inCompiledCode(); // should always return false
053    }
054
055    public static boolean deoptimizeAndInvalidateSnippet() {
056        GraalDirectives.deoptimizeAndInvalidate();
057        return GraalDirectives.inCompiledCode(); // should always return false
058    }
059
060    @Test
061    public void testDeoptimize() {
062        test("deoptimizeSnippet");
063    }
064
065    private boolean testDeoptimizeCheckValid(ResolvedJavaMethod method) {
066        Result expected = executeExpected(method, null);
067
068        InstalledCode code = getCode(method);
069        Result actual;
070        try {
071            actual = new Result(code.executeVarargs(), null);
072        } catch (Throwable e) {
073            actual = new Result(null, e);
074        }
075
076        assertEquals(expected, actual);
077        return code.isValid();
078    }
079
080    @Test
081    public void testDeoptimizeAndInvalidate() {
082        ResolvedJavaMethod method = getResolvedJavaMethod("deoptimizeAndInvalidateSnippet");
083        boolean valid = testDeoptimizeCheckValid(method);
084        Assert.assertFalse("code should be invalidated", valid);
085    }
086
087    @Test
088    public void testDeoptimizeDontInvalidate() {
089        ResolvedJavaMethod method = getResolvedJavaMethod("deoptimizeSnippet");
090        boolean valid = testDeoptimizeCheckValid(method);
091        Assert.assertTrue("code should still be valid", valid);
092    }
093}