1 /*
   2  * Copyright (c) 2015, 2016, 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.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
  28 
  29 import jdk.internal.vm.compiler.collections.EconomicMap;
  30 import org.graalvm.compiler.bytecode.BytecodeProvider;
  31 import org.graalvm.compiler.debug.DebugContext;
  32 import org.graalvm.compiler.graph.SourceLanguagePositionProvider;
  33 import org.graalvm.compiler.java.GraphBuilderPhase;
  34 import org.graalvm.compiler.nodes.EncodedGraph;
  35 import org.graalvm.compiler.nodes.GraphEncoder;
  36 import org.graalvm.compiler.nodes.StructuredGraph;
  37 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  38 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
  39 import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
  40 import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
  41 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  42 import org.graalvm.compiler.nodes.graphbuilderconf.LoopExplosionPlugin;
  43 import org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin;
  44 import org.graalvm.compiler.nodes.graphbuilderconf.ParameterPlugin;
  45 import org.graalvm.compiler.phases.OptimisticOptimizations;
  46 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  47 import org.graalvm.compiler.phases.common.ConvertDeoptimizeToGuardPhase;
  48 import org.graalvm.compiler.phases.tiers.PhaseContext;
  49 import org.graalvm.compiler.phases.util.Providers;
  50 
  51 import jdk.vm.ci.code.Architecture;
  52 import jdk.vm.ci.meta.ResolvedJavaMethod;
  53 
  54 /**
  55  * A graph decoder that provides all necessary encoded graphs on-the-fly (by parsing the methods and
  56  * encoding the graphs).
  57  */
  58 public class CachingPEGraphDecoder extends PEGraphDecoder {
  59 
  60     protected final Providers providers;
  61     protected final GraphBuilderConfiguration graphBuilderConfig;
  62     protected final OptimisticOptimizations optimisticOpts;
  63     private final AllowAssumptions allowAssumptions;
  64     private final EconomicMap<ResolvedJavaMethod, EncodedGraph> graphCache;
  65 
  66     public CachingPEGraphDecoder(Architecture architecture, StructuredGraph graph, Providers providers, GraphBuilderConfiguration graphBuilderConfig, OptimisticOptimizations optimisticOpts,
  67                     AllowAssumptions allowAssumptions, LoopExplosionPlugin loopExplosionPlugin, InvocationPlugins invocationPlugins, InlineInvokePlugin[] inlineInvokePlugins,
  68                     ParameterPlugin parameterPlugin,
  69                     NodePlugin[] nodePlugins, ResolvedJavaMethod callInlinedMethod, SourceLanguagePositionProvider sourceLanguagePositionProvider) {
  70         super(architecture, graph, providers.getMetaAccess(), providers.getConstantReflection(), providers.getConstantFieldProvider(), providers.getStampProvider(), loopExplosionPlugin,
  71                         invocationPlugins, inlineInvokePlugins, parameterPlugin, nodePlugins, callInlinedMethod, sourceLanguagePositionProvider);
  72 
  73         this.providers = providers;
  74         this.graphBuilderConfig = graphBuilderConfig;
  75         this.optimisticOpts = optimisticOpts;
  76         this.allowAssumptions = allowAssumptions;
  77         this.graphCache = EconomicMap.create();
  78     }
  79 
  80     protected GraphBuilderPhase.Instance createGraphBuilderPhaseInstance(IntrinsicContext initialIntrinsicContext) {
  81         return new GraphBuilderPhase.Instance(providers.getMetaAccess(), providers.getStampProvider(), providers.getConstantReflection(), providers.getConstantFieldProvider(), graphBuilderConfig,
  82                         optimisticOpts, initialIntrinsicContext);
  83     }
  84 
  85     @SuppressWarnings("try")
  86     private EncodedGraph createGraph(ResolvedJavaMethod method, ResolvedJavaMethod originalMethod, BytecodeProvider intrinsicBytecodeProvider, boolean isSubstitution) {
  87         // @formatter:off
  88         StructuredGraph graphToEncode = new StructuredGraph.Builder(options, debug, allowAssumptions).
  89                         useProfilingInfo(false).
  90                         trackNodeSourcePosition(graphBuilderConfig.trackNodeSourcePosition()).
  91                         method(method).
  92                         setIsSubstitution(isSubstitution).
  93                         build();
  94         // @formatter:on
  95         try (DebugContext.Scope scope = debug.scope("createGraph", graphToEncode)) {
  96             IntrinsicContext initialIntrinsicContext = intrinsicBytecodeProvider != null ? new IntrinsicContext(originalMethod, method, intrinsicBytecodeProvider, INLINE_AFTER_PARSING) : null;
  97             GraphBuilderPhase.Instance graphBuilderPhaseInstance = createGraphBuilderPhaseInstance(initialIntrinsicContext);
  98             graphBuilderPhaseInstance.apply(graphToEncode);
  99 
 100             PhaseContext context = new PhaseContext(providers);
 101             new CanonicalizerPhase().apply(graphToEncode, context);
 102             /*
 103              * ConvertDeoptimizeToGuardPhase reduces the number of merges in the graph, so that
 104              * fewer frame states will be created. This significantly reduces the number of nodes in
 105              * the initial graph.
 106              */
 107             new ConvertDeoptimizeToGuardPhase().apply(graphToEncode, context);
 108 
 109             EncodedGraph encodedGraph = GraphEncoder.encodeSingleGraph(graphToEncode, architecture);
 110             graphCache.put(method, encodedGraph);
 111             return encodedGraph;
 112 
 113         } catch (Throwable ex) {
 114             throw debug.handle(ex);
 115         }
 116     }
 117 
 118     @Override
 119     protected EncodedGraph lookupEncodedGraph(ResolvedJavaMethod method, ResolvedJavaMethod originalMethod, BytecodeProvider intrinsicBytecodeProvider, boolean isSubstitution,
 120                     boolean trackNodeSourcePosition) {
 121         EncodedGraph result = graphCache.get(method);
 122         if (result == null && method.hasBytecodes()) {
 123             result = createGraph(method, originalMethod, intrinsicBytecodeProvider, isSubstitution);
 124         }
 125         return result;
 126     }
 127 }