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.*;
032import com.oracle.graal.nodes.calc.*;
033
034/**
035 * Tests for {@link GraalDirectives#opaque}.
036 *
037 * There are two snippets for each kind:
038 * <ul>
039 * <li>opaque&lt;Kind&gt;Snippet verifies that constant folding is prevented by the opaque
040 * directive.
041 * <li>&lt;kind&gt;Snippet verifies that constant folding does happen if the opaque directive is not
042 * there.
043 * </ul>
044 *
045 */
046public class OpaqueDirectiveTest extends GraalCompilerTest {
047
048    @Retention(RetentionPolicy.RUNTIME)
049    @Target(ElementType.METHOD)
050    private @interface OpaqueSnippet {
051        Class<?> expectedReturnNode();
052    }
053
054    @OpaqueSnippet(expectedReturnNode = ConstantNode.class)
055    public static boolean booleanSnippet() {
056        return 5 > 3;
057    }
058
059    @OpaqueSnippet(expectedReturnNode = ConditionalNode.class)
060    public static boolean opaqueBooleanSnippet() {
061        return 5 > GraalDirectives.opaque(3);
062    }
063
064    @Test
065    public void testBoolean() {
066        test("booleanSnippet");
067        test("opaqueBooleanSnippet");
068    }
069
070    @OpaqueSnippet(expectedReturnNode = ConstantNode.class)
071    public static int intSnippet() {
072        return 5 + 3;
073    }
074
075    @OpaqueSnippet(expectedReturnNode = AddNode.class)
076    public static int opaqueIntSnippet() {
077        return 5 + GraalDirectives.opaque(3);
078    }
079
080    @Test
081    public void testInt() {
082        test("intSnippet");
083        test("opaqueIntSnippet");
084    }
085
086    @OpaqueSnippet(expectedReturnNode = ConstantNode.class)
087    public static double doubleSnippet() {
088        return 5. + 3.;
089    }
090
091    @OpaqueSnippet(expectedReturnNode = AddNode.class)
092    public static double opaqueDoubleSnippet() {
093        return 5. + GraalDirectives.opaque(3.);
094    }
095
096    @Test
097    public void testDouble() {
098        test("doubleSnippet");
099        test("opaqueDoubleSnippet");
100    }
101
102    private static class Dummy {
103    }
104
105    @OpaqueSnippet(expectedReturnNode = ConstantNode.class)
106    public static boolean objectSnippet() {
107        Object obj = new Dummy();
108        return obj == null;
109    }
110
111    @OpaqueSnippet(expectedReturnNode = ConditionalNode.class)
112    public static boolean opaqueObjectSnippet() {
113        Object obj = new Dummy();
114        return GraalDirectives.opaque(obj) == null;
115    }
116
117    @Test
118    public void testObject() {
119        test("objectSnippet");
120        test("opaqueObjectSnippet");
121    }
122
123    @Override
124    protected boolean checkLowTierGraph(StructuredGraph graph) {
125        OpaqueSnippet snippet = graph.method().getAnnotation(OpaqueSnippet.class);
126        for (ReturnNode returnNode : graph.getNodes(ReturnNode.TYPE)) {
127            Assert.assertEquals(snippet.expectedReturnNode(), returnNode.result().getClass());
128        }
129        return true;
130    }
131}