1 /*
   2  * Copyright (c) 2009, 2018, 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 
  24 
  25 package org.graalvm.compiler.nodes.calc;
  26 
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
  29 
  30 import java.io.Serializable;
  31 import java.util.function.Function;
  32 
  33 import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
  34 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp;
  35 import org.graalvm.compiler.core.common.type.IntegerStamp;
  36 import org.graalvm.compiler.core.common.type.Stamp;
  37 import org.graalvm.compiler.debug.GraalError;
  38 import org.graalvm.compiler.graph.Graph;
  39 import org.graalvm.compiler.graph.Node;
  40 import org.graalvm.compiler.graph.NodeClass;
  41 import org.graalvm.compiler.graph.iterators.NodePredicate;
  42 import org.graalvm.compiler.graph.spi.Canonicalizable;
  43 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  44 import org.graalvm.compiler.nodeinfo.NodeInfo;
  45 import org.graalvm.compiler.nodes.ArithmeticOperation;
  46 import org.graalvm.compiler.nodes.ConstantNode;
  47 import org.graalvm.compiler.nodes.NodeView;
  48 import org.graalvm.compiler.nodes.StructuredGraph;
  49 import org.graalvm.compiler.nodes.ValueNode;
  50 import org.graalvm.compiler.nodes.ValuePhiNode;
  51 import org.graalvm.compiler.nodes.spi.ArithmeticLIRLowerable;
  52 import org.graalvm.compiler.nodes.spi.NodeValueMap;
  53 
  54 import jdk.vm.ci.meta.Constant;
  55 
  56 @NodeInfo(cycles = CYCLES_1, size = SIZE_1)
  57 public abstract class BinaryArithmeticNode<OP> extends BinaryNode implements ArithmeticOperation, ArithmeticLIRLowerable, Canonicalizable.Binary<ValueNode> {
  58 
  59     @SuppressWarnings("rawtypes") public static final NodeClass<BinaryArithmeticNode> TYPE = NodeClass.create(BinaryArithmeticNode.class);
  60 
  61     protected interface SerializableBinaryFunction<T> extends Function<ArithmeticOpTable, BinaryOp<T>>, Serializable {
  62     }
  63 
  64     protected final SerializableBinaryFunction<OP> getOp;
  65 
  66     protected BinaryArithmeticNode(NodeClass<? extends BinaryArithmeticNode<OP>> c, SerializableBinaryFunction<OP> getOp, ValueNode x, ValueNode y) {
  67         super(c, getOp.apply(ArithmeticOpTable.forStamp(x.stamp(NodeView.DEFAULT))).foldStamp(x.stamp(NodeView.DEFAULT), y.stamp(NodeView.DEFAULT)), x, y);
  68         this.getOp = getOp;
  69     }
  70 
  71     protected final BinaryOp<OP> getOp(ValueNode forX, ValueNode forY) {
  72         ArithmeticOpTable table = ArithmeticOpTable.forStamp(forX.stamp(NodeView.DEFAULT));
  73         assert table.equals(ArithmeticOpTable.forStamp(forY.stamp(NodeView.DEFAULT)));
  74         return getOp.apply(table);
  75     }
  76 
  77     @Override
  78     public final BinaryOp<OP> getArithmeticOp() {
  79         return getOp(getX(), getY());
  80     }
  81 
  82     public boolean isAssociative() {
  83         return getArithmeticOp().isAssociative();
  84     }
  85 
  86     @Override
  87     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  88         NodeView view = NodeView.from(tool);
  89         ValueNode result = tryConstantFold(getOp(forX, forY), forX, forY, stamp(view), view);
  90         if (result != null) {
  91             return result;
  92         }
  93         if (forX instanceof ConditionalNode && forY.isConstant() && forX.hasExactlyOneUsage()) {
  94             ConditionalNode conditionalNode = (ConditionalNode) forX;
  95             BinaryOp<OP> arithmeticOp = getArithmeticOp();
  96             ConstantNode trueConstant = tryConstantFold(arithmeticOp, conditionalNode.trueValue(), forY, this.stamp(view), view);
  97             if (trueConstant != null) {
  98                 ConstantNode falseConstant = tryConstantFold(arithmeticOp, conditionalNode.falseValue(), forY, this.stamp(view), view);
  99                 if (falseConstant != null) {
 100                     // @formatter:off
 101                     /* The arithmetic is folded into a constant on both sides of the conditional.
 102                      * Example:
 103                      *            (cond ? -5 : 5) + 100
 104                      * canonicalizes to:
 105                      *            (cond ? 95 : 105)
 106                      */
 107                     // @formatter:on
 108                     return ConditionalNode.create(conditionalNode.condition, trueConstant,
 109                                     falseConstant, view);
 110                 }
 111             }
 112         }
 113         return this;
 114     }
 115 
 116     @SuppressWarnings("unused")
 117     public static <OP> ConstantNode tryConstantFold(BinaryOp<OP> op, ValueNode forX, ValueNode forY, Stamp stamp, NodeView view) {
 118         if (forX.isConstant() && forY.isConstant()) {
 119             Constant ret = op.foldConstant(forX.asConstant(), forY.asConstant());
 120             if (ret != null) {
 121                 return ConstantNode.forPrimitive(stamp, ret);
 122             }
 123         }
 124         return null;
 125     }
 126 
 127     @Override
 128     public Stamp foldStamp(Stamp stampX, Stamp stampY) {
 129         assert stampX.isCompatible(x.stamp(NodeView.DEFAULT)) && stampY.isCompatible(y.stamp(NodeView.DEFAULT));
 130         return getArithmeticOp().foldStamp(stampX, stampY);
 131     }
 132 
 133     public static ValueNode add(StructuredGraph graph, ValueNode v1, ValueNode v2, NodeView view) {
 134         return graph.addOrUniqueWithInputs(AddNode.create(v1, v2, view));
 135     }
 136 
 137     public static ValueNode add(ValueNode v1, ValueNode v2, NodeView view) {
 138         return AddNode.create(v1, v2, view);
 139     }
 140 
 141     public static ValueNode add(ValueNode v1, ValueNode v2) {
 142         return add(v1, v2, NodeView.DEFAULT);
 143     }
 144 
 145     public static ValueNode mul(StructuredGraph graph, ValueNode v1, ValueNode v2, NodeView view) {
 146         return graph.addOrUniqueWithInputs(MulNode.create(v1, v2, view));
 147     }
 148 
 149     public static ValueNode mul(ValueNode v1, ValueNode v2, NodeView view) {
 150         return MulNode.create(v1, v2, view);
 151     }
 152 
 153     public static ValueNode mul(ValueNode v1, ValueNode v2) {
 154         return mul(v1, v2, NodeView.DEFAULT);
 155     }
 156 
 157     public static ValueNode sub(StructuredGraph graph, ValueNode v1, ValueNode v2, NodeView view) {
 158         return graph.addOrUniqueWithInputs(SubNode.create(v1, v2, view));
 159     }
 160 
 161     public static ValueNode sub(ValueNode v1, ValueNode v2, NodeView view) {
 162         return SubNode.create(v1, v2, view);
 163     }
 164 
 165     public static ValueNode sub(ValueNode v1, ValueNode v2) {
 166         return sub(v1, v2, NodeView.DEFAULT);
 167     }
 168 
 169     public static ValueNode branchlessMin(ValueNode v1, ValueNode v2, NodeView view) {
 170         if (v1.isDefaultConstant() && !v2.isDefaultConstant()) {
 171             return branchlessMin(v2, v1, view);
 172         }
 173         int bits = ((IntegerStamp) v1.stamp(view)).getBits();
 174         assert ((IntegerStamp) v2.stamp(view)).getBits() == bits;
 175         ValueNode t1 = sub(v1, v2, view);
 176         ValueNode t2 = RightShiftNode.create(t1, bits - 1, view);
 177         ValueNode t3 = AndNode.create(t1, t2, view);
 178         return add(v2, t3, view);
 179     }
 180 
 181     public static ValueNode branchlessMax(ValueNode v1, ValueNode v2, NodeView view) {
 182         if (v1.isDefaultConstant() && !v2.isDefaultConstant()) {
 183             return branchlessMax(v2, v1, view);
 184         }
 185         int bits = ((IntegerStamp) v1.stamp(view)).getBits();
 186         assert ((IntegerStamp) v2.stamp(view)).getBits() == bits;
 187         if (v2.isDefaultConstant()) {
 188             // prefer a & ~(a>>31) to a - (a & (a>>31))
 189             return AndNode.create(v1, NotNode.create(RightShiftNode.create(v1, bits - 1, view)), view);
 190         } else {
 191             ValueNode t1 = sub(v1, v2, view);
 192             ValueNode t2 = RightShiftNode.create(t1, bits - 1, view);
 193             ValueNode t3 = AndNode.create(t1, t2, view);
 194             return sub(v1, t3, view);
 195         }
 196     }
 197 
 198     private enum ReassociateMatch {
 199         x,
 200         y;
 201 
 202         public ValueNode getValue(BinaryNode binary) {
 203             switch (this) {
 204                 case x:
 205                     return binary.getX();
 206                 case y:
 207                     return binary.getY();
 208                 default:
 209                     throw GraalError.shouldNotReachHere();
 210             }
 211         }
 212 
 213         public ValueNode getOtherValue(BinaryNode binary) {
 214             switch (this) {
 215                 case x:
 216                     return binary.getY();
 217                 case y:
 218                     return binary.getX();
 219                 default:
 220                     throw GraalError.shouldNotReachHere();
 221             }
 222         }
 223     }
 224 
 225     private static ReassociateMatch findReassociate(BinaryNode binary, NodePredicate criterion) {
 226         boolean resultX = criterion.apply(binary.getX());
 227         boolean resultY = criterion.apply(binary.getY());
 228         if (resultX && !resultY) {
 229             return ReassociateMatch.x;
 230         }
 231         if (!resultX && resultY) {
 232             return ReassociateMatch.y;
 233         }
 234         return null;
 235     }
 236 
 237     //@formatter:off
 238     /*
 239      * In reassociate, complexity comes from the handling of IntegerSub (non commutative) which can
 240      * be mixed with IntegerAdd. It first tries to find m1, m2 which match the criterion :
 241      * (a o m2) o m1
 242      * (m2 o a) o m1
 243      * m1 o (a o m2)
 244      * m1 o (m2 o a)
 245      * It then produces 4 boolean for the -/+ cases:
 246      * invertA : should the final expression be like *-a (rather than a+*)
 247      * aSub : should the final expression be like a-* (rather than a+*)
 248      * invertM1 : should the final expression contain -m1
 249      * invertM2 : should the final expression contain -m2
 250      *
 251      */
 252     //@formatter:on
 253     /**
 254      * Tries to re-associate values which satisfy the criterion. For example with a constantness
 255      * criterion: {@code (a + 2) + 1 => a + (1 + 2)}
 256      * <p>
 257      * This method accepts only {@linkplain BinaryOp#isAssociative() associative} operations such as
 258      * +, -, *, &amp;, | and ^
 259      *
 260      * @param forY
 261      * @param forX
 262      */
 263     public static ValueNode reassociate(BinaryArithmeticNode<?> node, NodePredicate criterion, ValueNode forX, ValueNode forY, NodeView view) {
 264         assert node.getOp(forX, forY).isAssociative();
 265         ReassociateMatch match1 = findReassociate(node, criterion);
 266         if (match1 == null) {
 267             return node;
 268         }
 269         ValueNode otherValue = match1.getOtherValue(node);
 270         boolean addSub = false;
 271         boolean subAdd = false;
 272         if (otherValue.getClass() != node.getClass()) {
 273             if (node instanceof AddNode && otherValue instanceof SubNode) {
 274                 addSub = true;
 275             } else if (node instanceof SubNode && otherValue instanceof AddNode) {
 276                 subAdd = true;
 277             } else {
 278                 return node;
 279             }
 280         }
 281         BinaryNode other = (BinaryNode) otherValue;
 282         ReassociateMatch match2 = findReassociate(other, criterion);
 283         if (match2 == null) {
 284             return node;
 285         }
 286         boolean invertA = false;
 287         boolean aSub = false;
 288         boolean invertM1 = false;
 289         boolean invertM2 = false;
 290         if (addSub) {
 291             invertM2 = match2 == ReassociateMatch.y;
 292             invertA = !invertM2;
 293         } else if (subAdd) {
 294             invertA = invertM2 = match1 == ReassociateMatch.x;
 295             invertM1 = !invertM2;
 296         } else if (node instanceof SubNode && other instanceof SubNode) {
 297             invertA = match1 == ReassociateMatch.x ^ match2 == ReassociateMatch.x;
 298             aSub = match1 == ReassociateMatch.y && match2 == ReassociateMatch.y;
 299             invertM1 = match1 == ReassociateMatch.y && match2 == ReassociateMatch.x;
 300             invertM2 = match1 == ReassociateMatch.x && match2 == ReassociateMatch.x;
 301         }
 302         assert !(invertM1 && invertM2) && !(invertA && aSub);
 303         ValueNode m1 = match1.getValue(node);
 304         ValueNode m2 = match2.getValue(other);
 305         ValueNode a = match2.getOtherValue(other);
 306         if (node instanceof AddNode || node instanceof SubNode) {
 307             ValueNode associated;
 308             if (invertM1) {
 309                 associated = BinaryArithmeticNode.sub(m2, m1, view);
 310             } else if (invertM2) {
 311                 associated = BinaryArithmeticNode.sub(m1, m2, view);
 312             } else {
 313                 associated = BinaryArithmeticNode.add(m1, m2, view);
 314             }
 315             if (invertA) {
 316                 return BinaryArithmeticNode.sub(associated, a, view);
 317             }
 318             if (aSub) {
 319                 return BinaryArithmeticNode.sub(a, associated, view);
 320             }
 321             return BinaryArithmeticNode.add(a, associated, view);
 322         } else if (node instanceof MulNode) {
 323             return BinaryArithmeticNode.mul(a, AddNode.mul(m1, m2, view), view);
 324         } else if (node instanceof AndNode) {
 325             return new AndNode(a, new AndNode(m1, m2));
 326         } else if (node instanceof OrNode) {
 327             return new OrNode(a, new OrNode(m1, m2));
 328         } else if (node instanceof XorNode) {
 329             return new XorNode(a, new XorNode(m1, m2));
 330         } else {
 331             throw GraalError.shouldNotReachHere();
 332         }
 333     }
 334 
 335     /**
 336      * Ensure a canonical ordering of inputs for commutative nodes to improve GVN results. Order the
 337      * inputs by increasing {@link Node#id} and call {@link Graph#findDuplicate(Node)} on the node
 338      * if it's currently in a graph. It's assumed that if there was a constant on the left it's been
 339      * moved to the right by other code and that ordering is left alone.
 340      *
 341      * @return the original node or another node with the same input ordering
 342      */
 343     @SuppressWarnings("deprecation")
 344     public BinaryNode maybeCommuteInputs() {
 345         assert this instanceof BinaryCommutative;
 346         if (!y.isConstant() && (x.isConstant() || x.getId() > y.getId())) {
 347             ValueNode tmp = x;
 348             x = y;
 349             y = tmp;
 350             if (graph() != null) {
 351                 // See if this node already exists
 352                 BinaryNode duplicate = graph().findDuplicate(this);
 353                 if (duplicate != null) {
 354                     return duplicate;
 355                 }
 356             }
 357         }
 358         return this;
 359     }
 360 
 361     /**
 362      * Determines if it would be better to swap the inputs in order to produce better assembly code.
 363      * First we try to pick a value which is dead after this use. If both values are dead at this
 364      * use then we try pick an induction variable phi to encourage the phi to live in a single
 365      * register.
 366      *
 367      * @param nodeValueMap
 368      * @return true if inputs should be swapped, false otherwise
 369      */
 370     protected boolean shouldSwapInputs(NodeValueMap nodeValueMap) {
 371         final boolean xHasOtherUsages = getX().hasUsagesOtherThan(this, nodeValueMap);
 372         final boolean yHasOtherUsages = getY().hasUsagesOtherThan(this, nodeValueMap);
 373 
 374         if (!getY().isConstant() && !yHasOtherUsages) {
 375             if (xHasOtherUsages == yHasOtherUsages) {
 376                 return getY() instanceof ValuePhiNode && getY().inputs().contains(this);
 377             } else {
 378                 return true;
 379             }
 380         }
 381         return false;
 382     }
 383 
 384 }