annotate graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/OptimizedCallTarget.java @ 10751:7b5d7c42598b

Truffle: change sorting of inlinable call sites
author Andreas Woess <andreas.woess@jku.at>
date Thu, 11 Jul 2013 19:34:27 +0200
parents fd53f9f7007b
children e2f5ae9afdc5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1 /*
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
4 *
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
8 *
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
13 * accompanied this code).
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
14 *
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
18 *
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
21 * questions.
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
22 */
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
23 package com.oracle.graal.truffle;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
24
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
25 import static com.oracle.graal.truffle.TruffleCompilerOptions.*;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
26
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
27 import java.io.*;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
28 import java.util.*;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
29
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
30 import com.oracle.graal.api.code.*;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
31 import com.oracle.graal.debug.*;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
32 import com.oracle.truffle.api.*;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
33 import com.oracle.truffle.api.frame.*;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
34 import com.oracle.truffle.api.impl.*;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
35 import com.oracle.truffle.api.nodes.*;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
36
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
37 /**
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
38 * Call target that is optimized by Graal upon surpassing a specific invocation threshold.
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
39 */
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
40 public final class OptimizedCallTarget extends DefaultCallTarget implements LoopCountReceiver, FrameFactory {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
41
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
42 private static final PrintStream OUT = TTY.out().out();
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
43
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
44 private final int inliningReprofileCount;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
45 private final int invalidationReprofileCount;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
46
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
47 protected OptimizedCallTarget(RootNode rootNode, FrameDescriptor descriptor, TruffleCompiler compiler, int compilationThreshold, int inliningReprofileCount, int invalidationReprofileCount) {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
48 super(rootNode, descriptor);
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
49 this.compiler = compiler;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
50 this.invokeCounter = compilationThreshold >> 7;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
51 this.loopAndInvokeCounter = compilationThreshold;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
52 this.originalInvokeCounter = compilationThreshold;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
53 this.rootNode.setCallTarget(this);
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
54 this.inliningReprofileCount = inliningReprofileCount;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
55 this.invalidationReprofileCount = invalidationReprofileCount;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
56 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
57
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
58 private InstalledCode compiledMethod;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
59 private final TruffleCompiler compiler;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
60 private int invokeCounter;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
61 private int originalInvokeCounter;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
62 private int loopAndInvokeCounter;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
63 private boolean disableCompilation;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
64
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
65 long timeCompilationStarted;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
66 long timePartialEvaluationFinished;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
67 long timeCompilationFinished;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
68 int codeSize;
10488
77016aeda39a TraceTruffleCompilation: output truffle and graal node counts
Andreas Woess <andreas.woess@jku.at>
parents: 10484
diff changeset
69 int nodeCountPartialEval;
77016aeda39a TraceTruffleCompilation: output truffle and graal node counts
Andreas Woess <andreas.woess@jku.at>
parents: 10484
diff changeset
70 int nodeCountLowered;
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
71
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
72 @Override
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
73 public Object call(PackedFrame caller, Arguments args) {
10659
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
74 if (CompilerDirectives.injectBranchProbability(CompilerDirectives.FASTPATH_PROBABILITY, compiledMethod != null)) {
10654
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
75 try {
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
76 return compiledMethod.execute(this, caller, args);
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
77 } catch (InvalidInstalledCodeException ex) {
10659
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
78 return compiledCodeInvalidated(caller, args);
10654
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
79 }
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
80 } else {
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
81 return interpreterCall(caller, args);
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
82 }
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
83 }
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
84
10659
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
85 protected Object compiledCodeInvalidated(PackedFrame caller, Arguments args) {
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
86 compiledMethod = null;
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
87 invokeCounter = invalidationReprofileCount;
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
88 if (TruffleFunctionInlining.getValue()) {
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
89 originalInvokeCounter += invalidationReprofileCount;
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
90 }
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
91 if (TraceTruffleCompilation.getValue()) {
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
92 OUT.printf("[truffle] invalidated %-48s |Alive %5.0fms\n", rootNode, (System.nanoTime() - timeCompilationFinished) / 1e6);
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
93 }
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
94 return call(caller, args);
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
95 }
63083745d390 Clean up OptimizedCallTarget and HotSpotNmethod.execute.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10654
diff changeset
96
10654
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
97 private Object interpreterCall(PackedFrame caller, Arguments args) {
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
98 invokeCounter--;
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
99 loopAndInvokeCounter--;
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
100 if (disableCompilation || loopAndInvokeCounter > 0 || invokeCounter > 0) {
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
101 return executeHelper(caller, args);
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
102 } else {
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
103 if (TruffleFunctionInlining.getValue() && inline()) {
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
104 invokeCounter = 2;
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
105 loopAndInvokeCounter = inliningReprofileCount;
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
106 originalInvokeCounter = inliningReprofileCount;
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
107 } else {
10654
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
108 compile();
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
109 }
10654
d71c56c67921 Improve performance of calling Truffle call targets.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 10643
diff changeset
110 return call(caller, args);
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
111 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
112 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
113
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
114 public boolean inline() {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
115 return new InliningHelper(this).inline();
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
116 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
117
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
118 public void compile() {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
119 try {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
120 compiledMethod = compiler.compile(this);
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
121 if (compiledMethod == null) {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
122 throw new BailoutException(String.format("code installation failed (codeSize=%s)", codeSize));
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
123 } else {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
124 if (TraceTruffleCompilation.getValue()) {
10705
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
125 int nodeCountTruffle = NodeUtil.countNodes(rootNode);
10488
77016aeda39a TraceTruffleCompilation: output truffle and graal node counts
Andreas Woess <andreas.woess@jku.at>
parents: 10484
diff changeset
126 OUT.printf("[truffle] optimized %-50s |Nodes %7d |Time %5.0f(%4.0f+%-4.0f)ms |Nodes %5d/%5d |CodeSize %d\n", rootNode, nodeCountTruffle,
77016aeda39a TraceTruffleCompilation: output truffle and graal node counts
Andreas Woess <andreas.woess@jku.at>
parents: 10484
diff changeset
127 (timeCompilationFinished - timeCompilationStarted) / 1e6, (timePartialEvaluationFinished - timeCompilationStarted) / 1e6,
77016aeda39a TraceTruffleCompilation: output truffle and graal node counts
Andreas Woess <andreas.woess@jku.at>
parents: 10484
diff changeset
128 (timeCompilationFinished - timePartialEvaluationFinished) / 1e6, nodeCountPartialEval, nodeCountLowered, codeSize);
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
129 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
130 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
131 } catch (Throwable e) {
10489
175a4900c230 OptimizedCallTarget: always disable compilation on exception; cleanup
Andreas Woess <andreas.woess@jku.at>
parents: 10488
diff changeset
132 disableCompilation = true;
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
133 if (TraceTruffleCompilation.getValue()) {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
134 if (e instanceof BailoutException) {
10489
175a4900c230 OptimizedCallTarget: always disable compilation on exception; cleanup
Andreas Woess <andreas.woess@jku.at>
parents: 10488
diff changeset
135 OUT.printf("[truffle] opt bailout %-48s %s\n", rootNode, e.getMessage());
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
136 } else {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
137 OUT.printf("[truffle] opt failed %-49s %s\n", rootNode, e.toString());
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
138 if (TraceTruffleCompilationExceptions.getValue()) {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
139 e.printStackTrace(OUT);
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
140 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
141 if (TruffleCompilationExceptionsAreFatal.getValue()) {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
142 System.exit(-1);
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
143 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
144 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
145 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
146 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
147 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
148
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
149 public Object executeHelper(PackedFrame caller, Arguments args) {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
150 VirtualFrame frame = createFrame(frameDescriptor, caller, args);
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
151 return rootNode.execute(frame);
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
152 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
153
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
154 private static FrameWithoutBoxing createFrame(FrameDescriptor descriptor, PackedFrame caller, Arguments args) {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
155 return new FrameWithoutBoxing(descriptor, caller, args);
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
156 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
157
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
158 @Override
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
159 public VirtualFrame create(FrameDescriptor descriptor, PackedFrame caller, Arguments args) {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
160 return createFrame(descriptor, caller, args);
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
161 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
162
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
163 @Override
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
164 public String toString() {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
165 return "CallTarget " + rootNode;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
166 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
167
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
168 @Override
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
169 public void reportLoopCount(int count) {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
170 loopAndInvokeCounter -= count;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
171 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
172
10488
77016aeda39a TraceTruffleCompilation: output truffle and graal node counts
Andreas Woess <andreas.woess@jku.at>
parents: 10484
diff changeset
173 private static class InliningHelper {
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
174
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
175 private final OptimizedCallTarget target;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
176
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
177 public InliningHelper(OptimizedCallTarget target) {
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
178 this.target = target;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
179 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
180
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
181 public boolean inline() {
10705
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
182 final InliningPolicy policy = new InliningPolicy(target);
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
183 if (!policy.continueInlining()) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
184 if (TraceTruffleInliningDetails.getValue()) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
185 List<InlinableCallSiteInfo> inlinableCallSites = getInlinableCallSites(target);
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
186 if (!inlinableCallSites.isEmpty()) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
187 OUT.printf("[truffle] inlining hit caller size limit (%3d >= %3d).%3d remaining call sites in %s:\n", policy.callerNodeCount, TruffleInliningMaxCallerSize.getValue(),
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
188 inlinableCallSites.size(), target.getRootNode());
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
189 policy.sortByRelevance(inlinableCallSites);
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
190 printCallSiteInfo(policy, inlinableCallSites, "");
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
191 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
192 }
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
193 return false;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
194 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
195
10705
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
196 List<InlinableCallSiteInfo> inlinableCallSites = getInlinableCallSites(target);
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
197 if (inlinableCallSites.isEmpty()) {
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
198 return false;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
199 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
200
10705
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
201 policy.sortByRelevance(inlinableCallSites);
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
202
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
203 boolean inlined = false;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
204 for (InlinableCallSiteInfo inlinableCallSite : inlinableCallSites) {
10751
7b5d7c42598b Truffle: change sorting of inlinable call sites
Andreas Woess <andreas.woess@jku.at>
parents: 10705
diff changeset
205 if (!policy.isWorthInlining(inlinableCallSite)) {
7b5d7c42598b Truffle: change sorting of inlinable call sites
Andreas Woess <andreas.woess@jku.at>
parents: 10705
diff changeset
206 break;
7b5d7c42598b Truffle: change sorting of inlinable call sites
Andreas Woess <andreas.woess@jku.at>
parents: 10705
diff changeset
207 }
7b5d7c42598b Truffle: change sorting of inlinable call sites
Andreas Woess <andreas.woess@jku.at>
parents: 10705
diff changeset
208 if (inlinableCallSite.getCallSite().inline(target)) {
10705
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
209 if (TraceTruffleInlining.getValue()) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
210 printCallSiteInfo(policy, inlinableCallSite, "inlined");
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
211 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
212 inlined = true;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
213 break;
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
214 }
10705
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
215 }
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
216
10705
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
217 if (inlined) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
218 for (InlinableCallSiteInfo callSite : inlinableCallSites) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
219 callSite.getCallSite().resetCallCount();
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
220 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
221 } else {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
222 if (TraceTruffleInliningDetails.getValue()) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
223 OUT.printf("[truffle] inlining stopped.%3d remaining call sites in %s:\n", inlinableCallSites.size(), target.getRootNode());
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
224 printCallSiteInfo(policy, inlinableCallSites, "");
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
225 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
226 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
227
10705
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
228 return inlined;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
229 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
230
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
231 private void printCallSiteInfo(InliningPolicy policy, List<InlinableCallSiteInfo> inlinableCallSites, String msg) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
232 for (InlinableCallSiteInfo candidate : inlinableCallSites) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
233 printCallSiteInfo(policy, candidate, msg);
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
234 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
235 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
236
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
237 private void printCallSiteInfo(InliningPolicy policy, InlinableCallSiteInfo callSite, String msg) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
238 String calls = String.format("%4s/%4s", callSite.getCallCount(), policy.callerInvocationCount);
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
239 String nodes = String.format("%3s/%3s", callSite.getInlineNodeCount(), policy.callerNodeCount);
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
240 OUT.printf("[truffle] %-9s %-50s |Nodes %6s |Calls %6s %7.3f |into %s\n", msg, callSite.getCallSite(), nodes, calls, policy.metric(callSite), target.getRootNode());
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
241 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
242
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
243 private static final class InliningPolicy {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
244
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
245 private final int callerNodeCount;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
246 private final int callerInvocationCount;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
247
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
248 public InliningPolicy(OptimizedCallTarget caller) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
249 this.callerNodeCount = NodeUtil.countNodes(caller.getRootNode());
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
250 this.callerInvocationCount = caller.originalInvokeCounter;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
251 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
252
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
253 public boolean continueInlining() {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
254 return callerNodeCount < TruffleInliningMaxCallerSize.getValue();
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
255 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
256
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
257 public boolean isWorthInlining(InlinableCallSiteInfo callSite) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
258 return callSite.getInlineNodeCount() <= TruffleInliningMaxCalleeSize.getValue() && callSite.getInlineNodeCount() + callerNodeCount <= TruffleInliningMaxCallerSize.getValue() &&
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
259 callSite.getCallCount() > 0;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
260 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
261
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
262 public double metric(InlinableCallSiteInfo callSite) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
263 double frequency = (double) callSite.getCallCount() / (double) callerInvocationCount;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
264 double metric = ((double) callSite.getCallCount() / (double) callSite.getInlineNodeCount()) + frequency;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
265 return metric;
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
266 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
267
10705
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
268 public void sortByRelevance(List<InlinableCallSiteInfo> inlinableCallSites) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
269 Collections.sort(inlinableCallSites, new Comparator<InlinableCallSiteInfo>() {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
270
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
271 @Override
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
272 public int compare(InlinableCallSiteInfo cs1, InlinableCallSiteInfo cs2) {
10751
7b5d7c42598b Truffle: change sorting of inlinable call sites
Andreas Woess <andreas.woess@jku.at>
parents: 10705
diff changeset
273 int result = (isWorthInlining(cs2) ? 1 : 0) - (isWorthInlining(cs1) ? 1 : 0);
7b5d7c42598b Truffle: change sorting of inlinable call sites
Andreas Woess <andreas.woess@jku.at>
parents: 10705
diff changeset
274 if (result == 0) {
7b5d7c42598b Truffle: change sorting of inlinable call sites
Andreas Woess <andreas.woess@jku.at>
parents: 10705
diff changeset
275 return Double.compare(metric(cs2), metric(cs1));
7b5d7c42598b Truffle: change sorting of inlinable call sites
Andreas Woess <andreas.woess@jku.at>
parents: 10705
diff changeset
276 }
7b5d7c42598b Truffle: change sorting of inlinable call sites
Andreas Woess <andreas.woess@jku.at>
parents: 10705
diff changeset
277 return result;
10705
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
278 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
279 });
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
280 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
281 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
282
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
283 private static final class InlinableCallSiteInfo {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
284
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
285 private final InlinableCallSite callSite;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
286 private final int callCount;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
287 private final int nodeCount;
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
288
10705
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
289 public InlinableCallSiteInfo(InlinableCallSite callSite) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
290 this.callSite = callSite;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
291 this.callCount = callSite.getCallCount();
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
292 this.nodeCount = NodeUtil.countNodes(callSite.getInlineTree());
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
293 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
294
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
295 public InlinableCallSite getCallSite() {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
296 return callSite;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
297 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
298
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
299 public int getCallCount() {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
300 return callCount;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
301 }
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
302
10705
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
303 public int getInlineNodeCount() {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
304 return nodeCount;
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
305 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
306 }
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
307
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
308 private static List<InlinableCallSiteInfo> getInlinableCallSites(final DefaultCallTarget target) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
309 final ArrayList<InlinableCallSiteInfo> inlinableCallSites = new ArrayList<>();
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
310 target.getRootNode().accept(new NodeVisitor() {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
311
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
312 @Override
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
313 public boolean visit(Node node) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
314 if (node instanceof InlinableCallSite) {
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
315 inlinableCallSites.add(new InlinableCallSiteInfo((InlinableCallSite) node));
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
316 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
317 return true;
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
318 }
10705
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
319 });
fd53f9f7007b Truffle: refactor inlining
Andreas Woess <andreas.woess@jku.at>
parents: 10659
diff changeset
320 return inlinableCallSites;
10484
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
321 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
322 }
0097fb11c16f Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
323 }