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.replacements.test;
024
025import static com.oracle.graal.nodeinfo.InputType.*;
026import static org.hamcrest.CoreMatchers.*;
027import jdk.internal.jvmci.meta.*;
028
029import org.junit.*;
030
031import com.oracle.graal.api.replacements.*;
032import com.oracle.graal.compiler.common.type.*;
033import com.oracle.graal.compiler.test.*;
034import com.oracle.graal.graph.*;
035import com.oracle.graal.graph.iterators.*;
036import com.oracle.graal.nodeinfo.*;
037import com.oracle.graal.nodeinfo.StructuralInput.Guard;
038import com.oracle.graal.nodeinfo.StructuralInput.Memory;
039import com.oracle.graal.nodes.*;
040import com.oracle.graal.nodes.calc.*;
041import com.oracle.graal.nodes.extended.*;
042import com.oracle.graal.nodes.memory.*;
043
044public class SubstitutionsTest extends GraalCompilerTest {
045
046    @NodeInfo(allowedUsageTypes = {Memory})
047    private static class TestMemory extends FixedWithNextNode implements MemoryNode {
048        private static final NodeClass<TestMemory> TYPE = NodeClass.create(TestMemory.class);
049
050        public TestMemory() {
051            super(TYPE, StampFactory.forVoid());
052        }
053
054        @NodeIntrinsic
055        public static native Memory memory();
056    }
057
058    @NodeInfo(allowedUsageTypes = {Guard})
059    private static class TestGuard extends FloatingNode implements GuardingNode {
060        private static final NodeClass<TestGuard> TYPE = NodeClass.create(TestGuard.class);
061
062        @Input(Memory) MemoryNode memory;
063
064        public TestGuard(ValueNode memory) {
065            super(TYPE, StampFactory.forVoid());
066            this.memory = (MemoryNode) memory;
067        }
068
069        @NodeIntrinsic
070        public static native Guard guard(Memory memory);
071    }
072
073    @NodeInfo
074    private static class TestValue extends FloatingNode {
075        private static final NodeClass<TestValue> TYPE = NodeClass.create(TestValue.class);
076
077        @Input(Guard) GuardingNode guard;
078
079        public TestValue(ValueNode guard) {
080            super(TYPE, StampFactory.forKind(Kind.Int));
081            this.guard = (GuardingNode) guard;
082        }
083
084        @NodeIntrinsic
085        public static native int value(Guard guard);
086    }
087
088    private static class TestMethod {
089
090        public static int test() {
091            return 42;
092        }
093    }
094
095    @ClassSubstitution(TestMethod.class)
096    private static class TestMethodSubstitution {
097
098        @MethodSubstitution
099        public static int test() {
100            Memory memory = TestMemory.memory();
101            Guard guard = TestGuard.guard(memory);
102            return TestValue.value(guard);
103        }
104    }
105
106    private static boolean substitutionsInstalled;
107
108    public SubstitutionsTest() {
109        if (!substitutionsInstalled) {
110            getProviders().getReplacements().registerSubstitutions(TestMethod.class, TestMethodSubstitution.class);
111            substitutionsInstalled = true;
112        }
113    }
114
115    public static int callTest() {
116        return TestMethod.test();
117    }
118
119    @Override
120    protected boolean checkHighTierGraph(StructuredGraph graph) {
121        // Check that the graph contains the expected test nodes.
122        NodeIterable<ReturnNode> retNodes = graph.getNodes().filter(ReturnNode.class);
123        Assert.assertTrue("expected exactly one ReturnNode", retNodes.count() == 1);
124        ReturnNode ret = retNodes.first();
125
126        Assert.assertThat(ret.result(), instanceOf(TestValue.class));
127        TestValue value = (TestValue) ret.result();
128
129        Assert.assertThat(value.guard, instanceOf(TestGuard.class));
130        TestGuard guard = (TestGuard) value.guard;
131
132        Assert.assertThat(guard.memory, instanceOf(TestMemory.class));
133        TestMemory memory = (TestMemory) guard.memory;
134
135        // Remove the test nodes, replacing them by the constant 42.
136        // This implicitly makes sure that the rest of the graph is valid.
137        ret.replaceFirstInput(value, graph.unique(ConstantNode.forInt(42)));
138        value.safeDelete();
139        guard.safeDelete();
140        graph.removeFixed(memory);
141
142        return true;
143    }
144
145    @Test
146    public void snippetTest() {
147        test("callTest");
148    }
149}