43972
|
1 |
/*
|
58533
|
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 |
|
48861
|
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.ZeroExtend;
|
|
35 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
36 |
import org.graalvm.compiler.core.common.type.PrimitiveStamp;
|
|
37 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
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 |
|
|
46 |
import jdk.vm.ci.code.CodeUtil;
|
|
47 |
|
|
48 |
/**
|
|
49 |
* The {@code ZeroExtendNode} converts an integer to a wider integer using zero extension.
|
|
50 |
*/
|
|
51 |
@NodeInfo(cycles = CYCLES_1)
|
|
52 |
public final class ZeroExtendNode extends IntegerConvertNode<ZeroExtend, Narrow> {
|
|
53 |
|
|
54 |
public static final NodeClass<ZeroExtendNode> TYPE = NodeClass.create(ZeroExtendNode.class);
|
|
55 |
|
48861
|
56 |
private final boolean inputAlwaysPositive;
|
|
57 |
|
43972
|
58 |
public ZeroExtendNode(ValueNode input, int resultBits) {
|
48861
|
59 |
this(input, PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT)), resultBits, false);
|
48190
|
60 |
assert 0 < PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT)) && PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT)) <= resultBits;
|
43972
|
61 |
}
|
|
62 |
|
48861
|
63 |
public ZeroExtendNode(ValueNode input, int inputBits, int resultBits, boolean inputAlwaysPositive) {
|
58533
|
64 |
super(TYPE, getArithmeticOpTable(input).getZeroExtend(), inputBits, resultBits, input);
|
48861
|
65 |
this.inputAlwaysPositive = inputAlwaysPositive;
|
43972
|
66 |
}
|
|
67 |
|
48190
|
68 |
public static ValueNode create(ValueNode input, int resultBits, NodeView view) {
|
48861
|
69 |
return create(input, PrimitiveStamp.getBits(input.stamp(view)), resultBits, view, false);
|
43972
|
70 |
}
|
|
71 |
|
48190
|
72 |
public static ValueNode create(ValueNode input, int inputBits, int resultBits, NodeView view) {
|
48861
|
73 |
return create(input, inputBits, resultBits, view, false);
|
|
74 |
}
|
|
75 |
|
|
76 |
public static ValueNode create(ValueNode input, int inputBits, int resultBits, NodeView view, boolean alwaysPositive) {
|
48190
|
77 |
IntegerConvertOp<ZeroExtend> signExtend = ArithmeticOpTable.forStamp(input.stamp(view)).getZeroExtend();
|
|
78 |
ValueNode synonym = findSynonym(signExtend, input, inputBits, resultBits, signExtend.foldStamp(inputBits, resultBits, input.stamp(view)));
|
43972
|
79 |
if (synonym != null) {
|
|
80 |
return synonym;
|
|
81 |
}
|
48861
|
82 |
return canonical(null, input, inputBits, resultBits, view, alwaysPositive);
|
43972
|
83 |
}
|
|
84 |
|
|
85 |
@Override
|
58533
|
86 |
protected IntegerConvertOp<ZeroExtend> getOp(ArithmeticOpTable table) {
|
|
87 |
return table.getZeroExtend();
|
|
88 |
}
|
|
89 |
|
|
90 |
@Override
|
|
91 |
protected IntegerConvertOp<Narrow> getReverseOp(ArithmeticOpTable table) {
|
|
92 |
return table.getNarrow();
|
|
93 |
}
|
|
94 |
|
|
95 |
@Override
|
43972
|
96 |
public boolean isLossless() {
|
|
97 |
return true;
|
|
98 |
}
|
|
99 |
|
48861
|
100 |
public boolean isInputAlwaysPositive() {
|
|
101 |
return inputAlwaysPositive;
|
|
102 |
}
|
|
103 |
|
43972
|
104 |
@Override
|
48861
|
105 |
public boolean preservesOrder(CanonicalCondition cond) {
|
43972
|
106 |
switch (cond) {
|
|
107 |
case LT:
|
|
108 |
return false;
|
|
109 |
default:
|
|
110 |
return true;
|
|
111 |
}
|
|
112 |
}
|
|
113 |
|
|
114 |
@Override
|
|
115 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
|
48190
|
116 |
NodeView view = NodeView.from(tool);
|
43972
|
117 |
ValueNode ret = super.canonical(tool, forValue);
|
|
118 |
if (ret != this) {
|
|
119 |
return ret;
|
|
120 |
}
|
|
121 |
|
48861
|
122 |
return canonical(this, forValue, getInputBits(), getResultBits(), view, inputAlwaysPositive);
|
46371
|
123 |
}
|
|
124 |
|
48861
|
125 |
private static ValueNode canonical(ZeroExtendNode zeroExtendNode, ValueNode forValue, int inputBits, int resultBits, NodeView view, boolean alwaysPositive) {
|
46371
|
126 |
ZeroExtendNode self = zeroExtendNode;
|
43972
|
127 |
if (forValue instanceof ZeroExtendNode) {
|
|
128 |
// xxxx -(zero-extend)-> 0000 xxxx -(zero-extend)-> 00000000 0000xxxx
|
|
129 |
// ==> xxxx -(zero-extend)-> 00000000 0000xxxx
|
|
130 |
ZeroExtendNode other = (ZeroExtendNode) forValue;
|
48861
|
131 |
return new ZeroExtendNode(other.getValue(), other.getInputBits(), resultBits, other.isInputAlwaysPositive());
|
43972
|
132 |
}
|
|
133 |
if (forValue instanceof NarrowNode) {
|
|
134 |
NarrowNode narrow = (NarrowNode) forValue;
|
48190
|
135 |
Stamp inputStamp = narrow.getValue().stamp(view);
|
46344
|
136 |
if (inputStamp instanceof IntegerStamp) {
|
43972
|
137 |
IntegerStamp istamp = (IntegerStamp) inputStamp;
|
48190
|
138 |
long mask = CodeUtil.mask(PrimitiveStamp.getBits(narrow.stamp(view)));
|
46344
|
139 |
|
|
140 |
if ((istamp.upMask() & ~mask) == 0) {
|
|
141 |
// The original value cannot change because of the narrow and zero extend.
|
|
142 |
|
|
143 |
if (istamp.getBits() < resultBits) {
|
|
144 |
// Need to keep the zero extend, skip the narrow.
|
48190
|
145 |
return create(narrow.getValue(), resultBits, view);
|
46344
|
146 |
} else if (istamp.getBits() > resultBits) {
|
|
147 |
// Need to keep the narrow, skip the zero extend.
|
48190
|
148 |
return NarrowNode.create(narrow.getValue(), resultBits, view);
|
46344
|
149 |
} else {
|
|
150 |
assert istamp.getBits() == resultBits;
|
|
151 |
// Just return the original value.
|
|
152 |
return narrow.getValue();
|
|
153 |
}
|
43972
|
154 |
}
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
46371
|
158 |
if (self == null) {
|
48861
|
159 |
self = new ZeroExtendNode(forValue, inputBits, resultBits, alwaysPositive);
|
46371
|
160 |
}
|
|
161 |
return self;
|
43972
|
162 |
}
|
|
163 |
|
|
164 |
@Override
|
|
165 |
public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
|
|
166 |
nodeValueMap.setResult(this, gen.emitZeroExtend(nodeValueMap.operand(getValue()), getInputBits(), getResultBits()));
|
|
167 |
}
|
46344
|
168 |
|
|
169 |
@Override
|
|
170 |
public boolean mayNullCheckSkipConversion() {
|
|
171 |
return true;
|
|
172 |
}
|
43972
|
173 |
}
|