43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, 2013, 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.common;
|
|
24 |
|
|
25 |
import org.graalvm.compiler.debug.GraalError;
|
|
26 |
import org.graalvm.compiler.graph.Graph;
|
|
27 |
import org.graalvm.compiler.graph.Node;
|
|
28 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
|
29 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
|
30 |
import org.graalvm.compiler.nodes.BeginNode;
|
|
31 |
import org.graalvm.compiler.nodes.EndNode;
|
|
32 |
import org.graalvm.compiler.nodes.IfNode;
|
|
33 |
import org.graalvm.compiler.nodes.LogicNode;
|
|
34 |
import org.graalvm.compiler.nodes.MergeNode;
|
|
35 |
import org.graalvm.compiler.nodes.ShortCircuitOrNode;
|
|
36 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
37 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
38 |
import org.graalvm.compiler.nodes.calc.ConditionalNode;
|
|
39 |
import org.graalvm.compiler.phases.Phase;
|
|
40 |
|
|
41 |
public class ExpandLogicPhase extends Phase {
|
|
42 |
|
|
43 |
@Override
|
|
44 |
protected void run(StructuredGraph graph) {
|
|
45 |
for (ShortCircuitOrNode logic : graph.getNodes(ShortCircuitOrNode.TYPE)) {
|
|
46 |
processBinary(logic);
|
|
47 |
}
|
|
48 |
assert graph.getNodes(ShortCircuitOrNode.TYPE).isEmpty();
|
46344
|
49 |
graph.setAllowShortCircuitOr(false);
|
43972
|
50 |
}
|
|
51 |
|
|
52 |
private static void processBinary(ShortCircuitOrNode binary) {
|
|
53 |
while (binary.usages().isNotEmpty()) {
|
|
54 |
Node usage = binary.usages().first();
|
|
55 |
if (usage instanceof ShortCircuitOrNode) {
|
|
56 |
processBinary((ShortCircuitOrNode) usage);
|
|
57 |
} else if (usage instanceof IfNode) {
|
|
58 |
processIf(binary.getX(), binary.isXNegated(), binary.getY(), binary.isYNegated(), (IfNode) usage, binary.getShortCircuitProbability());
|
|
59 |
} else if (usage instanceof ConditionalNode) {
|
|
60 |
processConditional(binary.getX(), binary.isXNegated(), binary.getY(), binary.isYNegated(), (ConditionalNode) usage);
|
|
61 |
} else {
|
|
62 |
throw GraalError.shouldNotReachHere();
|
|
63 |
}
|
|
64 |
}
|
|
65 |
binary.safeDelete();
|
|
66 |
}
|
|
67 |
|
|
68 |
private static void processIf(LogicNode x, boolean xNegated, LogicNode y, boolean yNegated, IfNode ifNode, double shortCircuitProbability) {
|
|
69 |
AbstractBeginNode trueTarget = ifNode.trueSuccessor();
|
|
70 |
AbstractBeginNode falseTarget = ifNode.falseSuccessor();
|
|
71 |
double firstIfProbability = shortCircuitProbability;
|
|
72 |
/*
|
|
73 |
* P(Y | not(X)) = P(Y inter not(X)) / P(not(X)) = (P(X union Y) - P(X)) / (1 - P(X))
|
|
74 |
*
|
|
75 |
* P(X) = shortCircuitProbability
|
|
76 |
*
|
|
77 |
* P(X union Y) = ifNode.probability(trueTarget)
|
|
78 |
*/
|
|
79 |
double secondIfProbability = (ifNode.probability(trueTarget) - shortCircuitProbability) / (1 - shortCircuitProbability);
|
|
80 |
secondIfProbability = Math.min(1.0, Math.max(0.0, secondIfProbability));
|
|
81 |
if (Double.isNaN(secondIfProbability)) {
|
|
82 |
secondIfProbability = 0.5;
|
|
83 |
}
|
|
84 |
ifNode.clearSuccessors();
|
|
85 |
Graph graph = ifNode.graph();
|
|
86 |
AbstractMergeNode trueTargetMerge = graph.add(new MergeNode());
|
|
87 |
trueTargetMerge.setNext(trueTarget);
|
|
88 |
EndNode firstTrueEnd = graph.add(new EndNode());
|
|
89 |
EndNode secondTrueEnd = graph.add(new EndNode());
|
|
90 |
trueTargetMerge.addForwardEnd(firstTrueEnd);
|
|
91 |
trueTargetMerge.addForwardEnd(secondTrueEnd);
|
|
92 |
AbstractBeginNode firstTrueTarget = BeginNode.begin(firstTrueEnd);
|
|
93 |
AbstractBeginNode secondTrueTarget = BeginNode.begin(secondTrueEnd);
|
46344
|
94 |
if (yNegated) {
|
|
95 |
secondIfProbability = 1.0 - secondIfProbability;
|
|
96 |
}
|
|
97 |
if (xNegated) {
|
|
98 |
firstIfProbability = 1.0 - firstIfProbability;
|
|
99 |
}
|
43972
|
100 |
AbstractBeginNode secondIf = BeginNode.begin(graph.add(new IfNode(y, yNegated ? falseTarget : secondTrueTarget, yNegated ? secondTrueTarget : falseTarget, secondIfProbability)));
|
|
101 |
IfNode firstIf = graph.add(new IfNode(x, xNegated ? secondIf : firstTrueTarget, xNegated ? firstTrueTarget : secondIf, firstIfProbability));
|
|
102 |
ifNode.replaceAtPredecessor(firstIf);
|
|
103 |
ifNode.safeDelete();
|
|
104 |
}
|
|
105 |
|
|
106 |
private static void processConditional(LogicNode x, boolean xNegated, LogicNode y, boolean yNegated, ConditionalNode conditional) {
|
|
107 |
ValueNode trueTarget = conditional.trueValue();
|
|
108 |
ValueNode falseTarget = conditional.falseValue();
|
|
109 |
Graph graph = conditional.graph();
|
|
110 |
ConditionalNode secondConditional = graph.unique(new ConditionalNode(y, yNegated ? falseTarget : trueTarget, yNegated ? trueTarget : falseTarget));
|
|
111 |
ConditionalNode firstConditional = graph.unique(new ConditionalNode(x, xNegated ? secondConditional : trueTarget, xNegated ? trueTarget : secondConditional));
|
|
112 |
conditional.replaceAndDelete(firstConditional);
|
|
113 |
}
|
|
114 |
|
|
115 |
@Override
|
|
116 |
public boolean checkContract() {
|
|
117 |
return false;
|
|
118 |
}
|
|
119 |
}
|