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