1 /* 2 * Copyright (c) 2011, 2015, 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.ArithmeticOpTable; 26 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp; 27 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp.Div; 28 import org.graalvm.compiler.core.common.type.Stamp; 29 import org.graalvm.compiler.graph.NodeClass; 30 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 31 import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool; 32 import org.graalvm.compiler.nodeinfo.NodeCycles; 33 import org.graalvm.compiler.nodeinfo.NodeInfo; 34 import org.graalvm.compiler.nodes.ConstantNode; 35 import org.graalvm.compiler.nodes.ValueNode; 36 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 37 38 import jdk.vm.ci.code.CodeUtil; 39 import jdk.vm.ci.meta.Constant; 40 import jdk.vm.ci.meta.PrimitiveConstant; 41 42 @NodeInfo(shortName = "/", cycles = NodeCycles.CYCLES_30) 43 public class DivNode extends BinaryArithmeticNode<Div> { 44 45 public static final NodeClass<DivNode> TYPE = NodeClass.create(DivNode.class); 46 47 public DivNode(ValueNode x, ValueNode y) { 48 super(TYPE, ArithmeticOpTable::getDiv, x, y); 49 } 50 51 protected DivNode(NodeClass<? extends DivNode> c, ValueNode x, ValueNode y) { 52 super(c, ArithmeticOpTable::getDiv, x, y); 53 } 54 55 public static ValueNode create(ValueNode x, ValueNode y) { 56 BinaryOp<Div> op = ArithmeticOpTable.forStamp(x.stamp()).getDiv(); 57 Stamp stamp = op.foldStamp(x.stamp(), y.stamp()); 58 ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp); 59 if (tryConstantFold != null) { 60 return tryConstantFold; 61 } else { 62 return new DivNode(x, y); 63 } 64 } 65 66 @Override 67 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 68 ValueNode ret = super.canonical(tool, forX, forY); 69 if (ret != this) { 70 return ret; 71 } 72 73 if (forY.isConstant()) { 74 Constant c = forY.asConstant(); 75 if (getOp(forX, forY).isNeutral(c)) { 76 return forX; 77 } 78 if (c instanceof PrimitiveConstant && ((PrimitiveConstant) c).getJavaKind().isNumericInteger()) { 79 long i = ((PrimitiveConstant) c).asLong(); 80 boolean signFlip = false; 81 if (i < 0) { 82 i = -i; 83 signFlip = true; 84 } 85 ValueNode divResult = null; 86 if (CodeUtil.isPowerOf2(i)) { 87 divResult = new RightShiftNode(forX, ConstantNode.forInt(CodeUtil.log2(i))); 88 } 89 if (divResult != null) { 90 if (signFlip) { 91 return new NegateNode(divResult); 92 } else { 93 return divResult; 94 } 95 } 96 } 97 } 98 return this; 99 } 100 101 @Override 102 public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) { 103 nodeValueMap.setResult(this, gen.emitDiv(nodeValueMap.operand(getX()), nodeValueMap.operand(getY()), null)); 104 } 105 }