1 /*
   2  * Copyright (c) 2015, 2019, 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 jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
  28 import static org.graalvm.compiler.core.common.GraalOptions.UseEncodedGraphs;
  29 import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
  30 
  31 import jdk.internal.vm.compiler.collections.EconomicMap;
  32 import org.graalvm.compiler.bytecode.BytecodeProvider;
  33 import org.graalvm.compiler.debug.DebugContext;
  34 import org.graalvm.compiler.graph.SourceLanguagePositionProvider;
  35 import org.graalvm.compiler.java.GraphBuilderPhase;
  36 import org.graalvm.compiler.loop.phases.ConvertDeoptimizeToGuardPhase;
  37 import org.graalvm.compiler.nodes.EncodedGraph;
  38 import org.graalvm.compiler.nodes.GraphEncoder;
  39 import org.graalvm.compiler.nodes.StructuredGraph;
  40 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  41 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
  42 import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
  43 import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
  44 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  45 import org.graalvm.compiler.nodes.graphbuilderconf.LoopExplosionPlugin;
  46 import org.graalvm.compiler.nodes.graphbuilderconf.MethodSubstitutionPlugin;
  47 import org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin;
  48 import org.graalvm.compiler.nodes.graphbuilderconf.ParameterPlugin;
  49 import org.graalvm.compiler.nodes.spi.CoreProviders;
  50 import org.graalvm.compiler.phases.BasePhase;
  51 import org.graalvm.compiler.phases.OptimisticOptimizations;
  52 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  53 import org.graalvm.compiler.phases.util.Providers;
  54 
  55 import jdk.vm.ci.code.Architecture;
  56 import jdk.vm.ci.meta.ResolvedJavaMethod;
  57 
  58 /**
  59  * A graph decoder that provides all necessary encoded graphs on-the-fly (by parsing the methods and
  60  * encoding the graphs).
  61  */
  62 public class CachingPEGraphDecoder extends PEGraphDecoder {
  63 
  64     protected final Providers providers;
  65     protected final GraphBuilderConfiguration graphBuilderConfig;
  66     protected final OptimisticOptimizations optimisticOpts;
  67     private final AllowAssumptions allowAssumptions;
  68     private final EconomicMap<ResolvedJavaMethod, EncodedGraph> graphCache;
  69     private final BasePhase<? super CoreProviders> postParsingPhase;
  70 
  71     public CachingPEGraphDecoder(Architecture architecture, StructuredGraph graph, Providers providers, GraphBuilderConfiguration graphBuilderConfig, OptimisticOptimizations optimisticOpts,
  72                     AllowAssumptions allowAssumptions, LoopExplosionPlugin loopExplosionPlugin, InvocationPlugins invocationPlugins, InlineInvokePlugin[] inlineInvokePlugins,
  73                     ParameterPlugin parameterPlugin,
  74                     NodePlugin[] nodePlugins, ResolvedJavaMethod callInlinedMethod, ResolvedJavaMethod callInlinedAgnosticMethod, SourceLanguagePositionProvider sourceLanguagePositionProvider,
  75                     BasePhase<? super CoreProviders> postParsingPhase, EconomicMap<ResolvedJavaMethod, EncodedGraph> graphCache) {
  76         super(architecture, graph, providers, loopExplosionPlugin,
  77                         invocationPlugins, inlineInvokePlugins, parameterPlugin, nodePlugins, callInlinedMethod, callInlinedAgnosticMethod, sourceLanguagePositionProvider);
  78 
  79         this.providers = providers;
  80         this.graphBuilderConfig = graphBuilderConfig;
  81         this.optimisticOpts = optimisticOpts;
  82         this.allowAssumptions = allowAssumptions;
  83         this.graphCache = graphCache;
  84         this.postParsingPhase = postParsingPhase;
  85     }
  86 
  87     protected GraphBuilderPhase.Instance createGraphBuilderPhaseInstance(IntrinsicContext initialIntrinsicContext) {
  88         return new GraphBuilderPhase.Instance(providers, graphBuilderConfig, optimisticOpts, initialIntrinsicContext);
  89     }
  90 
  91     @SuppressWarnings("try")
  92     private EncodedGraph createGraph(ResolvedJavaMethod method, MethodSubstitutionPlugin plugin, BytecodeProvider intrinsicBytecodeProvider, boolean isSubstitution) {
  93         StructuredGraph graphToEncode;
  94         if (isSubstitution && (UseEncodedGraphs.getValue(options) || IS_IN_NATIVE_IMAGE)) {
  95             // These must go through Replacements to find the graph to use.
  96             graphToEncode = providers.getReplacements().getMethodSubstitution(plugin, method, INLINE_AFTER_PARSING, allowAssumptions,
  97                             null, options);
  98         } else {
  99             graphToEncode = buildGraph(method, plugin, intrinsicBytecodeProvider, isSubstitution);
 100         }
 101 
 102         /*
 103          * ConvertDeoptimizeToGuardPhase reduces the number of merges in the graph, so that fewer
 104          * frame states will be created. This significantly reduces the number of nodes in the
 105          * initial graph.
 106          */
 107         try (DebugContext.Scope scope = debug.scope("createGraph", graphToEncode)) {
 108             new ConvertDeoptimizeToGuardPhase().apply(graphToEncode, providers);
 109         } catch (Throwable t) {
 110             throw debug.handle(t);
 111         }
 112 
 113         EncodedGraph encodedGraph = GraphEncoder.encodeSingleGraph(graphToEncode, architecture);
 114         graphCache.put(method, encodedGraph);
 115         return encodedGraph;
 116     }
 117 
 118     @SuppressWarnings("try")
 119     private StructuredGraph buildGraph(ResolvedJavaMethod method, MethodSubstitutionPlugin plugin, BytecodeProvider intrinsicBytecodeProvider, boolean isSubstitution) {
 120         StructuredGraph graphToEncode;// @formatter:off
 121         graphToEncode = new StructuredGraph.Builder(options, debug, allowAssumptions).
 122                 useProfilingInfo(false).
 123                 trackNodeSourcePosition(graphBuilderConfig.trackNodeSourcePosition()).
 124                 method(plugin != null ? plugin.getSubstitute(providers.getMetaAccess()) : method).
 125                 setIsSubstitution(isSubstitution).
 126                 cancellable(graph.getCancellable()).
 127                 build();
 128         // @formatter:on
 129         try (DebugContext.Scope scope = debug.scope("createGraph", graphToEncode)) {
 130             IntrinsicContext initialIntrinsicContext = intrinsicBytecodeProvider != null
 131                             ? new IntrinsicContext(method, plugin.getSubstitute(providers.getMetaAccess()), intrinsicBytecodeProvider, INLINE_AFTER_PARSING)
 132                             : null;
 133             GraphBuilderPhase.Instance graphBuilderPhaseInstance = createGraphBuilderPhaseInstance(initialIntrinsicContext);
 134             graphBuilderPhaseInstance.apply(graphToEncode);
 135             new CanonicalizerPhase().apply(graphToEncode, providers);
 136             if (postParsingPhase != null) {
 137                 postParsingPhase.apply(graphToEncode, providers);
 138             }
 139         } catch (Throwable ex) {
 140             throw debug.handle(ex);
 141         }
 142         return graphToEncode;
 143     }
 144 
 145     @Override
 146     protected EncodedGraph lookupEncodedGraph(ResolvedJavaMethod method, MethodSubstitutionPlugin plugin, BytecodeProvider intrinsicBytecodeProvider, boolean isSubstitution,
 147                     boolean trackNodeSourcePosition) {
 148         EncodedGraph result = graphCache.get(method);
 149         if (result == null && method.hasBytecodes()) {
 150             result = createGraph(method, plugin, intrinsicBytecodeProvider, isSubstitution);
 151         }
 152         return result;
 153     }
 154 }