001/*
002 * Copyright (c) 2013, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.phases.common;
024
025import jdk.internal.jvmci.common.*;
026
027import com.oracle.graal.graph.*;
028import com.oracle.graal.nodes.*;
029import com.oracle.graal.nodes.calc.*;
030import com.oracle.graal.phases.*;
031
032public class ExpandLogicPhase extends Phase {
033
034    @Override
035    protected void run(StructuredGraph graph) {
036        for (ShortCircuitOrNode logic : graph.getNodes(ShortCircuitOrNode.TYPE)) {
037            processBinary(logic);
038        }
039        assert graph.getNodes(ShortCircuitOrNode.TYPE).isEmpty();
040    }
041
042    private static void processBinary(ShortCircuitOrNode binary) {
043        while (binary.usages().isNotEmpty()) {
044            Node usage = binary.usages().first();
045            if (usage instanceof ShortCircuitOrNode) {
046                processBinary((ShortCircuitOrNode) usage);
047            } else if (usage instanceof IfNode) {
048                processIf(binary.getX(), binary.isXNegated(), binary.getY(), binary.isYNegated(), (IfNode) usage, binary.getShortCircuitProbability());
049            } else if (usage instanceof ConditionalNode) {
050                processConditional(binary.getX(), binary.isXNegated(), binary.getY(), binary.isYNegated(), (ConditionalNode) usage);
051            } else {
052                throw JVMCIError.shouldNotReachHere();
053            }
054        }
055        binary.safeDelete();
056    }
057
058    private static void processIf(LogicNode x, boolean xNegated, LogicNode y, boolean yNegated, IfNode ifNode, double shortCircuitProbability) {
059        AbstractBeginNode trueTarget = ifNode.trueSuccessor();
060        AbstractBeginNode falseTarget = ifNode.falseSuccessor();
061        double firstIfProbability = shortCircuitProbability;
062        /*
063         * P(Y | not(X)) = P(Y inter not(X)) / P(not(X)) = (P(X union Y) - P(X)) / (1 - P(X))
064         *
065         * P(X) = shortCircuitProbability
066         *
067         * P(X union Y) = ifNode.probability(trueTarget)
068         */
069        double secondIfProbability = (ifNode.probability(trueTarget) - shortCircuitProbability) / (1 - shortCircuitProbability);
070        secondIfProbability = Math.min(1.0, Math.max(0.0, secondIfProbability));
071        if (Double.isNaN(secondIfProbability)) {
072            secondIfProbability = 0.5;
073        }
074        ifNode.clearSuccessors();
075        Graph graph = ifNode.graph();
076        AbstractMergeNode trueTargetMerge = graph.add(new MergeNode());
077        trueTargetMerge.setNext(trueTarget);
078        EndNode firstTrueEnd = graph.add(new EndNode());
079        EndNode secondTrueEnd = graph.add(new EndNode());
080        trueTargetMerge.addForwardEnd(firstTrueEnd);
081        trueTargetMerge.addForwardEnd(secondTrueEnd);
082        AbstractBeginNode firstTrueTarget = BeginNode.begin(firstTrueEnd);
083        AbstractBeginNode secondTrueTarget = BeginNode.begin(secondTrueEnd);
084        AbstractBeginNode secondIf = BeginNode.begin(graph.add(new IfNode(y, yNegated ? falseTarget : secondTrueTarget, yNegated ? secondTrueTarget : falseTarget, secondIfProbability)));
085        IfNode firstIf = graph.add(new IfNode(x, xNegated ? secondIf : firstTrueTarget, xNegated ? firstTrueTarget : secondIf, firstIfProbability));
086        ifNode.replaceAtPredecessor(firstIf);
087        ifNode.safeDelete();
088    }
089
090    private static void processConditional(LogicNode x, boolean xNegated, LogicNode y, boolean yNegated, ConditionalNode conditional) {
091        ValueNode trueTarget = conditional.trueValue();
092        ValueNode falseTarget = conditional.falseValue();
093        Graph graph = conditional.graph();
094        ConditionalNode secondConditional = graph.unique(new ConditionalNode(y, yNegated ? falseTarget : trueTarget, yNegated ? trueTarget : falseTarget));
095        ConditionalNode firstConditional = graph.unique(new ConditionalNode(x, xNegated ? secondConditional : trueTarget, xNegated ? trueTarget : secondConditional));
096        conditional.replaceAndDelete(firstConditional);
097    }
098}