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