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 |
|
48861
|
25 |
import static org.graalvm.compiler.core.common.calc.CanonicalCondition.LT;
|
46344
|
26 |
|
|
27 |
import org.graalvm.compiler.core.common.NumUtil;
|
48861
|
28 |
import org.graalvm.compiler.core.common.calc.CanonicalCondition;
|
43972
|
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;
|
46393
|
33 |
import org.graalvm.compiler.graph.Node;
|
43972
|
34 |
import org.graalvm.compiler.graph.NodeClass;
|
46393
|
35 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
43972
|
36 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
46344
|
37 |
import org.graalvm.compiler.nodes.ConstantNode;
|
46393
|
38 |
import org.graalvm.compiler.nodes.LogicConstantNode;
|
46371
|
39 |
import org.graalvm.compiler.nodes.LogicNegationNode;
|
43972
|
40 |
import org.graalvm.compiler.nodes.LogicNode;
|
48190
|
41 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
42 |
import org.graalvm.compiler.nodes.ValueNode;
|
48861
|
43 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
44 |
|
|
45 |
import jdk.vm.ci.code.CodeUtil;
|
|
46 |
import jdk.vm.ci.meta.Constant;
|
48861
|
47 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
46344
|
48 |
import jdk.vm.ci.meta.JavaConstant;
|
43972
|
49 |
import jdk.vm.ci.meta.JavaKind;
|
48861
|
50 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
43972
|
51 |
import jdk.vm.ci.meta.PrimitiveConstant;
|
|
52 |
|
|
53 |
@NodeInfo(shortName = "<")
|
46344
|
54 |
public final class IntegerLessThanNode extends IntegerLowerThanNode {
|
43972
|
55 |
public static final NodeClass<IntegerLessThanNode> TYPE = NodeClass.create(IntegerLessThanNode.class);
|
47798
|
56 |
private static final LessThanOp OP = new LessThanOp();
|
43972
|
57 |
|
|
58 |
public IntegerLessThanNode(ValueNode x, ValueNode y) {
|
46344
|
59 |
super(TYPE, x, y, OP);
|
43972
|
60 |
assert !x.getStackKind().isNumericFloat() && x.getStackKind() != JavaKind.Object;
|
|
61 |
assert !y.getStackKind().isNumericFloat() && y.getStackKind() != JavaKind.Object;
|
|
62 |
}
|
|
63 |
|
48190
|
64 |
public static LogicNode create(ValueNode x, ValueNode y, NodeView view) {
|
|
65 |
return OP.create(x, y, view);
|
43972
|
66 |
}
|
|
67 |
|
46393
|
68 |
public static LogicNode create(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth,
|
48190
|
69 |
ValueNode x, ValueNode y, NodeView view) {
|
|
70 |
LogicNode value = OP.canonical(constantReflection, metaAccess, options, smallestCompareWidth, OP.getCondition(), false, x, y, view);
|
46393
|
71 |
if (value != null) {
|
|
72 |
return value;
|
|
73 |
}
|
48190
|
74 |
return create(x, y, view);
|
46393
|
75 |
}
|
|
76 |
|
43972
|
77 |
@Override
|
46393
|
78 |
public Node canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
|
48190
|
79 |
NodeView view = NodeView.from(tool);
|
|
80 |
ValueNode value = OP.canonical(tool.getConstantReflection(), tool.getMetaAccess(), tool.getOptions(), tool.smallestCompareWidth(), OP.getCondition(), false, forX, forY, view);
|
46393
|
81 |
if (value != null) {
|
|
82 |
return value;
|
43972
|
83 |
}
|
|
84 |
return this;
|
|
85 |
}
|
|
86 |
|
|
87 |
public static boolean subtractMayUnderflow(long x, long y, long minValue) {
|
|
88 |
long r = x - y;
|
|
89 |
// HD 2-12 Overflow iff the arguments have different signs and
|
|
90 |
// the sign of the result is different than the sign of x
|
|
91 |
return (((x ^ y) & (x ^ r)) < 0) || r <= minValue;
|
|
92 |
}
|
|
93 |
|
|
94 |
public static boolean subtractMayOverflow(long x, long y, long maxValue) {
|
|
95 |
long r = x - y;
|
|
96 |
// HD 2-12 Overflow iff the arguments have different signs and
|
|
97 |
// the sign of the result is different than the sign of x
|
|
98 |
return (((x ^ y) & (x ^ r)) < 0) || r > maxValue;
|
|
99 |
}
|
|
100 |
|
46393
|
101 |
public static class LessThanOp extends LowerOp {
|
|
102 |
@Override
|
48190
|
103 |
protected CompareNode duplicateModified(ValueNode newX, ValueNode newY, boolean unorderedIsTrue, NodeView view) {
|
|
104 |
if (newX.stamp(view) instanceof FloatStamp && newY.stamp(view) instanceof FloatStamp) {
|
46393
|
105 |
return new FloatLessThanNode(newX, newY, unorderedIsTrue); // TODO: Is the last arg
|
|
106 |
// supposed to be true?
|
48190
|
107 |
} else if (newX.stamp(view) instanceof IntegerStamp && newY.stamp(view) instanceof IntegerStamp) {
|
46393
|
108 |
return new IntegerLessThanNode(newX, newY);
|
|
109 |
}
|
|
110 |
throw GraalError.shouldNotReachHere();
|
43972
|
111 |
}
|
|
112 |
|
46393
|
113 |
@Override
|
|
114 |
protected LogicNode optimizeNormalizeCompare(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth,
|
48190
|
115 |
Constant constant, NormalizeCompareNode normalizeNode, boolean mirrored, NodeView view) {
|
46393
|
116 |
PrimitiveConstant primitive = (PrimitiveConstant) constant;
|
|
117 |
/* @formatter:off
|
|
118 |
* a NC b < c (not mirrored)
|
|
119 |
* cases for c:
|
|
120 |
* 0 -> a < b
|
|
121 |
* [MIN, -1] -> false
|
|
122 |
* 1 -> a <= b
|
|
123 |
* [2, MAX] -> true
|
|
124 |
* unordered-is-less means unordered-is-true.
|
|
125 |
*
|
|
126 |
* c < a NC b (mirrored)
|
|
127 |
* cases for c:
|
|
128 |
* 0 -> a > b
|
|
129 |
* [1, MAX] -> false
|
|
130 |
* -1 -> a >= b
|
|
131 |
* [MIN, -2] -> true
|
|
132 |
* unordered-is-less means unordered-is-false.
|
|
133 |
*
|
|
134 |
* We can handle mirroring by swapping a & b and negating the constant.
|
|
135 |
* @formatter:on
|
|
136 |
*/
|
|
137 |
ValueNode a = mirrored ? normalizeNode.getY() : normalizeNode.getX();
|
|
138 |
ValueNode b = mirrored ? normalizeNode.getX() : normalizeNode.getY();
|
|
139 |
long cst = mirrored ? -primitive.asLong() : primitive.asLong();
|
|
140 |
|
|
141 |
if (cst == 0) {
|
|
142 |
if (normalizeNode.getX().getStackKind() == JavaKind.Double || normalizeNode.getX().getStackKind() == JavaKind.Float) {
|
48190
|
143 |
return FloatLessThanNode.create(constantReflection, metaAccess, options, smallestCompareWidth, a, b, mirrored ^ normalizeNode.isUnorderedLess, view);
|
46393
|
144 |
} else {
|
48190
|
145 |
return IntegerLessThanNode.create(constantReflection, metaAccess, options, smallestCompareWidth, a, b, view);
|
46393
|
146 |
}
|
|
147 |
} else if (cst == 1) {
|
|
148 |
// a <= b <=> !(a > b)
|
|
149 |
LogicNode compare;
|
|
150 |
if (normalizeNode.getX().getStackKind() == JavaKind.Double || normalizeNode.getX().getStackKind() == JavaKind.Float) {
|
|
151 |
// since we negate, we have to reverse the unordered result
|
48190
|
152 |
compare = FloatLessThanNode.create(constantReflection, metaAccess, options, smallestCompareWidth, b, a, mirrored == normalizeNode.isUnorderedLess, view);
|
46393
|
153 |
} else {
|
48190
|
154 |
compare = IntegerLessThanNode.create(constantReflection, metaAccess, options, smallestCompareWidth, b, a, view);
|
46393
|
155 |
}
|
|
156 |
return LogicNegationNode.create(compare);
|
|
157 |
} else if (cst <= -1) {
|
|
158 |
return LogicConstantNode.contradiction();
|
|
159 |
} else {
|
|
160 |
assert cst >= 2;
|
|
161 |
return LogicConstantNode.tautology();
|
|
162 |
}
|
|
163 |
}
|
46344
|
164 |
|
|
165 |
@Override
|
48190
|
166 |
protected LogicNode findSynonym(ValueNode forX, ValueNode forY, NodeView view) {
|
|
167 |
LogicNode result = super.findSynonym(forX, forY, view);
|
46371
|
168 |
if (result != null) {
|
|
169 |
return result;
|
|
170 |
}
|
48190
|
171 |
if (forX.stamp(view) instanceof IntegerStamp && forY.stamp(view) instanceof IntegerStamp) {
|
|
172 |
if (IntegerStamp.sameSign((IntegerStamp) forX.stamp(view), (IntegerStamp) forY.stamp(view))) {
|
46371
|
173 |
return new IntegerBelowNode(forX, forY);
|
|
174 |
}
|
|
175 |
}
|
|
176 |
if (forY.isConstant() && forX instanceof SubNode) {
|
|
177 |
SubNode sub = (SubNode) forX;
|
|
178 |
ValueNode xx = null;
|
|
179 |
ValueNode yy = null;
|
|
180 |
boolean negate = false;
|
|
181 |
if (forY.asConstant().isDefaultForKind()) {
|
|
182 |
// (x - y) < 0 when x - y is known not to underflow <=> x < y
|
|
183 |
xx = sub.getX();
|
|
184 |
yy = sub.getY();
|
|
185 |
} else if (forY.isJavaConstant() && forY.asJavaConstant().asLong() == 1) {
|
|
186 |
// (x - y) < 1 when x - y is known not to underflow <=> !(y < x)
|
|
187 |
xx = sub.getY();
|
|
188 |
yy = sub.getX();
|
|
189 |
negate = true;
|
|
190 |
}
|
|
191 |
if (xx != null) {
|
|
192 |
assert yy != null;
|
48190
|
193 |
IntegerStamp xStamp = (IntegerStamp) sub.getX().stamp(view);
|
|
194 |
IntegerStamp yStamp = (IntegerStamp) sub.getY().stamp(view);
|
46371
|
195 |
long minValue = CodeUtil.minValue(xStamp.getBits());
|
|
196 |
long maxValue = CodeUtil.maxValue(xStamp.getBits());
|
|
197 |
|
|
198 |
if (!subtractMayUnderflow(xStamp.lowerBound(), yStamp.upperBound(), minValue) && !subtractMayOverflow(xStamp.upperBound(), yStamp.lowerBound(), maxValue)) {
|
|
199 |
LogicNode logic = new IntegerLessThanNode(xx, yy);
|
|
200 |
if (negate) {
|
|
201 |
logic = LogicNegationNode.create(logic);
|
|
202 |
}
|
|
203 |
return logic;
|
|
204 |
}
|
|
205 |
}
|
|
206 |
}
|
|
207 |
|
48190
|
208 |
if (forX.stamp(view) instanceof IntegerStamp) {
|
|
209 |
assert forY.stamp(view) instanceof IntegerStamp;
|
|
210 |
int bits = ((IntegerStamp) forX.stamp(view)).getBits();
|
|
211 |
assert ((IntegerStamp) forY.stamp(view)).getBits() == bits;
|
47798
|
212 |
long min = OP.minValue(bits);
|
|
213 |
long xResidue = 0;
|
|
214 |
ValueNode left = null;
|
|
215 |
JavaConstant leftCst = null;
|
|
216 |
if (forX instanceof AddNode) {
|
|
217 |
AddNode xAdd = (AddNode) forX;
|
|
218 |
if (xAdd.getY().isJavaConstant()) {
|
|
219 |
long xCst = xAdd.getY().asJavaConstant().asLong();
|
|
220 |
xResidue = xCst - min;
|
|
221 |
left = xAdd.getX();
|
|
222 |
}
|
|
223 |
} else if (forX.isJavaConstant()) {
|
|
224 |
leftCst = forX.asJavaConstant();
|
46371
|
225 |
}
|
47798
|
226 |
if (left != null || leftCst != null) {
|
|
227 |
long yResidue = 0;
|
|
228 |
ValueNode right = null;
|
|
229 |
JavaConstant rightCst = null;
|
|
230 |
if (forY instanceof AddNode) {
|
|
231 |
AddNode yAdd = (AddNode) forY;
|
|
232 |
if (yAdd.getY().isJavaConstant()) {
|
|
233 |
long yCst = yAdd.getY().asJavaConstant().asLong();
|
|
234 |
yResidue = yCst - min;
|
|
235 |
right = yAdd.getX();
|
|
236 |
}
|
|
237 |
} else if (forY.isJavaConstant()) {
|
|
238 |
rightCst = forY.asJavaConstant();
|
46371
|
239 |
}
|
47798
|
240 |
if (right != null || rightCst != null) {
|
|
241 |
if ((xResidue == 0 && left != null) || (yResidue == 0 && right != null)) {
|
|
242 |
if (left == null) {
|
|
243 |
left = ConstantNode.forIntegerBits(bits, leftCst.asLong() - min);
|
|
244 |
} else if (xResidue != 0) {
|
48190
|
245 |
left = AddNode.create(left, ConstantNode.forIntegerBits(bits, xResidue), view);
|
47798
|
246 |
}
|
|
247 |
if (right == null) {
|
|
248 |
right = ConstantNode.forIntegerBits(bits, rightCst.asLong() - min);
|
|
249 |
} else if (yResidue != 0) {
|
48190
|
250 |
right = AddNode.create(right, ConstantNode.forIntegerBits(bits, yResidue), view);
|
47798
|
251 |
}
|
|
252 |
return new IntegerBelowNode(left, right);
|
46371
|
253 |
}
|
|
254 |
}
|
|
255 |
}
|
|
256 |
}
|
|
257 |
return null;
|
|
258 |
}
|
|
259 |
|
|
260 |
@Override
|
48861
|
261 |
protected CanonicalCondition getCondition() {
|
46344
|
262 |
return LT;
|
|
263 |
}
|
|
264 |
|
|
265 |
@Override
|
46371
|
266 |
protected IntegerLowerThanNode createNode(ValueNode x, ValueNode y) {
|
46344
|
267 |
return new IntegerLessThanNode(x, y);
|
43972
|
268 |
}
|
46344
|
269 |
|
|
270 |
@Override
|
|
271 |
protected long upperBound(IntegerStamp stamp) {
|
|
272 |
return stamp.upperBound();
|
|
273 |
}
|
|
274 |
|
|
275 |
@Override
|
|
276 |
protected long lowerBound(IntegerStamp stamp) {
|
|
277 |
return stamp.lowerBound();
|
|
278 |
}
|
|
279 |
|
|
280 |
@Override
|
|
281 |
protected int compare(long a, long b) {
|
|
282 |
return Long.compare(a, b);
|
|
283 |
}
|
43972
|
284 |
|
46344
|
285 |
@Override
|
|
286 |
protected long min(long a, long b) {
|
|
287 |
return Math.min(a, b);
|
|
288 |
}
|
|
289 |
|
|
290 |
@Override
|
|
291 |
protected long max(long a, long b) {
|
|
292 |
return Math.max(a, b);
|
|
293 |
}
|
|
294 |
|
|
295 |
@Override
|
|
296 |
protected long cast(long a, int bits) {
|
|
297 |
return CodeUtil.signExtend(a, bits);
|
43972
|
298 |
}
|
46344
|
299 |
|
|
300 |
@Override
|
|
301 |
protected long minValue(int bits) {
|
|
302 |
return NumUtil.minValue(bits);
|
|
303 |
}
|
43972
|
304 |
|
46344
|
305 |
@Override
|
|
306 |
protected long maxValue(int bits) {
|
|
307 |
return NumUtil.maxValue(bits);
|
43972
|
308 |
}
|
46344
|
309 |
|
|
310 |
@Override
|
|
311 |
protected IntegerStamp forInteger(int bits, long min, long max) {
|
|
312 |
return StampFactory.forInteger(bits, cast(min, bits), cast(max, bits));
|
|
313 |
}
|
43972
|
314 |
}
|
|
315 |
}
|