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.nodes.graphbuilderconf;
  26 
  27 import org.graalvm.compiler.bytecode.BytecodeProvider;
  28 import org.graalvm.compiler.nodes.Invoke;
  29 import org.graalvm.compiler.nodes.ValueNode;
  30 
  31 import jdk.vm.ci.meta.ResolvedJavaMethod;
  32 
  33 /**
  34  * Plugin for specifying what is inlined during graph parsing. This plugin is also notified
  35  * {@link #notifyBeforeInline before} and {@link #notifyAfterInline} the inlining, as well as of
  36  * {@link #notifyNotInlined non-inlined} invocations (i.e., those for which an {@link Invoke} node
  37  * is created).
  38  */
  39 public interface InlineInvokePlugin extends GraphBuilderPlugin {
  40 
  41     /**
  42      * Result of a {@link #shouldInlineInvoke inlining decision}.
  43      */
  44     final class InlineInfo {
  45 
  46         /**
  47          * Denotes a call site that must not be inlined and should be implemented by a node that
  48          * does not speculate on the call not raising an exception.
  49          */
  50         public static final InlineInfo DO_NOT_INLINE_WITH_EXCEPTION = new InlineInfo();
  51 
  52         /**
  53          * Denotes a call site must not be inlined and can be implemented by a node that speculates
  54          * the call will not throw an exception.
  55          */
  56         public static final InlineInfo DO_NOT_INLINE_NO_EXCEPTION = new InlineInfo();
  57 
  58         /**
  59          * Denotes a call site must not be inlined and the execution should be transferred to
  60          * interpreter in case of an exception.
  61          */
  62         public static final InlineInfo DO_NOT_INLINE_DEOPTIMIZE_ON_EXCEPTION = new InlineInfo();
  63 
  64         private final ResolvedJavaMethod methodToInline;
  65         private final MethodSubstitutionPlugin plugin;
  66         private final BytecodeProvider intrinsicBytecodeProvider;
  67 
  68         public static InlineInfo createStandardInlineInfo(ResolvedJavaMethod methodToInline) {
  69             return new InlineInfo(methodToInline, null, null);
  70         }
  71 
  72         public static InlineInfo createIntrinsicInlineInfo(ResolvedJavaMethod methodToInline, BytecodeProvider intrinsicBytecodeProvider) {
  73             return new InlineInfo(methodToInline, null, intrinsicBytecodeProvider);
  74         }
  75 
  76         public static InlineInfo createMethodSubstitutionInlineInfo(ResolvedJavaMethod methodToInline, MethodSubstitutionPlugin plugin) {
  77             return new InlineInfo(methodToInline, plugin, plugin.getBytecodeProvider());
  78         }
  79 
  80         private InlineInfo() {
  81             this(null, null, null);
  82         }
  83 
  84         private InlineInfo(ResolvedJavaMethod methodToInline, MethodSubstitutionPlugin plugin, BytecodeProvider intrinsicBytecodeProvider) {
  85             this.methodToInline = methodToInline;
  86             this.plugin = plugin;
  87             this.intrinsicBytecodeProvider = intrinsicBytecodeProvider;
  88         }
  89 
  90         /**
  91          * Returns the method to be inlined, or {@code null} if the call site must not be inlined.
  92          */
  93         public ResolvedJavaMethod getMethodToInline() {
  94             return methodToInline;
  95         }
  96 
  97         public boolean allowsInlining() {
  98             return methodToInline != null;
  99         }
 100 
 101         /**
 102          * Gets the provider of bytecode to be parsed for {@link #getMethodToInline()} if is is an
 103          * intrinsic for the original method (i.e., the {@code method} passed to
 104          * {@link InlineInvokePlugin#shouldInlineInvoke}). A {@code null} return value indicates
 105          * that this is not an intrinsic inlining.
 106          */
 107         public BytecodeProvider getIntrinsicBytecodeProvider() {
 108             return intrinsicBytecodeProvider;
 109         }
 110 
 111         public boolean isSubstitution() {
 112             return plugin != null;
 113         }
 114 
 115         public MethodSubstitutionPlugin getPlugin() {
 116             return plugin;
 117         }
 118     }
 119 
 120     /**
 121      * Determines whether a call to a given method is to be inlined. The return value is a
 122      * tri-state:
 123      * <p>
 124      * Non-null return value with a non-null {@link InlineInfo#getMethodToInline method}: That
 125      * {@link InlineInfo#getMethodToInline method} is inlined. Note that it can be a different
 126      * method than the one specified here as the parameter, which allows method substitutions.
 127      * <p>
 128      * Non-null return value with a null {@link InlineInfo#getMethodToInline method}, e.g.,
 129      * {@link InlineInfo#DO_NOT_INLINE_WITH_EXCEPTION}: The method is not inlined, and other plugins
 130      * with a lower priority cannot overwrite this decision.
 131      * <p>
 132      * Null return value: This plugin made no decision, other plugins with a lower priority are
 133      * asked.
 134      *
 135      * @param b the context
 136      * @param method the target method of an invoke
 137      * @param args the arguments to the invoke
 138      */
 139     default InlineInfo shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
 140         return null;
 141     }
 142 
 143     /**
 144      * Notification that a method is about to be inlined.
 145      *
 146      * @param methodToInline the inlined method
 147      */
 148     default void notifyBeforeInline(ResolvedJavaMethod methodToInline) {
 149     }
 150 
 151     /**
 152      * Notification that a method was inlined.
 153      *
 154      * @param methodToInline the inlined method
 155      */
 156     default void notifyAfterInline(ResolvedJavaMethod methodToInline) {
 157     }
 158 
 159     /**
 160      * Notifies this plugin of the {@link Invoke} node created for a method that was not inlined per
 161      * {@link #shouldInlineInvoke}.
 162      *
 163      * @param b the context
 164      * @param method the method that was not inlined
 165      * @param invoke the invoke node created for the call to {@code method}
 166      */
 167     default void notifyNotInlined(GraphBuilderContext b, ResolvedJavaMethod method, Invoke invoke) {
 168     }
 169 }