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