annotate graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/LeftShiftNode.java @ 11851:04b039d82e86

Simplify constant folding.
author Roland Schatz <roland.schatz@oracle.com>
date Wed, 02 Oct 2013 12:09:51 +0200
parents 2ad1435489d1
children 673f93db4adc
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
5507
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5504
diff changeset
25 import com.oracle.graal.api.meta.*;
5060
4ed4295ce15f Update import statements.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5059
diff changeset
26 import com.oracle.graal.graph.*;
4ed4295ce15f Update import statements.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5059
diff changeset
27 import com.oracle.graal.nodes.*;
4ed4295ce15f Update import statements.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5059
diff changeset
28 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
29 import com.oracle.graal.nodes.type.*;
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
30
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
31 @NodeInfo(shortName = "<<")
11289
2ad1435489d1 Use ArithmeticLIRGenerator in arithmetic nodes.
Roland Schatz <roland.schatz@oracle.com>
parents: 11265
diff changeset
32 public final class LeftShiftNode extends ShiftNode implements Canonicalizable {
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
33
5540
a891c53a295b Renaming RiKind => Kind.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5507
diff changeset
34 public LeftShiftNode(Kind kind, ValueNode x, ValueNode y) {
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
35 super(kind, x, y);
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
36 }
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 @Override
5657
d71eb56d6bb0 new stamp inference in CanonicalizerPhase, IntegerStamp.mask
Lukas Stadler <lukas.stadler@jku.at>
parents: 5540
diff changeset
39 public boolean inferStamp() {
11265
ef6915cf1e59 Add illegal stamp
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 10575
diff changeset
40 return updateStamp(StampTool.leftShift(x().stamp(), y().stamp()));
5657
d71eb56d6bb0 new stamp inference in CanonicalizerPhase, IntegerStamp.mask
Lukas Stadler <lukas.stadler@jku.at>
parents: 5540
diff changeset
41 }
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 @Override
11851
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
44 public Constant evalConst(Constant... inputs) {
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
45 assert inputs.length == 2;
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
46 if (kind() == Kind.Int) {
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
47 return Constant.forInt(inputs[0].asInt() << inputs[1].asInt());
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
48 } else {
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
49 assert kind() == Kind.Long;
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
50 return Constant.forLong(inputs[0].asLong() << inputs[1].asLong());
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
51 }
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
52 }
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
53
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
54 @Override
4309
4a609a685fa4 changes to Node structures:
Lukas Stadler <lukas.stadler@jku.at>
parents: 4199
diff changeset
55 public ValueNode canonical(CanonicalizerTool tool) {
11851
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
56 if (x().isConstant() && y().isConstant()) {
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
57 return ConstantNode.forPrimitive(evalConst(x().asConstant(), y().asConstant()), graph());
04b039d82e86 Simplify constant folding.
Roland Schatz <roland.schatz@oracle.com>
parents: 11289
diff changeset
58 } else if (y().isConstant()) {
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
59 int amount = y().asConstant().asInt();
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
60 int originalAmout = amount;
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
61 int mask;
5540
a891c53a295b Renaming RiKind => Kind.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5507
diff changeset
62 if (kind() == Kind.Int) {
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
63 mask = 0x1f;
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
64 } else {
5540
a891c53a295b Renaming RiKind => Kind.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5507
diff changeset
65 assert kind() == Kind.Long;
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
66 mask = 0x3f;
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
67 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
68 amount &= mask;
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
69 if (amount == 0) {
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
70 return x();
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
71 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
72 if (x() instanceof ShiftNode) {
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
73 ShiftNode other = (ShiftNode) x();
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
74 if (other.y().isConstant()) {
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
75 int otherAmount = other.y().asConstant().asInt() & mask;
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
76 if (other instanceof LeftShiftNode) {
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
77 int total = amount + otherAmount;
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
78 if (total != (total & mask)) {
10575
97d280a879ff ShiftNode: add constant with correct stamp in canonical()
Bernhard Urban <bernhard.urban@jku.at>
parents: 5657
diff changeset
79 return ConstantNode.forIntegerKind(kind(), 0, graph());
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
80 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
81 return graph().unique(new LeftShiftNode(kind(), other.x(), ConstantNode.forInt(total, graph())));
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
82 } else if ((other instanceof RightShiftNode || other instanceof UnsignedRightShiftNode) && otherAmount == amount) {
5540
a891c53a295b Renaming RiKind => Kind.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5507
diff changeset
83 if (kind() == Kind.Long) {
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
84 return graph().unique(new AndNode(kind(), other.x(), ConstantNode.forLong(-1L << amount, graph())));
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
85 } else {
5540
a891c53a295b Renaming RiKind => Kind.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5507
diff changeset
86 assert kind() == Kind.Int;
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
87 return graph().unique(new AndNode(kind(), other.x(), ConstantNode.forInt(-1 << amount, graph())));
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
88 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
89 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
90 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
91 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
92 if (originalAmout != amount) {
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
93 return graph().unique(new LeftShiftNode(kind(), x(), ConstantNode.forInt(amount, graph())));
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
94 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
95 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
96 return this;
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
97 }
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
98
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
99 @Override
11289
2ad1435489d1 Use ArithmeticLIRGenerator in arithmetic nodes.
Roland Schatz <roland.schatz@oracle.com>
parents: 11265
diff changeset
100 public void generate(ArithmeticLIRGenerator gen) {
3733
e233f5660da4 Added Java files from Maxine project.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
101 gen.setResult(this, gen.emitShl(gen.operand(x()), gen.operand(y())));
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 }