1 /*
   2  * Copyright (c) 2013, 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.nodes;
  24 
  25 import static org.graalvm.compiler.nodeinfo.InputType.Condition;
  26 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
  27 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
  28 
  29 import org.graalvm.compiler.graph.IterableNodeType;
  30 import org.graalvm.compiler.graph.NodeClass;
  31 import org.graalvm.compiler.graph.spi.Canonicalizable;
  32 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  33 import org.graalvm.compiler.nodeinfo.NodeInfo;
  34 
  35 @NodeInfo(cycles = CYCLES_0, size = SIZE_0)
  36 public final class ShortCircuitOrNode extends LogicNode implements IterableNodeType, Canonicalizable.Binary<LogicNode> {
  37 
  38     public static final NodeClass<ShortCircuitOrNode> TYPE = NodeClass.create(ShortCircuitOrNode.class);
  39     @Input(Condition) LogicNode x;
  40     @Input(Condition) LogicNode y;
  41     protected boolean xNegated;
  42     protected boolean yNegated;
  43     protected double shortCircuitProbability;
  44 
  45     public ShortCircuitOrNode(LogicNode x, boolean xNegated, LogicNode y, boolean yNegated, double shortCircuitProbability) {
  46         super(TYPE);
  47         this.x = x;
  48         this.xNegated = xNegated;
  49         this.y = y;
  50         this.yNegated = yNegated;
  51         this.shortCircuitProbability = shortCircuitProbability;
  52     }
  53 
  54     @Override
  55     public LogicNode getX() {
  56         return x;
  57     }
  58 
  59     @Override
  60     public LogicNode getY() {
  61         return y;
  62     }
  63 
  64     public boolean isXNegated() {
  65         return xNegated;
  66     }
  67 
  68     public boolean isYNegated() {
  69         return yNegated;
  70     }
  71 
  72     /**
  73      * Gets the probability that the {@link #getY() y} part of this binary node is <b>not</b>
  74      * evaluated. This is the probability that this operator will short-circuit its execution.
  75      */
  76     public double getShortCircuitProbability() {
  77         return shortCircuitProbability;
  78     }
  79 
  80     protected ShortCircuitOrNode canonicalizeNegation(LogicNode forX, LogicNode forY) {
  81         LogicNode xCond = forX;
  82         boolean xNeg = xNegated;
  83         while (xCond instanceof LogicNegationNode) {
  84             xCond = ((LogicNegationNode) xCond).getValue();
  85             xNeg = !xNeg;
  86         }
  87 
  88         LogicNode yCond = forY;
  89         boolean yNeg = yNegated;
  90         while (yCond instanceof LogicNegationNode) {
  91             yCond = ((LogicNegationNode) yCond).getValue();
  92             yNeg = !yNeg;
  93         }
  94 
  95         if (xCond != forX || yCond != forY) {
  96             return new ShortCircuitOrNode(xCond, xNeg, yCond, yNeg, shortCircuitProbability);
  97         } else {
  98             return this;
  99         }
 100     }
 101 
 102     @Override
 103     public LogicNode canonical(CanonicalizerTool tool, LogicNode forX, LogicNode forY) {
 104         ShortCircuitOrNode ret = canonicalizeNegation(forX, forY);
 105         if (ret != this) {
 106             return ret;
 107         }
 108 
 109         if (forX == forY) {
 110             // @formatter:off
 111             //  a ||  a = a
 112             //  a || !a = true
 113             // !a ||  a = true
 114             // !a || !a = !a
 115             // @formatter:on
 116             if (isXNegated()) {
 117                 if (isYNegated()) {
 118                     // !a || !a = !a
 119                     return LogicNegationNode.create(forX);
 120                 } else {
 121                     // !a || a = true
 122                     return LogicConstantNode.tautology();
 123                 }
 124             } else {
 125                 if (isYNegated()) {
 126                     // a || !a = true
 127                     return LogicConstantNode.tautology();
 128                 } else {
 129                     // a || a = a
 130                     return forX;
 131                 }
 132             }
 133         }
 134         if (forX instanceof LogicConstantNode) {
 135             if (((LogicConstantNode) forX).getValue() ^ isXNegated()) {
 136                 return LogicConstantNode.tautology();
 137             } else {
 138                 if (isYNegated()) {
 139                     return new LogicNegationNode(forY);
 140                 } else {
 141                     return forY;
 142                 }
 143             }
 144         }
 145         if (forY instanceof LogicConstantNode) {
 146             if (((LogicConstantNode) forY).getValue() ^ isYNegated()) {
 147                 return LogicConstantNode.tautology();
 148             } else {
 149                 if (isXNegated()) {
 150                     return new LogicNegationNode(forX);
 151                 } else {
 152                     return forX;
 153                 }
 154             }
 155         }
 156         return this;
 157     }
 158 }