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