1 /* 2 * Copyright (c) 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.replacements.test; 24 25 import static org.graalvm.compiler.nodeinfo.InputType.Guard; 26 import static org.graalvm.compiler.nodeinfo.InputType.Memory; 27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_IGNORED; 28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_IGNORED; 29 import static org.hamcrest.CoreMatchers.instanceOf; 30 31 import org.junit.Assert; 32 import org.junit.Test; 33 34 import org.graalvm.compiler.api.replacements.ClassSubstitution; 35 import org.graalvm.compiler.api.replacements.MethodSubstitution; 36 import org.graalvm.compiler.bytecode.BytecodeProvider; 37 import org.graalvm.compiler.core.common.type.StampFactory; 38 import org.graalvm.compiler.core.test.GraalCompilerTest; 39 import org.graalvm.compiler.graph.NodeClass; 40 import org.graalvm.compiler.graph.iterators.NodeIterable; 41 import org.graalvm.compiler.nodeinfo.NodeInfo; 42 import org.graalvm.compiler.nodeinfo.StructuralInput.Guard; 43 import org.graalvm.compiler.nodeinfo.StructuralInput.Memory; 44 import org.graalvm.compiler.nodes.ConstantNode; 45 import org.graalvm.compiler.nodes.FixedWithNextNode; 46 import org.graalvm.compiler.nodes.ReturnNode; 47 import org.graalvm.compiler.nodes.StructuredGraph; 48 import org.graalvm.compiler.nodes.ValueNode; 49 import org.graalvm.compiler.nodes.calc.FloatingNode; 50 import org.graalvm.compiler.nodes.extended.GuardingNode; 51 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; 52 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins; 53 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins; 54 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration; 55 import org.graalvm.compiler.nodes.memory.MemoryNode; 56 57 import jdk.vm.ci.meta.JavaKind; 58 59 public class SubstitutionsTest extends GraalCompilerTest { 60 61 @NodeInfo(allowedUsageTypes = {Memory}, cycles = CYCLES_IGNORED, size = SIZE_IGNORED) 62 static class TestMemory extends FixedWithNextNode implements MemoryNode { 63 private static final NodeClass<TestMemory> TYPE = NodeClass.create(TestMemory.class); 64 65 protected TestMemory() { 66 super(TYPE, StampFactory.forVoid()); 67 } 68 69 @NodeIntrinsic 70 public static native Memory memory(); 71 } 72 73 @NodeInfo(allowedUsageTypes = {Guard}, cycles = CYCLES_IGNORED, size = SIZE_IGNORED) 74 static class TestGuard extends FloatingNode implements GuardingNode { 75 private static final NodeClass<TestGuard> TYPE = NodeClass.create(TestGuard.class); 76 77 @Input(Memory) MemoryNode memory; 78 79 protected TestGuard(ValueNode memory) { 80 super(TYPE, StampFactory.forVoid()); 81 this.memory = (MemoryNode) memory; 82 } 83 84 @NodeIntrinsic 85 public static native Guard guard(Memory memory); 86 } 87 88 @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED) 89 static class TestValue extends FloatingNode { 90 private static final NodeClass<TestValue> TYPE = NodeClass.create(TestValue.class); 91 92 @Input(Guard) GuardingNode guard; 93 94 protected TestValue(ValueNode guard) { 95 super(TYPE, StampFactory.forKind(JavaKind.Int)); 96 this.guard = (GuardingNode) guard; 97 } 98 99 @NodeIntrinsic 100 public static native int value(Guard guard); 101 } 102 103 private static class TestMethod { 104 105 public static int test() { 106 return 42; 107 } 108 } 109 110 @ClassSubstitution(TestMethod.class) 111 private static class TestMethodSubstitution { 112 113 @MethodSubstitution 114 public static int test() { 115 Memory memory = TestMemory.memory(); 116 Guard guard = TestGuard.guard(memory); 117 return TestValue.value(guard); 118 } 119 } 120 121 @Override 122 protected GraphBuilderConfiguration editGraphBuilderConfiguration(GraphBuilderConfiguration conf) { 123 InvocationPlugins invocationPlugins = conf.getPlugins().getInvocationPlugins(); 124 BytecodeProvider replacementBytecodeProvider = getReplacements().getReplacementBytecodeProvider(); 125 Registration r = new Registration(invocationPlugins, TestMethod.class, replacementBytecodeProvider); 126 r.registerMethodSubstitution(TestMethodSubstitution.class, "test"); 127 return super.editGraphBuilderConfiguration(conf); 128 } 129 130 public static int callTest() { 131 return TestMethod.test(); 132 } 133 134 @Override 135 protected Plugins getDefaultGraphBuilderPlugins() { 136 Plugins ret = super.getDefaultGraphBuilderPlugins(); 137 // manually register generated factories, jvmci service providers don't work from unit tests 138 new PluginFactory_SubstitutionsTest().registerPlugins(ret.getInvocationPlugins(), null); 139 return ret; 140 } 141 142 @Override 143 protected boolean checkHighTierGraph(StructuredGraph graph) { 144 // Check that the graph contains the expected test nodes. 145 NodeIterable<ReturnNode> retNodes = graph.getNodes().filter(ReturnNode.class); 146 Assert.assertTrue("expected exactly one ReturnNode", retNodes.count() == 1); 147 ReturnNode ret = retNodes.first(); 148 149 Assert.assertThat(ret.result(), instanceOf(TestValue.class)); 150 TestValue value = (TestValue) ret.result(); 151 152 Assert.assertThat(value.guard, instanceOf(TestGuard.class)); 153 TestGuard guard = (TestGuard) value.guard; 154 155 Assert.assertThat(guard.memory, instanceOf(TestMemory.class)); 156 TestMemory memory = (TestMemory) guard.memory; 157 158 // Remove the test nodes, replacing them by the constant 42. 159 // This implicitly makes sure that the rest of the graph is valid. 160 ret.replaceFirstInput(value, graph.unique(ConstantNode.forInt(42))); 161 value.safeDelete(); 162 guard.safeDelete(); 163 graph.removeFixed(memory); 164 165 return true; 166 } 167 168 @Test 169 public void snippetTest() { 170 test("callTest"); 171 } 172 }