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 UnsignedDivNode extends FixedBinaryNode implements Lowerable, LIRLowerable { 036 037 public static final NodeClass<UnsignedDivNode> TYPE = NodeClass.create(UnsignedDivNode.class); 038 039 public UnsignedDivNode(ValueNode x, ValueNode y) { 040 this(TYPE, x, y); 041 } 042 043 protected UnsignedDivNode(NodeClass<? extends UnsignedDivNode> c, ValueNode x, ValueNode y) { 044 super(c, x.stamp().unrestricted(), x, y); 045 } 046 047 @Override 048 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 049 int bits = ((IntegerStamp) stamp()).getBits(); 050 if (forX.isConstant() && forY.isConstant()) { 051 long yConst = CodeUtil.zeroExtend(forY.asJavaConstant().asLong(), bits); 052 if (yConst == 0) { 053 return this; // this will trap, cannot canonicalize 054 } 055 return ConstantNode.forIntegerStamp(stamp(), UnsignedMath.divide(CodeUtil.zeroExtend(forX.asJavaConstant().asLong(), bits), yConst)); 056 } else if (forY.isConstant()) { 057 long c = CodeUtil.zeroExtend(forY.asJavaConstant().asLong(), bits); 058 if (c == 1) { 059 return forX; 060 } 061 if (CodeUtil.isPowerOf2(c)) { 062 return new UnsignedRightShiftNode(forX, ConstantNode.forInt(CodeUtil.log2(c))); 063 } 064 } 065 return this; 066 } 067 068 @Override 069 public void lower(LoweringTool tool) { 070 tool.getLowerer().lower(this, tool); 071 } 072 073 @Override 074 public void generate(NodeLIRBuilderTool gen) { 075 gen.setResult(this, gen.getLIRGeneratorTool().emitUDiv(gen.operand(getX()), gen.operand(getY()), gen.state(this))); 076 } 077 078 @Override 079 public boolean canDeoptimize() { 080 return !(getY().stamp() instanceof IntegerStamp) || ((IntegerStamp) getY().stamp()).contains(0); 081 } 082 083 @NodeIntrinsic 084 public static native int unsignedDivide(int a, int b); 085 086 @NodeIntrinsic 087 public static native long unsignedDivide(long a, long b); 088}