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 
IntegerMulExactNode(ValueNode x, ValueNode y, SpeculationReason speculation)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
inferStamp()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
canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY)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 
canonicalXconstant(ValueNode forX, ValueNode forY)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
createSplit(AbstractBeginNode next, AbstractBeginNode deopt)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
getSpeculation()121     public SpeculationReason getSpeculation() {
122         return speculation;
123     }
124 
125     @Override
getAnchor()126     public AnchoringNode getAnchor() {
127         return anchor;
128     }
129 
130     @Override
setAnchor(AnchoringNode x)131     public void setAnchor(AnchoringNode x) {
132         updateUsagesInterface(this.anchor, x);
133         this.anchor = x;
134     }
135 
136     @Override
lower(LoweringTool tool)137     public void lower(LoweringTool tool) {
138         IntegerExactArithmeticSplitNode.lower(tool, this);
139     }
140 }
141