001/*
002 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.replacements.nodes.arithmetic;
024
025import jdk.internal.jvmci.meta.*;
026
027import com.oracle.graal.compiler.common.type.*;
028import com.oracle.graal.graph.*;
029import com.oracle.graal.graph.spi.*;
030import com.oracle.graal.nodeinfo.*;
031import com.oracle.graal.nodes.*;
032import com.oracle.graal.nodes.calc.*;
033import com.oracle.graal.nodes.spi.*;
034import com.oracle.graal.nodes.util.*;
035
036/**
037 * Node representing an exact integer substraction that will throw an {@link ArithmeticException} in
038 * case the addition would overflow the 32 bit range.
039 */
040@NodeInfo
041public final class IntegerSubExactNode extends SubNode implements IntegerExactArithmeticNode {
042    public static final NodeClass<IntegerSubExactNode> TYPE = NodeClass.create(IntegerSubExactNode.class);
043
044    public IntegerSubExactNode(ValueNode x, ValueNode y) {
045        super(TYPE, x, y);
046        setStamp(x.stamp().unrestricted());
047        assert x.stamp().isCompatible(y.stamp()) && x.stamp() instanceof IntegerStamp;
048    }
049
050    @Override
051    public boolean inferStamp() {
052        // TODO Should probably use a specialized version which understands that it can't overflow
053        return false;
054    }
055
056    @Override
057    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
058        if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) {
059            return ConstantNode.forIntegerStamp(stamp(), 0);
060        }
061        if (forX.isConstant() && forY.isConstant()) {
062            return canonicalXYconstant(forX, forY);
063        } else if (forY.isConstant()) {
064            long c = forY.asJavaConstant().asLong();
065            if (c == 0) {
066                return forX;
067            }
068        }
069        return this;
070    }
071
072    private ValueNode canonicalXYconstant(ValueNode forX, ValueNode forY) {
073        JavaConstant xConst = forX.asJavaConstant();
074        JavaConstant yConst = forY.asJavaConstant();
075        assert xConst.getKind() == yConst.getKind();
076        try {
077            if (xConst.getKind() == Kind.Int) {
078                return ConstantNode.forInt(Math.subtractExact(xConst.asInt(), yConst.asInt()));
079            } else {
080                assert xConst.getKind() == Kind.Long;
081                return ConstantNode.forLong(Math.subtractExact(xConst.asLong(), yConst.asLong()));
082            }
083        } catch (ArithmeticException ex) {
084            // The operation will result in an overflow exception, so do not canonicalize.
085        }
086        return this;
087    }
088
089    @Override
090    public IntegerExactArithmeticSplitNode createSplit(AbstractBeginNode next, AbstractBeginNode deopt) {
091        return graph().add(new IntegerSubExactSplitNode(stamp(), getX(), getY(), next, deopt));
092    }
093
094    @Override
095    public void lower(LoweringTool tool) {
096        IntegerExactArithmeticSplitNode.lower(tool, this);
097    }
098}