001/*
002 * Copyright (c) 2011, 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.nodes.calc;
024
025import jdk.internal.jvmci.code.*;
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.spi.*;
033
034@NodeInfo(shortName = "/")
035public class IntegerDivNode extends FixedBinaryNode implements Lowerable, LIRLowerable {
036    public static final NodeClass<IntegerDivNode> TYPE = NodeClass.create(IntegerDivNode.class);
037
038    public IntegerDivNode(ValueNode x, ValueNode y) {
039        this(TYPE, x, y);
040    }
041
042    protected IntegerDivNode(NodeClass<? extends IntegerDivNode> c, ValueNode x, ValueNode y) {
043        super(c, IntegerStamp.OPS.getDiv().foldStamp(x.stamp(), y.stamp()), x, y);
044    }
045
046    @Override
047    public boolean inferStamp() {
048        return updateStamp(IntegerStamp.OPS.getDiv().foldStamp(getX().stamp(), getY().stamp()));
049    }
050
051    @Override
052    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
053        if (forX.isConstant() && forY.isConstant()) {
054            @SuppressWarnings("hiding")
055            long y = forY.asJavaConstant().asLong();
056            if (y == 0) {
057                return this; // this will trap, can not canonicalize
058            }
059            return ConstantNode.forIntegerStamp(stamp(), forX.asJavaConstant().asLong() / y);
060        } else if (forY.isConstant()) {
061            long c = forY.asJavaConstant().asLong();
062            if (c == 1) {
063                return forX;
064            }
065            if (c == -1) {
066                return new NegateNode(forX);
067            }
068            long abs = Math.abs(c);
069            if (CodeUtil.isPowerOf2(abs) && forX.stamp() instanceof IntegerStamp) {
070                ValueNode dividend = forX;
071                IntegerStamp stampX = (IntegerStamp) forX.stamp();
072                int log2 = CodeUtil.log2(abs);
073                // no rounding if dividend is positive or if its low bits are always 0
074                if (stampX.canBeNegative() || (stampX.upMask() & (abs - 1)) != 0) {
075                    int bits = PrimitiveStamp.getBits(stamp());
076                    RightShiftNode sign = new RightShiftNode(forX, ConstantNode.forInt(bits - 1));
077                    UnsignedRightShiftNode round = new UnsignedRightShiftNode(sign, ConstantNode.forInt(bits - log2));
078                    dividend = BinaryArithmeticNode.add(dividend, round);
079                }
080                RightShiftNode shift = new RightShiftNode(dividend, ConstantNode.forInt(log2));
081                if (c < 0) {
082                    return new NegateNode(shift);
083                }
084                return shift;
085            }
086        }
087
088        // Convert the expression ((a - a % b) / b) into (a / b).
089        if (forX instanceof SubNode) {
090            SubNode integerSubNode = (SubNode) forX;
091            if (integerSubNode.getY() instanceof IntegerRemNode) {
092                IntegerRemNode integerRemNode = (IntegerRemNode) integerSubNode.getY();
093                if (integerSubNode.stamp().isCompatible(this.stamp()) && integerRemNode.stamp().isCompatible(this.stamp()) && integerSubNode.getX() == integerRemNode.getX() &&
094                                forY == integerRemNode.getY()) {
095                    return new IntegerDivNode(integerSubNode.getX(), forY);
096                }
097            }
098        }
099
100        if (next() instanceof IntegerDivNode) {
101            NodeClass<?> nodeClass = getNodeClass();
102            if (next().getClass() == this.getClass() && nodeClass.getInputEdges().areEqualIn(this, next()) && valueEquals(next())) {
103                return next();
104            }
105        }
106
107        return this;
108    }
109
110    @Override
111    public void lower(LoweringTool tool) {
112        tool.getLowerer().lower(this, tool);
113    }
114
115    @Override
116    public void generate(NodeLIRBuilderTool gen) {
117        gen.setResult(this, gen.getLIRGeneratorTool().emitDiv(gen.operand(getX()), gen.operand(getY()), gen.state(this)));
118    }
119
120    @Override
121    public boolean canDeoptimize() {
122        return !(getY().stamp() instanceof IntegerStamp) || ((IntegerStamp) getY().stamp()).contains(0);
123    }
124}