001/*
002 * Copyright (c) 2011, 2014, 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.*;
026import jdk.internal.jvmci.meta.*;
027
028import com.oracle.graal.compiler.common.type.*;
029import com.oracle.graal.compiler.common.type.ArithmeticOpTable.BinaryOp;
030import com.oracle.graal.compiler.common.type.ArithmeticOpTable.BinaryOp.Div;
031import com.oracle.graal.graph.*;
032import com.oracle.graal.graph.spi.*;
033import com.oracle.graal.lir.gen.*;
034import com.oracle.graal.nodeinfo.*;
035import com.oracle.graal.nodes.*;
036import com.oracle.graal.nodes.spi.*;
037
038@NodeInfo(shortName = "/")
039public final class DivNode extends BinaryArithmeticNode<Div> {
040
041    public static final NodeClass<DivNode> TYPE = NodeClass.create(DivNode.class);
042
043    public DivNode(ValueNode x, ValueNode y) {
044        super(TYPE, ArithmeticOpTable::getDiv, x, y);
045    }
046
047    public static ValueNode create(ValueNode x, ValueNode y) {
048        BinaryOp<Div> op = ArithmeticOpTable.forStamp(x.stamp()).getDiv();
049        Stamp stamp = op.foldStamp(x.stamp(), y.stamp());
050        ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp);
051        if (tryConstantFold != null) {
052            return tryConstantFold;
053        } else {
054            return new DivNode(x, y);
055        }
056    }
057
058    @Override
059    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
060        ValueNode ret = super.canonical(tool, forX, forY);
061        if (ret != this) {
062            return ret;
063        }
064
065        if (forY.isConstant()) {
066            Constant c = forY.asConstant();
067            if (getOp(forX, forY).isNeutral(c)) {
068                return forX;
069            }
070            if (c instanceof PrimitiveConstant && ((PrimitiveConstant) c).getKind().isNumericInteger()) {
071                long i = ((PrimitiveConstant) c).asLong();
072                boolean signFlip = false;
073                if (i < 0) {
074                    i = -i;
075                    signFlip = true;
076                }
077                ValueNode divResult = null;
078                if (CodeUtil.isPowerOf2(i)) {
079                    divResult = new RightShiftNode(forX, ConstantNode.forInt(CodeUtil.log2(i)));
080                }
081                if (divResult != null) {
082                    if (signFlip) {
083                        return new NegateNode(divResult);
084                    } else {
085                        return divResult;
086                    }
087                }
088            }
089        }
090        return this;
091    }
092
093    @Override
094    public void generate(NodeValueMap nodeValueMap, ArithmeticLIRGenerator gen) {
095        nodeValueMap.setResult(this, gen.emitDiv(nodeValueMap.operand(getX()), nodeValueMap.operand(getY()), null));
096    }
097}