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 import jdk.vm.ci.services.Services;
  39 
  40 public final class InlineDuringParsingPlugin implements InlineInvokePlugin {
  41 
  42     private static int getInteger(String name, int def) {
  43         String value = Services.getSavedProperties().get(name);
  44         if (value != null) {
  45             return Integer.parseInt(value);
  46         }
  47         return def;
  48     }
  49 
  50     /**
  51      * Budget which when exceeded reduces the effective value of
  52      * {@link BytecodeParserOptions#InlineDuringParsingMaxDepth} to
  53      * {@link #MaxDepthAfterBudgetExceeded}.
  54      */
  55     private static final int NodeBudget = getInteger("InlineDuringParsingPlugin.NodeBudget", 2000);
  56 
  57     private static final int MaxDepthAfterBudgetExceeded = getInteger("InlineDuringParsingPlugin.MaxDepthAfterBudgetExceeded", 3);
  58 
  59     @Override
  60     public InlineInfo shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
  61         // @formatter:off
  62         if (method.hasBytecodes() &&
  63             method.getDeclaringClass().isLinked() &&
  64             method.canBeInlined()) {
  65 
  66             // Test force inlining first
  67             if (method.shouldBeInlined() && checkInliningDepth(b)) {
  68                 return createStandardInlineInfo(method);
  69             }
  70 
  71             if (!method.isSynchronized() &&
  72                 checkSize(method, args, b.getGraph()) &&
  73                 checkInliningDepth(b)) {
  74                 return createStandardInlineInfo(method);
  75             }
  76         }
  77         // @formatter:on
  78         return null;
  79     }
  80 
  81     private static boolean checkInliningDepth(GraphBuilderContext b) {
  82         int nodeCount = b.getGraph().getNodeCount();
  83         int maxDepth = InlineDuringParsingMaxDepth.getValue(b.getOptions());
  84         if (nodeCount > NodeBudget && MaxDepthAfterBudgetExceeded < maxDepth) {
  85             maxDepth = MaxDepthAfterBudgetExceeded;
  86         }
  87         return b.getDepth() < maxDepth;
  88     }
  89 
  90     private static boolean checkSize(ResolvedJavaMethod method, ValueNode[] args, StructuredGraph graph) {
  91         int bonus = 1;
  92         for (ValueNode v : args) {
  93             if (v.isConstant()) {
  94                 bonus++;
  95             }
  96         }
  97         return method.getCode().length <= TrivialInliningSize.getValue(graph.getOptions()) * bonus;
  98     }
  99 }