1 /* 2 * Copyright (c) 2013, 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.replacements.nodes.arithmetic; 26 27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2; 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.InputType; 34 import org.graalvm.compiler.nodeinfo.NodeInfo; 35 import org.graalvm.compiler.nodes.AbstractBeginNode; 36 import org.graalvm.compiler.nodes.ConstantNode; 37 import org.graalvm.compiler.nodes.NodeView; 38 import org.graalvm.compiler.nodes.ValueNode; 39 import org.graalvm.compiler.nodes.calc.SubNode; 40 import org.graalvm.compiler.nodes.extended.AnchoringNode; 41 import org.graalvm.compiler.nodes.spi.LoweringTool; 42 import org.graalvm.compiler.nodes.util.GraphUtil; 43 44 import jdk.vm.ci.meta.JavaConstant; 45 import jdk.vm.ci.meta.JavaKind; 46 import jdk.vm.ci.meta.SpeculationLog.SpeculationReason; 47 48 /** 49 * Node representing an exact integer substraction that will throw an {@link ArithmeticException} in 50 * case the addition would overflow the 32 bit range. 51 */ 52 @NodeInfo(cycles = CYCLES_2, size = SIZE_2) 53 public final class IntegerSubExactNode extends SubNode implements IntegerExactArithmeticNode { 54 public static final NodeClass<IntegerSubExactNode> TYPE = NodeClass.create(IntegerSubExactNode.class); 55 56 @OptionalInput(InputType.Anchor) protected AnchoringNode anchor; 57 protected final SpeculationReason speculation; 58 59 public IntegerSubExactNode(ValueNode x, ValueNode y, SpeculationReason speculation) { 60 super(TYPE, x, y); 61 setStamp(x.stamp(NodeView.DEFAULT).unrestricted()); 62 assert x.stamp(NodeView.DEFAULT).isCompatible(y.stamp(NodeView.DEFAULT)) && x.stamp(NodeView.DEFAULT) instanceof IntegerStamp; 63 this.speculation = speculation; 64 } 65 66 @Override 67 public boolean inferStamp() { 68 /* 69 * Note: it is not allowed to use the foldStamp method of the regular sub node as we do not 70 * know the result stamp of this node if we do not know whether we may deopt. If we know we 71 * can never overflow we will replace this node with its non overflow checking counterpart 72 * anyway. 73 */ 74 return false; 75 } 76 77 @Override 78 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 79 if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) { 80 return ConstantNode.forIntegerStamp(stamp(NodeView.DEFAULT), 0); 81 } 82 if (forX.isConstant() && forY.isConstant()) { 83 return canonicalXYconstant(forX, forY); 94 } 95 96 private ValueNode canonicalXYconstant(ValueNode forX, ValueNode forY) { 97 JavaConstant xConst = forX.asJavaConstant(); 98 JavaConstant yConst = forY.asJavaConstant(); 99 assert xConst.getJavaKind() == yConst.getJavaKind(); 100 try { 101 if (xConst.getJavaKind() == JavaKind.Int) { 102 return ConstantNode.forInt(Math.subtractExact(xConst.asInt(), yConst.asInt())); 103 } else { 104 assert xConst.getJavaKind() == JavaKind.Long; 105 return ConstantNode.forLong(Math.subtractExact(xConst.asLong(), yConst.asLong())); 106 } 107 } catch (ArithmeticException ex) { 108 // The operation will result in an overflow exception, so do not canonicalize. 109 } 110 return this; 111 } 112 113 @Override 114 public IntegerExactArithmeticSplitNode createSplit(AbstractBeginNode next, AbstractBeginNode deopt) { 115 return graph().add(new IntegerSubExactSplitNode(stamp(NodeView.DEFAULT), getX(), getY(), next, deopt)); 116 } 117 118 @Override 119 public SpeculationReason getSpeculation() { 120 return speculation; 121 } 122 123 @Override 124 public AnchoringNode getAnchor() { 125 return anchor; 126 } 127 128 @Override 129 public void setAnchor(AnchoringNode x) { 130 updateUsagesInterface(this.anchor, x); 131 this.anchor = x; 132 } 133 134 @Override 135 public void lower(LoweringTool tool) { 136 IntegerExactArithmeticSplitNode.lower(tool, this); 137 } 138 } | 1 /* 2 * Copyright (c) 2013, 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 package org.graalvm.compiler.replacements.nodes.arithmetic; 26 27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2; 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.InputType; 34 import org.graalvm.compiler.nodeinfo.NodeInfo; 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.SubNode; 39 import org.graalvm.compiler.nodes.extended.GuardedNode; 40 import org.graalvm.compiler.nodes.extended.GuardingNode; 41 import org.graalvm.compiler.nodes.util.GraphUtil; 42 43 import jdk.vm.ci.meta.JavaConstant; 44 import jdk.vm.ci.meta.JavaKind; 45 46 /** 47 * Node representing an exact integer substraction that will throw an {@link ArithmeticException} in 48 * case the addition would overflow the 32 bit range. 49 */ 50 @NodeInfo(cycles = CYCLES_2, size = SIZE_2) 51 public final class IntegerSubExactNode extends SubNode implements GuardedNode, IntegerExactArithmeticNode { 52 public static final NodeClass<IntegerSubExactNode> TYPE = NodeClass.create(IntegerSubExactNode.class); 53 54 @Input(InputType.Guard) protected GuardingNode guard; 55 56 public IntegerSubExactNode(ValueNode x, ValueNode y, GuardingNode guard) { 57 super(TYPE, x, y); 58 setStamp(x.stamp(NodeView.DEFAULT).unrestricted()); 59 assert x.stamp(NodeView.DEFAULT).isCompatible(y.stamp(NodeView.DEFAULT)) && x.stamp(NodeView.DEFAULT) instanceof IntegerStamp; 60 this.guard = guard; 61 } 62 63 @Override 64 public boolean inferStamp() { 65 /* 66 * Note: it is not allowed to use the foldStamp method of the regular sub node as we do not 67 * know the result stamp of this node if we do not know whether we may deopt. If we know we 68 * can never overflow we will replace this node with its non overflow checking counterpart 69 * anyway. 70 */ 71 return false; 72 } 73 74 @Override 75 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 76 if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) { 77 return ConstantNode.forIntegerStamp(stamp(NodeView.DEFAULT), 0); 78 } 79 if (forX.isConstant() && forY.isConstant()) { 80 return canonicalXYconstant(forX, forY); 91 } 92 93 private ValueNode canonicalXYconstant(ValueNode forX, ValueNode forY) { 94 JavaConstant xConst = forX.asJavaConstant(); 95 JavaConstant yConst = forY.asJavaConstant(); 96 assert xConst.getJavaKind() == yConst.getJavaKind(); 97 try { 98 if (xConst.getJavaKind() == JavaKind.Int) { 99 return ConstantNode.forInt(Math.subtractExact(xConst.asInt(), yConst.asInt())); 100 } else { 101 assert xConst.getJavaKind() == JavaKind.Long; 102 return ConstantNode.forLong(Math.subtractExact(xConst.asLong(), yConst.asLong())); 103 } 104 } catch (ArithmeticException ex) { 105 // The operation will result in an overflow exception, so do not canonicalize. 106 } 107 return this; 108 } 109 110 @Override 111 public GuardingNode getGuard() { 112 return guard; 113 } 114 115 @Override 116 public void setGuard(GuardingNode guard) { 117 updateUsagesInterface(this.guard, guard); 118 this.guard = guard; 119 } 120 } |