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