1 /*
   2  * Copyright (c) 2015, 2018, 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 
  24 
  25 package org.graalvm.compiler.api.directives.test;
  26 
  27 import jdk.vm.ci.code.InstalledCode;
  28 import jdk.vm.ci.meta.ResolvedJavaMethod;
  29 
  30 import org.junit.Assert;
  31 import org.junit.Test;
  32 
  33 import org.graalvm.compiler.api.directives.GraalDirectives;
  34 import org.graalvm.compiler.core.test.GraalCompilerTest;
  35 
  36 public class DeoptimizeDirectiveTest extends GraalCompilerTest {
  37 
  38     public static boolean inCompiledCode() {
  39         return GraalDirectives.inCompiledCode();
  40     }
  41 
  42     @Test
  43     public void testInCompiledCode() {
  44         ResolvedJavaMethod method = getResolvedJavaMethod("inCompiledCode");
  45 
  46         Result interpreted = executeExpected(method, null);
  47         assertEquals(new Result(false, null), interpreted);
  48 
  49         Result compiled = executeActual(method, null);
  50         assertEquals(new Result(true, null), compiled);
  51     }
  52 
  53     public static boolean deoptimizeSnippet() {
  54         GraalDirectives.deoptimize();
  55         return GraalDirectives.inCompiledCode(); // should always return false
  56     }
  57 
  58     public static boolean deoptimizeAndInvalidateSnippet() {
  59         GraalDirectives.deoptimizeAndInvalidate();
  60         return GraalDirectives.inCompiledCode(); // should always return false
  61     }
  62 
  63     @Test
  64     public void testDeoptimize() {
  65         test("deoptimizeSnippet");
  66     }
  67 
  68     private boolean testDeoptimizeCheckValid(ResolvedJavaMethod method) {
  69         Result expected = executeExpected(method, null);
  70 
  71         InstalledCode code = getCode(method);
  72         Result actual;
  73         try {
  74             actual = new Result(code.executeVarargs(), null);
  75         } catch (Exception e) {
  76             actual = new Result(null, e);
  77         }
  78 
  79         assertEquals(expected, actual);
  80         return code.isValid();
  81     }
  82 
  83     @Test
  84     public void testDeoptimizeAndInvalidate() {
  85         ResolvedJavaMethod method = getResolvedJavaMethod("deoptimizeAndInvalidateSnippet");
  86         boolean valid = testDeoptimizeCheckValid(method);
  87         Assert.assertFalse("code should be invalidated", valid);
  88     }
  89 
  90     @Test
  91     public void testDeoptimizeDontInvalidate() {
  92         ResolvedJavaMethod method = getResolvedJavaMethod("deoptimizeSnippet");
  93         boolean valid = testDeoptimizeCheckValid(method);
  94         Assert.assertTrue("code should still be valid", valid);
  95     }
  96 
  97     public static int zeroBranchProbabilitySnippet(boolean flag) {
  98         if (GraalDirectives.injectBranchProbability(0, flag)) {
  99             GraalDirectives.controlFlowAnchor(); // prevent removal of the if
 100             return 1;
 101         } else {
 102             GraalDirectives.controlFlowAnchor(); // prevent removal of the if
 103             return 2;
 104         }
 105     }
 106 
 107     @Test
 108     public void testZeroBranchProbability() {
 109         ResolvedJavaMethod method = getResolvedJavaMethod("zeroBranchProbabilitySnippet");
 110         Result expected = executeExpected(method, null, true);
 111 
 112         InstalledCode code = getCode(method);
 113         Result actual;
 114         try {
 115             actual = new Result(code.executeVarargs(true), null);
 116         } catch (Exception e) {
 117             actual = new Result(null, e);
 118         }
 119 
 120         assertEquals(expected, actual);
 121         assertFalse("code should be invalidated", code.isValid());
 122     }
 123 }