< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.replacements/src/org/graalvm/compiler/replacements/nodes/arithmetic/IntegerMulExactNode.java

Print this page


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

  44 
  45 /**
  46  * Node representing an exact integer multiplication that will throw an {@link ArithmeticException}
  47  * in case the addition would overflow the 32 bit range.
  48  */
  49 @NodeInfo(cycles = CYCLES_4, cyclesRationale = "mul+cmp", size = SIZE_2)
  50 public final class IntegerMulExactNode extends MulNode implements GuardedNode, IntegerExactArithmeticNode {
  51     public static final NodeClass<IntegerMulExactNode> TYPE = NodeClass.create(IntegerMulExactNode.class);
  52 
  53     @Input(InputType.Guard) protected GuardingNode guard;

  54 
  55     public IntegerMulExactNode(ValueNode x, ValueNode y, GuardingNode guard) {
  56         super(TYPE, x, y);
  57         setStamp(x.stamp(NodeView.DEFAULT).unrestricted());
  58         assert x.stamp(NodeView.DEFAULT).isCompatible(y.stamp(NodeView.DEFAULT)) && x.stamp(NodeView.DEFAULT) instanceof IntegerStamp;
  59         this.guard = guard;
  60     }
  61 
  62     @Override
  63     public boolean inferStamp() {
  64         /*
  65          * Note: it is not allowed to use the foldStamp method of the regular mul node as we do not
  66          * know the result stamp of this node if we do not know whether we may deopt. If we know we
  67          * can never overflow we will replace this node with its non overflow checking counterpart
  68          * anyway.
  69          */
  70         return false;
  71     }
  72 
  73     @Override
  74     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  75         if (forX.isConstant() && !forY.isConstant()) {
  76             return new IntegerMulExactNode(forY, forX, guard).canonical(tool);
  77         }
  78         if (forX.isConstant() && forY.isConstant()) {
  79             return canonicalXYconstant(forX, forY);
  80         } else if (forY.isConstant()) {
  81             long c = forY.asJavaConstant().asLong();
  82             if (c == 1) {
  83                 return forX;
  84             }
  85             if (c == 0) {
  86                 return ConstantNode.forIntegerStamp(stamp(NodeView.DEFAULT), 0);
  87             }
  88         }
  89         if (!IntegerStamp.multiplicationCanOverflow((IntegerStamp) x.stamp(NodeView.DEFAULT), (IntegerStamp) y.stamp(NodeView.DEFAULT))) {
  90             return new MulNode(x, y).canonical(tool);
  91         }
  92         return this;
  93     }
  94 
  95     private ValueNode canonicalXYconstant(ValueNode forX, ValueNode forY) {
  96         JavaConstant xConst = forX.asJavaConstant();
  97         JavaConstant yConst = forY.asJavaConstant();
  98         assert xConst.getJavaKind() == yConst.getJavaKind();
  99         try {
 100             if (xConst.getJavaKind() == JavaKind.Int) {
 101                 return ConstantNode.forInt(Math.multiplyExact(xConst.asInt(), yConst.asInt()));
 102             } else {
 103                 assert xConst.getJavaKind() == JavaKind.Long;
 104                 return ConstantNode.forLong(Math.multiplyExact(xConst.asLong(), yConst.asLong()));
 105             }
 106         } catch (ArithmeticException ex) {
 107             // The operation will result in an overflow exception, so do not canonicalize.
 108         }
 109         return this;
 110     }
 111 
 112     @Override
 113     public GuardingNode getGuard() {
 114         return guard;
 115     }
 116 
 117     @Override
 118     public void setGuard(GuardingNode guard) {
 119         updateUsagesInterface(this.guard, guard);
 120         this.guard = guard;















 121     }
 122 }
< prev index next >