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 SignedRemNode extends IntegerDivRemNode implements LIRLowerable { 38 39 public static final NodeClass<SignedRemNode> TYPE = NodeClass.create(SignedRemNode.class); 40 41 public SignedRemNode(ValueNode x, ValueNode y) { 42 this(TYPE, x, y); 43 } 44 45 protected SignedRemNode(NodeClass<? extends SignedRemNode> c, ValueNode x, ValueNode y) { 46 super(c, IntegerStamp.OPS.getRem().foldStamp(x.stamp(), y.stamp()), Op.REM, Type.SIGNED, x, y); 47 } 48 49 @Override 50 public boolean inferStamp() { 51 return updateStamp(IntegerStamp.OPS.getRem().foldStamp(getX().stamp(), getY().stamp())); 52 } 53 54 @Override 55 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 56 if (forX.isConstant() && forY.isConstant()) { 57 @SuppressWarnings("hiding") 58 long y = forY.asJavaConstant().asLong(); 59 if (y == 0) { 60 return this; // this will trap, can not canonicalize 61 } 62 return ConstantNode.forIntegerStamp(stamp(), forX.asJavaConstant().asLong() % y); 63 } else if (forY.isConstant()) { 64 long c = forY.asJavaConstant().asLong(); 65 if (c == 1 || c == -1) { 66 return ConstantNode.forIntegerStamp(stamp(), 0); 67 } else if (c > 0 && CodeUtil.isPowerOf2(c) && forX.stamp() instanceof IntegerStamp && ((IntegerStamp) forX.stamp()).isPositive()) { 68 return new AndNode(forX, ConstantNode.forIntegerStamp(stamp(), c - 1)); 69 } 70 } 71 return this; 72 } 73 74 @Override 75 public void generate(NodeLIRBuilderTool gen) { 76 gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitRem(gen.operand(getX()), gen.operand(getY()), gen.state(this))); 77 } 78 }