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