1 /*
   2  * Copyright (c) 2013, 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.nodes.spi;
  24 
  25 import org.graalvm.compiler.api.replacements.MethodSubstitution;
  26 import org.graalvm.compiler.api.replacements.SnippetTemplateCache;
  27 import org.graalvm.compiler.bytecode.BytecodeProvider;
  28 import org.graalvm.compiler.nodes.StructuredGraph;
  29 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  30 
  31 import jdk.vm.ci.meta.ResolvedJavaMethod;
  32 
  33 /**
  34  * Interface for managing replacements.
  35  */
  36 public interface Replacements {
  37 
  38     /**
  39      * Gets the snippet graph derived from a given method.
  40      *
  41      * @param args arguments to the snippet if available, otherwise {@code null}
  42      * @return the snippet graph, if any, that is derived from {@code method}
  43      */
  44     StructuredGraph getSnippet(ResolvedJavaMethod method, Object[] args);
  45 
  46     /**
  47      * Gets the snippet graph derived from a given method.
  48      *
  49      * @param recursiveEntry if the snippet contains a call to this method, it's considered as
  50      *            recursive call and won't be processed for {@linkplain MethodSubstitution
  51      *            substitutions}.
  52      * @param args arguments to the snippet if available, otherwise {@code null}
  53      * @return the snippet graph, if any, that is derived from {@code method}
  54      */
  55     StructuredGraph getSnippet(ResolvedJavaMethod method, ResolvedJavaMethod recursiveEntry, Object[] args);
  56 
  57     /**
  58      * Registers a method as snippet.
  59      */
  60     void registerSnippet(ResolvedJavaMethod method);
  61 
  62     /**
  63      * Gets a graph that is a substitution for a given method.
  64      *
  65      * @param invokeBci the call site BCI if this request is made for inlining a substitute
  66      *            otherwise {@code -1}
  67      * @return the graph, if any, that is a substitution for {@code method}
  68      */
  69     StructuredGraph getSubstitution(ResolvedJavaMethod method, int invokeBci);
  70 
  71     /**
  72      * Gets a method that is a substitution for a given method.
  73      *
  74      * @return the method, if any, whose bytecode are a substitution for {@code method}
  75      */
  76     ResolvedJavaMethod getSubstitutionMethod(ResolvedJavaMethod method);
  77 
  78     /**
  79      * Determines if there may be a {@linkplain #getSubstitution(ResolvedJavaMethod, int)
  80      * substitution graph} for a given method.
  81      *
  82      * A call to {@link #getSubstitution} may still return {@code null} for {@code method} and
  83      * {@code invokeBci}. A substitution may be based on an {@link InvocationPlugin} that returns
  84      * {@code false} for {@link InvocationPlugin#execute} making it impossible to create a
  85      * substitute graph.
  86      *
  87      * @param invokeBci the call site BCI if this request is made for inlining a substitute
  88      *            otherwise {@code -1}
  89      * @return true iff there may be a substitution graph available for {@code method}
  90      */
  91     boolean hasSubstitution(ResolvedJavaMethod method, int invokeBci);
  92 
  93     /**
  94      * Gets the provider for accessing the bytecode of a substitution method.
  95      */
  96     BytecodeProvider getReplacementBytecodeProvider();
  97 
  98     /**
  99      * Register snippet templates.
 100      */
 101     void registerSnippetTemplateCache(SnippetTemplateCache snippetTemplates);
 102 
 103     /**
 104      * Get snippet templates that were registered with
 105      * {@link Replacements#registerSnippetTemplateCache(SnippetTemplateCache)}.
 106      */
 107     <T extends SnippetTemplateCache> T getSnippetTemplateCache(Class<T> templatesClass);
 108 }