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