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