1 /*
   2  * Copyright (c) 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 
  26 package org.graalvm.compiler.replacements.nodes.arithmetic;
  27 
  28 import jdk.vm.ci.meta.JavaConstant;
  29 import jdk.vm.ci.meta.JavaKind;
  30 import org.graalvm.compiler.core.common.type.IntegerStamp;
  31 import org.graalvm.compiler.core.common.type.Stamp;
  32 import org.graalvm.compiler.graph.NodeClass;
  33 import org.graalvm.compiler.graph.spi.Canonicalizable.BinaryCommutative;
  34 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  35 import org.graalvm.compiler.graph.spi.Simplifiable;
  36 import org.graalvm.compiler.nodeinfo.NodeInfo;
  37 import org.graalvm.compiler.nodes.AbstractBeginNode;
  38 import org.graalvm.compiler.nodes.LogicConstantNode;
  39 import org.graalvm.compiler.nodes.NodeView;
  40 import org.graalvm.compiler.nodes.ValueNode;
  41 import org.graalvm.compiler.nodes.calc.BinaryNode;
  42 
  43 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
  44 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2;
  45 
  46 @NodeInfo(cycles = CYCLES_2, size = SIZE_2)
  47 public final class IntegerAddExactOverflowNode extends IntegerExactOverflowNode implements Simplifiable, BinaryCommutative<ValueNode> {
  48     public static final NodeClass<IntegerAddExactOverflowNode> TYPE = NodeClass.create(IntegerAddExactOverflowNode.class);
  49 
  50     public IntegerAddExactOverflowNode(ValueNode x, ValueNode y) {
  51         super(TYPE, x, y);
  52     }
  53 
  54     @Override
  55     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  56         if (forX.isConstant() && !forY.isConstant()) {
  57             return new IntegerAddExactOverflowNode(forY, forX).canonical(tool);
  58         }
  59         if (forX.isConstant() && forY.isConstant()) {
  60             return canonicalXYconstant(forX, forY);
  61         } else if (forY.isConstant()) {
  62             long c = forY.asJavaConstant().asLong();
  63             if (c == 0) {
  64                 return LogicConstantNode.forBoolean(false);
  65             }
  66         }
  67         if (!IntegerStamp.addCanOverflow((IntegerStamp) forX.stamp(NodeView.DEFAULT), (IntegerStamp) forY.stamp(NodeView.DEFAULT))) {
  68             return LogicConstantNode.forBoolean(false);
  69         }
  70         return this;
  71     }
  72 
  73     private static LogicConstantNode canonicalXYconstant(ValueNode forX, ValueNode forY) {
  74         JavaConstant xConst = forX.asJavaConstant();
  75         JavaConstant yConst = forY.asJavaConstant();
  76         assert xConst.getJavaKind() == yConst.getJavaKind();
  77         try {
  78             if (xConst.getJavaKind() == JavaKind.Int) {
  79                 Math.addExact(xConst.asInt(), yConst.asInt());
  80             } else {
  81                 assert xConst.getJavaKind() == JavaKind.Long;
  82                 Math.addExact(xConst.asLong(), yConst.asLong());
  83             }
  84         } catch (ArithmeticException ex) {
  85             // Always overflows
  86             return LogicConstantNode.forBoolean(true);
  87         }
  88         // Never overflows
  89         return LogicConstantNode.forBoolean(false);
  90     }
  91 
  92     @Override
  93     protected IntegerExactArithmeticSplitNode createSplit(Stamp splitStamp, AbstractBeginNode next, AbstractBeginNode overflow) {
  94         return new IntegerAddExactSplitNode(splitStamp, x, y, next, overflow);
  95     }
  96 
  97     @Override
  98     protected Class<? extends BinaryNode> getCoupledType() {
  99         return IntegerAddExactNode.class;
 100     }
 101 }