001/* 002 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.api.directives.test; 024 025import java.lang.annotation.*; 026 027import org.junit.*; 028 029import com.oracle.graal.api.directives.*; 030import com.oracle.graal.compiler.test.*; 031import com.oracle.graal.nodes.*; 032 033/** 034 * Tests for {@link GraalDirectives#blackhole}. 035 * 036 * There are two snippets for each kind: 037 * <ul> 038 * <li>blackhole<Kind>Snippet verifies that dead code elimination is prevented by the 039 * blackhole directive. 040 * <li><kind>Snippet verifies that dead code elimination does happen if the blackhole 041 * directive is not there. 042 * </ul> 043 * 044 */ 045public class BlackholeDirectiveTest extends GraalCompilerTest { 046 047 @Retention(RetentionPolicy.RUNTIME) 048 @Target(ElementType.METHOD) 049 private @interface BlackholeSnippet { 050 boolean expectParameterUsage(); 051 } 052 053 @BlackholeSnippet(expectParameterUsage = false) 054 public static int booleanSnippet(int arg) { 055 boolean b = arg > 3; 056 if (b) { 057 return 1; 058 } else { 059 return 1; 060 } 061 } 062 063 @BlackholeSnippet(expectParameterUsage = true) 064 public static int blackholeBooleanSnippet(int arg) { 065 boolean b = arg > 3; 066 GraalDirectives.blackhole(b); 067 if (b) { 068 return 1; 069 } else { 070 return 1; 071 } 072 } 073 074 @Test 075 public void testBoolean() { 076 test("booleanSnippet", 5); 077 test("blackholeBooleanSnippet", 5); 078 } 079 080 @BlackholeSnippet(expectParameterUsage = false) 081 public static int intSnippet(int arg) { 082 int x = 42 + arg; 083 return x - arg; 084 } 085 086 @BlackholeSnippet(expectParameterUsage = true) 087 public static int blackholeIntSnippet(int arg) { 088 int x = 42 + arg; 089 GraalDirectives.blackhole(x); 090 return x - arg; 091 } 092 093 @Test 094 public void testInt() { 095 test("intSnippet", 17); 096 test("blackholeIntSnippet", 17); 097 } 098 099 private static class Dummy { 100 private int x = 42; 101 } 102 103 @BlackholeSnippet(expectParameterUsage = false) 104 public static int objectSnippet(int arg) { 105 Dummy obj = new Dummy(); 106 int ret = obj.x; 107 obj.x = arg; 108 return ret; 109 } 110 111 @BlackholeSnippet(expectParameterUsage = true) 112 public static int blackholeObjectSnippet(int arg) { 113 Dummy obj = new Dummy(); 114 int ret = obj.x; 115 obj.x = arg; 116 GraalDirectives.blackhole(obj); 117 return ret; 118 } 119 120 @Test 121 public void testObject() { 122 test("objectSnippet", 37); 123 test("blackholeObjectSnippet", 37); 124 } 125 126 @Override 127 protected boolean checkLowTierGraph(StructuredGraph graph) { 128 BlackholeSnippet snippet = graph.method().getAnnotation(BlackholeSnippet.class); 129 ParameterNode arg = graph.getParameter(0); 130 if (snippet.expectParameterUsage()) { 131 Assert.assertNotNull("couldn't find ParameterNode(0)", arg); 132 Assert.assertFalse("expected usages of " + arg, arg.hasNoUsages()); 133 } else { 134 Assert.assertTrue("expected no usages of ParameterNode", arg == null || arg.hasNoUsages()); 135 } 136 return true; 137 } 138}