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