changeset 10537:8b2065558490

Merge.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Tue, 25 Jun 2013 19:49:09 +0200
parents 26c69598db3e (current diff) 254fab64b343 (diff)
children 347d444a6fb7
files graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCompilerImpl.java
diffstat 6 files changed, 171 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/PartialEvaluationTest.java	Tue Jun 25 19:48:16 2013 +0200
+++ b/graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/PartialEvaluationTest.java	Tue Jun 25 19:49:09 2013 +0200
@@ -52,12 +52,12 @@
 public class PartialEvaluationTest extends GraalCompilerTest {
 
     private static final long UNROLL_LIMIT = 100;
-    private final PartialEvaluator nodeCompiler;
+    private final PartialEvaluator partialEvaluator;
 
     public PartialEvaluationTest() {
         // Make sure Truffle runtime is initialized.
         Assert.assertTrue(Truffle.getRuntime() instanceof GraalTruffleRuntime);
-        this.nodeCompiler = new PartialEvaluator(runtime, runtime);
+        this.partialEvaluator = new PartialEvaluator(runtime, ((GraalTruffleRuntime) Truffle.getRuntime()).getReplacements());
 
         DebugEnvironment.initialize(System.out);
     }
@@ -103,7 +103,7 @@
 
             @Override
             public StructuredGraph call() {
-                StructuredGraph resultGraph = nodeCompiler.createGraph(compilable, assumptions);
+                StructuredGraph resultGraph = partialEvaluator.createGraph(compilable, assumptions);
                 CanonicalizerPhase canonicalizer = new CanonicalizerPhase(canonicalizeReads);
                 HighTierContext context = new HighTierContext(runtime, assumptions, replacements);
 
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/GraalTruffleRuntime.java	Tue Jun 25 19:48:16 2013 +0200
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/GraalTruffleRuntime.java	Tue Jun 25 19:49:09 2013 +0200
@@ -26,9 +26,7 @@
 
 import java.util.*;
 
-import com.oracle.graal.api.runtime.*;
 import com.oracle.graal.nodes.spi.*;
-import com.oracle.graal.truffle.substitutions.*;
 import com.oracle.truffle.api.*;
 import com.oracle.truffle.api.frame.*;
 import com.oracle.truffle.api.impl.*;
@@ -44,20 +42,11 @@
     }
 
     private TruffleCompiler truffleCompiler;
+    private Replacements truffleReplacements;
     private ArrayList<String> includes;
     private ArrayList<String> excludes;
 
     private GraalTruffleRuntime() {
-        Replacements replacements = Graal.getRequiredCapability(Replacements.class);
-        replacements.registerSubstitutions(CompilerAssertsSubstitutions.class);
-        replacements.registerSubstitutions(CompilerDirectivesSubstitutions.class);
-        replacements.registerSubstitutions(ExactMathSubstitutions.class);
-        replacements.registerSubstitutions(UnexpectedResultExceptionSubstitutions.class);
-        replacements.registerSubstitutions(SlowPathExceptionSubstitutions.class);
-        replacements.registerSubstitutions(FrameWithoutBoxingSubstitutions.class);
-        replacements.registerSubstitutions(OptimizedAssumptionSubstitutions.class);
-        replacements.registerSubstitutions(OptimizedCallTargetSubstitutions.class);
-        replacements.registerSubstitutions(DefaultCallTargetSubstitutions.class);
     }
 
     public String getName() {
@@ -100,6 +89,13 @@
         return new OptimizedAssumption(name);
     }
 
+    public Replacements getReplacements() {
+        if (truffleReplacements == null) {
+            truffleReplacements = TruffleReplacements.makeInstance();
+        }
+        return truffleReplacements;
+    }
+
     private boolean acceptForCompilation(RootNode rootNode) {
         if (TruffleCompileOnly.getValue() != null) {
             if (includes == null) {
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PartialEvaluator.java	Tue Jun 25 19:48:16 2013 +0200
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PartialEvaluator.java	Tue Jun 25 19:49:09 2013 +0200
@@ -30,7 +30,6 @@
 
 import com.oracle.graal.api.code.*;
 import com.oracle.graal.api.meta.*;
-import com.oracle.graal.api.runtime.*;
 import com.oracle.graal.debug.*;
 import com.oracle.graal.graph.*;
 import com.oracle.graal.graph.Node;
@@ -72,16 +71,16 @@
     private Set<Constant> constantReceivers;
     private final HotSpotGraphCache cache;
 
-    public PartialEvaluator(GraalCodeCacheProvider runtime, MetaAccessProvider metaAccessProvider) {
+    public PartialEvaluator(MetaAccessProvider metaAccessProvider, Replacements replacements) {
         this.metaAccessProvider = metaAccessProvider;
-        this.nodeClass = runtime.lookupJavaType(com.oracle.truffle.api.nodes.Node.class);
-        this.customCanonicalizer = new PartialEvaluatorCanonicalizer(runtime, nodeClass);
+        this.nodeClass = metaAccessProvider.lookupJavaType(com.oracle.truffle.api.nodes.Node.class);
+        this.customCanonicalizer = new PartialEvaluatorCanonicalizer(metaAccessProvider, nodeClass);
         this.skippedExceptionTypes = TruffleCompilerImpl.getSkippedExceptionTypes(metaAccessProvider);
-        this.replacements = Graal.getRequiredCapability(Replacements.class);
+        this.replacements = replacements;
         this.cache = HotSpotGraalRuntime.graalRuntime().getCache();
 
         try {
-            executeHelperMethod = runtime.lookupJavaMethod(OptimizedCallTarget.class.getDeclaredMethod("executeHelper", PackedFrame.class, Arguments.class));
+            executeHelperMethod = metaAccessProvider.lookupJavaMethod(OptimizedCallTarget.class.getDeclaredMethod("executeHelper", PackedFrame.class, Arguments.class));
         } catch (NoSuchMethodException ex) {
             throw new RuntimeException(ex);
         }
@@ -198,44 +197,47 @@
     }
 
     private void expandTree(GraphBuilderConfiguration config, StructuredGraph graph, NewFrameNode newFrameNode, Assumptions assumptions) {
-        for (Node usage : newFrameNode.usages().snapshot()) {
-            if (usage instanceof MethodCallTargetNode && !usage.isDeleted()) {
-                MethodCallTargetNode methodCallTargetNode = (MethodCallTargetNode) usage;
-                InvokeKind kind = methodCallTargetNode.invokeKind();
-                if (kind == InvokeKind.Special || kind == InvokeKind.Static) {
-                    if (TruffleInlinePrinter.getValue()) {
-                        InlinePrinterProcessor.addInlining(MethodHolder.getNewTruffleExecuteMethod(methodCallTargetNode));
-                    }
-                    if (TraceTruffleCompilationDetails.getValue() && kind == InvokeKind.Special && methodCallTargetNode.arguments().first() instanceof ConstantNode) {
-                        ConstantNode constantNode = (ConstantNode) methodCallTargetNode.arguments().first();
-                        constantReceivers.add(constantNode.asConstant());
-                    }
-                    StructuredGraph inlineGraph = replacements.getMethodSubstitution(methodCallTargetNode.targetMethod());
-                    NewFrameNode otherNewFrame = null;
-                    if (inlineGraph == null) {
-                        inlineGraph = parseGraph(config, methodCallTargetNode.targetMethod(), methodCallTargetNode.arguments(), assumptions, !AOTCompilation.getValue());
-                        otherNewFrame = inlineGraph.getNodes(NewFrameNode.class).first();
-                    }
-                    int nodeCountBefore = graph.getNodeCount();
-                    Map<Node, Node> mapping = InliningUtil.inline(methodCallTargetNode.invoke(), inlineGraph, false);
-                    if (Debug.isDumpEnabled()) {
-                        int nodeCountAfter = graph.getNodeCount();
-                        Debug.dump(graph, "After inlining %s %+d (%d)", methodCallTargetNode.targetMethod().toString(), nodeCountAfter - nodeCountBefore, nodeCountAfter);
-                    }
+        boolean changed;
+        do {
+            changed = false;
+            for (Node usage : newFrameNode.usages().snapshot()) {
+                if (usage instanceof MethodCallTargetNode && !usage.isDeleted()) {
+                    MethodCallTargetNode methodCallTargetNode = (MethodCallTargetNode) usage;
+                    InvokeKind kind = methodCallTargetNode.invokeKind();
+                    if (kind == InvokeKind.Special || kind == InvokeKind.Static) {
+                        if (TruffleInlinePrinter.getValue()) {
+                            InlinePrinterProcessor.addInlining(MethodHolder.getNewTruffleExecuteMethod(methodCallTargetNode));
+                        }
+                        if (TraceTruffleCompilationDetails.getValue() && kind == InvokeKind.Special && methodCallTargetNode.arguments().first() instanceof ConstantNode) {
+                            ConstantNode constantNode = (ConstantNode) methodCallTargetNode.arguments().first();
+                            constantReceivers.add(constantNode.asConstant());
+                        }
+                        StructuredGraph inlineGraph = replacements.getMethodSubstitution(methodCallTargetNode.targetMethod());
+                        NewFrameNode otherNewFrame = null;
+                        if (inlineGraph == null) {
+                            inlineGraph = parseGraph(config, methodCallTargetNode.targetMethod(), methodCallTargetNode.arguments(), assumptions, !AOTCompilation.getValue());
+                            otherNewFrame = inlineGraph.getNodes(NewFrameNode.class).first();
+                        }
 
-                    if (newFrameNode.isAlive() && newFrameNode.usages().isNotEmpty()) {
-                        expandTree(config, graph, newFrameNode, assumptions);
-                    }
+                        int nodeCountBefore = graph.getNodeCount();
+                        Map<Node, Node> mapping = InliningUtil.inline(methodCallTargetNode.invoke(), inlineGraph, false);
+                        if (Debug.isDumpEnabled()) {
+                            int nodeCountAfter = graph.getNodeCount();
+                            Debug.dump(graph, "After inlining %s %+d (%d)", methodCallTargetNode.targetMethod().toString(), nodeCountAfter - nodeCountBefore, nodeCountAfter);
+                        }
 
-                    if (otherNewFrame != null) {
-                        otherNewFrame = (NewFrameNode) mapping.get(otherNewFrame);
-                        if (otherNewFrame.isAlive() && otherNewFrame.usages().isNotEmpty()) {
-                            expandTree(config, graph, otherNewFrame, assumptions);
+                        changed = true;
+
+                        if (otherNewFrame != null) {
+                            otherNewFrame = (NewFrameNode) mapping.get(otherNewFrame);
+                            if (otherNewFrame.isAlive() && otherNewFrame.usages().isNotEmpty()) {
+                                expandTree(config, graph, otherNewFrame, assumptions);
+                            }
                         }
                     }
                 }
             }
-        }
+        } while (changed && newFrameNode.isAlive() && newFrameNode.usages().isNotEmpty());
     }
 
     private StructuredGraph parseGraph(final GraphBuilderConfiguration config, final ResolvedJavaMethod targetMethod, final NodeInputList<ValueNode> arguments, final Assumptions assumptions,
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PartialEvaluatorCanonicalizer.java	Tue Jun 25 19:48:16 2013 +0200
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PartialEvaluatorCanonicalizer.java	Tue Jun 25 19:49:09 2013 +0200
@@ -35,11 +35,11 @@
 
 final class PartialEvaluatorCanonicalizer implements CanonicalizerPhase.CustomCanonicalizer {
 
-    private final MetaAccessProvider runtime;
+    private final MetaAccessProvider metaAccessProvider;
     private final ResolvedJavaType nodeClass;
 
-    PartialEvaluatorCanonicalizer(MetaAccessProvider runtime, ResolvedJavaType nodeClass) {
-        this.runtime = runtime;
+    PartialEvaluatorCanonicalizer(MetaAccessProvider metaAccessProvider, ResolvedJavaType nodeClass) {
+        this.metaAccessProvider = metaAccessProvider;
         this.nodeClass = nodeClass;
     }
 
@@ -53,7 +53,7 @@
                             ((loadFieldNode.kind() == Kind.Object && loadFieldNode.field().getAnnotation(Child.class) != null) || Modifier.isFinal(loadFieldNode.field().getModifiers()) || loadFieldNode.field().getAnnotation(
                                             CompilerDirectives.CompilationFinal.class) != null)) {
                 Constant constant = loadFieldNode.field().readValue(loadFieldNode.object().asConstant());
-                return ConstantNode.forConstant(constant, this.runtime, node.graph());
+                return ConstantNode.forConstant(constant, metaAccessProvider, node.graph());
             }
         } else if (node instanceof LoadIndexedNode) {
             LoadIndexedNode loadIndexedNode = (LoadIndexedNode) node;
@@ -65,7 +65,7 @@
                     Object array = loadIndexedNode.array().asConstant().asObject();
                     int index = loadIndexedNode.index().asConstant().asInt();
                     Object value = Array.get(array, index);
-                    return ConstantNode.forObject(value, this.runtime, node.graph());
+                    return ConstantNode.forObject(value, metaAccessProvider, node.graph());
                 }
             }
         } else if (node instanceof UnsafeLoadNode) {
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCompilerImpl.java	Tue Jun 25 19:48:16 2013 +0200
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCompilerImpl.java	Tue Jun 25 19:49:09 2013 +0200
@@ -46,6 +46,7 @@
 import com.oracle.graal.phases.tiers.*;
 import com.oracle.graal.printer.*;
 import com.oracle.graal.truffle.nodes.*;
+import com.oracle.truffle.api.*;
 import com.oracle.truffle.api.nodes.*;
 
 /**
@@ -55,7 +56,7 @@
 
     private final GraalCodeCacheProvider runtime;
     private final Suites suites;
-    private final PartialEvaluator nodeCompiler;
+    private final PartialEvaluator partialEvaluator;
     private final MetaAccessProvider metaAccessProvider;
     private final Replacements replacements;
     private final Backend backend;
@@ -72,10 +73,10 @@
         this.suites = Graal.getRequiredCapability(SuitesProvider.class).createSuites();
         this.metaAccessProvider = Graal.getRequiredCapability(MetaAccessProvider.class);
         this.backend = Graal.getRequiredCapability(Backend.class);
-        this.replacements = Graal.getRequiredCapability(Replacements.class);
+        this.replacements = ((GraalTruffleRuntime) Truffle.getRuntime()).getReplacements();
         this.graalRuntime = HotSpotGraalRuntime.graalRuntime();
 
-        this.nodeCompiler = new PartialEvaluator(runtime, metaAccessProvider);
+        this.partialEvaluator = new PartialEvaluator(metaAccessProvider, replacements);
         this.skippedExceptionTypes = getSkippedExceptionTypes(metaAccessProvider);
 
         if (DebugEnabled.getValue()) {
@@ -110,7 +111,7 @@
 
         compilable.timeCompilationStarted = System.nanoTime();
         Assumptions assumptions = new Assumptions(true);
-        graph = nodeCompiler.createGraph(compilable, assumptions);
+        graph = partialEvaluator.createGraph(compilable, assumptions);
         compilable.timePartialEvaluationFinished = System.nanoTime();
         compilable.nodeCountPartialEval = graph.getNodeCount();
         InstalledCode compiledMethod = compileMethodHelper(graph, config, compilable, assumptions);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleReplacements.java	Tue Jun 25 19:49:09 2013 +0200
@@ -0,0 +1,110 @@
+/*
+ * 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 java.util.*;
+
+import com.oracle.graal.api.code.*;
+import com.oracle.graal.api.meta.*;
+import com.oracle.graal.api.replacements.*;
+import com.oracle.graal.api.runtime.*;
+import com.oracle.graal.graph.*;
+import com.oracle.graal.nodes.*;
+import com.oracle.graal.nodes.spi.*;
+import com.oracle.graal.replacements.*;
+import com.oracle.graal.truffle.substitutions.*;
+
+/**
+ * Custom {@link Replacements} for Truffle compilation.
+ */
+public final class TruffleReplacements extends ReplacementsImpl {
+
+    private final Replacements graalReplacements;
+
+    private TruffleReplacements(MetaAccessProvider runtime, Assumptions assumptions, TargetDescription target, Replacements graalReplacements) {
+        super(runtime, assumptions, target);
+        this.graalReplacements = graalReplacements;
+    }
+
+    static Replacements makeInstance() {
+        MetaAccessProvider metaAccessProvider = Graal.getRequiredCapability(MetaAccessProvider.class);
+        TargetDescription targetDescription = Graal.getRequiredCapability(CodeCacheProvider.class).getTarget();
+        Replacements graalReplacements = Graal.getRequiredCapability(Replacements.class);
+        Replacements truffleReplacements = new TruffleReplacements(metaAccessProvider, graalReplacements.getAssumptions(), targetDescription, graalReplacements);
+
+        truffleReplacements.registerSubstitutions(CompilerAssertsSubstitutions.class);
+        truffleReplacements.registerSubstitutions(CompilerDirectivesSubstitutions.class);
+        truffleReplacements.registerSubstitutions(ExactMathSubstitutions.class);
+        truffleReplacements.registerSubstitutions(UnexpectedResultExceptionSubstitutions.class);
+        truffleReplacements.registerSubstitutions(SlowPathExceptionSubstitutions.class);
+        truffleReplacements.registerSubstitutions(FrameWithoutBoxingSubstitutions.class);
+        truffleReplacements.registerSubstitutions(OptimizedAssumptionSubstitutions.class);
+        truffleReplacements.registerSubstitutions(OptimizedCallTargetSubstitutions.class);
+        truffleReplacements.registerSubstitutions(DefaultCallTargetSubstitutions.class);
+
+        return truffleReplacements;
+    }
+
+    @Override
+    public StructuredGraph getSnippet(ResolvedJavaMethod method) {
+        return graalReplacements.getSnippet(method);
+    }
+
+    @Override
+    public StructuredGraph getMethodSubstitution(ResolvedJavaMethod method) {
+        StructuredGraph graph = graalReplacements.getMethodSubstitution(method);
+        if (graph == null) {
+            return super.getMethodSubstitution(method);
+        }
+        return graph;
+    }
+
+    @Override
+    public Class<? extends FixedWithNextNode> getMacroSubstitution(ResolvedJavaMethod method) {
+        Class<? extends FixedWithNextNode> clazz = graalReplacements.getMacroSubstitution(method);
+        if (clazz == null) {
+            return super.getMacroSubstitution(method);
+        }
+        return clazz;
+    }
+
+    @Override
+    public Collection<ResolvedJavaMethod> getAllReplacements() {
+        throw GraalInternalError.shouldNotReachHere();
+    }
+
+    @Override
+    public boolean isForcedSubstitution(ResolvedJavaMethod method) {
+        return graalReplacements.isForcedSubstitution(method) || super.isForcedSubstitution(method);
+    }
+
+    @Override
+    public void registerSnippetTemplateCache(SnippetTemplateCache templates) {
+        throw GraalInternalError.shouldNotReachHere();
+    }
+
+    @Override
+    public <T extends SnippetTemplateCache> T getSnippetTemplateCache(Class<T> templatesClass) {
+        return graalReplacements.getSnippetTemplateCache(templatesClass);
+    }
+}