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