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