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.*;
034
035/**
036 * Node representing an exact integer multiplication that will throw an {@link ArithmeticException}
037 * in case the addition would overflow the 32 bit range.
038 */
039@NodeInfo
040public final class IntegerMulExactNode extends MulNode implements IntegerExactArithmeticNode {
041    public static final NodeClass<IntegerMulExactNode> TYPE = NodeClass.create(IntegerMulExactNode.class);
042
043    public IntegerMulExactNode(ValueNode x, ValueNode y) {
044        super(TYPE, x, y);
045        setStamp(x.stamp().unrestricted());
046        assert x.stamp().isCompatible(y.stamp()) && x.stamp() instanceof IntegerStamp;
047    }
048
049    @Override
050    public boolean inferStamp() {
051        return false;
052    }
053
054    @Override
055    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
056        if (forX.isConstant() && !forY.isConstant()) {
057            return new IntegerMulExactNode(forY, forX);
058        }
059        if (forX.isConstant()) {
060            return canonicalXconstant(forX, forY);
061        } else if (forY.isConstant()) {
062            long c = forY.asJavaConstant().asLong();
063            if (c == 1) {
064                return forX;
065            }
066            if (c == 0) {
067                return ConstantNode.forIntegerStamp(stamp(), 0);
068            }
069        }
070        return this;
071    }
072
073    private ValueNode canonicalXconstant(ValueNode forX, ValueNode forY) {
074        JavaConstant xConst = forX.asJavaConstant();
075        JavaConstant yConst = forY.asJavaConstant();
076        assert xConst.getKind() == yConst.getKind();
077        try {
078            if (xConst.getKind() == Kind.Int) {
079                return ConstantNode.forInt(Math.multiplyExact(xConst.asInt(), yConst.asInt()));
080            } else {
081                assert xConst.getKind() == Kind.Long;
082                return ConstantNode.forLong(Math.multiplyExact(xConst.asLong(), yConst.asLong()));
083            }
084        } catch (ArithmeticException ex) {
085            // The operation will result in an overflow exception, so do not canonicalize.
086        }
087        return this;
088    }
089
090    @Override
091    public IntegerExactArithmeticSplitNode createSplit(AbstractBeginNode next, AbstractBeginNode deopt) {
092        return graph().add(new IntegerMulExactSplitNode(stamp(), getX(), getY(), next, deopt));
093    }
094
095    @Override
096    public void lower(LoweringTool tool) {
097        IntegerExactArithmeticSplitNode.lower(tool, this);
098    }
099}