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 */
23 package org.graalvm.compiler.nodes.calc;
24
25 import java.io.Serializable;
26 import java.util.function.Function;
27
28 import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
29 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp;
30 import org.graalvm.compiler.core.common.type.IntegerStamp;
31 import org.graalvm.compiler.core.common.type.Stamp;
32 import org.graalvm.compiler.graph.NodeClass;
33 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
34 import org.graalvm.compiler.nodeinfo.NodeInfo;
35 import org.graalvm.compiler.nodes.ArithmeticOperation;
36 import org.graalvm.compiler.nodes.ConstantNode;
37 import org.graalvm.compiler.nodes.StructuredGraph;
38 import org.graalvm.compiler.nodes.ValueNode;
39 import org.graalvm.compiler.nodes.spi.ArithmeticLIRLowerable;
40
41 import jdk.vm.ci.meta.Constant;
42 import jdk.vm.ci.meta.ConstantReflectionProvider;
43
44 /**
45 * An {@code IntegerConvert} converts an integer to an integer of different width.
46 */
47 @NodeInfo
48 public abstract class IntegerConvertNode<OP, REV> extends UnaryNode implements ArithmeticOperation, ConvertNode, ArithmeticLIRLowerable {
49 @SuppressWarnings("rawtypes") public static final NodeClass<IntegerConvertNode> TYPE = NodeClass.create(IntegerConvertNode.class);
50
51 protected final SerializableIntegerConvertFunction<OP> getOp;
52 protected final SerializableIntegerConvertFunction<REV> getReverseOp;
53
54 protected final int inputBits;
55 protected final int resultBits;
56
57 protected interface SerializableIntegerConvertFunction<T> extends Function<ArithmeticOpTable, IntegerConvertOp<T>>, Serializable {
58 }
59
60 protected IntegerConvertNode(NodeClass<? extends IntegerConvertNode<OP, REV>> c, SerializableIntegerConvertFunction<OP> getOp, SerializableIntegerConvertFunction<REV> getReverseOp, int inputBits,
61 int resultBits, ValueNode input) {
62 super(c, getOp.apply(ArithmeticOpTable.forStamp(input.stamp())).foldStamp(inputBits, resultBits, input.stamp()), input);
63 this.getOp = getOp;
64 this.getReverseOp = getReverseOp;
65 this.inputBits = inputBits;
66 this.resultBits = resultBits;
67 }
68
69 public int getInputBits() {
70 return inputBits;
71 }
72
73 public int getResultBits() {
74 return resultBits;
75 }
76
77 protected final IntegerConvertOp<OP> getOp(ValueNode forValue) {
78 return getOp.apply(ArithmeticOpTable.forStamp(forValue.stamp()));
79 }
80
81 @Override
82 public final IntegerConvertOp<OP> getArithmeticOp() {
83 return getOp(getValue());
84 }
85
86 @Override
129 convert = graph.addOrUnique(convert);
130 }
131 return convert;
132 }
133
134 public static ValueNode convertUnsigned(ValueNode input, Stamp stamp) {
135 return convert(input, stamp, true);
136 }
137
138 public static ValueNode convert(ValueNode input, Stamp stamp, boolean zeroExtend) {
139 IntegerStamp fromStamp = (IntegerStamp) input.stamp();
140 IntegerStamp toStamp = (IntegerStamp) stamp;
141
142 ValueNode result;
143 if (toStamp.getBits() == fromStamp.getBits()) {
144 result = input;
145 } else if (toStamp.getBits() < fromStamp.getBits()) {
146 result = new NarrowNode(input, fromStamp.getBits(), toStamp.getBits());
147 } else if (zeroExtend) {
148 // toStamp.getBits() > fromStamp.getBits()
149 result = new ZeroExtendNode(input, toStamp.getBits());
150 } else {
151 // toStamp.getBits() > fromStamp.getBits()
152 result = new SignExtendNode(input, toStamp.getBits());
153 }
154
155 IntegerStamp resultStamp = (IntegerStamp) result.stamp();
156 assert toStamp.getBits() == resultStamp.getBits();
157 return result;
158 }
159 }
|
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 */
23 package org.graalvm.compiler.nodes.calc;
24
25 import java.io.Serializable;
26 import java.util.function.Function;
27
28 import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
29 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp;
30 import org.graalvm.compiler.core.common.type.IntegerStamp;
31 import org.graalvm.compiler.core.common.type.PrimitiveStamp;
32 import org.graalvm.compiler.core.common.type.Stamp;
33 import org.graalvm.compiler.graph.NodeClass;
34 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
35 import org.graalvm.compiler.nodeinfo.NodeInfo;
36 import org.graalvm.compiler.nodes.ArithmeticOperation;
37 import org.graalvm.compiler.nodes.ConstantNode;
38 import org.graalvm.compiler.nodes.StructuredGraph;
39 import org.graalvm.compiler.nodes.ValueNode;
40 import org.graalvm.compiler.nodes.spi.ArithmeticLIRLowerable;
41 import org.graalvm.compiler.nodes.spi.StampInverter;
42
43 import jdk.vm.ci.meta.Constant;
44 import jdk.vm.ci.meta.ConstantReflectionProvider;
45
46 /**
47 * An {@code IntegerConvert} converts an integer to an integer of different width.
48 */
49 @NodeInfo
50 public abstract class IntegerConvertNode<OP, REV> extends UnaryNode implements ArithmeticOperation, ConvertNode, ArithmeticLIRLowerable, StampInverter {
51 @SuppressWarnings("rawtypes") public static final NodeClass<IntegerConvertNode> TYPE = NodeClass.create(IntegerConvertNode.class);
52
53 protected final SerializableIntegerConvertFunction<OP> getOp;
54 protected final SerializableIntegerConvertFunction<REV> getReverseOp;
55
56 protected final int inputBits;
57 protected final int resultBits;
58
59 protected interface SerializableIntegerConvertFunction<T> extends Function<ArithmeticOpTable, IntegerConvertOp<T>>, Serializable {
60 }
61
62 protected IntegerConvertNode(NodeClass<? extends IntegerConvertNode<OP, REV>> c, SerializableIntegerConvertFunction<OP> getOp, SerializableIntegerConvertFunction<REV> getReverseOp, int inputBits,
63 int resultBits, ValueNode input) {
64 super(c, getOp.apply(ArithmeticOpTable.forStamp(input.stamp())).foldStamp(inputBits, resultBits, input.stamp()), input);
65 this.getOp = getOp;
66 this.getReverseOp = getReverseOp;
67 this.inputBits = inputBits;
68 this.resultBits = resultBits;
69 assert ((PrimitiveStamp) input.stamp()).getBits() == inputBits;
70 }
71
72 public int getInputBits() {
73 return inputBits;
74 }
75
76 public int getResultBits() {
77 return resultBits;
78 }
79
80 protected final IntegerConvertOp<OP> getOp(ValueNode forValue) {
81 return getOp.apply(ArithmeticOpTable.forStamp(forValue.stamp()));
82 }
83
84 @Override
85 public final IntegerConvertOp<OP> getArithmeticOp() {
86 return getOp(getValue());
87 }
88
89 @Override
132 convert = graph.addOrUnique(convert);
133 }
134 return convert;
135 }
136
137 public static ValueNode convertUnsigned(ValueNode input, Stamp stamp) {
138 return convert(input, stamp, true);
139 }
140
141 public static ValueNode convert(ValueNode input, Stamp stamp, boolean zeroExtend) {
142 IntegerStamp fromStamp = (IntegerStamp) input.stamp();
143 IntegerStamp toStamp = (IntegerStamp) stamp;
144
145 ValueNode result;
146 if (toStamp.getBits() == fromStamp.getBits()) {
147 result = input;
148 } else if (toStamp.getBits() < fromStamp.getBits()) {
149 result = new NarrowNode(input, fromStamp.getBits(), toStamp.getBits());
150 } else if (zeroExtend) {
151 // toStamp.getBits() > fromStamp.getBits()
152 result = ZeroExtendNode.create(input, toStamp.getBits());
153 } else {
154 // toStamp.getBits() > fromStamp.getBits()
155 result = SignExtendNode.create(input, toStamp.getBits());
156 }
157
158 IntegerStamp resultStamp = (IntegerStamp) result.stamp();
159 assert toStamp.getBits() == resultStamp.getBits();
160 return result;
161 }
162
163 @Override
164 public Stamp invertStamp(Stamp outStamp) {
165 return getArithmeticOp().invertStamp(inputBits, resultBits, outStamp);
166 }
167 }
|