DeoptimizeDirectiveTest.java revision 13304:5e9c41536bd2
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 */ 23package org.graalvm.compiler.api.directives.test; 24 25import jdk.vm.ci.code.InstalledCode; 26import jdk.vm.ci.meta.ResolvedJavaMethod; 27 28import org.junit.Assert; 29import org.junit.Test; 30 31import org.graalvm.compiler.api.directives.GraalDirectives; 32import org.graalvm.compiler.core.test.GraalCompilerTest; 33 34public 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 (Exception 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 95 public static int zeroBranchProbabilitySnippet(boolean flag) { 96 if (GraalDirectives.injectBranchProbability(0, flag)) { 97 GraalDirectives.controlFlowAnchor(); // prevent removal of the if 98 return 1; 99 } else { 100 GraalDirectives.controlFlowAnchor(); // prevent removal of the if 101 return 2; 102 } 103 } 104 105 @Test 106 public void testZeroBranchProbability() { 107 ResolvedJavaMethod method = getResolvedJavaMethod("zeroBranchProbabilitySnippet"); 108 Result expected = executeExpected(method, null, true); 109 110 InstalledCode code = getCode(method); 111 Result actual; 112 try { 113 actual = new Result(code.executeVarargs(true), null); 114 } catch (Exception e) { 115 actual = new Result(null, e); 116 } 117 118 assertEquals(expected, actual); 119 assertFalse("code should be invalidated", code.isValid()); 120 } 121} 122