43972
|
1 |
/*
|
58533
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.nodes.calc;
|
|
26 |
|
58533
|
27 |
import static org.graalvm.compiler.nodes.calc.BinaryArithmeticNode.getArithmeticOpTable;
|
|
28 |
|
43972
|
29 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
|
58533
|
30 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.ShiftOp;
|
43972
|
31 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.ShiftOp.Shl;
|
46371
|
32 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
33 |
import org.graalvm.compiler.core.common.type.Stamp;
|
43972
|
34 |
import org.graalvm.compiler.graph.NodeClass;
|
|
35 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
|
36 |
import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
|
|
37 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
38 |
import org.graalvm.compiler.nodes.ConstantNode;
|
48190
|
39 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
40 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
41 |
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
|
|
42 |
|
|
43 |
import jdk.vm.ci.meta.JavaKind;
|
|
44 |
|
|
45 |
@NodeInfo(shortName = "<<")
|
|
46 |
public final class LeftShiftNode extends ShiftNode<Shl> {
|
|
47 |
|
|
48 |
public static final NodeClass<LeftShiftNode> TYPE = NodeClass.create(LeftShiftNode.class);
|
|
49 |
|
|
50 |
public LeftShiftNode(ValueNode x, ValueNode y) {
|
58533
|
51 |
super(TYPE, getArithmeticOpTable(x).getShl(), x, y);
|
43972
|
52 |
}
|
|
53 |
|
48190
|
54 |
public static ValueNode create(ValueNode x, ValueNode y, NodeView view) {
|
|
55 |
ArithmeticOpTable.ShiftOp<Shl> op = ArithmeticOpTable.forStamp(x.stamp(view)).getShl();
|
|
56 |
Stamp stamp = op.foldStamp(x.stamp(view), (IntegerStamp) y.stamp(view));
|
|
57 |
ValueNode value = ShiftNode.canonical(op, stamp, x, y, view);
|
46371
|
58 |
if (value != null) {
|
|
59 |
return value;
|
|
60 |
}
|
|
61 |
|
|
62 |
return canonical(null, op, stamp, x, y);
|
|
63 |
}
|
|
64 |
|
43972
|
65 |
@Override
|
58533
|
66 |
protected ShiftOp<Shl> getOp(ArithmeticOpTable table) {
|
|
67 |
return table.getShl();
|
|
68 |
}
|
|
69 |
|
|
70 |
@Override
|
43972
|
71 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
|
|
72 |
ValueNode ret = super.canonical(tool, forX, forY);
|
|
73 |
if (ret != this) {
|
|
74 |
return ret;
|
|
75 |
}
|
|
76 |
|
48190
|
77 |
return canonical(this, getArithmeticOp(), stamp(NodeView.DEFAULT), forX, forY);
|
46371
|
78 |
}
|
|
79 |
|
|
80 |
private static ValueNode canonical(LeftShiftNode leftShiftNode, ArithmeticOpTable.ShiftOp<Shl> op, Stamp stamp, ValueNode forX, ValueNode forY) {
|
|
81 |
LeftShiftNode self = leftShiftNode;
|
43972
|
82 |
if (forY.isConstant()) {
|
|
83 |
int amount = forY.asJavaConstant().asInt();
|
46371
|
84 |
int originalAmount = amount;
|
|
85 |
int mask = op.getShiftAmountMask(stamp);
|
43972
|
86 |
amount &= mask;
|
|
87 |
if (amount == 0) {
|
|
88 |
return forX;
|
|
89 |
}
|
|
90 |
if (forX instanceof ShiftNode) {
|
|
91 |
ShiftNode<?> other = (ShiftNode<?>) forX;
|
|
92 |
if (other.getY().isConstant()) {
|
|
93 |
int otherAmount = other.getY().asJavaConstant().asInt() & mask;
|
|
94 |
if (other instanceof LeftShiftNode) {
|
|
95 |
int total = amount + otherAmount;
|
|
96 |
if (total != (total & mask)) {
|
46371
|
97 |
return ConstantNode.forIntegerKind(stamp.getStackKind(), 0);
|
43972
|
98 |
}
|
|
99 |
return new LeftShiftNode(other.getX(), ConstantNode.forInt(total));
|
|
100 |
} else if ((other instanceof RightShiftNode || other instanceof UnsignedRightShiftNode) && otherAmount == amount) {
|
46371
|
101 |
if (stamp.getStackKind() == JavaKind.Long) {
|
43972
|
102 |
return new AndNode(other.getX(), ConstantNode.forLong(-1L << amount));
|
|
103 |
} else {
|
46371
|
104 |
assert stamp.getStackKind() == JavaKind.Int;
|
43972
|
105 |
return new AndNode(other.getX(), ConstantNode.forInt(-1 << amount));
|
|
106 |
}
|
|
107 |
}
|
|
108 |
}
|
|
109 |
}
|
46371
|
110 |
if (originalAmount != amount) {
|
43972
|
111 |
return new LeftShiftNode(forX, ConstantNode.forInt(amount));
|
|
112 |
}
|
|
113 |
}
|
46371
|
114 |
if (self == null) {
|
|
115 |
self = new LeftShiftNode(forX, forY);
|
|
116 |
}
|
|
117 |
return self;
|
43972
|
118 |
}
|
|
119 |
|
|
120 |
@Override
|
|
121 |
public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
|
|
122 |
nodeValueMap.setResult(this, gen.emitShl(nodeValueMap.operand(getX()), nodeValueMap.operand(getY())));
|
|
123 |
}
|
|
124 |
}
|