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  */
  23 package org.graalvm.compiler.nodes.calc;
  24 
  25 import org.graalvm.compiler.core.common.type.IntegerStamp;
  26 import org.graalvm.compiler.graph.NodeClass;
  27 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  28 import org.graalvm.compiler.nodeinfo.NodeInfo;
  29 import org.graalvm.compiler.nodes.ConstantNode;
  30 import org.graalvm.compiler.nodes.ValueNode;
  31 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  32 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  33 
  34 import jdk.vm.ci.code.CodeUtil;
  35 
  36 @NodeInfo(shortName = "|/|")
  37 public 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     @NodeIntrinsic
  76     public static native int unsignedDivide(int a, int b);
  77 
  78     @NodeIntrinsic
  79     public static native long unsignedDivide(long a, long b);
  80 }