view graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCache.java @ 12570:af39ea2dc39d

made ConstantNodes (optionally) not record their usages (GRAAL-508)
author Doug Simon <doug.simon@oracle.com>
date Thu, 24 Oct 2013 14:57:57 +0200
parents f53dc8bbb88c
children ca8ab182026f
line wrap: on
line source

/*
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.graal.truffle;

import static com.oracle.graal.phases.GraalOptions.*;

import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.*;

import com.oracle.graal.api.code.*;
import com.oracle.graal.api.meta.*;
import com.oracle.graal.debug.*;
import com.oracle.graal.graph.*;
import com.oracle.graal.graph.Node;
import com.oracle.graal.graph.spi.*;
import com.oracle.graal.java.*;
import com.oracle.graal.nodes.*;
import com.oracle.graal.nodes.java.*;
import com.oracle.graal.nodes.java.MethodCallTargetNode.InvokeKind;
import com.oracle.graal.nodes.type.*;
import com.oracle.graal.nodes.util.*;
import com.oracle.graal.phases.*;
import com.oracle.graal.phases.common.*;
import com.oracle.graal.phases.tiers.*;
import com.oracle.graal.phases.util.*;
import com.oracle.graal.truffle.phases.*;
import com.oracle.graal.virtual.phases.ea.*;
import com.oracle.truffle.api.*;
import com.oracle.truffle.api.nodes.*;

/**
 * Implementation of a cache for Truffle graphs for improving partial evaluation time.
 */
public final class TruffleCache {

    private final Providers providers;
    private final GraphBuilderConfiguration config;
    private final OptimisticOptimizations optimisticOptimizations;

    private final HashMap<List<Object>, StructuredGraph> cache = new HashMap<>();
    private final StructuredGraph markerGraph = new StructuredGraph();
    private final ResolvedJavaType stringBuilderClass;

    public TruffleCache(Providers providers, GraphBuilderConfiguration config, OptimisticOptimizations optimisticOptimizations) {
        this.providers = providers;
        this.config = config;
        this.optimisticOptimizations = optimisticOptimizations;
        this.stringBuilderClass = providers.getMetaAccess().lookupJavaType(StringBuilder.class);
    }

    @SuppressWarnings("unused")
    public StructuredGraph lookup(final ResolvedJavaMethod method, final NodeInputList<ValueNode> arguments, final Assumptions assumptions, final CanonicalizerPhase finalCanonicalizer) {

        List<Object> key = new ArrayList<>(arguments.size() + 1);
        key.add(method);
        for (ValueNode v : arguments) {
            if (v.kind() == Kind.Object) {
                key.add(v.stamp());
            }
        }
        StructuredGraph resultGraph = cache.get(key);
        if (resultGraph != null) {
            return resultGraph;
        }

        if (resultGraph == markerGraph) {
            // Avoid recursive inline.
            return null;
        }

        cache.put(key, markerGraph);
        resultGraph = Debug.scope("TruffleCache", new Object[]{providers.getMetaAccess(), method}, new Callable<StructuredGraph>() {

            public StructuredGraph call() {

                final StructuredGraph graph = new StructuredGraph(method);
                PhaseContext phaseContext = new PhaseContext(providers, new Assumptions(false));
                new GraphBuilderPhase(phaseContext.getMetaAccess(), providers.getForeignCalls(), config, optimisticOptimizations).apply(graph);

                for (LocalNode l : graph.getNodes(LocalNode.class)) {
                    if (l.kind() == Kind.Object) {
                        ValueNode actualArgument = arguments.get(l.index());
                        l.setStamp(l.stamp().join(actualArgument.stamp()));
                    }
                }

                // Intrinsify methods.
                new ReplaceIntrinsicsPhase(providers.getReplacements()).apply(graph);

                // Convert deopt to guards.
                new ConvertDeoptimizeToGuardPhase().apply(graph);

                CanonicalizerPhase canonicalizerPhase = new CanonicalizerPhase(!AOTCompilation.getValue());
                PartialEscapePhase partialEscapePhase = new PartialEscapePhase(false, canonicalizerPhase);

                int mark = 0;
                while (true) {

                    partialEscapePhase.apply(graph, phaseContext);

                    // Conditional elimination.
                    ConditionalEliminationPhase conditionalEliminationPhase = new ConditionalEliminationPhase(phaseContext.getMetaAccess());
                    conditionalEliminationPhase.apply(graph);

                    // Canonicalize / constant propagate.
                    canonicalizerPhase.apply(graph, phaseContext);

                    boolean inliningProgress = false;
                    for (MethodCallTargetNode methodCallTarget : graph.getNodes(MethodCallTargetNode.class)) {
                        if (graph.getMark() != mark) {
                            // Make sure macro substitutions such as
                            // CompilerDirectives.transferToInterpreter get processed first.
                            for (Node newNode : graph.getNewNodes(mark)) {
                                if (newNode instanceof MethodCallTargetNode) {
                                    MethodCallTargetNode methodCallTargetNode = (MethodCallTargetNode) newNode;
                                    Class<? extends FixedWithNextNode> macroSubstitution = providers.getReplacements().getMacroSubstitution(methodCallTargetNode.targetMethod());
                                    if (macroSubstitution != null) {
                                        InliningUtil.inlineMacroNode(methodCallTargetNode.invoke(), methodCallTargetNode.targetMethod(), methodCallTargetNode.graph(), macroSubstitution);
                                    } else {
                                        tryCutOffRuntimeExceptions(methodCallTargetNode);
                                    }
                                }
                            }
                            mark = graph.getMark();
                        }
                        if (methodCallTarget.isAlive() && methodCallTarget.invoke() != null && shouldInline(methodCallTarget)) {
                            inliningProgress = true;
                            List<Node> canonicalizerUsages = new ArrayList<Node>();
                            for (Node n : methodCallTarget.invoke().asNode().usages()) {
                                if (n instanceof Canonicalizable) {
                                    canonicalizerUsages.add(n);
                                }
                            }
                            List<ValueNode> argumentSnapshot = methodCallTarget.arguments().snapshot();
                            int beforeInvokeMark = graph.getMark();
                            expandInvoke(methodCallTarget);
                            for (Node arg : argumentSnapshot) {
                                if (arg != null && arg.recordsUsages()) {
                                    for (Node argUsage : arg.usages()) {
                                        if (graph.isNew(beforeInvokeMark, argUsage) && argUsage instanceof Canonicalizable) {
                                            canonicalizerUsages.add(argUsage);
                                        }
                                    }
                                }
                            }
                            canonicalizerPhase.applyIncremental(graph, phaseContext, canonicalizerUsages);
                        }
                    }

                    // Convert deopt to guards.
                    new ConvertDeoptimizeToGuardPhase().apply(graph);

                    new EarlyReadEliminationPhase(canonicalizerPhase).apply(graph, phaseContext);

                    if (!inliningProgress) {
                        break;
                    }
                }

                if (TruffleCompilerOptions.TraceTruffleCacheDetails.getValue()) {
                    TTY.println(String.format("[truffle] added to graph cache method %s with %d nodes.", method, graph.getNodeCount()));
                }
                return graph;
            }
        });
        cache.put(key, resultGraph);
        return resultGraph;
    }

    private void expandInvoke(MethodCallTargetNode methodCallTargetNode) {
        StructuredGraph inlineGraph = providers.getReplacements().getMethodSubstitution(methodCallTargetNode.targetMethod());
        if (inlineGraph == null) {
            inlineGraph = TruffleCache.this.lookup(methodCallTargetNode.targetMethod(), methodCallTargetNode.arguments(), null, null);
        }
        if (inlineGraph == this.markerGraph) {
            // Can happen for recursive calls.
            throw GraphUtil.approxSourceException(methodCallTargetNode, new IllegalStateException("Found illegal recursive call to " + methodCallTargetNode.targetMethod() +
                            ", must annotate such calls with @CompilerDirectives.SlowPath!"));
        }
        Invoke invoke = methodCallTargetNode.invoke();
        InliningUtil.inline(invoke, inlineGraph, true);
    }

    private boolean tryCutOffRuntimeExceptions(MethodCallTargetNode methodCallTargetNode) {
        if (methodCallTargetNode.targetMethod().isConstructor()) {
            ResolvedJavaType runtimeException = providers.getMetaAccess().lookupJavaType(RuntimeException.class);
            ResolvedJavaType controlFlowException = providers.getMetaAccess().lookupJavaType(ControlFlowException.class);
            ResolvedJavaType exceptionType = Objects.requireNonNull(ObjectStamp.typeOrNull(methodCallTargetNode.receiver().stamp()));
            if (runtimeException.isAssignableFrom(methodCallTargetNode.targetMethod().getDeclaringClass()) && !controlFlowException.isAssignableFrom(exceptionType)) {
                DeoptimizeNode deoptNode = methodCallTargetNode.graph().add(new DeoptimizeNode(DeoptimizationAction.InvalidateRecompile, DeoptimizationReason.UnreachedCode));
                FixedNode invokeNode = methodCallTargetNode.invoke().asNode();
                invokeNode.replaceAtPredecessor(deoptNode);
                GraphUtil.killCFG(invokeNode);
                return true;
            }
        }
        return false;
    }

    private boolean shouldInline(final MethodCallTargetNode methodCallTargetNode) {
        return (methodCallTargetNode.invokeKind() == InvokeKind.Special || methodCallTargetNode.invokeKind() == InvokeKind.Static) &&
                        !Modifier.isNative(methodCallTargetNode.targetMethod().getModifiers()) && methodCallTargetNode.targetMethod().getAnnotation(ExplodeLoop.class) == null &&
                        methodCallTargetNode.targetMethod().getAnnotation(CompilerDirectives.SlowPath.class) == null && methodCallTargetNode.targetMethod().getDeclaringClass() != stringBuilderClass;
    }
}