1 /* 2 * Copyright (c) 2011, 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.replacements.test; 26 27 import org.graalvm.compiler.api.directives.GraalDirectives; 28 import org.graalvm.compiler.core.test.GraalCompilerTest; 29 import org.junit.Test; 30 31 import jdk.vm.ci.code.InstalledCode; 32 import jdk.vm.ci.meta.ResolvedJavaMethod; 33 34 /** 35 * Tests that deoptimization upon volatile read will not roll back the read. The test cases rely on 36 * the fact that invocations to {@link GraalDirectives} utilities are substituted and not valid 37 * targets for deoptimization. 38 */ 39 public class DeoptimizeOnVolatileReadTest extends GraalCompilerTest { 40 41 static class Dummy { 42 boolean f1 = false; 43 volatile boolean f2 = false; 44 } 45 test1Snippet(Dummy dummy)46 public static int test1Snippet(Dummy dummy) { 47 if (GraalDirectives.injectBranchProbability(0, GraalDirectives.inCompiledCode() & dummy.f1)) { 48 return 1; 49 } 50 51 return 0; 52 } 53 54 @Test test1()55 public void test1() { 56 ResolvedJavaMethod method = getResolvedJavaMethod("test1Snippet"); 57 58 Dummy dummy = new Dummy(); 59 Result expected = executeExpected(method, null, dummy); 60 assertEquals(new Result(0, null), expected); 61 62 dummy.f1 = true; 63 64 InstalledCode code = getCode(method); 65 Result actual; 66 67 try { 68 actual = new Result(code.executeVarargs(dummy), null); 69 } catch (Exception e) { 70 actual = new Result(null, e); 71 } 72 73 // The code should get deoptimized, and resume execution at the beginning of the method. 74 // Therefore it does not re-enter the branch as inCompiledCode() is false. 75 assertEquals(new Result(0, null), actual); 76 assertFalse(code.isValid()); 77 } 78 test2Snippet(Dummy dummy)79 public static int test2Snippet(Dummy dummy) { 80 if (GraalDirectives.injectBranchProbability(0, GraalDirectives.inCompiledCode() & dummy.f2)) { 81 return 1; 82 } 83 84 return 0; 85 } 86 87 @Test test2()88 public void test2() { 89 ResolvedJavaMethod method = getResolvedJavaMethod("test2Snippet"); 90 91 Dummy dummy = new Dummy(); 92 Result expected = executeExpected(method, null, dummy); 93 assertEquals(new Result(0, null), expected); 94 95 dummy.f2 = true; 96 97 InstalledCode code = getCode(method); 98 Result actual; 99 100 try { 101 actual = new Result(code.executeVarargs(dummy), null); 102 } catch (Exception e) { 103 actual = new Result(null, e); 104 } 105 106 // The code should get deoptimized, and resume execution at the then-branch. 107 assertEquals(new Result(1, null), actual); 108 assertFalse(code.isValid()); 109 } 110 111 } 112