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 
IntegerMulExactNode(ValueNode x, ValueNode y, GuardingNode guard)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
inferStamp()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
canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY)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 
canonicalXYconstant(ValueNode forX, ValueNode forY)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
getGuard()113     public GuardingNode getGuard() {
114         return guard;
115     }
116 
117     @Override
setGuard(GuardingNode guard)118     public void setGuard(GuardingNode guard) {
119         updateUsagesInterface(this.guard, guard);
120         this.guard = guard;
121     }
122 }
123