Mercurial > hg > graal-compiler
annotate graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/arithmetic/IntegerMulExactNode.java @ 16841:cbd42807a31f
moved NodeInfo and friends into separate com.oracle.graal.nodeinfo project so that annotation processor can be applied to the base Node class
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Fri, 15 Aug 2014 11:34:38 +0200 |
parents | 5d468add216f |
children | 06c15e88d383 |
rev | line source |
---|---|
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
1 /* |
16217
561070049e73
implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents:
16215
diff
changeset
|
2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
4 * |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
8 * |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
13 * accompanied this code). |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
14 * |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
18 * |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
21 * questions. |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
22 */ |
11312
f0c8303cf88e
Restructure Truffle API node intrinsics and introduce new intrinsics for type system modelling.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
10484
diff
changeset
|
23 package com.oracle.graal.truffle.nodes.arithmetic; |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
24 |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
25 import com.oracle.graal.api.meta.*; |
15261
882f4cb7cfcf
Move Stamps to graal.compiler.common.
Josef Eisl <josef.eisl@jku.at>
parents:
15145
diff
changeset
|
26 import com.oracle.graal.compiler.common.type.*; |
11881
da9db8331658
moved Canonicalizable and Simplifiable to the com.oracle.graal.graph project (GRAAL-506)
Doug Simon <doug.simon@oracle.com>
parents:
11880
diff
changeset
|
27 import com.oracle.graal.graph.spi.*; |
16841
cbd42807a31f
moved NodeInfo and friends into separate com.oracle.graal.nodeinfo project so that annotation processor can be applied to the base Node class
Doug Simon <doug.simon@oracle.com>
parents:
16811
diff
changeset
|
28 import com.oracle.graal.nodeinfo.*; |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
29 import com.oracle.graal.nodes.*; |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
30 import com.oracle.graal.nodes.calc.*; |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
31 import com.oracle.graal.nodes.spi.*; |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
32 import com.oracle.truffle.api.*; |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
33 |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
34 /** |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
35 * Node representing an exact integer multiplication that will throw an {@link ArithmeticException} |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
36 * in case the addition would overflow the 32 bit range. |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
37 */ |
16811
5d468add216f
added @NodeInfo annotations to all Node classes
Doug Simon <doug.simon@oracle.com>
parents:
16543
diff
changeset
|
38 @NodeInfo |
16217
561070049e73
implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents:
16215
diff
changeset
|
39 public class IntegerMulExactNode extends IntegerMulNode implements IntegerExactArithmeticNode { |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
40 |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
41 public IntegerMulExactNode(ValueNode x, ValueNode y) { |
16215
b558af6ff4bc
don't pass stamps to arithmetic node constructors
Lukas Stadler <lukas.stadler@oracle.com>
parents:
16207
diff
changeset
|
42 super(x, y); |
14024
34c07ef28bc9
Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents:
13741
diff
changeset
|
43 assert x.stamp().isCompatible(y.stamp()) && x.stamp() instanceof IntegerStamp; |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
44 } |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
45 |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
46 @Override |
16217
561070049e73
implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents:
16215
diff
changeset
|
47 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { |
561070049e73
implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents:
16215
diff
changeset
|
48 if (forX.isConstant() && !forY.isConstant()) { |
561070049e73
implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents:
16215
diff
changeset
|
49 return new IntegerMulExactNode(forY, forX); |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
50 } |
16217
561070049e73
implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents:
16215
diff
changeset
|
51 if (forX.isConstant()) { |
16543
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
52 return canonicalXconstant(forX, forY); |
16217
561070049e73
implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents:
16215
diff
changeset
|
53 } else if (forY.isConstant()) { |
561070049e73
implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents:
16215
diff
changeset
|
54 long c = forY.asConstant().asLong(); |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
55 if (c == 1) { |
16217
561070049e73
implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents:
16215
diff
changeset
|
56 return forX; |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
57 } |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
58 if (c == 0) { |
16217
561070049e73
implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents:
16215
diff
changeset
|
59 return ConstantNode.forIntegerStamp(stamp(), 0); |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
60 } |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
61 } |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
62 return this; |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
63 } |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
64 |
16543
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
65 private ValueNode canonicalXconstant(ValueNode forX, ValueNode forY) { |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
66 Constant xConst = forX.asConstant(); |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
67 Constant yConst = forY.asConstant(); |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
68 assert xConst.getKind() == yConst.getKind(); |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
69 try { |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
70 if (xConst.getKind() == Kind.Int) { |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
71 return ConstantNode.forInt(ExactMath.multiplyExact(xConst.asInt(), yConst.asInt())); |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
72 } else { |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
73 assert xConst.getKind() == Kind.Long; |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
74 return ConstantNode.forLong(ExactMath.multiplyExact(xConst.asLong(), yConst.asLong())); |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
75 } |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
76 } catch (ArithmeticException ex) { |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
77 // The operation will result in an overflow exception, so do not canonicalize. |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
78 } |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
79 return this; |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
80 } |
eb3209d37c50
extract methods in exact arithmetic nodes
Christian Wirth <christian.wirth@oracle.com>
parents:
16217
diff
changeset
|
81 |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
82 @Override |
15145
df2ef5204f2b
Remove AbstractBeginNode, move the framestate from AbstractBeginNode to BeginStateSplitNode.
Gilles Duboscq <duboscq@ssw.jku.at>
parents:
14024
diff
changeset
|
83 public IntegerExactArithmeticSplitNode createSplit(BeginNode next, BeginNode deopt) { |
16207
df6f2365b153
rename of x() to getX(), y() to getY() and object() to getValue()
Lukas Stadler <lukas.stadler@oracle.com>
parents:
15261
diff
changeset
|
84 return graph().add(new IntegerMulExactSplitNode(stamp(), getX(), getY(), next, deopt)); |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
85 } |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
86 |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
87 @Override |
11579
ae619d70bf4b
Rename LoweringType to GuardsPhase and make it an attribute of StructuredGraphs
Gilles Duboscq <duboscq@ssw.jku.at>
parents:
11312
diff
changeset
|
88 public void lower(LoweringTool tool) { |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
89 IntegerExactArithmeticSplitNode.lower(tool, this); |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
90 } |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
91 |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
92 @NodeIntrinsic |
13741
7b80276a4e65
Add bodies so exact math can deopt
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
11881
diff
changeset
|
93 public static int multiplyExact(int a, int b) { |
7b80276a4e65
Add bodies so exact math can deopt
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
11881
diff
changeset
|
94 return ExactMath.multiplyExact(a, b); |
7b80276a4e65
Add bodies so exact math can deopt
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
11881
diff
changeset
|
95 } |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
96 |
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
97 @NodeIntrinsic |
13741
7b80276a4e65
Add bodies so exact math can deopt
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
11881
diff
changeset
|
98 public static long multiplyExact(long a, long b) { |
7b80276a4e65
Add bodies so exact math can deopt
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
11881
diff
changeset
|
99 return ExactMath.multiplyExact(a, b); |
7b80276a4e65
Add bodies so exact math can deopt
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
11881
diff
changeset
|
100 } |
10484
0097fb11c16f
Add basic version of Graal's Truffle runtime.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff
changeset
|
101 } |