43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
|
|
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 |
*/
|
|
23 |
package org.graalvm.compiler.nodes.calc;
|
|
24 |
|
|
25 |
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
|
|
26 |
|
|
27 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
|
|
28 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp;
|
|
29 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp.Narrow;
|
|
30 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp.SignExtend;
|
|
31 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
32 |
import org.graalvm.compiler.core.common.type.PrimitiveStamp;
|
|
33 |
import org.graalvm.compiler.graph.NodeClass;
|
|
34 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
|
35 |
import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
|
|
36 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
48190
|
37 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
38 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
39 |
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* The {@code SignExtendNode} converts an integer to a wider integer using sign extension.
|
|
43 |
*/
|
|
44 |
@NodeInfo(cycles = CYCLES_1)
|
|
45 |
public final class SignExtendNode extends IntegerConvertNode<SignExtend, Narrow> {
|
|
46 |
|
|
47 |
public static final NodeClass<SignExtendNode> TYPE = NodeClass.create(SignExtendNode.class);
|
|
48 |
|
|
49 |
public SignExtendNode(ValueNode input, int resultBits) {
|
48190
|
50 |
this(input, PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT)), resultBits);
|
|
51 |
assert 0 < PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT)) && PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT)) <= resultBits;
|
43972
|
52 |
}
|
|
53 |
|
|
54 |
public SignExtendNode(ValueNode input, int inputBits, int resultBits) {
|
|
55 |
super(TYPE, ArithmeticOpTable::getSignExtend, ArithmeticOpTable::getNarrow, inputBits, resultBits, input);
|
|
56 |
}
|
|
57 |
|
48190
|
58 |
public static ValueNode create(ValueNode input, int resultBits, NodeView view) {
|
|
59 |
return create(input, PrimitiveStamp.getBits(input.stamp(view)), resultBits, view);
|
43972
|
60 |
}
|
|
61 |
|
48190
|
62 |
public static ValueNode create(ValueNode input, int inputBits, int resultBits, NodeView view) {
|
|
63 |
IntegerConvertOp<SignExtend> signExtend = ArithmeticOpTable.forStamp(input.stamp(view)).getSignExtend();
|
|
64 |
ValueNode synonym = findSynonym(signExtend, input, inputBits, resultBits, signExtend.foldStamp(inputBits, resultBits, input.stamp(view)));
|
43972
|
65 |
if (synonym != null) {
|
|
66 |
return synonym;
|
|
67 |
}
|
48190
|
68 |
return canonical(null, input, inputBits, resultBits, view);
|
43972
|
69 |
}
|
|
70 |
|
|
71 |
@Override
|
|
72 |
public boolean isLossless() {
|
|
73 |
return true;
|
|
74 |
}
|
|
75 |
|
|
76 |
@Override
|
|
77 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
|
48190
|
78 |
NodeView view = NodeView.from(tool);
|
43972
|
79 |
ValueNode ret = super.canonical(tool, forValue);
|
|
80 |
if (ret != this) {
|
|
81 |
return ret;
|
|
82 |
}
|
|
83 |
|
48190
|
84 |
return canonical(this, forValue, getInputBits(), getResultBits(), view);
|
46371
|
85 |
}
|
|
86 |
|
48190
|
87 |
private static ValueNode canonical(SignExtendNode self, ValueNode forValue, int inputBits, int resultBits, NodeView view) {
|
43972
|
88 |
if (forValue instanceof SignExtendNode) {
|
|
89 |
// sxxx -(sign-extend)-> ssss sxxx -(sign-extend)-> ssssssss sssssxxx
|
|
90 |
// ==> sxxx -(sign-extend)-> ssssssss sssssxxx
|
|
91 |
SignExtendNode other = (SignExtendNode) forValue;
|
48190
|
92 |
return SignExtendNode.create(other.getValue(), other.getInputBits(), resultBits, view);
|
43972
|
93 |
} else if (forValue instanceof ZeroExtendNode) {
|
|
94 |
ZeroExtendNode other = (ZeroExtendNode) forValue;
|
|
95 |
if (other.getResultBits() > other.getInputBits()) {
|
|
96 |
// sxxx -(zero-extend)-> 0000 sxxx -(sign-extend)-> 00000000 0000sxxx
|
|
97 |
// ==> sxxx -(zero-extend)-> 00000000 0000sxxx
|
48861
|
98 |
return ZeroExtendNode.create(other.getValue(), other.getInputBits(), resultBits, view, other.isInputAlwaysPositive());
|
43972
|
99 |
}
|
|
100 |
}
|
|
101 |
|
48190
|
102 |
if (forValue.stamp(view) instanceof IntegerStamp) {
|
|
103 |
IntegerStamp inputStamp = (IntegerStamp) forValue.stamp(view);
|
46371
|
104 |
if ((inputStamp.upMask() & (1L << (inputBits - 1))) == 0L) {
|
43972
|
105 |
// 0xxx -(sign-extend)-> 0000 0xxx
|
|
106 |
// ==> 0xxx -(zero-extend)-> 0000 0xxx
|
48861
|
107 |
return ZeroExtendNode.create(forValue, inputBits, resultBits, view, true);
|
43972
|
108 |
}
|
|
109 |
}
|
|
110 |
|
46371
|
111 |
return self != null ? self : new SignExtendNode(forValue, inputBits, resultBits);
|
43972
|
112 |
}
|
|
113 |
|
|
114 |
@Override
|
|
115 |
public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
|
|
116 |
nodeValueMap.setResult(this, gen.emitSignExtend(nodeValueMap.operand(getValue()), getInputBits(), getResultBits()));
|
|
117 |
}
|
46344
|
118 |
|
|
119 |
@Override
|
|
120 |
public boolean mayNullCheckSkipConversion() {
|
|
121 |
return true;
|
|
122 |
}
|
43972
|
123 |
}
|