1 /*
   2  * Copyright (c) 2017, 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.function.IntPredicate;
  26 import java.lang.invoke.MethodHandle;
  27 import java.lang.invoke.MethodHandles;
  28 import java.lang.invoke.MethodType;
  29 import jdk.vm.ci.meta.ResolvedJavaMethod;
  30 import java.security.PrivilegedAction;
  31 import org.graalvm.compiler.core.common.GraalOptions;
  32 import org.graalvm.compiler.hotspot.meta.HotSpotClassInitializationPlugin;
  33 import org.graalvm.compiler.hotspot.meta.HotSpotInvokeDynamicPlugin;
  34 import org.graalvm.compiler.hotspot.nodes.aot.ResolveDynamicConstantNode;
  35 import org.graalvm.compiler.hotspot.nodes.aot.ResolveDynamicStubCall;
  36 import org.graalvm.compiler.nodes.StructuredGraph;
  37 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  38 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  39 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  40 import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin.InlineInfo;
  41 import org.graalvm.compiler.nodes.ValueNode;
  42 import org.graalvm.compiler.nodes.spi.LoweringTool;
  43 import org.graalvm.compiler.options.OptionValues;
  44 import org.graalvm.compiler.phases.OptimisticOptimizations;
  45 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  46 import org.graalvm.compiler.phases.common.LoweringPhase;
  47 import org.graalvm.compiler.phases.common.FrameStateAssignmentPhase;
  48 import org.graalvm.compiler.phases.common.GuardLoweringPhase;
  49 import org.graalvm.compiler.phases.tiers.PhaseContext;
  50 import org.graalvm.compiler.phases.tiers.MidTierContext;
  51 import org.junit.Assert;
  52 import org.junit.Test;
  53 
  54 public class HotSpotInvokeDynamicPluginTest extends HotSpotGraalCompilerTest {
  55     @Override
  56     protected Plugins getDefaultGraphBuilderPlugins() {
  57         Plugins plugins = super.getDefaultGraphBuilderPlugins();
  58         plugins.setClassInitializationPlugin(new HotSpotClassInitializationPlugin());
  59         plugins.setInvokeDynamicPlugin(new HotSpotInvokeDynamicPlugin() {
  60             @Override
  61             public boolean isResolvedDynamicInvoke(GraphBuilderContext builder, int index, int opcode) {
  62                 // Allow invokedynamic testing with older JVMCI
  63                 ResolvedJavaMethod m = builder.getMethod();
  64                 if (m.getName().startsWith("invokeDynamic") && m.getDeclaringClass().getName().equals("Lorg/graalvm/compiler/hotspot/test/HotSpotInvokeDynamicPluginTest;")) {
  65                     return false;
  66                 }
  67                 return super.isResolvedDynamicInvoke(builder, index, opcode);
  68             }
  69 
  70             @Override
  71             public boolean supportsDynamicInvoke(GraphBuilderContext builder, int index, int opcode) {
  72                 // Allow invokehandle testing with older JVMCI
  73                 ResolvedJavaMethod m = builder.getMethod();
  74                 if (m.getName().startsWith("invokeHandle") && m.getDeclaringClass().getName().equals("Lorg/graalvm/compiler/hotspot/test/HotSpotInvokeDynamicPluginTest;")) {
  75                     return true;
  76                 }
  77                 return super.supportsDynamicInvoke(builder, index, opcode);
  78             }
  79         });
  80         return plugins;
  81     }
  82 
  83     @Override
  84     protected InlineInfo bytecodeParserShouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
  85         return InlineInfo.DO_NOT_INLINE_NO_EXCEPTION;
  86     }
  87 
  88     private void test(String name, int expectedResolves, int expectedStubCalls) {
  89         StructuredGraph graph = parseEager(name, AllowAssumptions.NO, new OptionValues(getInitialOptions(), GraalOptions.GeneratePIC, true));
  90         MidTierContext midTierContext = new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, graph.getProfilingInfo());
  91 
  92         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
  93         Assert.assertEquals(expectedResolves, graph.getNodes().filter(ResolveDynamicConstantNode.class).count());
  94         Assert.assertEquals(0, graph.getNodes().filter(ResolveDynamicStubCall.class).count());
  95         PhaseContext context = new PhaseContext(getProviders());
  96         new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
  97         new GuardLoweringPhase().apply(graph, midTierContext);
  98         new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, context);
  99         new FrameStateAssignmentPhase().apply(graph);
 100         new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.LOW_TIER).apply(graph, context);
 101         Assert.assertEquals(0, graph.getNodes().filter(ResolveDynamicConstantNode.class).count());
 102         Assert.assertEquals(expectedStubCalls, graph.getNodes().filter(ResolveDynamicStubCall.class).count());
 103     }
 104 
 105     public static IntPredicate invokeDynamic1() {
 106         IntPredicate i = (v) -> v > 1;
 107         return i;
 108     }
 109 
 110     public static PrivilegedAction<Integer> invokeDynamic2(String s) {
 111         return s::length;
 112     }
 113 
 114     static final MethodHandle objToStringMH;
 115 
 116     static {
 117         MethodHandle mh = null;
 118         try {
 119             mh = MethodHandles.lookup().findVirtual(Object.class, "toString", MethodType.methodType(String.class));
 120         } catch (Exception e) {
 121         }
 122         objToStringMH = mh;
 123     }
 124 
 125     // invokehandle
 126     public static String invokeHandle1(Object o) throws Throwable {
 127         return (String) objToStringMH.invokeExact(o);
 128     }
 129 
 130     @Test
 131     public void test1() {
 132         test("invokeDynamic1", 1, 1);
 133     }
 134 
 135     @Test
 136     public void test2() {
 137         test("invokeDynamic2", 1, 1);
 138     }
 139 
 140     @Test
 141     public void test3() {
 142         test("invokeHandle1", 1, 1);
 143     }
 144 
 145 }