1 /* 2 * Copyright (c) 2013, 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 24 25 package org.graalvm.compiler.replacements.nodes.arithmetic; 26 27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_4; 28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2; 29 30 import org.graalvm.compiler.core.common.type.IntegerStamp; 31 import org.graalvm.compiler.graph.NodeClass; 32 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 33 import org.graalvm.compiler.nodeinfo.NodeInfo; 34 import org.graalvm.compiler.nodes.AbstractBeginNode; 35 import org.graalvm.compiler.nodes.ConstantNode; 36 import org.graalvm.compiler.nodes.NodeView; 37 import org.graalvm.compiler.nodes.ValueNode; 38 import org.graalvm.compiler.nodes.calc.MulNode; 39 import org.graalvm.compiler.nodes.spi.LoweringTool; 40 41 import jdk.vm.ci.meta.JavaConstant; 42 import jdk.vm.ci.meta.JavaKind; 43 44 /** 45 * Node representing an exact integer multiplication that will throw an {@link ArithmeticException} 46 * in case the addition would overflow the 32 bit range. 47 */ 48 @NodeInfo(cycles = CYCLES_4, cyclesRationale = "mul+cmp", size = SIZE_2) 49 public final class IntegerMulExactNode extends MulNode implements IntegerExactArithmeticNode { 50 public static final NodeClass<IntegerMulExactNode> TYPE = NodeClass.create(IntegerMulExactNode.class); 51 IntegerMulExactNode(ValueNode x, ValueNode y)52 public IntegerMulExactNode(ValueNode x, ValueNode y) { 53 super(TYPE, x, y); 54 setStamp(x.stamp(NodeView.DEFAULT).unrestricted()); 55 assert x.stamp(NodeView.DEFAULT).isCompatible(y.stamp(NodeView.DEFAULT)) && x.stamp(NodeView.DEFAULT) instanceof IntegerStamp; 56 } 57 58 @Override inferStamp()59 public boolean inferStamp() { 60 /* 61 * Note: it is not allowed to use the foldStamp method of the regular mul node as we do not 62 * know the result stamp of this node if we do not know whether we may deopt. If we know we 63 * can never overflow we will replace this node with its non overflow checking counterpart 64 * anyway. 65 */ 66 return false; 67 } 68 69 @Override canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY)70 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 71 if (forX.isConstant() && !forY.isConstant()) { 72 return new IntegerMulExactNode(forY, forX).canonical(tool); 73 } 74 if (forX.isConstant()) { 75 return canonicalXconstant(forX, forY); 76 } else if (forY.isConstant()) { 77 long c = forY.asJavaConstant().asLong(); 78 if (c == 1) { 79 return forX; 80 } 81 if (c == 0) { 82 return ConstantNode.forIntegerStamp(stamp(NodeView.DEFAULT), 0); 83 } 84 } 85 if (!IntegerStamp.multiplicationCanOverflow((IntegerStamp) x.stamp(NodeView.DEFAULT), (IntegerStamp) y.stamp(NodeView.DEFAULT))) { 86 return new MulNode(x, y).canonical(tool); 87 } 88 return this; 89 } 90 canonicalXconstant(ValueNode forX, ValueNode forY)91 private ValueNode canonicalXconstant(ValueNode forX, ValueNode forY) { 92 JavaConstant xConst = forX.asJavaConstant(); 93 JavaConstant yConst = forY.asJavaConstant(); 94 assert xConst.getJavaKind() == yConst.getJavaKind(); 95 try { 96 if (xConst.getJavaKind() == JavaKind.Int) { 97 return ConstantNode.forInt(Math.multiplyExact(xConst.asInt(), yConst.asInt())); 98 } else { 99 assert xConst.getJavaKind() == JavaKind.Long; 100 return ConstantNode.forLong(Math.multiplyExact(xConst.asLong(), yConst.asLong())); 101 } 102 } catch (ArithmeticException ex) { 103 // The operation will result in an overflow exception, so do not canonicalize. 104 } 105 return this; 106 } 107 108 @Override createSplit(AbstractBeginNode next, AbstractBeginNode deopt)109 public IntegerExactArithmeticSplitNode createSplit(AbstractBeginNode next, AbstractBeginNode deopt) { 110 return graph().add(new IntegerMulExactSplitNode(stamp(NodeView.DEFAULT), getX(), getY(), next, deopt)); 111 } 112 113 @Override lower(LoweringTool tool)114 public void lower(LoweringTool tool) { 115 IntegerExactArithmeticSplitNode.lower(tool, this); 116 } 117 } 118