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