Search in sources :

Example 1 with OptionalInvocationPlugin

use of org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.OptionalInvocationPlugin in project graal by oracle.

the class StandardGraphBuilderPlugins method registerJMHBlackholePlugins.

private static void registerJMHBlackholePlugins(InvocationPlugins plugins, Replacements replacements) {
    // The purpose of this plugin is to help Blackhole.consume function mostly correctly even if
    // it's been inlined.
    String[] names = { "org.openjdk.jmh.infra.Blackhole", "org.openjdk.jmh.logic.BlackHole" };
    for (String name : names) {
        Registration r = new Registration(plugins, name, replacements);
        for (JavaKind kind : JavaKind.values()) {
            if ((kind.isPrimitive() && kind != JavaKind.Void) || kind == JavaKind.Object) {
                Class<?> javaClass = getJavaClass(kind);
                r.register(new OptionalInvocationPlugin("consume", Receiver.class, javaClass) {

                    @Override
                    public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver blackhole, ValueNode value) {
                        blackhole.get();
                        b.add(new BlackholeNode(value));
                        return true;
                    }

                    @Override
                    public boolean isDecorator() {
                        return true;
                    }
                });
            }
        }
        r.register(new OptionalInvocationPlugin("consume", Receiver.class, Object[].class) {

            @Override
            public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver blackhole, ValueNode value) {
                blackhole.get();
                b.add(new BlackholeNode(value));
                return true;
            }

            @Override
            public boolean isDecorator() {
                return true;
            }
        });
    }
}
Also used : BlackholeNode(org.graalvm.compiler.nodes.debug.BlackholeNode) OptionalInvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.OptionalInvocationPlugin) GraphBuilderContext(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext) Registration(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration) ValueNode(org.graalvm.compiler.nodes.ValueNode) Receiver(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) JavaKind(jdk.vm.ci.meta.JavaKind)

Example 2 with OptionalInvocationPlugin

use of org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.OptionalInvocationPlugin in project graal by oracle.

the class IntrinsifyMethodHandlesInvocationPlugin method registerInvocationPlugins.

private static void registerInvocationPlugins(InvocationPlugins plugins, Replacements replacements) {
    Registration r = new Registration(plugins, "java.lang.invoke.DirectMethodHandle", replacements);
    r.register(new RequiredInvocationPlugin("ensureInitialized", Receiver.class) {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
            /*
                 * Method handles for static methods have a guard that initializes the class (if the
                 * class was not yet initialized when the method handle was created). We emit the
                 * class initialization check manually later on when appending nodes to the target
                 * graph.
                 */
            return true;
        }
    });
    r = new Registration(plugins, "java.lang.invoke.Invokers", replacements);
    r.register(new OptionalInvocationPlugin("maybeCustomize", MethodHandle.class) {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode mh) {
            /*
                 * JDK 8 update 60 added an additional customization possibility for method handles.
                 * For all use cases that we care about, that seems to be unnecessary, so we can
                 * just do nothing.
                 */
            return true;
        }
    });
    r = new Registration(plugins, Objects.class, replacements);
    r.register(new RequiredInvocationPlugin("requireNonNull", Object.class) {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unused, ValueNode object) {
            /*
                 * Instead of inlining the method, intrinsify it to a pattern that we can easily
                 * detect when looking at the parsed graph.
                 */
            b.push(JavaKind.Object, b.addNonNullCast(object));
            return true;
        }
    });
}
Also used : OptionalInvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.OptionalInvocationPlugin) GraphBuilderContext(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext) Registration(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration) RequiredInvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.RequiredInvocationPlugin) ValueNode(org.graalvm.compiler.nodes.ValueNode) Objects(java.util.Objects) Receiver(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) MethodHandle(java.lang.invoke.MethodHandle)

Example 3 with OptionalInvocationPlugin

use of org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.OptionalInvocationPlugin in project graal by oracle.

the class StandardGraphBuilderPlugins method registerMethodHandleImplPlugins.

private static void registerMethodHandleImplPlugins(InvocationPlugins plugins, Replacements replacements) {
    Registration r = new Registration(plugins, "java.lang.invoke.MethodHandleImpl", replacements);
    // In later JDKs this no longer exists and the usage is replace by Class.cast which is
    // already an intrinsic
    r.register(new OptionalInvocationPlugin("castReference", Class.class, Object.class) {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode javaClass, ValueNode object) {
            b.genCheckcastDynamic(object, javaClass);
            return true;
        }

        @Override
        public boolean inlineOnly() {
            return true;
        }
    });
    r.register(new InlineOnlyInvocationPlugin("profileBoolean", boolean.class, int[].class) {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode result, ValueNode counters) {
            if (b.needsExplicitException()) {
                return false;
            }
            if (result.isConstant()) {
                b.push(JavaKind.Boolean, result);
                return true;
            }
            if (counters.isConstant()) {
                ValueNode newResult = result;
                int[] ctrs = ConstantReflectionUtil.loadIntArrayConstant(b.getConstantReflection(), (JavaConstant) counters.asConstant(), 2);
                if (ctrs != null && ctrs.length == 2) {
                    int falseCount = ctrs[0];
                    int trueCount = ctrs[1];
                    int totalCount = trueCount + falseCount;
                    if (totalCount == 0) {
                        b.add(new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.TransferToInterpreter));
                    } else if (falseCount == 0 || trueCount == 0) {
                        boolean expected = falseCount == 0;
                        LogicNode condition = b.add(IntegerEqualsNode.create(b.getConstantReflection(), b.getMetaAccess(), b.getOptions(), null, result, b.add(ConstantNode.forBoolean(!expected)), NodeView.DEFAULT));
                        b.append(new FixedGuardNode(condition, DeoptimizationReason.UnreachedCode, DeoptimizationAction.InvalidateReprofile, true));
                        newResult = b.add(ConstantNode.forBoolean(expected));
                    } else {
                    // We cannot use BranchProbabilityNode here since there's no guarantee
                    // the result of MethodHandleImpl.profileBoolean() is used as the
                    // test in an `if` statement (as required by BranchProbabilityNode).
                    }
                }
                b.addPush(JavaKind.Boolean, newResult);
                return true;
            }
            b.addPush(JavaKind.Boolean, new ProfileBooleanNode(b.getConstantReflection(), MacroParams.of(b, targetMethod, result, counters)));
            return true;
        }
    });
    r.register(new RequiredInlineOnlyInvocationPlugin("isCompileConstant", Object.class) {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode obj) {
            b.addPush(JavaKind.Boolean, ConstantNode.forBoolean(obj.isConstant()));
            return true;
        }
    });
}
Also used : ProfileBooleanNode(org.graalvm.compiler.replacements.nodes.ProfileBooleanNode) RequiredInlineOnlyInvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.RequiredInlineOnlyInvocationPlugin) Receiver(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver) JavaConstant(jdk.vm.ci.meta.JavaConstant) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) OptionalInvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.OptionalInvocationPlugin) GraphBuilderContext(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext) Registration(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration) RequiredInlineOnlyInvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.RequiredInlineOnlyInvocationPlugin) InlineOnlyInvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.InlineOnlyInvocationPlugin) ValueNode(org.graalvm.compiler.nodes.ValueNode) DeoptimizeNode(org.graalvm.compiler.nodes.DeoptimizeNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Aggregations

ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)3 ValueNode (org.graalvm.compiler.nodes.ValueNode)3 GraphBuilderContext (org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext)3 OptionalInvocationPlugin (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.OptionalInvocationPlugin)3 Receiver (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver)3 Registration (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration)3 MethodHandle (java.lang.invoke.MethodHandle)1 Objects (java.util.Objects)1 JavaConstant (jdk.vm.ci.meta.JavaConstant)1 JavaKind (jdk.vm.ci.meta.JavaKind)1 DeoptimizeNode (org.graalvm.compiler.nodes.DeoptimizeNode)1 FixedGuardNode (org.graalvm.compiler.nodes.FixedGuardNode)1 LogicNode (org.graalvm.compiler.nodes.LogicNode)1 BlackholeNode (org.graalvm.compiler.nodes.debug.BlackholeNode)1 InlineOnlyInvocationPlugin (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.InlineOnlyInvocationPlugin)1 RequiredInlineOnlyInvocationPlugin (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.RequiredInlineOnlyInvocationPlugin)1 RequiredInvocationPlugin (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.RequiredInvocationPlugin)1 ProfileBooleanNode (org.graalvm.compiler.replacements.nodes.ProfileBooleanNode)1