43972
|
1 |
/*
|
54204
|
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.UShr;
|
|
32 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
48190
|
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 |
|
58533
|
43 |
import jdk.vm.ci.code.CodeUtil;
|
43972
|
44 |
import jdk.vm.ci.meta.JavaKind;
|
|
45 |
|
|
46 |
@NodeInfo(shortName = ">>>")
|
|
47 |
public final class UnsignedRightShiftNode extends ShiftNode<UShr> {
|
|
48 |
|
|
49 |
public static final NodeClass<UnsignedRightShiftNode> TYPE = NodeClass.create(UnsignedRightShiftNode.class);
|
|
50 |
|
|
51 |
public UnsignedRightShiftNode(ValueNode x, ValueNode y) {
|
58533
|
52 |
super(TYPE, getArithmeticOpTable(x).getUShr(), x, y);
|
43972
|
53 |
}
|
|
54 |
|
48190
|
55 |
public static ValueNode create(ValueNode x, ValueNode y, NodeView view) {
|
|
56 |
ArithmeticOpTable.ShiftOp<UShr> op = ArithmeticOpTable.forStamp(x.stamp(view)).getUShr();
|
|
57 |
Stamp stamp = op.foldStamp(x.stamp(view), (IntegerStamp) y.stamp(view));
|
|
58 |
ValueNode value = ShiftNode.canonical(op, stamp, x, y, view);
|
|
59 |
if (value != null) {
|
|
60 |
return value;
|
|
61 |
}
|
|
62 |
|
|
63 |
return canonical(null, op, stamp, x, y, view);
|
|
64 |
}
|
|
65 |
|
43972
|
66 |
@Override
|
58533
|
67 |
protected ShiftOp<UShr> getOp(ArithmeticOpTable table) {
|
|
68 |
return table.getUShr();
|
|
69 |
}
|
|
70 |
|
|
71 |
@Override
|
43972
|
72 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
|
48190
|
73 |
NodeView view = NodeView.from(tool);
|
43972
|
74 |
ValueNode ret = super.canonical(tool, forX, forY);
|
|
75 |
if (ret != this) {
|
|
76 |
return ret;
|
|
77 |
}
|
|
78 |
|
48190
|
79 |
return canonical(this, this.getArithmeticOp(), this.stamp(view), forX, forY, view);
|
|
80 |
}
|
|
81 |
|
|
82 |
@SuppressWarnings("unused")
|
|
83 |
private static ValueNode canonical(UnsignedRightShiftNode node, ArithmeticOpTable.ShiftOp<UShr> op, Stamp stamp, ValueNode forX, ValueNode forY, NodeView view) {
|
43972
|
84 |
if (forY.isConstant()) {
|
|
85 |
int amount = forY.asJavaConstant().asInt();
|
|
86 |
int originalAmout = amount;
|
48190
|
87 |
int mask = op.getShiftAmountMask(stamp);
|
43972
|
88 |
amount &= mask;
|
|
89 |
if (amount == 0) {
|
|
90 |
return forX;
|
|
91 |
}
|
54084
|
92 |
|
|
93 |
Stamp xStampGeneric = forX.stamp(view);
|
|
94 |
if (xStampGeneric instanceof IntegerStamp) {
|
|
95 |
IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
|
54204
|
96 |
long xMask = CodeUtil.mask(xStamp.getBits());
|
|
97 |
long xLowerBound = xStamp.lowerBound() & xMask;
|
|
98 |
long xUpperBound = xStamp.upperBound() & xMask;
|
54084
|
99 |
|
54204
|
100 |
if (xLowerBound >>> amount == xUpperBound >>> amount) {
|
54084
|
101 |
// The result of the shift is constant.
|
54204
|
102 |
return ConstantNode.forIntegerKind(stamp.getStackKind(), xLowerBound >>> amount);
|
54084
|
103 |
}
|
|
104 |
|
|
105 |
if (amount == xStamp.getBits() - 1 && xStamp.lowerBound() == -1 && xStamp.upperBound() == 0) {
|
|
106 |
// Shift is equivalent to a negation, i.e., turns -1 into 1 and keeps 0 at 0.
|
|
107 |
return NegateNode.create(forX, view);
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
43972
|
111 |
if (forX instanceof ShiftNode) {
|
|
112 |
ShiftNode<?> other = (ShiftNode<?>) forX;
|
|
113 |
if (other.getY().isConstant()) {
|
|
114 |
int otherAmount = other.getY().asJavaConstant().asInt() & mask;
|
|
115 |
if (other instanceof UnsignedRightShiftNode) {
|
|
116 |
int total = amount + otherAmount;
|
|
117 |
if (total != (total & mask)) {
|
48190
|
118 |
return ConstantNode.forIntegerKind(stamp.getStackKind(), 0);
|
43972
|
119 |
}
|
|
120 |
return new UnsignedRightShiftNode(other.getX(), ConstantNode.forInt(total));
|
|
121 |
} else if (other instanceof LeftShiftNode && otherAmount == amount) {
|
48190
|
122 |
if (stamp.getStackKind() == JavaKind.Long) {
|
43972
|
123 |
return new AndNode(other.getX(), ConstantNode.forLong(-1L >>> amount));
|
|
124 |
} else {
|
48190
|
125 |
assert stamp.getStackKind() == JavaKind.Int;
|
43972
|
126 |
return new AndNode(other.getX(), ConstantNode.forInt(-1 >>> amount));
|
|
127 |
}
|
|
128 |
}
|
|
129 |
}
|
|
130 |
}
|
|
131 |
if (originalAmout != amount) {
|
|
132 |
return new UnsignedRightShiftNode(forX, ConstantNode.forInt(amount));
|
|
133 |
}
|
|
134 |
}
|
48190
|
135 |
|
|
136 |
if (node != null) {
|
|
137 |
return node;
|
|
138 |
}
|
|
139 |
return new UnsignedRightShiftNode(forX, forY);
|
43972
|
140 |
}
|
|
141 |
|
|
142 |
@Override
|
|
143 |
public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
|
|
144 |
nodeValueMap.setResult(this, gen.emitUShr(nodeValueMap.operand(getX()), nodeValueMap.operand(getY())));
|
|
145 |
}
|
|
146 |
|
|
147 |
@Override
|
|
148 |
public boolean isNarrowable(int resultBits) {
|
|
149 |
if (super.isNarrowable(resultBits)) {
|
|
150 |
/*
|
|
151 |
* For unsigned right shifts, the narrow can be done before the shift if the cut off
|
|
152 |
* bits are all zero.
|
|
153 |
*/
|
48190
|
154 |
IntegerStamp inputStamp = (IntegerStamp) getX().stamp(NodeView.DEFAULT);
|
43972
|
155 |
return (inputStamp.upMask() & ~(resultBits - 1)) == 0;
|
|
156 |
} else {
|
|
157 |
return false;
|
|
158 |
}
|
|
159 |
}
|
|
160 |
}
|