< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.api.directives.test/src/org/graalvm/compiler/api/directives/test/BlackholeDirectiveTest.java

Print this page




  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&lt;Kind&gt;Snippet verifies that dead code elimination is prevented by the
  44  * blackhole directive.
  45  * <li>&lt;kind&gt;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     }


 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 }


  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 import org.graalvm.compiler.phases.OptimisticOptimizations;
  38 import org.graalvm.compiler.phases.OptimisticOptimizations.Optimization;
  39 import org.graalvm.compiler.phases.tiers.HighTierContext;
  40 
  41 /**
  42  * Tests for {@link GraalDirectives#blackhole}.
  43  *
  44  * There are two snippets for each kind:
  45  * <ul>
  46  * <li>blackhole&lt;Kind&gt;Snippet verifies that dead code elimination is prevented by the
  47  * blackhole directive.
  48  * <li>&lt;kind&gt;Snippet verifies that dead code elimination does happen if the blackhole
  49  * directive is not there.
  50  * </ul>
  51  *
  52  */
  53 public class BlackholeDirectiveTest extends GraalCompilerTest {
  54 
  55     @Retention(RetentionPolicy.RUNTIME)
  56     @Target(ElementType.METHOD)
  57     private @interface BlackholeSnippet {
  58         boolean expectParameterUsage();
  59     }


 112     public static int objectSnippet(int arg) {
 113         Dummy obj = new Dummy();
 114         int ret = obj.x;
 115         obj.x = arg;
 116         return ret;
 117     }
 118 
 119     @BlackholeSnippet(expectParameterUsage = true)
 120     public static int blackholeObjectSnippet(int arg) {
 121         Dummy obj = new Dummy();
 122         int ret = obj.x;
 123         obj.x = arg;
 124         GraalDirectives.blackhole(obj);
 125         return ret;
 126     }
 127 
 128     @Test
 129     public void testObject() {
 130         test("objectSnippet", 37);
 131         test("blackholeObjectSnippet", 37);
 132     }
 133 
 134     @Override
 135     protected HighTierContext getDefaultHighTierContext() {
 136         return new HighTierContext(getProviders(), getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL.remove(Optimization.RemoveNeverExecutedCode));
 137     }
 138 
 139     @Override
 140     protected boolean checkLowTierGraph(StructuredGraph graph) {
 141         BlackholeSnippet snippet = graph.method().getAnnotation(BlackholeSnippet.class);
 142         ParameterNode arg = graph.getParameter(0);
 143         if (snippet.expectParameterUsage()) {
 144             Assert.assertNotNull("couldn't find ParameterNode(0)", arg);
 145             Assert.assertFalse("expected usages of " + arg, arg.hasNoUsages());
 146         } else {
 147             Assert.assertTrue("expected no usages of ParameterNode", arg == null || arg.hasNoUsages());
 148         }
 149         return true;
 150     }
 151 }
< prev index next >