001/*
002 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.meta;
024
025import jdk.internal.jvmci.hotspot.*;
026import jdk.internal.jvmci.meta.*;
027
028import com.oracle.graal.compiler.common.*;
029import com.oracle.graal.graph.*;
030import com.oracle.graal.graph.iterators.*;
031import com.oracle.graal.graphbuilderconf.*;
032import com.oracle.graal.hotspot.phases.*;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.nodes.type.*;
035import com.oracle.graal.replacements.StandardGraphBuilderPlugins.BoxPlugin;
036import com.oracle.graal.replacements.nodes.*;
037
038/**
039 * Extension of {@link InvocationPlugins} that disables plugins based on runtime configuration.
040 */
041final class HotSpotInvocationPlugins extends InvocationPlugins {
042    final HotSpotVMConfig config;
043
044    public HotSpotInvocationPlugins(HotSpotVMConfig config, MetaAccessProvider metaAccess) {
045        super(metaAccess);
046        this.config = config;
047    }
048
049    @Override
050    public void register(InvocationPlugin plugin, Class<?> declaringClass, String name, Class<?>... argumentTypes) {
051        if (!config.usePopCountInstruction) {
052            if (name.equals("bitCount")) {
053                assert declaringClass.equals(Integer.class) || declaringClass.equals(Long.class);
054                return;
055            }
056        }
057        if (config.useHeapProfiler) {
058            if (plugin instanceof BoxPlugin) {
059                // The heap profiler wants to see all allocations related to boxing
060                return;
061            }
062        }
063
064        super.register(plugin, declaringClass, name, argumentTypes);
065    }
066
067    @Override
068    public void checkNewNodes(GraphBuilderContext b, InvocationPlugin plugin, NodeIterable<Node> newNodes) {
069        for (Node node : newNodes) {
070            if (node instanceof MacroNode) {
071                // MacroNode based plugins can only be used for inlining since they
072                // require a valid bci should they need to replace themselves with
073                // an InvokeNode during lowering.
074                assert plugin.inlineOnly() : String.format("plugin that creates a %s (%s) must return true for inlineOnly(): %s", MacroNode.class.getSimpleName(), node, plugin);
075            }
076        }
077        if (GraalOptions.ImmutableCode.getValue()) {
078            for (Node node : newNodes) {
079                if (node.hasUsages() && node instanceof ConstantNode) {
080                    ConstantNode c = (ConstantNode) node;
081                    if (c.getKind() == Kind.Object && AheadOfTimeVerificationPhase.isIllegalObjectConstant(c)) {
082                        if (isClass(c)) {
083                            // This will be handled later by LoadJavaMirrorWithKlassPhase
084                        } else {
085                            // Tolerate uses in unused FrameStates
086                            if (node.usages().filter((n) -> !(n instanceof FrameState) || n.hasUsages()).isNotEmpty()) {
087                                throw new AssertionError("illegal constant node in AOT: " + node);
088                            }
089                        }
090                    }
091                }
092            }
093        }
094        super.checkNewNodes(b, plugin, newNodes);
095    }
096
097    private static boolean isClass(ConstantNode node) {
098        ResolvedJavaType typeOrNull = StampTool.typeOrNull(node);
099        return typeOrNull != null && "Ljava/lang/Class;".equals(typeOrNull.getName());
100    }
101}