1 /*
   2  * Copyright (c) 2015, 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.meta;
  24 
  25 import java.lang.reflect.Type;
  26 
  27 import org.graalvm.compiler.core.common.GraalOptions;
  28 import org.graalvm.compiler.graph.Node;
  29 import org.graalvm.compiler.graph.iterators.NodeIterable;
  30 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  31 import org.graalvm.compiler.hotspot.phases.AheadOfTimeVerificationPhase;
  32 import org.graalvm.compiler.nodes.ConstantNode;
  33 import org.graalvm.compiler.nodes.FrameState;
  34 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  35 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  36 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  37 import org.graalvm.compiler.nodes.type.StampTool;
  38 import org.graalvm.compiler.replacements.nodes.MacroNode;
  39 
  40 import jdk.vm.ci.meta.JavaKind;
  41 import jdk.vm.ci.meta.MetaAccessProvider;
  42 import jdk.vm.ci.meta.ResolvedJavaType;
  43 
  44 /**
  45  * Extension of {@link InvocationPlugins} that disables plugins based on runtime configuration.
  46  */
  47 final class HotSpotInvocationPlugins extends InvocationPlugins {
  48     final GraalHotSpotVMConfig config;
  49 
  50     HotSpotInvocationPlugins(GraalHotSpotVMConfig config, MetaAccessProvider metaAccess) {
  51         super(metaAccess);
  52         this.config = config;
  53     }
  54 
  55     @Override
  56     public void register(InvocationPlugin plugin, Type declaringClass, String name, Type... argumentTypes) {
  57         if (!config.usePopCountInstruction) {
  58             if (name.equals("bitCount")) {
  59                 assert declaringClass.equals(Integer.class) || declaringClass.equals(Long.class);
  60                 return;
  61             }
  62         }
  63         super.register(plugin, declaringClass, name, argumentTypes);
  64     }
  65 
  66     @Override
  67     public void checkNewNodes(GraphBuilderContext b, InvocationPlugin plugin, NodeIterable<Node> newNodes) {
  68         for (Node node : newNodes) {
  69             if (node instanceof MacroNode) {
  70                 // MacroNode based plugins can only be used for inlining since they
  71                 // require a valid bci should they need to replace themselves with
  72                 // an InvokeNode during lowering.
  73                 assert plugin.inlineOnly() : String.format("plugin that creates a %s (%s) must return true for inlineOnly(): %s", MacroNode.class.getSimpleName(), node, plugin);
  74             }
  75         }
  76         if (GraalOptions.ImmutableCode.getValue()) {
  77             for (Node node : newNodes) {
  78                 if (node.hasUsages() && node instanceof ConstantNode) {
  79                     ConstantNode c = (ConstantNode) node;
  80                     if (c.getStackKind() == JavaKind.Object && AheadOfTimeVerificationPhase.isIllegalObjectConstant(c)) {
  81                         if (isClass(c)) {
  82                             // This will be handled later by LoadJavaMirrorWithKlassPhase
  83                         } else {
  84                             // Tolerate uses in unused FrameStates
  85                             if (node.usages().filter((n) -> !(n instanceof FrameState) || n.hasUsages()).isNotEmpty()) {
  86                                 throw new AssertionError("illegal constant node in AOT: " + node);
  87                             }
  88                         }
  89                     }
  90                 }
  91             }
  92         }
  93         super.checkNewNodes(b, plugin, newNodes);
  94     }
  95 
  96     private static boolean isClass(ConstantNode node) {
  97         ResolvedJavaType type = StampTool.typeOrNull(node);
  98         return type != null && "Ljava/lang/Class;".equals(type.getName());
  99     }
 100 }