1 /* 2 * Copyright (c) 2015, 2015, 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 package org.graalvm.compiler.api.directives.test; 24 25 import jdk.vm.ci.code.InstalledCode; 26 import jdk.vm.ci.meta.ResolvedJavaMethod; 27 28 import org.junit.Assert; 29 import org.junit.Test; 30 31 import org.graalvm.compiler.api.directives.GraalDirectives; 32 import org.graalvm.compiler.core.test.GraalCompilerTest; 33 34 public class DeoptimizeDirectiveTest extends GraalCompilerTest { 35 36 public static boolean inCompiledCode() { 37 return GraalDirectives.inCompiledCode(); 38 } 39 40 @Test 41 public void testInCompiledCode() { 42 ResolvedJavaMethod method = getResolvedJavaMethod("inCompiledCode"); 43 44 Result interpreted = executeExpected(method, null); 45 assertEquals(new Result(false, null), interpreted); 46 47 Result compiled = executeActual(method, null); 48 assertEquals(new Result(true, null), compiled); 49 } 50 51 public static boolean deoptimizeSnippet() { 52 GraalDirectives.deoptimize(); 53 return GraalDirectives.inCompiledCode(); // should always return false 54 } 55 56 public static boolean deoptimizeAndInvalidateSnippet() { 57 GraalDirectives.deoptimizeAndInvalidate(); 58 return GraalDirectives.inCompiledCode(); // should always return false 59 } 60 61 @Test 62 public void testDeoptimize() { 63 test("deoptimizeSnippet"); 64 } 65 66 private boolean testDeoptimizeCheckValid(ResolvedJavaMethod method) { 67 Result expected = executeExpected(method, null); 68 69 InstalledCode code = getCode(method); 70 Result actual; 71 try { 72 actual = new Result(code.executeVarargs(), null); 73 } catch (Throwable e) { 74 actual = new Result(null, e); 75 } 76 77 assertEquals(expected, actual); 78 return code.isValid(); 79 } 80 81 @Test 82 public void testDeoptimizeAndInvalidate() { 83 ResolvedJavaMethod method = getResolvedJavaMethod("deoptimizeAndInvalidateSnippet"); 84 boolean valid = testDeoptimizeCheckValid(method); 85 Assert.assertFalse("code should be invalidated", valid); 86 } 87 88 @Test 89 public void testDeoptimizeDontInvalidate() { 90 ResolvedJavaMethod method = getResolvedJavaMethod("deoptimizeSnippet"); 91 boolean valid = testDeoptimizeCheckValid(method); 92 Assert.assertTrue("code should still be valid", valid); 93 } 94 }