|
1 /* |
|
2 * Copyright (c) 2017, 2017, 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.graph.NodeStack; |
|
26 import org.graalvm.compiler.nodes.AbstractBeginNode; |
|
27 import org.graalvm.compiler.nodes.AbstractDeoptimizeNode; |
|
28 import org.graalvm.compiler.nodes.AbstractEndNode; |
|
29 import org.graalvm.compiler.nodes.AbstractMergeNode; |
|
30 import org.graalvm.compiler.nodes.ControlSplitNode; |
|
31 import org.graalvm.compiler.nodes.FixedNode; |
|
32 import org.graalvm.compiler.nodes.StructuredGraph; |
|
33 import org.graalvm.compiler.phases.BasePhase; |
|
34 import org.graalvm.compiler.phases.tiers.PhaseContext; |
|
35 import org.graalvm.util.EconomicMap; |
|
36 import org.graalvm.util.EconomicSet; |
|
37 import org.graalvm.util.MapCursor; |
|
38 |
|
39 /** |
|
40 * This phase will make sure that the branch leading towards this deopt has 0.0 probability. |
|
41 * |
|
42 */ |
|
43 public class PropagateDeoptimizeProbabilityPhase extends BasePhase<PhaseContext> { |
|
44 |
|
45 @Override |
|
46 @SuppressWarnings("try") |
|
47 protected void run(final StructuredGraph graph, PhaseContext context) { |
|
48 assert !graph.hasValueProxies() : "ConvertDeoptimizeToGuardPhase always creates proxies"; |
|
49 |
|
50 if (graph.hasNode(AbstractDeoptimizeNode.TYPE)) { |
|
51 |
|
52 NodeStack stack = new NodeStack(); |
|
53 EconomicMap<ControlSplitNode, EconomicSet<AbstractBeginNode>> reachableSplits = EconomicMap.create(); |
|
54 |
|
55 // Mark all control flow nodes that are post-dominated by a deoptimization. |
|
56 for (AbstractDeoptimizeNode d : graph.getNodes(AbstractDeoptimizeNode.TYPE)) { |
|
57 stack.push(AbstractBeginNode.prevBegin(d)); |
|
58 while (!stack.isEmpty()) { |
|
59 AbstractBeginNode beginNode = (AbstractBeginNode) stack.pop(); |
|
60 FixedNode fixedNode = (FixedNode) beginNode.predecessor(); |
|
61 |
|
62 if (fixedNode == null) { |
|
63 // Can happen for start node. |
|
64 } else if (fixedNode instanceof AbstractMergeNode) { |
|
65 AbstractMergeNode mergeNode = (AbstractMergeNode) fixedNode; |
|
66 for (AbstractEndNode end : mergeNode.forwardEnds()) { |
|
67 AbstractBeginNode newBeginNode = AbstractBeginNode.prevBegin(end); |
|
68 stack.push(newBeginNode); |
|
69 } |
|
70 } else if (fixedNode instanceof ControlSplitNode) { |
|
71 ControlSplitNode controlSplitNode = (ControlSplitNode) fixedNode; |
|
72 EconomicSet<AbstractBeginNode> reachableSuccessors = reachableSplits.get(controlSplitNode); |
|
73 if (reachableSuccessors == null) { |
|
74 reachableSuccessors = EconomicSet.create(); |
|
75 reachableSplits.put(controlSplitNode, reachableSuccessors); |
|
76 } |
|
77 |
|
78 if (controlSplitNode.getSuccessorCount() == reachableSuccessors.size() - 1) { |
|
79 // All successors of this split lead to deopt, propagate reachability |
|
80 // further upwards. |
|
81 reachableSplits.removeKey(controlSplitNode); |
|
82 stack.push(AbstractBeginNode.prevBegin((FixedNode) controlSplitNode.predecessor())); |
|
83 } else { |
|
84 reachableSuccessors.add(beginNode); |
|
85 } |
|
86 } else { |
|
87 stack.push(AbstractBeginNode.prevBegin(fixedNode)); |
|
88 } |
|
89 } |
|
90 } |
|
91 |
|
92 // Make sure the probability on the path towards the deoptimization is 0.0. |
|
93 MapCursor<ControlSplitNode, EconomicSet<AbstractBeginNode>> entries = reachableSplits.getEntries(); |
|
94 while (entries.advance()) { |
|
95 ControlSplitNode controlSplitNode = entries.getKey(); |
|
96 EconomicSet<AbstractBeginNode> value = entries.getValue(); |
|
97 for (AbstractBeginNode begin : value) { |
|
98 double probability = controlSplitNode.probability(begin); |
|
99 if (probability != 0.0) { |
|
100 controlSplitNode.setProbability(begin, 0.0); |
|
101 } |
|
102 } |
|
103 } |
|
104 } |
|
105 } |
|
106 } |