annotate graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerDivNode.java @ 14024:34c07ef28bc9

Support integer arithmetic for arbitrary types.
author Roland Schatz <roland.schatz@oracle.com>
date Wed, 26 Feb 2014 15:55:04 +0100
parents 2c4aa758ee18
children 1cf373d2b415
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1 /*
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
4 *
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
8 *
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
13 * accompanied this code).
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
14 *
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
18 *
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
21 * questions.
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
22 */
5060
4ed4295ce15f Update import statements.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5059
diff changeset
23 package com.oracle.graal.nodes.calc;
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
24
5657
d71eb56d6bb0 new stamp inference in CanonicalizerPhase, IntegerStamp.mask
Lukas Stadler <lukas.stadler@jku.at>
parents: 5540
diff changeset
25 import com.oracle.graal.api.code.*;
5060
4ed4295ce15f Update import statements.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5059
diff changeset
26 import com.oracle.graal.graph.*;
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.*;
5060
4ed4295ce15f Update import statements.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5059
diff changeset
28 import com.oracle.graal.nodes.*;
4ed4295ce15f Update import statements.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5059
diff changeset
29 import com.oracle.graal.nodes.spi.*;
5657
d71eb56d6bb0 new stamp inference in CanonicalizerPhase, IntegerStamp.mask
Lukas Stadler <lukas.stadler@jku.at>
parents: 5540
diff changeset
30 import com.oracle.graal.nodes.type.*;
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
31
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
32 @NodeInfo(shortName = "/")
7305
cd205ca515dd Make integer division and remainder nodes fixed so that they can be lowered via snippets to do exception checks
Christian Wimmer <christian.wimmer@oracle.com>
parents: 7098
diff changeset
33 public class IntegerDivNode extends FixedBinaryNode implements Canonicalizable, Lowerable, LIRLowerable {
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
34
14024
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
35 public IntegerDivNode(Stamp stamp, ValueNode x, ValueNode y) {
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
36 super(stamp, x, y);
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
37 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
38
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
39 @Override
5657
d71eb56d6bb0 new stamp inference in CanonicalizerPhase, IntegerStamp.mask
Lukas Stadler <lukas.stadler@jku.at>
parents: 5540
diff changeset
40 public boolean inferStamp() {
11265
ef6915cf1e59 Add illegal stamp
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 9449
diff changeset
41 return updateStamp(StampTool.div(x().stamp(), y().stamp()));
5657
d71eb56d6bb0 new stamp inference in CanonicalizerPhase, IntegerStamp.mask
Lukas Stadler <lukas.stadler@jku.at>
parents: 5540
diff changeset
42 }
d71eb56d6bb0 new stamp inference in CanonicalizerPhase, IntegerStamp.mask
Lukas Stadler <lukas.stadler@jku.at>
parents: 5540
diff changeset
43
d71eb56d6bb0 new stamp inference in CanonicalizerPhase, IntegerStamp.mask
Lukas Stadler <lukas.stadler@jku.at>
parents: 5540
diff changeset
44 @Override
11880
9a747d8e0d0f broadened types in Canonicalizable interface so that it can be moved to the com.oracle.graal.graph project (GRAAL-506)
Doug Simon <doug.simon@oracle.com>
parents: 11579
diff changeset
45 public Node canonical(CanonicalizerTool tool) {
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
46 if (x().isConstant() && y().isConstant()) {
14024
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
47 long y = y().asConstant().asLong();
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
48 if (y == 0) {
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
49 return this; // this will trap, can not canonicalize
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
50 }
14024
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
51 return ConstantNode.forIntegerStamp(stamp(), x().asConstant().asLong() / y, graph());
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
52 } else if (y().isConstant()) {
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
53 long c = y().asConstant().asLong();
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
54 if (c == 1) {
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
55 return x();
5689
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
56 }
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
57 if (c == -1) {
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
58 return graph().unique(new NegateNode(x()));
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
59 }
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
60 long abs = Math.abs(c);
11265
ef6915cf1e59 Add illegal stamp
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 9449
diff changeset
61 if (CodeUtil.isPowerOf2(abs) && x().stamp() instanceof IntegerStamp) {
14024
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
62 Stamp unrestricted = stamp().unrestricted();
5689
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
63 ValueNode dividend = x();
11265
ef6915cf1e59 Add illegal stamp
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 9449
diff changeset
64 IntegerStamp stampX = (IntegerStamp) x().stamp();
5689
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
65 int log2 = CodeUtil.log2(abs);
6299
1ed726759f65 Add helper methods to IntegerStamp for sign, use them
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5689
diff changeset
66 // no rounding if dividend is positive or if its low bits are always 0
11352
8185c119d731 "always set" bit mask on IntegerStamps
Lukas Stadler <lukas.stadler@jku.at>
parents: 11265
diff changeset
67 if (stampX.canBeNegative() || (stampX.upMask() & (abs - 1)) != 0) {
14024
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
68 int bits = PrimitiveStamp.getBits(stamp());
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
69 RightShiftNode sign = graph().unique(new RightShiftNode(unrestricted, x(), ConstantNode.forInt(bits - 1, graph())));
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
70 UnsignedRightShiftNode round = graph().unique(new UnsignedRightShiftNode(unrestricted, sign, ConstantNode.forInt(bits - log2, graph())));
12655
2c4aa758ee18 made ConstantNodes external to a Graph (GRAAL-508)
Doug Simon <doug.simon@oracle.com>
parents: 12045
diff changeset
71 dividend = IntegerArithmeticNode.add(graph(), dividend, round);
5689
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
72 }
14024
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
73 RightShiftNode shift = graph().unique(new RightShiftNode(unrestricted, dividend, ConstantNode.forInt(log2, graph())));
5689
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
74 if (c < 0) {
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
75 return graph().unique(new NegateNode(shift));
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
76 }
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
77 return shift;
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
78 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
79 }
7378
88506cfc3bab Canonicalize ((a - a % b) / b) into (a / b).
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7305
diff changeset
80
88506cfc3bab Canonicalize ((a - a % b) / b) into (a / b).
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7305
diff changeset
81 // Convert the expression ((a - a % b) / b) into (a / b).
88506cfc3bab Canonicalize ((a - a % b) / b) into (a / b).
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7305
diff changeset
82 if (x() instanceof IntegerSubNode) {
88506cfc3bab Canonicalize ((a - a % b) / b) into (a / b).
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7305
diff changeset
83 IntegerSubNode integerSubNode = (IntegerSubNode) x();
88506cfc3bab Canonicalize ((a - a % b) / b) into (a / b).
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7305
diff changeset
84 if (integerSubNode.y() instanceof IntegerRemNode) {
88506cfc3bab Canonicalize ((a - a % b) / b) into (a / b).
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7305
diff changeset
85 IntegerRemNode integerRemNode = (IntegerRemNode) integerSubNode.y();
14024
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
86 if (integerSubNode.stamp().isCompatible(this.stamp()) && integerRemNode.stamp().isCompatible(this.stamp()) && integerSubNode.x() == integerRemNode.x() &&
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
87 this.y() == integerRemNode.y()) {
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
88 return graph().add(new IntegerDivNode(stamp(), integerSubNode.x(), this.y()));
7378
88506cfc3bab Canonicalize ((a - a % b) / b) into (a / b).
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7305
diff changeset
89 }
88506cfc3bab Canonicalize ((a - a % b) / b) into (a / b).
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7305
diff changeset
90 }
88506cfc3bab Canonicalize ((a - a % b) / b) into (a / b).
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7305
diff changeset
91 }
88506cfc3bab Canonicalize ((a - a % b) / b) into (a / b).
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 7305
diff changeset
92
9449
3d309a26d4d5 Add canonicalization to interger div
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 9251
diff changeset
93 if (next() instanceof IntegerDivNode) {
3d309a26d4d5 Add canonicalization to interger div
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 9251
diff changeset
94 NodeClass nodeClass = NodeClass.get(this.getClass());
3d309a26d4d5 Add canonicalization to interger div
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 9251
diff changeset
95 if (next().getClass() == this.getClass() && nodeClass.inputsEqual(this, next()) && nodeClass.valueEqual(this, next())) {
3d309a26d4d5 Add canonicalization to interger div
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 9251
diff changeset
96 return next();
3d309a26d4d5 Add canonicalization to interger div
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 9251
diff changeset
97 }
3d309a26d4d5 Add canonicalization to interger div
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 9251
diff changeset
98 }
3d309a26d4d5 Add canonicalization to interger div
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 9251
diff changeset
99
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
100 return this;
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
101 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
102
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
103 @Override
11579
ae619d70bf4b Rename LoweringType to GuardsPhase and make it an attribute of StructuredGraphs
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 11352
diff changeset
104 public void lower(LoweringTool tool) {
12045
0fc653a9e019 made GraalCodeCacheProvider independent of CodeCacheProvider and renamed the former to LoweringProvider (GRAAL-511)
Doug Simon <doug.simon@oracle.com>
parents: 11959
diff changeset
105 tool.getLowerer().lower(this, tool);
7305
cd205ca515dd Make integer division and remainder nodes fixed so that they can be lowered via snippets to do exception checks
Christian Wimmer <christian.wimmer@oracle.com>
parents: 7098
diff changeset
106 }
cd205ca515dd Make integer division and remainder nodes fixed so that they can be lowered via snippets to do exception checks
Christian Wimmer <christian.wimmer@oracle.com>
parents: 7098
diff changeset
107
cd205ca515dd Make integer division and remainder nodes fixed so that they can be lowered via snippets to do exception checks
Christian Wimmer <christian.wimmer@oracle.com>
parents: 7098
diff changeset
108 @Override
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
109 public void generate(LIRGeneratorTool gen) {
8892
2c0c708a0ad6 Introduce DeoptimizingNode interface
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 7378
diff changeset
110 gen.setResult(this, gen.emitDiv(gen.operand(x()), gen.operand(y()), this));
2c0c708a0ad6 Introduce DeoptimizingNode interface
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 7378
diff changeset
111 }
2c0c708a0ad6 Introduce DeoptimizingNode interface
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 7378
diff changeset
112
2c0c708a0ad6 Introduce DeoptimizingNode interface
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 7378
diff changeset
113 @Override
2c0c708a0ad6 Introduce DeoptimizingNode interface
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 7378
diff changeset
114 public boolean canDeoptimize() {
11265
ef6915cf1e59 Add illegal stamp
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 9449
diff changeset
115 return !(y().stamp() instanceof IntegerStamp) || ((IntegerStamp) y().stamp()).contains(0);
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
116 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
117 }