1 /*
   2  * Copyright (c) 2013, 2019, 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.replacements.nodes.arithmetic;
  26 
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2;
  29 
  30 import org.graalvm.compiler.core.common.type.IntegerStamp;
  31 import org.graalvm.compiler.graph.NodeClass;
  32 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  33 import org.graalvm.compiler.nodeinfo.InputType;
  34 import org.graalvm.compiler.nodeinfo.NodeInfo;
  35 import org.graalvm.compiler.nodes.ConstantNode;
  36 import org.graalvm.compiler.nodes.NodeView;
  37 import org.graalvm.compiler.nodes.ValueNode;
  38 import org.graalvm.compiler.nodes.calc.SubNode;
  39 import org.graalvm.compiler.nodes.extended.GuardedNode;
  40 import org.graalvm.compiler.nodes.extended.GuardingNode;
  41 import org.graalvm.compiler.nodes.util.GraphUtil;
  42 
  43 import jdk.vm.ci.meta.JavaConstant;
  44 import jdk.vm.ci.meta.JavaKind;
  45 
  46 /**
  47  * Node representing an exact integer substraction that will throw an {@link ArithmeticException} in
  48  * case the addition would overflow the 32 bit range.
  49  */
  50 @NodeInfo(cycles = CYCLES_2, size = SIZE_2)
  51 public final class IntegerSubExactNode extends SubNode implements GuardedNode, IntegerExactArithmeticNode {
  52     public static final NodeClass<IntegerSubExactNode> TYPE = NodeClass.create(IntegerSubExactNode.class);
  53 
  54     @Input(InputType.Guard) protected GuardingNode guard;
  55 
  56     public IntegerSubExactNode(ValueNode x, ValueNode y, GuardingNode guard) {
  57         super(TYPE, x, y);
  58         setStamp(x.stamp(NodeView.DEFAULT).unrestricted());
  59         assert x.stamp(NodeView.DEFAULT).isCompatible(y.stamp(NodeView.DEFAULT)) && x.stamp(NodeView.DEFAULT) instanceof IntegerStamp;
  60         this.guard = guard;
  61     }
  62 
  63     @Override
  64     public boolean inferStamp() {
  65         /*
  66          * Note: it is not allowed to use the foldStamp method of the regular sub node as we do not
  67          * know the result stamp of this node if we do not know whether we may deopt. If we know we
  68          * can never overflow we will replace this node with its non overflow checking counterpart
  69          * anyway.
  70          */
  71         return false;
  72     }
  73 
  74     @Override
  75     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  76         if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) {
  77             return ConstantNode.forIntegerStamp(stamp(NodeView.DEFAULT), 0);
  78         }
  79         if (forX.isConstant() && forY.isConstant()) {
  80             return canonicalXYconstant(forX, forY);
  81         } else if (forY.isConstant()) {
  82             long c = forY.asJavaConstant().asLong();
  83             if (c == 0) {
  84                 return forX;
  85             }
  86         }
  87         if (!IntegerStamp.subtractionCanOverflow((IntegerStamp) x.stamp(NodeView.DEFAULT), (IntegerStamp) y.stamp(NodeView.DEFAULT))) {
  88             return new SubNode(x, y).canonical(tool);
  89         }
  90         return this;
  91     }
  92 
  93     private ValueNode canonicalXYconstant(ValueNode forX, ValueNode forY) {
  94         JavaConstant xConst = forX.asJavaConstant();
  95         JavaConstant yConst = forY.asJavaConstant();
  96         assert xConst.getJavaKind() == yConst.getJavaKind();
  97         try {
  98             if (xConst.getJavaKind() == JavaKind.Int) {
  99                 return ConstantNode.forInt(Math.subtractExact(xConst.asInt(), yConst.asInt()));
 100             } else {
 101                 assert xConst.getJavaKind() == JavaKind.Long;
 102                 return ConstantNode.forLong(Math.subtractExact(xConst.asLong(), yConst.asLong()));
 103             }
 104         } catch (ArithmeticException ex) {
 105             // The operation will result in an overflow exception, so do not canonicalize.
 106         }
 107         return this;
 108     }
 109 
 110     @Override
 111     public GuardingNode getGuard() {
 112         return guard;
 113     }
 114 
 115     @Override
 116     public void setGuard(GuardingNode guard) {
 117         updateUsagesInterface(this.guard, guard);
 118         this.guard = guard;
 119     }
 120 }