1 /*
   2  * Copyright (c) 2014, 2016, 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.hotspot.test;
  24 
  25 import java.util.List;
  26 import java.util.Set;
  27 
  28 import org.junit.Test;
  29 
  30 import org.graalvm.compiler.api.test.Graal;
  31 import org.graalvm.compiler.core.common.CompilationIdentifier;
  32 import org.graalvm.compiler.core.test.GraalCompilerTest;
  33 import org.graalvm.compiler.hotspot.HotSpotGraalCompiler;
  34 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  35 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  36 import org.graalvm.compiler.nodes.StructuredGraph;
  37 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  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.runtime.RuntimeProvider;
  42 
  43 import jdk.vm.ci.hotspot.HotSpotVMConfigStore;
  44 import jdk.vm.ci.hotspot.VMIntrinsicMethod;
  45 import jdk.vm.ci.meta.ResolvedJavaMethod;
  46 import jdk.vm.ci.runtime.JVMCI;
  47 
  48 /**
  49  * Exercise the compilation of intrinsic method substitutions.
  50  */
  51 public class TestIntrinsicCompiles extends GraalCompilerTest {
  52 
  53     private static boolean match(ResolvedJavaMethod method, VMIntrinsicMethod intrinsic) {
  54         if (intrinsic.name.equals(method.getName())) {
  55             if (intrinsic.descriptor.equals(method.getSignature().toMethodDescriptor())) {
  56                 String declaringClass = method.getDeclaringClass().toClassName().replace('.', '/');
  57                 if (declaringClass.equals(intrinsic.declaringClass)) {
  58                     return true;
  59                 }
  60             }
  61         }
  62         return false;
  63     }
  64 
  65     private static ResolvedJavaMethod findMethod(Set<ResolvedJavaMethod> methods, VMIntrinsicMethod intrinsic) {
  66         for (ResolvedJavaMethod method : methods) {
  67             if (match(method, intrinsic)) {
  68                 return method;
  69             }
  70         }
  71         return null;
  72     }
  73 
  74     @Test
  75     @SuppressWarnings("try")
  76     public void test() {
  77         HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) JVMCI.getRuntime().getCompiler();
  78         HotSpotGraalRuntimeProvider rt = (HotSpotGraalRuntimeProvider) Graal.getRequiredCapability(RuntimeProvider.class);
  79         HotSpotProviders providers = rt.getHostBackend().getProviders();
  80         Plugins graphBuilderPlugins = providers.getGraphBuilderPlugins();
  81         InvocationPlugins invocationPlugins = graphBuilderPlugins.getInvocationPlugins();
  82 
  83         Set<ResolvedJavaMethod> pluginMethods = invocationPlugins.getMethods();
  84         HotSpotVMConfigStore store = rt.getVMConfig().getStore();
  85         List<VMIntrinsicMethod> intrinsics = store.getIntrinsics();
  86         for (VMIntrinsicMethod intrinsic : intrinsics) {
  87             ResolvedJavaMethod method = findMethod(pluginMethods, intrinsic);
  88             if (method != null) {
  89                 InvocationPlugin plugin = invocationPlugins.lookupInvocation(method);
  90                 if (plugin instanceof MethodSubstitutionPlugin && !method.isNative()) {
  91                     StructuredGraph graph = compiler.getIntrinsicGraph(method, providers, CompilationIdentifier.INVALID_COMPILATION_ID);
  92                     getCode(method, graph);
  93                 }
  94             }
  95         }
  96     }
  97 }