1 /*
   2  * Copyright (c) 2013, 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.nodes.spi;
  26 
  27 import org.graalvm.compiler.api.replacements.MethodSubstitution;
  28 import org.graalvm.compiler.api.replacements.SnippetTemplateCache;
  29 import org.graalvm.compiler.bytecode.Bytecode;
  30 import org.graalvm.compiler.bytecode.BytecodeProvider;
  31 import org.graalvm.compiler.core.common.CompilationIdentifier;
  32 import org.graalvm.compiler.debug.DebugContext;
  33 import org.graalvm.compiler.graph.NodeSourcePosition;
  34 import org.graalvm.compiler.nodes.StructuredGraph;
  35 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
  36 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderPlugin;
  37 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  38 import org.graalvm.compiler.options.OptionValues;
  39 
  40 import jdk.vm.ci.meta.ResolvedJavaMethod;
  41 
  42 /**
  43  * Interface for managing replacements.
  44  */
  45 public interface Replacements {
  46 
  47     OptionValues getOptions();
  48 
  49     /**
  50      * Gets the object managing the various graph builder plugins used by this object when parsing
  51      * bytecode into a graph.
  52      */
  53     GraphBuilderConfiguration.Plugins getGraphBuilderPlugins();
  54 
  55     /**
  56      * Gets the plugin type that intrinsifies calls to {@code method}.
  57      */
  58     Class<? extends GraphBuilderPlugin> getIntrinsifyingPlugin(ResolvedJavaMethod method);
  59 
  60     /**
  61      * Gets the snippet graph derived from a given method.
  62      *
  63      * @param args arguments to the snippet if available, otherwise {@code null}
  64      * @param trackNodeSourcePosition
  65      * @return the snippet graph, if any, that is derived from {@code method}
  66      */
  67     default StructuredGraph getSnippet(ResolvedJavaMethod method, Object[] args, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition) {
  68         return getSnippet(method, null, args, trackNodeSourcePosition, replaceePosition);
  69     }
  70 
  71     /**
  72      * Gets the snippet graph derived from a given method.
  73      *
  74      * @param recursiveEntry if the snippet contains a call to this method, it's considered as
  75      *            recursive call and won't be processed for {@linkplain MethodSubstitution
  76      *            substitutions}.
  77      * @param args arguments to the snippet if available, otherwise {@code null}
  78      * @param trackNodeSourcePosition
  79      * @return the snippet graph, if any, that is derived from {@code method}
  80      */
  81     StructuredGraph getSnippet(ResolvedJavaMethod method, ResolvedJavaMethod recursiveEntry, Object[] args, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition);
  82 
  83     /**
  84      * Registers a method as snippet.
  85      */
  86     void registerSnippet(ResolvedJavaMethod method, ResolvedJavaMethod original, Object receiver, boolean trackNodeSourcePosition);
  87 
  88     /**
  89      * Gets a graph that is a substitution for a given method.
  90      *
  91      * @param invokeBci the call site BCI if this request is made for inlining a substitute
  92      *            otherwise {@code -1}
  93      * @param trackNodeSourcePosition
  94      * @return the graph, if any, that is a substitution for {@code method}
  95      */
  96     StructuredGraph getSubstitution(ResolvedJavaMethod method, int invokeBci, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition);
  97 
  98     /**
  99      * Gets the substitute bytecode for a given method.
 100      *
 101      * @return the bytecode to substitute for {@code method} or {@code null} if there is no
 102      *         substitute bytecode for {@code method}
 103      */
 104     Bytecode getSubstitutionBytecode(ResolvedJavaMethod method);
 105 
 106     /**
 107      * Gets a graph produced from the intrinsic for a given method that can be compiled and
 108      * installed for the method.
 109      *
 110      * @param method
 111      * @param compilationId
 112      * @param debug
 113      * @return an intrinsic graph that can be compiled and installed for {@code method} or null
 114      */
 115     StructuredGraph getIntrinsicGraph(ResolvedJavaMethod method, CompilationIdentifier compilationId, DebugContext debug);
 116 
 117     /**
 118      * Determines if there may be a
 119      * {@linkplain #getSubstitution(ResolvedJavaMethod, int, boolean, NodeSourcePosition)
 120      * substitution graph} for a given method.
 121      *
 122      * A call to {@link #getSubstitution} may still return {@code null} for {@code method} and
 123      * {@code invokeBci}. A substitution may be based on an {@link InvocationPlugin} that returns
 124      * {@code false} for {@link InvocationPlugin#execute} making it impossible to create a
 125      * substitute graph.
 126      *
 127      * @param invokeBci the call site BCI if this request is made for inlining a substitute
 128      *            otherwise {@code -1}
 129      * @return true iff there may be a substitution graph available for {@code method}
 130      */
 131     boolean hasSubstitution(ResolvedJavaMethod method, int invokeBci);
 132 
 133     /**
 134      * Gets the provider for accessing the bytecode of a substitution method if no other provider is
 135      * associated with the substitution method.
 136      */
 137     BytecodeProvider getDefaultReplacementBytecodeProvider();
 138 
 139     /**
 140      * Register snippet templates.
 141      */
 142     void registerSnippetTemplateCache(SnippetTemplateCache snippetTemplates);
 143 
 144     /**
 145      * Get snippet templates that were registered with
 146      * {@link Replacements#registerSnippetTemplateCache(SnippetTemplateCache)}.
 147      */
 148     <T extends SnippetTemplateCache> T getSnippetTemplateCache(Class<T> templatesClass);
 149 
 150     /**
 151      * Notifies this method that no further snippets will be registered via {@link #registerSnippet}
 152      * or {@link #registerSnippetTemplateCache}.
 153      *
 154      * This is a hook for an implementation to check for or forbid late registration.
 155      */
 156     default void closeSnippetRegistration() {
 157     }
 158 }