annotate graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerDivNode.java @ 16217:561070049e73

implement Canonicalizable.Binary in the BinaryNode hierarchy
author Lukas Stadler <lukas.stadler@oracle.com>
date Wed, 25 Jun 2014 16:54:56 +0200
parents b558af6ff4bc
children c3260b05fd26
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 /*
16215
b558af6ff4bc don't pass stamps to arithmetic node constructors
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16207
diff changeset
2 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
3733
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.*;
15198
2c0cfbf454b5 Move LIRTypeTool and Stamp to graal.compiler.common.
Josef Eisl <josef.eisl@jku.at>
parents: 14950
diff changeset
26 import com.oracle.graal.compiler.common.type.*;
5060
4ed4295ce15f Update import statements.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5059
diff changeset
27 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
28 import com.oracle.graal.graph.spi.*;
5060
4ed4295ce15f Update import statements.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5059
diff changeset
29 import com.oracle.graal.nodes.*;
4ed4295ce15f Update import statements.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5059
diff changeset
30 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
31 import com.oracle.graal.nodes.type.*;
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
32
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
33 @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
34 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
35
16215
b558af6ff4bc don't pass stamps to arithmetic node constructors
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16207
diff changeset
36 public IntegerDivNode(ValueNode x, ValueNode y) {
b558af6ff4bc don't pass stamps to arithmetic node constructors
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16207
diff changeset
37 super(x.stamp().unrestricted(), x, y);
3733
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
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
40 @Override
5657
d71eb56d6bb0 new stamp inference in CanonicalizerPhase, IntegerStamp.mask
Lukas Stadler <lukas.stadler@jku.at>
parents: 5540
diff changeset
41 public boolean inferStamp() {
11265
ef6915cf1e59 Add illegal stamp
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 9449
diff changeset
42 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
43 }
d71eb56d6bb0 new stamp inference in CanonicalizerPhase, IntegerStamp.mask
Lukas Stadler <lukas.stadler@jku.at>
parents: 5540
diff changeset
44
d71eb56d6bb0 new stamp inference in CanonicalizerPhase, IntegerStamp.mask
Lukas Stadler <lukas.stadler@jku.at>
parents: 5540
diff changeset
45 @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
46 public Node canonical(CanonicalizerTool tool) {
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
47 if (x().isConstant() && y().isConstant()) {
14024
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
48 long y = y().asConstant().asLong();
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
49 if (y == 0) {
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
50 return this; // this will trap, can not canonicalize
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
51 }
14024
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
52 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
53 } else if (y().isConstant()) {
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
54 long c = y().asConstant().asLong();
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
55 if (c == 1) {
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
56 return x();
5689
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
57 }
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
58 if (c == -1) {
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
59 return graph().unique(new NegateNode(x()));
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
60 }
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
61 long abs = Math.abs(c);
11265
ef6915cf1e59 Add illegal stamp
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 9449
diff changeset
62 if (CodeUtil.isPowerOf2(abs) && x().stamp() instanceof IntegerStamp) {
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());
16217
561070049e73 implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16215
diff changeset
69 RightShiftNode sign = new RightShiftNode(x(), ConstantNode.forInt(bits - 1));
561070049e73 implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16215
diff changeset
70 UnsignedRightShiftNode round = new UnsignedRightShiftNode(sign, ConstantNode.forInt(bits - log2));
561070049e73 implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16215
diff changeset
71 dividend = IntegerArithmeticNode.add(dividend, round);
5689
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
72 }
16217
561070049e73 implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16215
diff changeset
73 RightShiftNode shift = new RightShiftNode(dividend, ConstantNode.forInt(log2));
5689
1d3df3a16940 Canonicalize more Mul/Div to shifts
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 5657
diff changeset
74 if (c < 0) {
16217
561070049e73 implement Canonicalizable.Binary in the BinaryNode hierarchy
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16215
diff changeset
75 return new NegateNode(shift);
5689
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();
16207
df6f2365b153 rename of x() to getX(), y() to getY() and object() to getValue()
Lukas Stadler <lukas.stadler@oracle.com>
parents: 15301
diff changeset
84 if (integerSubNode.getY() instanceof IntegerRemNode) {
df6f2365b153 rename of x() to getX(), y() to getY() and object() to getValue()
Lukas Stadler <lukas.stadler@oracle.com>
parents: 15301
diff changeset
85 IntegerRemNode integerRemNode = (IntegerRemNode) integerSubNode.getY();
df6f2365b153 rename of x() to getX(), y() to getY() and object() to getValue()
Lukas Stadler <lukas.stadler@oracle.com>
parents: 15301
diff changeset
86 if (integerSubNode.stamp().isCompatible(this.stamp()) && integerRemNode.stamp().isCompatible(this.stamp()) && integerSubNode.getX() == integerRemNode.x() &&
14024
34c07ef28bc9 Support integer arithmetic for arbitrary types.
Roland Schatz <roland.schatz@oracle.com>
parents: 12655
diff changeset
87 this.y() == integerRemNode.y()) {
16215
b558af6ff4bc don't pass stamps to arithmetic node constructors
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16207
diff changeset
88 return graph().add(new IntegerDivNode(integerSubNode.getX(), 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
14950
d5a1206e1923 NodeLIRBuilderTool: fix typo in interface name.
Josef Eisl <josef.eisl@jku.at>
parents: 14888
diff changeset
109 public void generate(NodeLIRBuilderTool gen) {
15301
468b2428c403 Change DeoptimizingNode and Access to LIRFrameState in *LIRGenerator*.
Josef Eisl <josef.eisl@jku.at>
parents: 15198
diff changeset
110 gen.setResult(this, gen.getLIRGeneratorTool().emitDiv(gen.operand(x()), gen.operand(y()), gen.state(this)));
8892
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 }