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