UnsignedDivNode.java revision 12968:4d8a004e5c6d
1/*
2 * Copyright (c) 2011, 2016, 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.nodes.calc;
24
25import org.graalvm.compiler.core.common.type.IntegerStamp;
26import org.graalvm.compiler.graph.NodeClass;
27import org.graalvm.compiler.graph.spi.CanonicalizerTool;
28import org.graalvm.compiler.nodeinfo.NodeInfo;
29import org.graalvm.compiler.nodes.ConstantNode;
30import org.graalvm.compiler.nodes.ValueNode;
31import org.graalvm.compiler.nodes.spi.LIRLowerable;
32import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
33
34import jdk.vm.ci.code.CodeUtil;
35
36@NodeInfo(shortName = "|/|")
37public class UnsignedDivNode extends IntegerDivRemNode implements LIRLowerable {
38
39    public static final NodeClass<UnsignedDivNode> TYPE = NodeClass.create(UnsignedDivNode.class);
40
41    public UnsignedDivNode(ValueNode x, ValueNode y) {
42        this(TYPE, x, y);
43    }
44
45    protected UnsignedDivNode(NodeClass<? extends UnsignedDivNode> c, ValueNode x, ValueNode y) {
46        super(c, x.stamp().unrestricted(), Op.DIV, Type.UNSIGNED, x, y);
47    }
48
49    @Override
50    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
51        int bits = ((IntegerStamp) stamp()).getBits();
52        if (forX.isConstant() && forY.isConstant()) {
53            long yConst = CodeUtil.zeroExtend(forY.asJavaConstant().asLong(), bits);
54            if (yConst == 0) {
55                return this; // this will trap, cannot canonicalize
56            }
57            return ConstantNode.forIntegerStamp(stamp(), Long.divideUnsigned(CodeUtil.zeroExtend(forX.asJavaConstant().asLong(), bits), yConst));
58        } else if (forY.isConstant()) {
59            long c = CodeUtil.zeroExtend(forY.asJavaConstant().asLong(), bits);
60            if (c == 1) {
61                return forX;
62            }
63            if (CodeUtil.isPowerOf2(c)) {
64                return new UnsignedRightShiftNode(forX, ConstantNode.forInt(CodeUtil.log2(c)));
65            }
66        }
67        return this;
68    }
69
70    @Override
71    public void generate(NodeLIRBuilderTool gen) {
72        gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitUDiv(gen.operand(getX()), gen.operand(getY()), gen.state(this)));
73    }
74}
75