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