001/*
002 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.graphbuilderconf;
024
025import jdk.internal.jvmci.meta.*;
026
027import com.oracle.graal.nodes.*;
028
029/**
030 * Plugin for specifying what is inlined during graph parsing. This plugin is also notified
031 * {@link #notifyBeforeInline before} and {@link #notifyAfterInline} the inlining, as well as of
032 * {@link #notifyNotInlined non-inlined} invocations (i.e., those for which an {@link Invoke} node
033 * is created).
034 */
035public interface InlineInvokePlugin extends GraphBuilderPlugin {
036
037    /**
038     * Result of a {@link #shouldInlineInvoke inlining decision}.
039     */
040    public static class InlineInfo {
041
042        /**
043         * Denotes a call site that must not be inlined and should be implemented by a node that
044         * does not speculate on the call not raising an exception.
045         */
046        public static final InlineInfo DO_NOT_INLINE_WITH_EXCEPTION = new InlineInfo(null, false);
047
048        /**
049         * Denotes a call site must not be inlined and can be implemented by a node that speculates
050         * the call will not throw an exception.
051         */
052        public static final InlineInfo DO_NOT_INLINE_NO_EXCEPTION = new InlineInfo(null, false);
053
054        private final ResolvedJavaMethod methodToInline;
055        private final boolean isIntrinsic;
056
057        public InlineInfo(ResolvedJavaMethod methodToInline, boolean isIntrinsic) {
058            this.methodToInline = methodToInline;
059            this.isIntrinsic = isIntrinsic;
060        }
061
062        /**
063         * Returns the method to be inlined, or {@code null} if the call site must not be inlined.
064         */
065        public ResolvedJavaMethod getMethodToInline() {
066            return methodToInline;
067        }
068
069        /**
070         * Specifies if {@link #methodToInline} is an intrinsic for the original method (i.e., the
071         * {@code method} passed to {@link InlineInvokePlugin#shouldInlineInvoke}).
072         */
073        public boolean isIntrinsic() {
074            return isIntrinsic;
075        }
076    }
077
078    /**
079     * Determines whether a call to a given method is to be inlined. The return value is a
080     * tri-state:
081     * <p>
082     * Non-null return value with a non-null {@link InlineInfo#getMethodToInline method}: That
083     * {@link InlineInfo#getMethodToInline method} is inlined. Note that it can be a different
084     * method than the one specified here as the parameter, which allows method substitutions.
085     * <p>
086     * Non-null return value with a null {@link InlineInfo#getMethodToInline method}, e.g.,
087     * {@link InlineInfo#DO_NOT_INLINE_WITH_EXCEPTION}: The method is not inlined, and other plugins
088     * with a lower priority cannot overwrite this decision.
089     * <p>
090     * Null return value: This plugin made no decision, other plugins with a lower priority are
091     * asked.
092     *
093     * @param b the context
094     * @param method the target method of an invoke
095     * @param args the arguments to the invoke
096     * @param returnType the return type derived from {@code method}'s signature
097     */
098    default InlineInfo shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args, JavaType returnType) {
099        return null;
100    }
101
102    /**
103     * Notification that a method is about to be inlined.
104     *
105     * @param methodToInline the inlined method
106     */
107    default void notifyBeforeInline(ResolvedJavaMethod methodToInline) {
108    }
109
110    /**
111     * Notification that a method was inlined.
112     *
113     * @param methodToInline the inlined method
114     */
115    default void notifyAfterInline(ResolvedJavaMethod methodToInline) {
116    }
117
118    /**
119     * Notifies this plugin of the {@link Invoke} node created for a method that was not inlined per
120     * {@link #shouldInlineInvoke}.
121     *
122     * @param b the context
123     * @param method the method that was not inlined
124     * @param invoke the invoke node created for the call to {@code method}
125     */
126    default void notifyNotInlined(GraphBuilderContext b, ResolvedJavaMethod method, Invoke invoke) {
127    }
128}