43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2014, 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 |
|
|
27 |
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
|
58533
|
28 |
import static org.graalvm.compiler.nodes.calc.BinaryArithmeticNode.getArithmeticOpTable;
|
43972
|
29 |
|
58299
|
30 |
import org.graalvm.compiler.core.common.calc.CanonicalCondition;
|
43972
|
31 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
|
|
32 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp;
|
|
33 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp.Narrow;
|
|
34 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp.SignExtend;
|
46344
|
35 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
43972
|
36 |
import org.graalvm.compiler.core.common.type.PrimitiveStamp;
|
58299
|
37 |
import org.graalvm.compiler.core.common.type.Stamp;
|
43972
|
38 |
import org.graalvm.compiler.graph.NodeClass;
|
|
39 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
|
40 |
import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
|
|
41 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
48190
|
42 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
43 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
44 |
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
|
|
45 |
|
46344
|
46 |
import jdk.vm.ci.code.CodeUtil;
|
|
47 |
|
43972
|
48 |
/**
|
|
49 |
* The {@code NarrowNode} converts an integer to a narrower integer.
|
|
50 |
*/
|
|
51 |
@NodeInfo(cycles = CYCLES_1)
|
|
52 |
public final class NarrowNode extends IntegerConvertNode<Narrow, SignExtend> {
|
|
53 |
|
|
54 |
public static final NodeClass<NarrowNode> TYPE = NodeClass.create(NarrowNode.class);
|
|
55 |
|
|
56 |
public NarrowNode(ValueNode input, int resultBits) {
|
48190
|
57 |
this(input, PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT)), resultBits);
|
|
58 |
assert 0 < resultBits && resultBits <= PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT));
|
43972
|
59 |
}
|
|
60 |
|
|
61 |
public NarrowNode(ValueNode input, int inputBits, int resultBits) {
|
58533
|
62 |
super(TYPE, getArithmeticOpTable(input).getNarrow(), inputBits, resultBits, input);
|
43972
|
63 |
}
|
|
64 |
|
48190
|
65 |
public static ValueNode create(ValueNode input, int resultBits, NodeView view) {
|
|
66 |
return create(input, PrimitiveStamp.getBits(input.stamp(view)), resultBits, view);
|
43972
|
67 |
}
|
|
68 |
|
48190
|
69 |
public static ValueNode create(ValueNode input, int inputBits, int resultBits, NodeView view) {
|
|
70 |
IntegerConvertOp<Narrow> signExtend = ArithmeticOpTable.forStamp(input.stamp(view)).getNarrow();
|
|
71 |
ValueNode synonym = findSynonym(signExtend, input, inputBits, resultBits, signExtend.foldStamp(inputBits, resultBits, input.stamp(view)));
|
43972
|
72 |
if (synonym != null) {
|
|
73 |
return synonym;
|
|
74 |
} else {
|
|
75 |
return new NarrowNode(input, inputBits, resultBits);
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
@Override
|
58533
|
80 |
protected IntegerConvertOp<Narrow> getOp(ArithmeticOpTable table) {
|
|
81 |
return table.getNarrow();
|
|
82 |
}
|
|
83 |
|
|
84 |
@Override
|
|
85 |
protected IntegerConvertOp<SignExtend> getReverseOp(ArithmeticOpTable table) {
|
|
86 |
return table.getSignExtend();
|
|
87 |
}
|
|
88 |
|
|
89 |
@Override
|
43972
|
90 |
public boolean isLossless() {
|
58299
|
91 |
return checkLossless(this.getResultBits());
|
|
92 |
}
|
|
93 |
|
|
94 |
private boolean checkLossless(int bits) {
|
|
95 |
Stamp valueStamp = this.getValue().stamp(NodeView.DEFAULT);
|
|
96 |
if (bits > 0 && valueStamp instanceof IntegerStamp) {
|
|
97 |
IntegerStamp integerStamp = (IntegerStamp) valueStamp;
|
|
98 |
long valueUpMask = integerStamp.upMask();
|
|
99 |
if ((valueUpMask & CodeUtil.mask(bits)) == valueUpMask) {
|
|
100 |
return true;
|
|
101 |
}
|
|
102 |
}
|
43972
|
103 |
return false;
|
|
104 |
}
|
|
105 |
|
|
106 |
@Override
|
58299
|
107 |
public boolean preservesOrder(CanonicalCondition cond) {
|
|
108 |
switch (cond) {
|
|
109 |
case LT:
|
|
110 |
// Must guarantee that also sign bit does not flip.
|
|
111 |
return checkLossless(this.getResultBits() - 1);
|
|
112 |
default:
|
|
113 |
return checkLossless(this.getResultBits());
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
@Override
|
43972
|
118 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
|
48190
|
119 |
NodeView view = NodeView.from(tool);
|
43972
|
120 |
ValueNode ret = super.canonical(tool, forValue);
|
|
121 |
if (ret != this) {
|
|
122 |
return ret;
|
|
123 |
}
|
|
124 |
|
|
125 |
if (forValue instanceof NarrowNode) {
|
|
126 |
// zzzzzzzz yyyyxxxx -(narrow)-> yyyyxxxx -(narrow)-> xxxx
|
|
127 |
// ==> zzzzzzzz yyyyxxxx -(narrow)-> xxxx
|
|
128 |
NarrowNode other = (NarrowNode) forValue;
|
|
129 |
return new NarrowNode(other.getValue(), other.getInputBits(), getResultBits());
|
|
130 |
} else if (forValue instanceof IntegerConvertNode) {
|
|
131 |
// SignExtendNode or ZeroExtendNode
|
|
132 |
IntegerConvertNode<?, ?> other = (IntegerConvertNode<?, ?>) forValue;
|
46344
|
133 |
if (other.getValue().hasExactlyOneUsage() && other.hasMoreThanOneUsage()) {
|
43972
|
134 |
// Do not perform if this will introduce a new live value.
|
|
135 |
// If the original value's usage count is > 1, there is already another user.
|
|
136 |
// If the convert's usage count is <=1, it will be dead code eliminated.
|
|
137 |
return this;
|
|
138 |
}
|
|
139 |
if (getResultBits() == other.getInputBits()) {
|
|
140 |
// xxxx -(extend)-> yyyy xxxx -(narrow)-> xxxx
|
|
141 |
// ==> no-op
|
|
142 |
return other.getValue();
|
|
143 |
} else if (getResultBits() < other.getInputBits()) {
|
|
144 |
// yyyyxxxx -(extend)-> zzzzzzzz yyyyxxxx -(narrow)-> xxxx
|
|
145 |
// ==> yyyyxxxx -(narrow)-> xxxx
|
|
146 |
return new NarrowNode(other.getValue(), other.getInputBits(), getResultBits());
|
|
147 |
} else {
|
|
148 |
if (other instanceof SignExtendNode) {
|
|
149 |
// sxxx -(sign-extend)-> ssssssss sssssxxx -(narrow)-> sssssxxx
|
|
150 |
// ==> sxxx -(sign-extend)-> sssssxxx
|
48190
|
151 |
return SignExtendNode.create(other.getValue(), other.getInputBits(), getResultBits(), view);
|
43972
|
152 |
} else if (other instanceof ZeroExtendNode) {
|
48861
|
153 |
// xxxx -(zero-extend)-> 00000000 0000xxxx -(narrow)-> 0000xxxx
|
43972
|
154 |
// ==> xxxx -(zero-extend)-> 0000xxxx
|
48861
|
155 |
return new ZeroExtendNode(other.getValue(), other.getInputBits(), getResultBits(), ((ZeroExtendNode) other).isInputAlwaysPositive());
|
43972
|
156 |
}
|
|
157 |
}
|
46344
|
158 |
} else if (forValue instanceof AndNode) {
|
|
159 |
AndNode andNode = (AndNode) forValue;
|
48190
|
160 |
IntegerStamp yStamp = (IntegerStamp) andNode.getY().stamp(view);
|
|
161 |
IntegerStamp xStamp = (IntegerStamp) andNode.getX().stamp(view);
|
46344
|
162 |
long relevantMask = CodeUtil.mask(this.getResultBits());
|
|
163 |
if ((relevantMask & yStamp.downMask()) == relevantMask) {
|
48190
|
164 |
return create(andNode.getX(), this.getResultBits(), view);
|
46344
|
165 |
} else if ((relevantMask & xStamp.downMask()) == relevantMask) {
|
48190
|
166 |
return create(andNode.getY(), this.getResultBits(), view);
|
46344
|
167 |
}
|
43972
|
168 |
}
|
46344
|
169 |
|
43972
|
170 |
return this;
|
|
171 |
}
|
|
172 |
|
|
173 |
@Override
|
|
174 |
public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
|
|
175 |
nodeValueMap.setResult(this, gen.emitNarrow(nodeValueMap.operand(getValue()), getResultBits()));
|
|
176 |
}
|
46344
|
177 |
|
|
178 |
@Override
|
|
179 |
public boolean mayNullCheckSkipConversion() {
|
|
180 |
return false;
|
|
181 |
}
|
43972
|
182 |
}
|