43972
|
1 |
/*
|
46344
|
2 |
* Copyright (c) 2011, 2017, 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 |
*/
|
|
23 |
package org.graalvm.compiler.nodes.calc;
|
|
24 |
|
46344
|
25 |
import static org.graalvm.compiler.core.common.calc.Condition.LT;
|
|
26 |
|
|
27 |
import org.graalvm.compiler.core.common.NumUtil;
|
43972
|
28 |
import org.graalvm.compiler.core.common.calc.Condition;
|
|
29 |
import org.graalvm.compiler.core.common.type.FloatStamp;
|
|
30 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
31 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
|
32 |
import org.graalvm.compiler.debug.GraalError;
|
|
33 |
import org.graalvm.compiler.graph.NodeClass;
|
|
34 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
|
35 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
46344
|
36 |
import org.graalvm.compiler.nodes.ConstantNode;
|
43972
|
37 |
import org.graalvm.compiler.nodes.LogicNode;
|
|
38 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
39 |
|
|
40 |
import jdk.vm.ci.code.CodeUtil;
|
|
41 |
import jdk.vm.ci.meta.Constant;
|
|
42 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
46344
|
43 |
import jdk.vm.ci.meta.JavaConstant;
|
43972
|
44 |
import jdk.vm.ci.meta.JavaKind;
|
|
45 |
import jdk.vm.ci.meta.PrimitiveConstant;
|
|
46 |
|
|
47 |
@NodeInfo(shortName = "<")
|
46344
|
48 |
public final class IntegerLessThanNode extends IntegerLowerThanNode {
|
43972
|
49 |
public static final NodeClass<IntegerLessThanNode> TYPE = NodeClass.create(IntegerLessThanNode.class);
|
46344
|
50 |
public static final LessThanOp OP = new LessThanOp();
|
43972
|
51 |
|
|
52 |
public IntegerLessThanNode(ValueNode x, ValueNode y) {
|
46344
|
53 |
super(TYPE, x, y, OP);
|
43972
|
54 |
assert !x.getStackKind().isNumericFloat() && x.getStackKind() != JavaKind.Object;
|
|
55 |
assert !y.getStackKind().isNumericFloat() && y.getStackKind() != JavaKind.Object;
|
|
56 |
}
|
|
57 |
|
|
58 |
public static LogicNode create(ValueNode x, ValueNode y, ConstantReflectionProvider constantReflection) {
|
46344
|
59 |
return OP.create(x, y, constantReflection);
|
43972
|
60 |
}
|
|
61 |
|
|
62 |
@Override
|
|
63 |
protected ValueNode optimizeNormalizeCmp(Constant constant, NormalizeCompareNode normalizeNode, boolean mirrored) {
|
|
64 |
PrimitiveConstant primitive = (PrimitiveConstant) constant;
|
46344
|
65 |
assert condition() == LT;
|
43972
|
66 |
if (primitive.getJavaKind() == JavaKind.Int && primitive.asInt() == 0) {
|
|
67 |
ValueNode a = mirrored ? normalizeNode.getY() : normalizeNode.getX();
|
|
68 |
ValueNode b = mirrored ? normalizeNode.getX() : normalizeNode.getY();
|
|
69 |
|
|
70 |
if (normalizeNode.getX().getStackKind() == JavaKind.Double || normalizeNode.getX().getStackKind() == JavaKind.Float) {
|
|
71 |
return new FloatLessThanNode(a, b, mirrored ^ normalizeNode.isUnorderedLess);
|
|
72 |
} else {
|
|
73 |
return new IntegerLessThanNode(a, b);
|
|
74 |
}
|
|
75 |
}
|
|
76 |
return this;
|
|
77 |
}
|
|
78 |
|
|
79 |
public static boolean subtractMayUnderflow(long x, long y, long minValue) {
|
|
80 |
long r = x - y;
|
|
81 |
// HD 2-12 Overflow iff the arguments have different signs and
|
|
82 |
// the sign of the result is different than the sign of x
|
|
83 |
return (((x ^ y) & (x ^ r)) < 0) || r <= minValue;
|
|
84 |
}
|
|
85 |
|
|
86 |
public static boolean subtractMayOverflow(long x, long y, long maxValue) {
|
|
87 |
long r = x - y;
|
|
88 |
// HD 2-12 Overflow iff the arguments have different signs and
|
|
89 |
// the sign of the result is different than the sign of x
|
|
90 |
return (((x ^ y) & (x ^ r)) < 0) || r > maxValue;
|
|
91 |
}
|
|
92 |
|
|
93 |
@Override
|
|
94 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
|
|
95 |
ValueNode result = super.canonical(tool, forX, forY);
|
|
96 |
if (result != this) {
|
|
97 |
return result;
|
|
98 |
}
|
|
99 |
if (forX.stamp() instanceof IntegerStamp && forY.stamp() instanceof IntegerStamp) {
|
|
100 |
if (IntegerStamp.sameSign((IntegerStamp) forX.stamp(), (IntegerStamp) forY.stamp())) {
|
|
101 |
return new IntegerBelowNode(forX, forY);
|
|
102 |
}
|
|
103 |
}
|
|
104 |
if (forY.isConstant() && forY.asConstant().isDefaultForKind() && forX instanceof SubNode) {
|
46344
|
105 |
// (x - y) < 0 when x - y is known not to underflow <=> x < y
|
43972
|
106 |
SubNode sub = (SubNode) forX;
|
|
107 |
IntegerStamp xStamp = (IntegerStamp) sub.getX().stamp();
|
|
108 |
IntegerStamp yStamp = (IntegerStamp) sub.getY().stamp();
|
|
109 |
long minValue = CodeUtil.minValue(xStamp.getBits());
|
|
110 |
long maxValue = CodeUtil.maxValue(xStamp.getBits());
|
|
111 |
|
|
112 |
if (!subtractMayUnderflow(xStamp.lowerBound(), yStamp.upperBound(), minValue) && !subtractMayOverflow(xStamp.upperBound(), yStamp.lowerBound(), maxValue)) {
|
|
113 |
return new IntegerLessThanNode(sub.getX(), sub.getY());
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
46344
|
117 |
int bits = ((IntegerStamp) getX().stamp()).getBits();
|
|
118 |
assert ((IntegerStamp) getY().stamp()).getBits() == bits;
|
|
119 |
long min = OP.minValue(bits);
|
|
120 |
long xResidue = 0;
|
|
121 |
ValueNode left = null;
|
|
122 |
JavaConstant leftCst = null;
|
|
123 |
if (forX instanceof AddNode) {
|
|
124 |
AddNode xAdd = (AddNode) forX;
|
|
125 |
if (xAdd.getY().isJavaConstant()) {
|
|
126 |
long xCst = xAdd.getY().asJavaConstant().asLong();
|
|
127 |
xResidue = xCst - min;
|
|
128 |
left = xAdd.getX();
|
|
129 |
}
|
|
130 |
} else if (forX.isJavaConstant()) {
|
|
131 |
leftCst = forX.asJavaConstant();
|
|
132 |
}
|
|
133 |
if (left != null || leftCst != null) {
|
|
134 |
long yResidue = 0;
|
|
135 |
ValueNode right = null;
|
|
136 |
JavaConstant rightCst = null;
|
|
137 |
if (forY instanceof AddNode) {
|
|
138 |
AddNode yAdd = (AddNode) forY;
|
|
139 |
if (yAdd.getY().isJavaConstant()) {
|
|
140 |
long yCst = yAdd.getY().asJavaConstant().asLong();
|
|
141 |
yResidue = yCst - min;
|
|
142 |
right = yAdd.getX();
|
|
143 |
}
|
|
144 |
} else if (forY.isJavaConstant()) {
|
|
145 |
rightCst = forY.asJavaConstant();
|
|
146 |
}
|
|
147 |
if (right != null || rightCst != null) {
|
|
148 |
if ((xResidue == 0 && left != null) || (yResidue == 0 && right != null)) {
|
|
149 |
if (left == null) {
|
|
150 |
left = ConstantNode.forIntegerBits(bits, leftCst.asLong() - min);
|
|
151 |
} else if (xResidue != 0) {
|
|
152 |
left = AddNode.create(left, ConstantNode.forIntegerBits(bits, xResidue));
|
|
153 |
}
|
|
154 |
if (right == null) {
|
|
155 |
right = ConstantNode.forIntegerBits(bits, rightCst.asLong() - min);
|
|
156 |
} else if (yResidue != 0) {
|
|
157 |
right = AddNode.create(right, ConstantNode.forIntegerBits(bits, yResidue));
|
|
158 |
}
|
|
159 |
return new IntegerBelowNode(left, right);
|
|
160 |
}
|
43972
|
161 |
}
|
|
162 |
}
|
46344
|
163 |
return this;
|
43972
|
164 |
}
|
|
165 |
|
|
166 |
@Override
|
|
167 |
protected CompareNode duplicateModified(ValueNode newX, ValueNode newY) {
|
|
168 |
if (newX.stamp() instanceof FloatStamp && newY.stamp() instanceof FloatStamp) {
|
|
169 |
return new FloatLessThanNode(newX, newY, true);
|
|
170 |
} else if (newX.stamp() instanceof IntegerStamp && newY.stamp() instanceof IntegerStamp) {
|
|
171 |
return new IntegerLessThanNode(newX, newY);
|
|
172 |
}
|
|
173 |
throw GraalError.shouldNotReachHere();
|
|
174 |
}
|
|
175 |
|
46344
|
176 |
public static class LessThanOp extends LowerOp {
|
|
177 |
|
|
178 |
@Override
|
|
179 |
protected Condition getCondition() {
|
|
180 |
return LT;
|
|
181 |
}
|
|
182 |
|
|
183 |
@Override
|
|
184 |
protected IntegerLowerThanNode create(ValueNode x, ValueNode y) {
|
|
185 |
return new IntegerLessThanNode(x, y);
|
43972
|
186 |
}
|
46344
|
187 |
|
|
188 |
@Override
|
|
189 |
protected long upperBound(IntegerStamp stamp) {
|
|
190 |
return stamp.upperBound();
|
|
191 |
}
|
|
192 |
|
|
193 |
@Override
|
|
194 |
protected long lowerBound(IntegerStamp stamp) {
|
|
195 |
return stamp.lowerBound();
|
|
196 |
}
|
|
197 |
|
|
198 |
@Override
|
|
199 |
protected int compare(long a, long b) {
|
|
200 |
return Long.compare(a, b);
|
|
201 |
}
|
43972
|
202 |
|
46344
|
203 |
@Override
|
|
204 |
protected long min(long a, long b) {
|
|
205 |
return Math.min(a, b);
|
|
206 |
}
|
|
207 |
|
|
208 |
@Override
|
|
209 |
protected long max(long a, long b) {
|
|
210 |
return Math.max(a, b);
|
|
211 |
}
|
|
212 |
|
|
213 |
@Override
|
|
214 |
protected long cast(long a, int bits) {
|
|
215 |
return CodeUtil.signExtend(a, bits);
|
43972
|
216 |
}
|
46344
|
217 |
|
|
218 |
@Override
|
|
219 |
protected long minValue(int bits) {
|
|
220 |
return NumUtil.minValue(bits);
|
|
221 |
}
|
43972
|
222 |
|
46344
|
223 |
@Override
|
|
224 |
protected long maxValue(int bits) {
|
|
225 |
return NumUtil.maxValue(bits);
|
43972
|
226 |
}
|
46344
|
227 |
|
|
228 |
@Override
|
|
229 |
protected IntegerStamp forInteger(int bits, long min, long max) {
|
|
230 |
return StampFactory.forInteger(bits, cast(min, bits), cast(max, bits));
|
|
231 |
}
|
43972
|
232 |
}
|
|
233 |
}
|