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.nodes;
  24 
  25 import org.graalvm.compiler.core.common.type.Stamp;
  26 import org.graalvm.compiler.graph.Graph;
  27 import org.graalvm.compiler.graph.Node;
  28 import org.graalvm.compiler.graph.NodeClass;
  29 import org.graalvm.compiler.graph.spi.Canonicalizable;
  30 import org.graalvm.compiler.nodeinfo.NodeInfo;
  31 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  32 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  33 
  34 import jdk.vm.ci.meta.TriState;
  35 
  36 @NodeInfo
  37 public abstract class BinaryOpLogicNode extends LogicNode implements LIRLowerable, Canonicalizable.Binary<ValueNode> {
  38 
  39     public static final NodeClass<BinaryOpLogicNode> TYPE = NodeClass.create(BinaryOpLogicNode.class);
  40     @Input protected ValueNode x;
  41     @Input protected ValueNode y;
  42 
  43     @Override
  44     public ValueNode getX() {
  45         return x;
  46     }
  47 
  48     @Override
  49     public ValueNode getY() {
  50         return y;
  51     }
  52 
  53     public BinaryOpLogicNode(NodeClass<? extends BinaryOpLogicNode> c, ValueNode x, ValueNode y) {
  54         super(c);
  55         assert x != null && y != null;
  56         this.x = x;
  57         this.y = y;
  58     }
  59 
  60     @Override
  61     public boolean verify() {
  62         return super.verify();
  63     }
  64 
  65     @Override
  66     public void generate(NodeLIRBuilderTool gen) {
  67     }
  68 
  69     /**
  70      * Ensure a canonical ordering of inputs for commutative nodes to improve GVN results. Order the
  71      * inputs by increasing {@link Node#id} and call {@link Graph#findDuplicate(Node)} on the node
  72      * if it's currently in a graph.
  73      *
  74      * @return the original node or another node with the same inputs, ignoring ordering.
  75      */
  76     @SuppressWarnings("deprecation")
  77     public LogicNode maybeCommuteInputs() {
  78         assert this instanceof BinaryCommutative;
  79         if (!y.isConstant() && (x.isConstant() || x.getId() > y.getId())) {
  80             ValueNode tmp = x;
  81             x = y;
  82             y = tmp;
  83             if (graph() != null) {
  84                 // See if this node already exists
  85                 LogicNode duplicate = graph().findDuplicate(this);
  86                 if (duplicate != null) {
  87                     return duplicate;
  88                 }
  89             }
  90         }
  91         return this;
  92     }
  93 
  94     public abstract Stamp getSucceedingStampForX(boolean negated, Stamp xStamp, Stamp yStamp);
  95 
  96     public abstract Stamp getSucceedingStampForY(boolean negated, Stamp xStamp, Stamp yStamp);
  97 
  98     public abstract TriState tryFold(Stamp xStamp, Stamp yStamp);
  99 }