1 /*
   2  * Copyright (c) 2014, 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.hotspot.test;
  26 
  27 import static org.graalvm.compiler.core.common.CompilationIdentifier.INVALID_COMPILATION_ID;
  28 
  29 import java.util.List;
  30 
  31 import jdk.internal.vm.compiler.collections.EconomicMap;
  32 import org.graalvm.compiler.api.test.Graal;
  33 import org.graalvm.compiler.core.test.GraalCompilerTest;
  34 import org.graalvm.compiler.debug.DebugContext;
  35 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  36 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  37 import org.graalvm.compiler.nodes.StructuredGraph;
  38 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  39 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  40 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  41 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Binding;
  42 import org.graalvm.compiler.nodes.graphbuilderconf.MethodSubstitutionPlugin;
  43 import org.graalvm.compiler.options.OptionValues;
  44 import org.graalvm.compiler.runtime.RuntimeProvider;
  45 import org.junit.Test;
  46 
  47 import jdk.vm.ci.hotspot.HotSpotVMConfigStore;
  48 import jdk.vm.ci.hotspot.VMIntrinsicMethod;
  49 import jdk.vm.ci.meta.ResolvedJavaMethod;
  50 
  51 /**
  52  * Exercise the compilation of intrinsic method substitutions.
  53  */
  54 public class TestIntrinsicCompiles extends GraalCompilerTest {
  55 
  56     @Test
  57     @SuppressWarnings("try")
  58     public void test() throws ClassNotFoundException {
  59         HotSpotGraalRuntimeProvider rt = (HotSpotGraalRuntimeProvider) Graal.getRequiredCapability(RuntimeProvider.class);
  60         HotSpotProviders providers = rt.getHostBackend().getProviders();
  61         Plugins graphBuilderPlugins = providers.getGraphBuilderPlugins();
  62         InvocationPlugins invocationPlugins = graphBuilderPlugins.getInvocationPlugins();
  63 
  64         EconomicMap<String, List<Binding>> bindings = invocationPlugins.getBindings(true);
  65         HotSpotVMConfigStore store = rt.getVMConfig().getStore();
  66         List<VMIntrinsicMethod> intrinsics = store.getIntrinsics();
  67         OptionValues options = getInitialOptions();
  68         DebugContext debug = getDebugContext(options);
  69         for (VMIntrinsicMethod intrinsic : intrinsics) {
  70             InvocationPlugin plugin = CheckGraalIntrinsics.findPlugin(bindings, intrinsic);
  71             if (plugin != null) {
  72                 if (plugin instanceof MethodSubstitutionPlugin) {
  73                     ResolvedJavaMethod method = CheckGraalIntrinsics.resolveIntrinsic(getMetaAccess(), intrinsic);
  74                     if (!method.isNative()) {
  75                         StructuredGraph graph = providers.getReplacements().getIntrinsicGraph(method, INVALID_COMPILATION_ID, debug, null);
  76                         getCode(method, graph);
  77                     }
  78                 }
  79             }
  80         }
  81     }
  82 }