1 /* 2 * Copyright (c) 2015, 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.api.directives.test; 24 25 import java.lang.annotation.ElementType; 26 import java.lang.annotation.Retention; 27 import java.lang.annotation.RetentionPolicy; 28 import java.lang.annotation.Target; 29 30 import org.junit.Assert; 31 import org.junit.Test; 32 33 import org.graalvm.compiler.api.directives.GraalDirectives; 34 import org.graalvm.compiler.core.test.GraalCompilerTest; 35 import org.graalvm.compiler.nodes.ParameterNode; 36 import org.graalvm.compiler.nodes.StructuredGraph; 37 38 /** 39 * Tests for {@link GraalDirectives#blackhole}. 40 * 41 * There are two snippets for each kind: 42 * <ul> 43 * <li>blackhole<Kind>Snippet verifies that dead code elimination is prevented by the 44 * blackhole directive. 45 * <li><kind>Snippet verifies that dead code elimination does happen if the blackhole 46 * directive is not there. 47 * </ul> 48 * 49 */ 50 public class BlackholeDirectiveTest extends GraalCompilerTest { 51 52 @Retention(RetentionPolicy.RUNTIME) 53 @Target(ElementType.METHOD) 54 private @interface BlackholeSnippet { 55 boolean expectParameterUsage(); 56 } 57 58 @BlackholeSnippet(expectParameterUsage = false) 59 public static int booleanSnippet(int arg) { 60 boolean b = arg > 3; 61 if (b) { 62 return 1; 63 } else { 64 return 1; 65 } 66 } 67 68 @BlackholeSnippet(expectParameterUsage = true) 69 public static int blackholeBooleanSnippet(int arg) { 70 boolean b = arg > 3; 71 GraalDirectives.blackhole(b); 72 if (b) { 73 return 1; 74 } else { 75 return 1; 76 } 77 } 78 79 @Test 80 public void testBoolean() { 81 test("booleanSnippet", 5); 82 test("blackholeBooleanSnippet", 5); 83 } 84 85 @BlackholeSnippet(expectParameterUsage = false) 86 public static int intSnippet(int arg) { 87 int x = 42 + arg; 88 return x - arg; 89 } 90 91 @BlackholeSnippet(expectParameterUsage = true) 92 public static int blackholeIntSnippet(int arg) { 93 int x = 42 + arg; 94 GraalDirectives.blackhole(x); 95 return x - arg; 96 } 97 98 @Test 99 public void testInt() { 100 test("intSnippet", 17); 101 test("blackholeIntSnippet", 17); 102 } 103 104 private static class Dummy { 105 private int x = 42; 106 } 107 108 @BlackholeSnippet(expectParameterUsage = false) 109 public static int objectSnippet(int arg) { 110 Dummy obj = new Dummy(); 111 int ret = obj.x; 112 obj.x = arg; 113 return ret; 114 } 115 116 @BlackholeSnippet(expectParameterUsage = true) 117 public static int blackholeObjectSnippet(int arg) { 118 Dummy obj = new Dummy(); 119 int ret = obj.x; 120 obj.x = arg; 121 GraalDirectives.blackhole(obj); 122 return ret; 123 } 124 125 @Test 126 public void testObject() { 127 test("objectSnippet", 37); 128 test("blackholeObjectSnippet", 37); 129 } 130 131 @Override 132 protected boolean checkLowTierGraph(StructuredGraph graph) { 133 BlackholeSnippet snippet = graph.method().getAnnotation(BlackholeSnippet.class); 134 ParameterNode arg = graph.getParameter(0); 135 if (snippet.expectParameterUsage()) { 136 Assert.assertNotNull("couldn't find ParameterNode(0)", arg); 137 Assert.assertFalse("expected usages of " + arg, arg.hasNoUsages()); 138 } else { 139 Assert.assertTrue("expected no usages of ParameterNode", arg == null || arg.hasNoUsages()); 140 } 141 return true; 142 } 143 }