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         private final ResolvedJavaMethod methodToInline;
  57         private final BytecodeProvider intrinsicBytecodeProvider;
  58 
  59         public static InlineInfo createStandardInlineInfo(ResolvedJavaMethod methodToInline) {
  60             return new InlineInfo(methodToInline, null);
  61         }
  62 
  63         public static InlineInfo createIntrinsicInlineInfo(ResolvedJavaMethod methodToInline, BytecodeProvider intrinsicBytecodeProvider) {
  64             return new InlineInfo(methodToInline, intrinsicBytecodeProvider);
  65         }
  66 
  67         private InlineInfo(ResolvedJavaMethod methodToInline, BytecodeProvider intrinsicBytecodeProvider) {
  68             this.methodToInline = methodToInline;
  69             this.intrinsicBytecodeProvider = intrinsicBytecodeProvider;
  70         }
  71 
  72         /**
  73          * Returns the method to be inlined, or {@code null} if the call site must not be inlined.
  74          */
  75         public ResolvedJavaMethod getMethodToInline() {
  76             return methodToInline;
  77         }
  78 
  79         /**
  80          * Gets the provider of bytecode to be parsed for {@link #getMethodToInline()} if is is an
  81          * intrinsic for the original method (i.e., the {@code method} passed to
  82          * {@link InlineInvokePlugin#shouldInlineInvoke}). A {@code null} return value indicates
  83          * that this is not an intrinsic inlining.
  84          */
  85         public BytecodeProvider getIntrinsicBytecodeProvider() {
  86             return intrinsicBytecodeProvider;
  87         }
  88     }
  89 
  90     /**
  91      * Determines whether a call to a given method is to be inlined. The return value is a
  92      * tri-state:
  93      * <p>
  94      * Non-null return value with a non-null {@link InlineInfo#getMethodToInline method}: That
  95      * {@link InlineInfo#getMethodToInline method} is inlined. Note that it can be a different
  96      * method than the one specified here as the parameter, which allows method substitutions.
  97      * <p>
  98      * Non-null return value with a null {@link InlineInfo#getMethodToInline method}, e.g.,
  99      * {@link InlineInfo#DO_NOT_INLINE_WITH_EXCEPTION}: The method is not inlined, and other plugins
 100      * with a lower priority cannot overwrite this decision.
 101      * <p>
 102      * Null return value: This plugin made no decision, other plugins with a lower priority are
 103      * asked.
 104      *
 105      * @param b the context
 106      * @param method the target method of an invoke
 107      * @param args the arguments to the invoke
 108      */
 109     default InlineInfo shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
 110         return null;
 111     }
 112 
 113     /**
 114      * Notification that a method is about to be inlined.
 115      *
 116      * @param methodToInline the inlined method
 117      */
 118     default void notifyBeforeInline(ResolvedJavaMethod methodToInline) {
 119     }
 120 
 121     /**
 122      * Notification that a method was inlined.
 123      *
 124      * @param methodToInline the inlined method
 125      */
 126     default void notifyAfterInline(ResolvedJavaMethod methodToInline) {
 127     }
 128 
 129     /**
 130      * Notifies this plugin of the {@link Invoke} node created for a method that was not inlined per
 131      * {@link #shouldInlineInvoke}.
 132      *
 133      * @param b the context
 134      * @param method the method that was not inlined
 135      * @param invoke the invoke node created for the call to {@code method}
 136      */
 137     default void notifyNotInlined(GraphBuilderContext b, ResolvedJavaMethod method, Invoke invoke) {
 138     }
 139 }