1 /*
   2  * Copyright (c) 2009, 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.calc;
  24 
  25 import org.graalvm.compiler.core.common.type.Stamp;
  26 import org.graalvm.compiler.graph.NodeClass;
  27 import org.graalvm.compiler.graph.spi.Canonicalizable;
  28 import org.graalvm.compiler.nodeinfo.NodeInfo;
  29 import org.graalvm.compiler.nodes.ValueNode;
  30 
  31 /**
  32  * The {@code BinaryNode} class is the base of arithmetic and logic operations with two inputs.
  33  */
  34 @NodeInfo
  35 public abstract class BinaryNode extends FloatingNode implements Canonicalizable.Binary<ValueNode> {
  36 
  37     public static final NodeClass<BinaryNode> TYPE = NodeClass.create(BinaryNode.class);
  38     @Input protected ValueNode x;
  39     @Input protected ValueNode y;
  40 
  41     @Override
  42     public ValueNode getX() {
  43         return x;
  44     }
  45 
  46     @Override
  47     public ValueNode getY() {
  48         return y;
  49     }
  50 
  51     public void setX(ValueNode x) {
  52         updateUsages(this.x, x);
  53         this.x = x;
  54     }
  55 
  56     public void setY(ValueNode y) {
  57         updateUsages(this.y, y);
  58         this.y = y;
  59     }
  60 
  61     /**
  62      * Creates a new BinaryNode instance.
  63      *
  64      * @param stamp the result type of this instruction
  65      * @param x the first input instruction
  66      * @param y the second input instruction
  67      */
  68     protected BinaryNode(NodeClass<? extends BinaryNode> c, Stamp stamp, ValueNode x, ValueNode y) {
  69         super(c, stamp);
  70         this.x = x;
  71         this.y = y;
  72     }
  73 
  74     @Override
  75     public boolean inferStamp() {
  76         return updateStamp(foldStamp(getX().stamp(), getY().stamp()));
  77     }
  78 
  79     /**
  80      * Compute an improved for this node using the passed in stamps. The stamps must be compatible
  81      * with the current values of {@link #x} and {@link #y}. This code is used to provide the
  82      * default implementation of {@link #inferStamp()} and may be used by external optimizations.
  83      *
  84      * @param stampX
  85      * @param stampY
  86      */
  87     public abstract Stamp foldStamp(Stamp stampX, Stamp stampY);
  88 }