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 UnsignedRemNode extends FixedBinaryNode implements Lowerable, LIRLowerable { 036 037 public static final NodeClass<UnsignedRemNode> TYPE = NodeClass.create(UnsignedRemNode.class); 038 039 public UnsignedRemNode(ValueNode x, ValueNode y) { 040 this(TYPE, x, y); 041 } 042 043 protected UnsignedRemNode(NodeClass<? extends UnsignedRemNode> 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.remainder(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 ConstantNode.forIntegerStamp(stamp(), 0); 060 } else if (CodeUtil.isPowerOf2(c)) { 061 return new AndNode(forX, ConstantNode.forIntegerStamp(stamp(), c - 1)); 062 } 063 } 064 return this; 065 } 066 067 @Override 068 public void lower(LoweringTool tool) { 069 tool.getLowerer().lower(this, tool); 070 } 071 072 @Override 073 public void generate(NodeLIRBuilderTool gen) { 074 gen.setResult(this, gen.getLIRGeneratorTool().emitURem(gen.operand(getX()), gen.operand(getY()), gen.state(this))); 075 } 076 077 @Override 078 public boolean canDeoptimize() { 079 return !(getY().stamp() instanceof IntegerStamp) || ((IntegerStamp) getY().stamp()).contains(0); 080 } 081 082 @NodeIntrinsic 083 public static native int unsignedRemainder(int a, int b); 084 085 @NodeIntrinsic 086 public static native long unsignedRemainder(long a, long b); 087}