1 /* 2 * Copyright (c) 2019, 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.replacements.test; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 import jdk.internal.vm.compiler.collections.MapCursor; 31 import org.graalvm.compiler.api.test.Graal; 32 import org.graalvm.compiler.core.common.CompilationIdentifier; 33 import org.graalvm.compiler.core.common.GraalOptions; 34 import org.graalvm.compiler.core.target.Backend; 35 import org.graalvm.compiler.core.test.GraalCompilerTest; 36 import org.graalvm.compiler.debug.DebugContext; 37 import org.graalvm.compiler.nodes.StructuredGraph; 38 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin; 39 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins; 40 import org.graalvm.compiler.nodes.graphbuilderconf.MethodSubstitutionPlugin; 41 import org.graalvm.compiler.options.OptionValues; 42 import org.graalvm.compiler.phases.util.Providers; 43 import org.graalvm.compiler.runtime.RuntimeProvider; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.junit.runners.Parameterized; 47 48 import jdk.vm.ci.meta.MetaAccessProvider; 49 import jdk.vm.ci.meta.ResolvedJavaMethod; 50 import jdk.vm.ci.meta.ResolvedJavaType; 51 52 /** 53 * Exercise 54 * {@link org.graalvm.compiler.nodes.spi.Replacements#getIntrinsicGraph(ResolvedJavaMethod, CompilationIdentifier, DebugContext)} 55 * with regular method substitutions and encoded graphs. 56 */ 57 @RunWith(Parameterized.class) 58 public class RootMethodSubstitutionTest extends GraalCompilerTest { 59 60 public RootMethodSubstitutionTest(ResolvedJavaMethod method) { 61 this.method = method; 62 } 63 64 @Parameterized.Parameters(name = "{0}") 65 public static List<Object[]> data() { 66 ArrayList<Object[]> ret = new ArrayList<>(); 67 68 Backend backend = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend(); 69 Providers providers = backend.getProviders(); 70 71 MapCursor<String, List<InvocationPlugins.Binding>> cursor = providers.getReplacements().getGraphBuilderPlugins().getInvocationPlugins().getBindings(true).getEntries(); 72 MetaAccessProvider metaAccess = providers.getMetaAccess(); 73 while (cursor.advance()) { 74 String className = cursor.getKey(); 75 ResolvedJavaType type = null; 76 try { 77 String typeName = className.substring(1, className.length() - 1).replace('/', '.'); 78 ClassLoader cl = ClassLoader.getSystemClassLoader(); 79 Class<?> clazz = Class.forName(typeName, true, cl); 80 type = metaAccess.lookupJavaType(clazz); 81 } catch (ClassNotFoundException e) { 82 continue; 83 } 84 85 for (InvocationPlugins.Binding binding : cursor.getValue()) { 86 if (binding.plugin instanceof MethodSubstitutionPlugin) { 87 ResolvedJavaMethod original = null; 88 for (ResolvedJavaMethod declared : type.getDeclaredMethods()) { 89 if (declared.getName().equals(binding.name)) { 90 if (declared.isStatic() == binding.isStatic) { 91 if (declared.getSignature().toMethodDescriptor().startsWith(binding.argumentsDescriptor)) { 92 original = declared; 93 break; 94 } 95 } 96 } 97 } 98 if (!original.isNative()) { 99 // Make sure the plugin we found hasn't been overridden. 100 InvocationPlugin plugin = providers.getReplacements().getGraphBuilderPlugins().getInvocationPlugins().lookupInvocation(original); 101 if (plugin instanceof MethodSubstitutionPlugin) { 102 ret.add(new Object[]{original}); 103 } 104 } 105 } 106 } 107 } 108 return ret; 109 } 110 111 private final ResolvedJavaMethod method; 112 113 private StructuredGraph getIntrinsicGraph(boolean useEncodedGraphs) { 114 OptionValues options = new OptionValues(getDebugContext().getOptions(), GraalOptions.UseEncodedGraphs, useEncodedGraphs); 115 DebugContext debugContext = DebugContext.create(options, getDebugContext().getDescription(), getDebugHandlersFactories()); 116 return getReplacements().getIntrinsicGraph(method, CompilationIdentifier.INVALID_COMPILATION_ID, debugContext); 117 } 118 119 StructuredGraph expectedGraph; 120 StructuredGraph actualGraph; 121 122 @Override 123 protected boolean checkHighTierGraph(StructuredGraph graph) { 124 // Capture the graphs after high tier 125 if (expectedGraph == null) { 126 expectedGraph = (StructuredGraph) graph.copy(graph.getDebug()); 127 } else { 128 assert actualGraph == null; 129 actualGraph = (StructuredGraph) graph.copy(graph.getDebug()); 130 } 131 return super.checkHighTierGraph(graph); 132 } 133 134 @Test 135 public void test() { 136 StructuredGraph regularGraph = getIntrinsicGraph(false); 137 assertTrue(regularGraph != null, "must produce a graph"); 138 getCode(method, regularGraph); 139 140 StructuredGraph encodedGraph = getIntrinsicGraph(true); 141 assertTrue(encodedGraph != null, "must produce a graph"); 142 getCode(method, encodedGraph); 143 144 // Compare the high tier graphs since the final graph might have scheduler 145 // differences because of different usage ordering. 146 assertEquals(expectedGraph, actualGraph, true, false); 147 } 148 149 }