1 /*
   2  * Copyright (c) 2013, 2018, 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.replacements;
  26 
  27 import static org.graalvm.compiler.core.common.GraalOptions.TrivialInliningSize;
  28 import static org.graalvm.compiler.java.BytecodeParserOptions.InlineDuringParsingMaxDepth;
  29 import static org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin.InlineInfo.createStandardInlineInfo;
  30 
  31 import org.graalvm.compiler.java.BytecodeParserOptions;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.nodes.ValueNode;
  34 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  35 import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
  36 
  37 import jdk.vm.ci.meta.ResolvedJavaMethod;
  38 
  39 public final class InlineDuringParsingPlugin implements InlineInvokePlugin {
  40 
  41     /**
  42      * Budget which when exceeded reduces the effective value of
  43      * {@link BytecodeParserOptions#InlineDuringParsingMaxDepth} to
  44      * {@link #MaxDepthAfterBudgetExceeded}.
  45      */
  46     private static final int NodeBudget = Integer.getInteger("InlineDuringParsingPlugin.NodeBudget", 2000);
  47 
  48     private static final int MaxDepthAfterBudgetExceeded = Integer.getInteger("InlineDuringParsingPlugin.MaxDepthAfterBudgetExceeded", 3);
  49 
  50     @Override
  51     public InlineInfo shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
  52         // @formatter:off
  53         if (method.hasBytecodes() &&
  54             method.getDeclaringClass().isLinked() &&
  55             method.canBeInlined()) {
  56 
  57             // Test force inlining first
  58             if (method.shouldBeInlined() && checkInliningDepth(b)) {
  59                 return createStandardInlineInfo(method);
  60             }
  61 
  62             if (!method.isSynchronized() &&
  63                 checkSize(method, args, b.getGraph()) &&
  64                 checkInliningDepth(b)) {
  65                 return createStandardInlineInfo(method);
  66             }
  67         }
  68         // @formatter:on
  69         return null;
  70     }
  71 
  72     private static boolean checkInliningDepth(GraphBuilderContext b) {
  73         int nodeCount = b.getGraph().getNodeCount();
  74         int maxDepth = InlineDuringParsingMaxDepth.getValue(b.getOptions());
  75         if (nodeCount > NodeBudget && MaxDepthAfterBudgetExceeded < maxDepth) {
  76             maxDepth = MaxDepthAfterBudgetExceeded;
  77         }
  78         return b.getDepth() < maxDepth;
  79     }
  80 
  81     private static boolean checkSize(ResolvedJavaMethod method, ValueNode[] args, StructuredGraph graph) {
  82         int bonus = 1;
  83         for (ValueNode v : args) {
  84             if (v.isConstant()) {
  85                 bonus++;
  86             }
  87         }
  88         return method.getCode().length <= TrivialInliningSize.getValue(graph.getOptions()) * bonus;
  89     }
  90 }