1 /* 2 * Copyright (c) 2019, 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 26 package org.graalvm.compiler.replacements.nodes.arithmetic; 27 28 import jdk.vm.ci.meta.JavaConstant; 29 import jdk.vm.ci.meta.JavaKind; 30 import org.graalvm.compiler.core.common.type.IntegerStamp; 31 import org.graalvm.compiler.core.common.type.Stamp; 32 import org.graalvm.compiler.graph.NodeClass; 33 import org.graalvm.compiler.graph.spi.Canonicalizable; 34 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 35 import org.graalvm.compiler.graph.spi.Simplifiable; 36 import org.graalvm.compiler.nodeinfo.NodeInfo; 37 import org.graalvm.compiler.nodes.AbstractBeginNode; 38 import org.graalvm.compiler.nodes.LogicConstantNode; 39 import org.graalvm.compiler.nodes.NodeView; 40 import org.graalvm.compiler.nodes.ValueNode; 41 import org.graalvm.compiler.nodes.calc.BinaryNode; 42 43 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_4; 44 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2; 45 46 @NodeInfo(cycles = CYCLES_4, cyclesRationale = "mul+cmp", size = SIZE_2) 47 public class IntegerMulExactOverflowNode extends IntegerExactOverflowNode implements Simplifiable, Canonicalizable.BinaryCommutative<ValueNode> { 48 public static final NodeClass<IntegerMulExactOverflowNode> TYPE = NodeClass.create(IntegerMulExactOverflowNode.class); 49 50 public IntegerMulExactOverflowNode(ValueNode x, ValueNode y) { 51 super(TYPE, x, y); 52 } 53 54 @Override 55 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 56 if (forX.isConstant() && !forY.isConstant()) { 57 return new IntegerMulExactOverflowNode(forY, forX).canonical(tool); 58 } 59 if (forX.isConstant() && forY.isConstant()) { 60 return canonicalXYconstant(forX, forY); 61 } else if (forY.isConstant()) { 62 long c = forY.asJavaConstant().asLong(); 63 if (c == 1 || c == 0) { 64 return LogicConstantNode.forBoolean(false); 65 } 66 } 67 if (!IntegerStamp.multiplicationCanOverflow((IntegerStamp) x.stamp(NodeView.DEFAULT), (IntegerStamp) y.stamp(NodeView.DEFAULT))) { 68 return LogicConstantNode.forBoolean(false); 69 } 70 return this; 71 } 72 73 private static LogicConstantNode canonicalXYconstant(ValueNode forX, ValueNode forY) { 74 JavaConstant xConst = forX.asJavaConstant(); 75 JavaConstant yConst = forY.asJavaConstant(); 76 assert xConst.getJavaKind() == yConst.getJavaKind(); 77 try { 78 if (xConst.getJavaKind() == JavaKind.Int) { 79 Math.multiplyExact(xConst.asInt(), yConst.asInt()); 80 } else { 81 assert xConst.getJavaKind() == JavaKind.Long; 82 Math.multiplyExact(xConst.asLong(), yConst.asLong()); 83 } 84 } catch (ArithmeticException ex) { 85 return LogicConstantNode.forBoolean(true); 86 } 87 return LogicConstantNode.forBoolean(false); 88 } 89 90 @Override 91 protected IntegerExactArithmeticSplitNode createSplit(Stamp splitStamp, AbstractBeginNode next, AbstractBeginNode overflow) { 92 return new IntegerMulExactSplitNode(splitStamp, x, y, next, overflow); 93 } 94 95 @Override 96 protected Class<? extends BinaryNode> getCoupledType() { 97 return IntegerMulExactNode.class; 98 } 99 100 }