1 /*
   2  * Copyright (c) 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 
  24 
  25 package org.graalvm.compiler.core.test.ea;
  26 
  27 import org.junit.Test;
  28 
  29 import org.graalvm.compiler.api.directives.GraalDirectives;
  30 import org.graalvm.compiler.code.SourceStackTraceBailoutException;
  31 import org.graalvm.compiler.core.test.GraalCompilerTest;
  32 import org.graalvm.compiler.phases.OptimisticOptimizations;
  33 import org.graalvm.compiler.phases.OptimisticOptimizations.Optimization;
  34 import org.graalvm.compiler.phases.tiers.HighTierContext;
  35 
  36 public class PEAAssertionsTest extends GraalCompilerTest {
  37 
  38     /**
  39      * These tests assume all code paths are reachable so disable profile based dead code removal.
  40      */
  41     @Override
  42     protected HighTierContext getDefaultHighTierContext() {
  43         return new HighTierContext(getProviders(), getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL.remove(Optimization.RemoveNeverExecutedCode));
  44     }
  45 
  46     public static Object field;
  47 
  48     @SuppressWarnings({"deprecation", "unused"})
  49     public static void snippet1(int i) {
  50         Integer object = new Integer(i);
  51         GraalDirectives.ensureVirtualized(object);
  52     }
  53 
  54     @Test
  55     public void test1() {
  56         test("snippet1", 1);
  57     }
  58 
  59     @SuppressWarnings({"deprecation", "unused"})
  60     public static void snippet2(int i) {
  61         Integer object = new Integer(i);
  62         GraalDirectives.ensureVirtualized(object);
  63         field = object; // assert here
  64     }
  65 
  66     @Test(expected = SourceStackTraceBailoutException.class)
  67     public void test2() {
  68         test("snippet2", 1);
  69     }
  70 
  71     @SuppressWarnings({"deprecation", "unused"})
  72     public static void snippet3(int i) {
  73         Integer object = new Integer(i);
  74         field = object;
  75         GraalDirectives.ensureVirtualized(object); // assert here
  76     }
  77 
  78     @Test(expected = SourceStackTraceBailoutException.class)
  79     public void test3() {
  80         test("snippet3", 1);
  81     }
  82 
  83     @SuppressWarnings({"deprecation", "unused"})
  84     public static void snippetHere1(int i) {
  85         Integer object = new Integer(i);
  86         GraalDirectives.ensureVirtualizedHere(object);
  87     }
  88 
  89     @Test
  90     public void testHere1() {
  91         test("snippetHere1", 1);
  92     }
  93 
  94     @SuppressWarnings({"deprecation", "unused"})
  95     public static void snippetHere2(int i) {
  96         Integer object = new Integer(i);
  97         GraalDirectives.ensureVirtualizedHere(object);
  98         field = object;
  99     }
 100 
 101     @Test
 102     public void testHere2() {
 103         test("snippetHere2", 1);
 104     }
 105 
 106     @SuppressWarnings({"deprecation", "unused"})
 107     public static void snippetHere3(int i) {
 108         Integer object = new Integer(i);
 109         field = object;
 110         GraalDirectives.ensureVirtualizedHere(object); // assert here
 111     }
 112 
 113     @Test(expected = SourceStackTraceBailoutException.class)
 114     public void testHere3() {
 115         test("snippetHere3", 1);
 116     }
 117 
 118     public static void snippetBoxing1(int i) {
 119         Integer object = i;
 120         GraalDirectives.ensureVirtualizedHere(object); // assert here
 121     }
 122 
 123     @Test(expected = SourceStackTraceBailoutException.class)
 124     public void testBoxing1() {
 125         test("snippetBoxing1", 1);
 126     }
 127 
 128     public static void snippetBoxing2(int i) {
 129         Integer object = i;
 130         GraalDirectives.ensureVirtualized(object); // assert here
 131         field = object;
 132     }
 133 
 134     @Test(expected = SourceStackTraceBailoutException.class)
 135     public void testBoxing2() {
 136         test("snippetBoxing2", 1);
 137     }
 138 
 139     @SuppressWarnings({"deprecation", "unused"})
 140     public static void snippetControlFlow1(boolean b, int i) {
 141         Integer object = new Integer(i);
 142         if (b) {
 143             GraalDirectives.ensureVirtualized(object);
 144         }
 145         GraalDirectives.controlFlowAnchor();
 146         field = object;
 147     }
 148 
 149     @Test
 150     public void testControlFlow1() {
 151         test("snippetControlFlow1", true, 1);
 152     }
 153 
 154     @SuppressWarnings({"deprecation", "unused"})
 155     public static void snippetControlFlow2(boolean b, int i) {
 156         Integer object = new Integer(i);
 157         if (b) {
 158             GraalDirectives.ensureVirtualized(object);
 159         } else {
 160             GraalDirectives.ensureVirtualized(object);
 161         }
 162         GraalDirectives.controlFlowAnchor();
 163         field = object; // assert here
 164     }
 165 
 166     @Test(expected = SourceStackTraceBailoutException.class)
 167     public void testControlFlow2() {
 168         test("snippetControlFlow2", true, 1);
 169     }
 170 
 171     @SuppressWarnings({"deprecation", "unused"})
 172     public static void snippetControlFlow3(boolean b, int i) {
 173         Integer object = new Integer(i);
 174         GraalDirectives.ensureVirtualized(object);
 175         if (b) {
 176             field = 1;
 177         } else {
 178             field = 2;
 179         }
 180         GraalDirectives.controlFlowAnchor();
 181         field = object; // assert here
 182     }
 183 
 184     @Test(expected = SourceStackTraceBailoutException.class)
 185     public void testControlFlow3() {
 186         test("snippetControlFlow3", true, 1);
 187     }
 188 
 189     @SuppressWarnings({"deprecation", "unused"})
 190     public static void snippetControlFlow4(boolean b, int i) {
 191         Integer object = new Integer(i);
 192         if (b) {
 193             field = object;
 194         } else {
 195             field = 2;
 196         }
 197         GraalDirectives.ensureVirtualized(object); // assert here
 198     }
 199 
 200     @Test(expected = SourceStackTraceBailoutException.class)
 201     public void testControlFlow4() {
 202         test("snippetControlFlow4", true, 1);
 203     }
 204 
 205     @SuppressWarnings({"deprecation", "unused"})
 206     public static void snippetControlFlow5(boolean b, int i) {
 207         Integer object = new Integer(i);
 208         if (b) {
 209             field = object;
 210         } else {
 211             field = 2;
 212         }
 213         GraalDirectives.ensureVirtualizedHere(object); // assert here
 214     }
 215 
 216     @Test(expected = SourceStackTraceBailoutException.class)
 217     public void testControlFlow5() {
 218         test("snippetControlFlow5", true, 1);
 219     }
 220 
 221     public static final class TestClass {
 222         Object a;
 223         Object b;
 224     }
 225 
 226     @SuppressWarnings({"deprecation", "unused"})
 227     public static void snippetIndirect1(boolean b, int i) {
 228         Integer object = new Integer(i);
 229         TestClass t = new TestClass();
 230         t.a = object;
 231         GraalDirectives.ensureVirtualized(object);
 232 
 233         if (b) {
 234             field = t; // assert here
 235         } else {
 236             field = 2;
 237         }
 238     }
 239 
 240     @Test(expected = SourceStackTraceBailoutException.class)
 241     public void testIndirect1() {
 242         test("snippetIndirect1", true, 1);
 243     }
 244 
 245     @SuppressWarnings({"deprecation", "unused"})
 246     public static void snippetIndirect2(boolean b, int i) {
 247         Integer object = new Integer(i);
 248         TestClass t = new TestClass();
 249         t.a = object;
 250         GraalDirectives.ensureVirtualized(t);
 251 
 252         if (b) {
 253             field = object;
 254         } else {
 255             field = 2;
 256         }
 257     }
 258 
 259     @Test
 260     public void testIndirect2() {
 261         test("snippetIndirect2", true, 1);
 262     }
 263 }