BranchProbabilityNode.java revision 12995:5e441a7ec5e3
1/*
2 * Copyright (c) 2012, 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 */
23package org.graalvm.compiler.nodes.extended;
24
25import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
26import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
27
28import org.graalvm.compiler.core.common.calc.Condition;
29import org.graalvm.compiler.debug.GraalError;
30import org.graalvm.compiler.graph.NodeClass;
31import org.graalvm.compiler.graph.iterators.NodePredicates;
32import org.graalvm.compiler.graph.spi.Simplifiable;
33import org.graalvm.compiler.graph.spi.SimplifierTool;
34import org.graalvm.compiler.nodeinfo.NodeInfo;
35import org.graalvm.compiler.nodes.FixedGuardNode;
36import org.graalvm.compiler.nodes.IfNode;
37import org.graalvm.compiler.nodes.ReturnNode;
38import org.graalvm.compiler.nodes.ValueNode;
39import org.graalvm.compiler.nodes.calc.ConditionalNode;
40import org.graalvm.compiler.nodes.calc.FloatingNode;
41import org.graalvm.compiler.nodes.calc.IntegerEqualsNode;
42import org.graalvm.compiler.nodes.spi.Lowerable;
43import org.graalvm.compiler.nodes.spi.LoweringTool;
44
45/**
46 * Instances of this node class will look for a preceding if node and put the given probability into
47 * the if node's taken probability. Then the branch probability node will be removed. This node is
48 * intended primarily for snippets, so that they can define their fast and slow paths.
49 */
50@NodeInfo(cycles = CYCLES_0, cyclesRationale = "Artificial Node", size = SIZE_0)
51public final class BranchProbabilityNode extends FloatingNode implements Simplifiable, Lowerable {
52
53    public static final NodeClass<BranchProbabilityNode> TYPE = NodeClass.create(BranchProbabilityNode.class);
54    public static final double LIKELY_PROBABILITY = 0.6;
55    public static final double NOT_LIKELY_PROBABILITY = 1 - LIKELY_PROBABILITY;
56
57    public static final double FREQUENT_PROBABILITY = 0.9;
58    public static final double NOT_FREQUENT_PROBABILITY = 1 - FREQUENT_PROBABILITY;
59
60    public static final double FAST_PATH_PROBABILITY = 0.99;
61    public static final double SLOW_PATH_PROBABILITY = 1 - FAST_PATH_PROBABILITY;
62
63    public static final double VERY_FAST_PATH_PROBABILITY = 0.999;
64    public static final double VERY_SLOW_PATH_PROBABILITY = 1 - VERY_FAST_PATH_PROBABILITY;
65
66    @Input ValueNode probability;
67    @Input ValueNode condition;
68
69    public BranchProbabilityNode(ValueNode probability, ValueNode condition) {
70        super(TYPE, condition.stamp());
71        this.probability = probability;
72        this.condition = condition;
73    }
74
75    public ValueNode getProbability() {
76        return probability;
77    }
78
79    public ValueNode getCondition() {
80        return condition;
81    }
82
83    @Override
84    public void simplify(SimplifierTool tool) {
85        if (probability.isConstant()) {
86            double probabilityValue = probability.asJavaConstant().asDouble();
87            if (probabilityValue < 0.0) {
88                throw new GraalError("A negative probability of " + probabilityValue + " is not allowed!");
89            } else if (probabilityValue > 1.0) {
90                throw new GraalError("A probability of more than 1.0 (" + probabilityValue + ") is not allowed!");
91            } else if (Double.isNaN(probabilityValue)) {
92                /*
93                 * We allow NaN if the node is in unreachable code that will eventually fall away,
94                 * or else an error will be thrown during lowering since we keep the node around.
95                 */
96                return;
97            }
98            boolean usageFound = false;
99            for (IntegerEqualsNode node : this.usages().filter(IntegerEqualsNode.class)) {
100                assert node.condition() == Condition.EQ;
101                ValueNode other = node.getX();
102                if (node.getX() == this) {
103                    other = node.getY();
104                }
105                if (other.isConstant()) {
106                    double probabilityToSet = probabilityValue;
107                    if (other.asJavaConstant().asInt() == 0) {
108                        probabilityToSet = 1.0 - probabilityToSet;
109                    }
110                    for (IfNode ifNodeUsages : node.usages().filter(IfNode.class)) {
111                        usageFound = true;
112                        ifNodeUsages.setTrueSuccessorProbability(probabilityToSet);
113                    }
114                    if (!usageFound) {
115                        usageFound = node.usages().filter(NodePredicates.isA(FixedGuardNode.class).or(ConditionalNode.class)).isNotEmpty();
116                    }
117                }
118            }
119            if (usageFound) {
120                ValueNode currentCondition = condition;
121                replaceAndDelete(currentCondition);
122                if (tool != null) {
123                    tool.addToWorkList(currentCondition.usages());
124                }
125            } else {
126                if (!isSubstitutionGraph()) {
127                    throw new GraalError("Wrong usage of branch probability injection!");
128                }
129            }
130        }
131    }
132
133    private boolean isSubstitutionGraph() {
134        return hasExactlyOneUsage() && usages().first() instanceof ReturnNode;
135    }
136
137    /**
138     * This intrinsic should only be used for the condition of an if statement. The parameter
139     * condition should also only denote a simple condition and not a combined condition involving
140     * &amp;&amp; or || operators. It injects the probability of the condition into the if
141     * statement.
142     *
143     * @param probability the probability that the given condition is true as a double value between
144     *            0.0 and 1.0.
145     * @param condition the simple condition without any &amp;&amp; or || operators
146     * @return the condition
147     */
148    @NodeIntrinsic
149    public static native boolean probability(double probability, boolean condition);
150
151    @Override
152    public void lower(LoweringTool tool) {
153        throw new GraalError("Branch probability could not be injected, because the probability value did not reduce to a constant value.");
154    }
155}
156