43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 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 |
package org.graalvm.compiler.phases.graph;
|
|
24 |
|
48861
|
25 |
import static org.graalvm.compiler.nodes.cfg.ControlFlowGraph.multiplyProbabilities;
|
|
26 |
|
43972
|
27 |
import java.util.function.ToDoubleFunction;
|
|
28 |
|
48861
|
29 |
import org.graalvm.collections.EconomicMap;
|
|
30 |
import org.graalvm.collections.Equivalence;
|
46640
|
31 |
import org.graalvm.compiler.debug.CounterKey;
|
|
32 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
33 |
import org.graalvm.compiler.graph.Node;
|
|
34 |
import org.graalvm.compiler.graph.NodeInputList;
|
|
35 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
|
36 |
import org.graalvm.compiler.nodes.AbstractEndNode;
|
|
37 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
|
38 |
import org.graalvm.compiler.nodes.ControlSplitNode;
|
|
39 |
import org.graalvm.compiler.nodes.EndNode;
|
|
40 |
import org.graalvm.compiler.nodes.FixedNode;
|
|
41 |
import org.graalvm.compiler.nodes.LoopBeginNode;
|
|
42 |
import org.graalvm.compiler.nodes.StartNode;
|
46963
|
43 |
|
43972
|
44 |
/**
|
|
45 |
* Compute probabilities for fixed nodes on the fly and cache them at {@link AbstractBeginNode}s.
|
|
46 |
*/
|
|
47 |
public class FixedNodeProbabilityCache implements ToDoubleFunction<FixedNode> {
|
|
48 |
|
46640
|
49 |
private static final CounterKey computeNodeProbabilityCounter = DebugContext.counter("ComputeNodeProbability");
|
43972
|
50 |
|
46344
|
51 |
private final EconomicMap<FixedNode, Double> cache = EconomicMap.create(Equivalence.IDENTITY);
|
43972
|
52 |
|
|
53 |
/**
|
|
54 |
* <p>
|
|
55 |
* Given a {@link FixedNode} this method finds the most immediate {@link AbstractBeginNode}
|
|
56 |
* preceding it that either:
|
|
57 |
* <ul>
|
|
58 |
* <li>has no predecessor (ie, the begin-node is a merge, in particular a loop-begin, or the
|
|
59 |
* start-node)</li>
|
|
60 |
* <li>has a control-split predecessor</li>
|
|
61 |
* </ul>
|
|
62 |
* </p>
|
|
63 |
*
|
|
64 |
* <p>
|
|
65 |
* The thus found {@link AbstractBeginNode} is equi-probable with the {@link FixedNode} it was
|
|
66 |
* obtained from. When computed for the first time (afterwards a cache lookup returns it) that
|
|
67 |
* probability is computed as follows, again depending on the begin-node's predecessor:
|
|
68 |
* <ul>
|
|
69 |
* <li>No predecessor. In this case the begin-node is either:</li>
|
|
70 |
* <ul>
|
|
71 |
* <li>a merge-node, whose probability adds up those of its forward-ends</li>
|
|
72 |
* <li>a loop-begin, with probability as above multiplied by the loop-frequency</li>
|
|
73 |
* </ul>
|
|
74 |
* <li>Control-split predecessor: probability of the branch times that of the control-split</li>
|
|
75 |
* </ul>
|
|
76 |
* </p>
|
|
77 |
*
|
|
78 |
* <p>
|
|
79 |
* As an exception to all the above, a probability of 1 is assumed for a {@link FixedNode} that
|
|
80 |
* appears to be dead-code (ie, lacks a predecessor).
|
|
81 |
* </p>
|
|
82 |
*
|
|
83 |
*/
|
|
84 |
@Override
|
|
85 |
public double applyAsDouble(FixedNode node) {
|
|
86 |
assert node != null;
|
46640
|
87 |
computeNodeProbabilityCounter.increment(node.getDebug());
|
43972
|
88 |
|
|
89 |
FixedNode current = findBegin(node);
|
|
90 |
if (current == null) {
|
|
91 |
// this should only appear for dead code
|
|
92 |
return 1D;
|
|
93 |
}
|
|
94 |
|
|
95 |
assert current instanceof AbstractBeginNode;
|
|
96 |
Double cachedValue = cache.get(current);
|
|
97 |
if (cachedValue != null) {
|
|
98 |
return cachedValue;
|
|
99 |
}
|
|
100 |
|
|
101 |
double probability = 0.0;
|
|
102 |
if (current.predecessor() == null) {
|
|
103 |
if (current instanceof AbstractMergeNode) {
|
|
104 |
probability = handleMerge(current, probability);
|
|
105 |
} else {
|
|
106 |
assert current instanceof StartNode;
|
|
107 |
probability = 1D;
|
|
108 |
}
|
|
109 |
} else {
|
|
110 |
ControlSplitNode split = (ControlSplitNode) current.predecessor();
|
46963
|
111 |
probability = multiplyProbabilities(split.probability((AbstractBeginNode) current), applyAsDouble(split));
|
43972
|
112 |
}
|
|
113 |
assert !Double.isNaN(probability) && !Double.isInfinite(probability) : current + " " + probability;
|
|
114 |
cache.put(current, probability);
|
|
115 |
return probability;
|
|
116 |
}
|
|
117 |
|
|
118 |
private double handleMerge(FixedNode current, double probability) {
|
|
119 |
double result = probability;
|
|
120 |
AbstractMergeNode currentMerge = (AbstractMergeNode) current;
|
|
121 |
NodeInputList<EndNode> currentForwardEnds = currentMerge.forwardEnds();
|
|
122 |
/*
|
|
123 |
* Use simple iteration instead of streams, since the stream infrastructure adds many frames
|
|
124 |
* which causes the recursion to overflow the stack earlier than it would otherwise.
|
|
125 |
*/
|
|
126 |
for (AbstractEndNode endNode : currentForwardEnds) {
|
|
127 |
result += applyAsDouble(endNode);
|
|
128 |
}
|
|
129 |
if (current instanceof LoopBeginNode) {
|
46963
|
130 |
result = multiplyProbabilities(result, ((LoopBeginNode) current).loopFrequency());
|
43972
|
131 |
}
|
|
132 |
return result;
|
|
133 |
}
|
|
134 |
|
|
135 |
private static FixedNode findBegin(FixedNode node) {
|
|
136 |
FixedNode current = node;
|
|
137 |
while (true) {
|
|
138 |
assert current != null;
|
|
139 |
Node predecessor = current.predecessor();
|
|
140 |
if (current instanceof AbstractBeginNode) {
|
|
141 |
if (predecessor == null) {
|
|
142 |
break;
|
|
143 |
} else if (predecessor.successors().count() != 1) {
|
|
144 |
assert predecessor instanceof ControlSplitNode : "a FixedNode with multiple successors needs to be a ControlSplitNode: " + current + " / " + predecessor;
|
|
145 |
break;
|
|
146 |
}
|
|
147 |
} else if (predecessor == null) {
|
|
148 |
current = null;
|
|
149 |
break;
|
|
150 |
}
|
|
151 |
current = (FixedNode) predecessor;
|
|
152 |
}
|
|
153 |
return current;
|
|
154 |
}
|
|
155 |
}
|