001/*
002 * Copyright (c) 2015, 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.replacements;
024
025import static com.oracle.graal.graphbuilderconf.IntrinsicContext.CompilationContext.*;
026
027import java.util.*;
028
029import jdk.internal.jvmci.code.*;
030import com.oracle.graal.debug.*;
031import jdk.internal.jvmci.meta.*;
032
033import com.oracle.graal.graphbuilderconf.*;
034import com.oracle.graal.java.*;
035import com.oracle.graal.nodes.*;
036import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
037import com.oracle.graal.phases.*;
038import com.oracle.graal.phases.common.*;
039import com.oracle.graal.phases.tiers.*;
040import com.oracle.graal.phases.util.*;
041
042/**
043 * A graph decoder that provides all necessary encoded graphs on-the-fly (by parsing the methods and
044 * encoding the graphs).
045 */
046public class CachingPEGraphDecoder extends PEGraphDecoder {
047
048    private final Providers providers;
049    private final GraphBuilderConfiguration graphBuilderConfig;
050    private final OptimisticOptimizations optimisticOpts;
051    private final AllowAssumptions allowAssumptions;
052    private final Map<ResolvedJavaMethod, EncodedGraph> graphCache;
053
054    public CachingPEGraphDecoder(Providers providers, GraphBuilderConfiguration graphBuilderConfig, OptimisticOptimizations optimisticOpts, AllowAssumptions allowAssumptions, Architecture architecture) {
055        super(providers.getMetaAccess(), providers.getConstantReflection(), providers.getStampProvider(), architecture);
056
057        this.providers = providers;
058        this.graphBuilderConfig = graphBuilderConfig;
059        this.optimisticOpts = optimisticOpts;
060        this.allowAssumptions = allowAssumptions;
061        this.graphCache = new HashMap<>();
062    }
063
064    private EncodedGraph createGraph(ResolvedJavaMethod method, boolean isIntrinsic) {
065        StructuredGraph graph = new StructuredGraph(method, allowAssumptions);
066        try (Debug.Scope scope = Debug.scope("createGraph", graph)) {
067
068            IntrinsicContext initialIntrinsicContext = isIntrinsic ? new IntrinsicContext(method, method, INLINE_AFTER_PARSING) : null;
069            new GraphBuilderPhase.Instance(providers.getMetaAccess(), providers.getStampProvider(), providers.getConstantReflection(), graphBuilderConfig, optimisticOpts, initialIntrinsicContext).apply(graph);
070
071            PhaseContext context = new PhaseContext(providers);
072            new CanonicalizerPhase().apply(graph, context);
073
074            EncodedGraph encodedGraph = GraphEncoder.encodeSingleGraph(graph, architecture);
075            graphCache.put(method, encodedGraph);
076            return encodedGraph;
077
078        } catch (Throwable ex) {
079            throw Debug.handle(ex);
080        }
081    }
082
083    @Override
084    protected EncodedGraph lookupEncodedGraph(ResolvedJavaMethod method, boolean isIntrinsic) {
085        EncodedGraph result = graphCache.get(method);
086        if (result == null && method.hasBytecodes()) {
087            result = createGraph(method, isIntrinsic);
088        }
089        return result;
090    }
091}