1 /* 2 * Copyright (c) 2011, 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 24 25 package org.graalvm.compiler.java; 26 27 import static org.graalvm.compiler.nodes.cfg.ControlFlowGraph.multiplyRelativeFrequencies; 28 29 import java.util.List; 30 31 import jdk.internal.vm.compiler.collections.EconomicMap; 32 import org.graalvm.compiler.nodes.AbstractBeginNode; 33 import org.graalvm.compiler.nodes.AbstractMergeNode; 34 import org.graalvm.compiler.nodes.ControlSplitNode; 35 import org.graalvm.compiler.nodes.FixedNode; 36 import org.graalvm.compiler.nodes.LoopBeginNode; 37 import org.graalvm.compiler.nodes.LoopExitNode; 38 import org.graalvm.compiler.nodes.StructuredGraph; 39 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph; 40 import org.graalvm.compiler.phases.Phase; 41 import org.graalvm.compiler.phases.graph.ReentrantNodeIterator; 42 43 public final class ComputeLoopFrequenciesClosure extends ReentrantNodeIterator.NodeIteratorClosure<Double> { 44 45 private static final ComputeLoopFrequenciesClosure INSTANCE = new ComputeLoopFrequenciesClosure(); 46 47 private ComputeLoopFrequenciesClosure() { 48 // nothing to do 49 } 50 51 @Override 52 protected Double processNode(FixedNode node, Double currentState) { 53 // normal nodes never change the probability of a path 54 return currentState; 55 } 56 57 @Override 58 protected Double merge(AbstractMergeNode merge, List<Double> states) { 59 // a merge has the sum of all predecessor probabilities 60 double result = 0.0; 61 for (double d : states) { 62 result += d; 63 } 64 return result; 65 } 66 67 @Override 68 protected Double afterSplit(AbstractBeginNode node, Double oldState) { 69 // a control split splits up the probability 70 ControlSplitNode split = (ControlSplitNode) node.predecessor(); 71 return oldState * split.probability(node); 72 } 73 74 @Override 75 protected EconomicMap<LoopExitNode, Double> processLoop(LoopBeginNode loop, Double initialState) { 76 EconomicMap<LoopExitNode, Double> exitStates = ReentrantNodeIterator.processLoop(this, loop, 1D).exitStates; 77 78 double exitRelativeFrequency = 0.0; 79 for (double d : exitStates.getValues()) { 80 exitRelativeFrequency += d; 81 } 82 exitRelativeFrequency = Math.min(1.0, exitRelativeFrequency); 83 exitRelativeFrequency = Math.max(ControlFlowGraph.MIN_RELATIVE_FREQUENCY, exitRelativeFrequency); 84 double loopFrequency = 1.0 / exitRelativeFrequency; 85 loop.setLoopFrequency(loopFrequency); 86 87 double adjustmentFactor = initialState * loopFrequency; 88 exitStates.replaceAll((exitNode, frequency) -> multiplyRelativeFrequencies(frequency, adjustmentFactor)); 89 90 return exitStates; 91 } 92 93 /** 94 * Computes the frequencies of all loops in the given graph. This is done by performing a 95 * reverse postorder iteration and computing the probability of all fixed nodes. The combined 96 * probability of all exits of a loop can be used to compute the loop's expected frequency. 97 */ 98 public static void compute(StructuredGraph graph) { 99 if (graph.hasLoops()) { 100 ReentrantNodeIterator.apply(INSTANCE, graph.start(), 1D); 101 } 102 } 103 104 public static class ComputeLoopFrequencyPhase extends Phase { 105 106 @Override 107 protected void run(StructuredGraph graph) { 108 compute(graph); 109 } 110 } 111 112 public static final ComputeLoopFrequencyPhase PHASE_INSTANCE = new ComputeLoopFrequencyPhase(); 113 114 }