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