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.ConstantNode;
  36 import org.graalvm.compiler.nodes.ReturnNode;
  37 import org.graalvm.compiler.nodes.StructuredGraph;
  38 import org.graalvm.compiler.nodes.calc.AddNode;
  39 import org.graalvm.compiler.nodes.calc.ConditionalNode;
  40 
  41 /**
  42  * Tests for {@link GraalDirectives#opaque}.
  43  *
  44  * There are two snippets for each kind:
  45  * <ul>
  46  * <li>opaque&lt;Kind&gt;Snippet verifies that constant folding is prevented by the opaque
  47  * directive.
  48  * <li>&lt;kind&gt;Snippet verifies that constant folding does happen if the opaque directive is not
  49  * there.
  50  * </ul>
  51  *
  52  */
  53 public class OpaqueDirectiveTest extends GraalCompilerTest {
  54 
  55     @Retention(RetentionPolicy.RUNTIME)
  56     @Target(ElementType.METHOD)
  57     private @interface OpaqueSnippet {
  58         Class<?> expectedReturnNode();
  59     }
  60 
  61     @OpaqueSnippet(expectedReturnNode = ConstantNode.class)
  62     public static boolean booleanSnippet() {
  63         return 5 > 3;
  64     }
  65 
  66     @OpaqueSnippet(expectedReturnNode = ConditionalNode.class)
  67     public static boolean opaqueBooleanSnippet() {
  68         return 5 > GraalDirectives.opaque(3);
  69     }
  70 
  71     @Test
  72     public void testBoolean() {
  73         test("booleanSnippet");
  74         test("opaqueBooleanSnippet");
  75     }
  76 
  77     @OpaqueSnippet(expectedReturnNode = ConstantNode.class)
  78     public static int intSnippet() {
  79         return 5 + 3;
  80     }
  81 
  82     @OpaqueSnippet(expectedReturnNode = AddNode.class)
  83     public static int opaqueIntSnippet() {
  84         return 5 + GraalDirectives.opaque(3);
  85     }
  86 
  87     @Test
  88     public void testInt() {
  89         test("intSnippet");
  90         test("opaqueIntSnippet");
  91     }
  92 
  93     @OpaqueSnippet(expectedReturnNode = ConstantNode.class)
  94     public static double doubleSnippet() {
  95         return 5. + 3.;
  96     }
  97 
  98     @OpaqueSnippet(expectedReturnNode = AddNode.class)
  99     public static double opaqueDoubleSnippet() {
 100         return 5. + GraalDirectives.opaque(3.);
 101     }
 102 
 103     @Test
 104     public void testDouble() {
 105         test("doubleSnippet");
 106         test("opaqueDoubleSnippet");
 107     }
 108 
 109     private static class Dummy {
 110     }
 111 
 112     @OpaqueSnippet(expectedReturnNode = ConstantNode.class)
 113     public static boolean objectSnippet() {
 114         Object obj = new Dummy();
 115         return obj == null;
 116     }
 117 
 118     @OpaqueSnippet(expectedReturnNode = ConditionalNode.class)
 119     public static boolean opaqueObjectSnippet() {
 120         Object obj = new Dummy();
 121         return GraalDirectives.opaque(obj) == null;
 122     }
 123 
 124     @Test
 125     public void testObject() {
 126         test("objectSnippet");
 127         test("opaqueObjectSnippet");
 128     }
 129 
 130     @Override
 131     protected boolean checkLowTierGraph(StructuredGraph graph) {
 132         OpaqueSnippet snippet = graph.method().getAnnotation(OpaqueSnippet.class);
 133         for (ReturnNode returnNode : graph.getNodes(ReturnNode.TYPE)) {
 134             Assert.assertEquals(snippet.expectedReturnNode(), returnNode.result().getClass());
 135         }
 136         return true;
 137     }
 138 }