1 /* 2 * Copyright (c) 2011, 2018, 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 24 25 package org.graalvm.compiler.nodes.calc; 26 27 import org.graalvm.compiler.core.common.type.IntegerStamp; 28 import org.graalvm.compiler.core.common.type.PrimitiveStamp; 29 import org.graalvm.compiler.core.common.type.Stamp; 30 import org.graalvm.compiler.graph.NodeClass; 31 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 32 import org.graalvm.compiler.nodeinfo.NodeInfo; 33 import org.graalvm.compiler.nodes.ConstantNode; 34 import org.graalvm.compiler.nodes.NodeView; 35 import org.graalvm.compiler.nodes.ValueNode; 36 import org.graalvm.compiler.nodes.extended.GuardingNode; 37 import org.graalvm.compiler.nodes.spi.LIRLowerable; 38 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 39 40 import jdk.vm.ci.code.CodeUtil; 41 42 @NodeInfo(shortName = "/") 43 public class SignedDivNode extends IntegerDivRemNode implements LIRLowerable { 44 45 public static final NodeClass<SignedDivNode> TYPE = NodeClass.create(SignedDivNode.class); 46 47 protected SignedDivNode(ValueNode x, ValueNode y, GuardingNode zeroCheck) { 48 this(TYPE, x, y, zeroCheck); 49 } 50 51 protected SignedDivNode(NodeClass<? extends SignedDivNode> c, ValueNode x, ValueNode y, GuardingNode zeroCheck) { 52 super(c, IntegerStamp.OPS.getDiv().foldStamp(x.stamp(NodeView.DEFAULT), y.stamp(NodeView.DEFAULT)), Op.DIV, Type.SIGNED, x, y, zeroCheck); 53 } 54 55 public static ValueNode create(ValueNode x, ValueNode y, GuardingNode zeroCheck, NodeView view) { 56 return canonical(null, x, y, zeroCheck, view); 57 } 58 59 @Override 60 public boolean inferStamp() { 61 return updateStamp(IntegerStamp.OPS.getDiv().foldStamp(getX().stamp(NodeView.DEFAULT), getY().stamp(NodeView.DEFAULT))); 62 } 63 64 @Override 65 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 66 NodeView view = NodeView.from(tool); 67 return canonical(this, forX, forY, getZeroCheck(), view); 68 } 69 70 public static ValueNode canonical(SignedDivNode self, ValueNode forX, ValueNode forY, GuardingNode zeroCheck, NodeView view) { 71 Stamp predictedStamp = IntegerStamp.OPS.getDiv().foldStamp(forX.stamp(NodeView.DEFAULT), forY.stamp(NodeView.DEFAULT)); 72 Stamp stamp = self != null ? self.stamp(view) : predictedStamp; 73 if (forX.isConstant() && forY.isConstant()) { 74 long y = forY.asJavaConstant().asLong(); 75 if (y == 0) { 76 /* This will trap, cannot canonicalize. */ 77 return self != null ? self : new SignedDivNode(forX, forY, zeroCheck); 78 } 79 return ConstantNode.forIntegerStamp(stamp, forX.asJavaConstant().asLong() / y); 80 } else if (forY.isConstant()) { 81 long c = forY.asJavaConstant().asLong(); 82 ValueNode v = canonical(forX, c, view); 83 if (v != null) { 84 return v; 85 } 86 } 87 88 // Convert the expression ((a - a % b) / b) into (a / b). 89 if (forX instanceof SubNode) { 90 SubNode integerSubNode = (SubNode) forX; 91 if (integerSubNode.getY() instanceof SignedRemNode) { 92 SignedRemNode integerRemNode = (SignedRemNode) integerSubNode.getY(); 93 if (integerSubNode.stamp(view).isCompatible(stamp) && integerRemNode.stamp(view).isCompatible(stamp) && integerSubNode.getX() == integerRemNode.getX() && 94 forY == integerRemNode.getY()) { 95 SignedDivNode sd = new SignedDivNode(integerSubNode.getX(), forY, zeroCheck); 96 sd.stateBefore = self != null ? self.stateBefore : null; 97 return sd; 98 } 99 } 100 } 101 102 if (self != null && self.next() instanceof SignedDivNode) { 103 NodeClass<?> nodeClass = self.getNodeClass(); 104 if (self.next().getClass() == self.getClass() && nodeClass.equalInputs(self, self.next()) && self.valueEquals(self.next())) { 105 return self.next(); 106 } 107 } 108 109 return self != null ? self : new SignedDivNode(forX, forY, zeroCheck); 110 } 111 112 public static ValueNode canonical(ValueNode forX, long c, NodeView view) { 113 if (c == 1) { 114 return forX; 115 } 116 if (c == -1) { 117 return NegateNode.create(forX, view); 118 } 119 long abs = Math.abs(c); 120 if (CodeUtil.isPowerOf2(abs) && forX.stamp(view) instanceof IntegerStamp) { 121 ValueNode dividend = forX; 122 IntegerStamp stampX = (IntegerStamp) forX.stamp(view); 123 int log2 = CodeUtil.log2(abs); 124 // no rounding if dividend is positive or if its low bits are always 0 125 if (stampX.canBeNegative() || (stampX.upMask() & (abs - 1)) != 0) { 126 int bits = PrimitiveStamp.getBits(forX.stamp(view)); 127 RightShiftNode sign = new RightShiftNode(forX, ConstantNode.forInt(bits - 1)); 128 UnsignedRightShiftNode round = new UnsignedRightShiftNode(sign, ConstantNode.forInt(bits - log2)); 129 dividend = BinaryArithmeticNode.add(dividend, round, view); 130 } 131 RightShiftNode shift = new RightShiftNode(dividend, ConstantNode.forInt(log2)); 132 if (c < 0) { 133 return NegateNode.create(shift, view); 134 } 135 return shift; 136 } 137 return null; 138 } 139 140 @Override 141 public void generate(NodeLIRBuilderTool gen) { 142 gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitDiv(gen.operand(getX()), gen.operand(getY()), gen.state(this))); 143 } 144 }