46344
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, 2017, 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 |
|
48861
|
25 |
import org.graalvm.compiler.core.common.calc.CanonicalCondition;
|
46344
|
26 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
27 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
28 |
import org.graalvm.compiler.graph.NodeClass;
|
|
29 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
46371
|
30 |
import org.graalvm.compiler.nodes.ConstantNode;
|
46344
|
31 |
import org.graalvm.compiler.nodes.LogicConstantNode;
|
|
32 |
import org.graalvm.compiler.nodes.LogicNegationNode;
|
|
33 |
import org.graalvm.compiler.nodes.LogicNode;
|
48190
|
34 |
import org.graalvm.compiler.nodes.NodeView;
|
46344
|
35 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
36 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
48861
|
37 |
import org.graalvm.compiler.options.OptionValues;
|
46344
|
38 |
|
48861
|
39 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
|
40 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
46344
|
41 |
import jdk.vm.ci.meta.TriState;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Common super-class for "a < b" comparisons both {@linkplain IntegerLowerThanNode signed} and
|
|
45 |
* {@linkplain IntegerBelowNode unsigned}.
|
|
46 |
*/
|
|
47 |
@NodeInfo()
|
|
48 |
public abstract class IntegerLowerThanNode extends CompareNode {
|
|
49 |
public static final NodeClass<IntegerLowerThanNode> TYPE = NodeClass.create(IntegerLowerThanNode.class);
|
|
50 |
private final LowerOp op;
|
|
51 |
|
|
52 |
protected IntegerLowerThanNode(NodeClass<? extends CompareNode> c, ValueNode x, ValueNode y, LowerOp op) {
|
|
53 |
super(c, op.getCondition(), false, x, y);
|
|
54 |
this.op = op;
|
|
55 |
}
|
|
56 |
|
|
57 |
protected LowerOp getOp() {
|
|
58 |
return op;
|
|
59 |
}
|
|
60 |
|
|
61 |
@Override
|
|
62 |
public Stamp getSucceedingStampForX(boolean negated, Stamp xStampGeneric, Stamp yStampGeneric) {
|
|
63 |
return getSucceedingStampForX(negated, !negated, xStampGeneric, yStampGeneric, getX(), getY());
|
|
64 |
}
|
|
65 |
|
|
66 |
@Override
|
|
67 |
public Stamp getSucceedingStampForY(boolean negated, Stamp xStampGeneric, Stamp yStampGeneric) {
|
|
68 |
return getSucceedingStampForX(!negated, !negated, yStampGeneric, xStampGeneric, getY(), getX());
|
|
69 |
}
|
|
70 |
|
|
71 |
private Stamp getSucceedingStampForX(boolean mirror, boolean strict, Stamp xStampGeneric, Stamp yStampGeneric, ValueNode forX, ValueNode forY) {
|
|
72 |
Stamp s = getSucceedingStampForX(mirror, strict, xStampGeneric, yStampGeneric);
|
|
73 |
if (s != null) {
|
|
74 |
return s;
|
|
75 |
}
|
|
76 |
if (forY instanceof AddNode && xStampGeneric instanceof IntegerStamp) {
|
|
77 |
IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
|
|
78 |
AddNode addNode = (AddNode) forY;
|
|
79 |
IntegerStamp aStamp = null;
|
48190
|
80 |
if (addNode.getX() == forX && addNode.getY().stamp(NodeView.DEFAULT) instanceof IntegerStamp) {
|
46344
|
81 |
// x < x + a
|
48190
|
82 |
aStamp = (IntegerStamp) addNode.getY().stamp(NodeView.DEFAULT);
|
|
83 |
} else if (addNode.getY() == forX && addNode.getX().stamp(NodeView.DEFAULT) instanceof IntegerStamp) {
|
46344
|
84 |
// x < a + x
|
48190
|
85 |
aStamp = (IntegerStamp) addNode.getX().stamp(NodeView.DEFAULT);
|
46344
|
86 |
}
|
|
87 |
if (aStamp != null) {
|
|
88 |
IntegerStamp result = getOp().getSucceedingStampForXLowerXPlusA(mirror, strict, aStamp);
|
|
89 |
result = (IntegerStamp) xStamp.tryImproveWith(result);
|
|
90 |
if (result != null) {
|
|
91 |
return result;
|
|
92 |
}
|
|
93 |
}
|
|
94 |
}
|
|
95 |
return null;
|
|
96 |
}
|
|
97 |
|
|
98 |
private Stamp getSucceedingStampForX(boolean mirror, boolean strict, Stamp xStampGeneric, Stamp yStampGeneric) {
|
|
99 |
if (xStampGeneric instanceof IntegerStamp) {
|
|
100 |
IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
|
|
101 |
if (yStampGeneric instanceof IntegerStamp) {
|
|
102 |
IntegerStamp yStamp = (IntegerStamp) yStampGeneric;
|
|
103 |
assert yStamp.getBits() == xStamp.getBits();
|
|
104 |
Stamp s = getOp().getSucceedingStampForX(xStamp, yStamp, mirror, strict);
|
|
105 |
if (s != null) {
|
|
106 |
return s;
|
|
107 |
}
|
|
108 |
}
|
|
109 |
}
|
|
110 |
return null;
|
|
111 |
}
|
|
112 |
|
|
113 |
@Override
|
|
114 |
public TriState tryFold(Stamp xStampGeneric, Stamp yStampGeneric) {
|
|
115 |
return getOp().tryFold(xStampGeneric, yStampGeneric);
|
|
116 |
}
|
|
117 |
|
46393
|
118 |
public abstract static class LowerOp extends CompareOp {
|
|
119 |
@Override
|
48861
|
120 |
public LogicNode canonical(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth, CanonicalCondition condition,
|
48190
|
121 |
boolean unorderedIsTrue, ValueNode forX, ValueNode forY, NodeView view) {
|
|
122 |
LogicNode result = super.canonical(constantReflection, metaAccess, options, smallestCompareWidth, condition, unorderedIsTrue, forX, forY, view);
|
46393
|
123 |
if (result != null) {
|
|
124 |
return result;
|
|
125 |
}
|
48190
|
126 |
LogicNode synonym = findSynonym(forX, forY, view);
|
46393
|
127 |
if (synonym != null) {
|
|
128 |
return synonym;
|
|
129 |
}
|
|
130 |
return null;
|
|
131 |
}
|
|
132 |
|
46344
|
133 |
protected abstract long upperBound(IntegerStamp stamp);
|
|
134 |
|
|
135 |
protected abstract long lowerBound(IntegerStamp stamp);
|
|
136 |
|
|
137 |
protected abstract int compare(long a, long b);
|
|
138 |
|
|
139 |
protected abstract long min(long a, long b);
|
|
140 |
|
|
141 |
protected abstract long max(long a, long b);
|
|
142 |
|
|
143 |
protected long min(long a, long b, int bits) {
|
|
144 |
return min(cast(a, bits), cast(b, bits));
|
|
145 |
}
|
|
146 |
|
|
147 |
protected long max(long a, long b, int bits) {
|
|
148 |
return max(cast(a, bits), cast(b, bits));
|
|
149 |
}
|
|
150 |
|
|
151 |
protected abstract long cast(long a, int bits);
|
|
152 |
|
|
153 |
protected abstract long minValue(int bits);
|
|
154 |
|
|
155 |
protected abstract long maxValue(int bits);
|
|
156 |
|
|
157 |
protected abstract IntegerStamp forInteger(int bits, long min, long max);
|
|
158 |
|
48861
|
159 |
protected abstract CanonicalCondition getCondition();
|
46344
|
160 |
|
46371
|
161 |
protected abstract IntegerLowerThanNode createNode(ValueNode x, ValueNode y);
|
46344
|
162 |
|
48190
|
163 |
public LogicNode create(ValueNode x, ValueNode y, NodeView view) {
|
|
164 |
LogicNode result = CompareNode.tryConstantFoldPrimitive(getCondition(), x, y, false, view);
|
46344
|
165 |
if (result != null) {
|
|
166 |
return result;
|
|
167 |
} else {
|
48190
|
168 |
result = findSynonym(x, y, view);
|
46344
|
169 |
if (result != null) {
|
|
170 |
return result;
|
|
171 |
}
|
46371
|
172 |
return createNode(x, y);
|
46344
|
173 |
}
|
|
174 |
}
|
|
175 |
|
48190
|
176 |
protected LogicNode findSynonym(ValueNode forX, ValueNode forY, NodeView view) {
|
46344
|
177 |
if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) {
|
|
178 |
return LogicConstantNode.contradiction();
|
|
179 |
}
|
48190
|
180 |
TriState fold = tryFold(forX.stamp(view), forY.stamp(view));
|
46344
|
181 |
if (fold.isTrue()) {
|
|
182 |
return LogicConstantNode.tautology();
|
|
183 |
} else if (fold.isFalse()) {
|
|
184 |
return LogicConstantNode.contradiction();
|
|
185 |
}
|
48190
|
186 |
if (forY.stamp(view) instanceof IntegerStamp) {
|
|
187 |
IntegerStamp yStamp = (IntegerStamp) forY.stamp(view);
|
46371
|
188 |
int bits = yStamp.getBits();
|
|
189 |
if (forX.isJavaConstant() && !forY.isConstant()) {
|
|
190 |
// bring the constant on the right
|
|
191 |
long xValue = forX.asJavaConstant().asLong();
|
|
192 |
if (xValue != maxValue(bits)) {
|
|
193 |
// c < x <=> !(c >= x) <=> !(x <= c) <=> !(x < c + 1)
|
48190
|
194 |
return LogicNegationNode.create(create(forY, ConstantNode.forIntegerStamp(yStamp, xValue + 1), view));
|
46371
|
195 |
}
|
|
196 |
}
|
|
197 |
if (forY.isJavaConstant()) {
|
|
198 |
long yValue = forY.asJavaConstant().asLong();
|
|
199 |
if (yValue == maxValue(bits)) {
|
|
200 |
// x < MAX <=> x != MAX
|
48190
|
201 |
return LogicNegationNode.create(IntegerEqualsNode.create(forX, forY, view));
|
46371
|
202 |
}
|
|
203 |
if (yValue == minValue(bits) + 1) {
|
|
204 |
// x < MIN + 1 <=> x <= MIN <=> x == MIN
|
48190
|
205 |
return IntegerEqualsNode.create(forX, ConstantNode.forIntegerStamp(yStamp, minValue(bits)), view);
|
46371
|
206 |
}
|
|
207 |
} else if (forY instanceof AddNode) {
|
|
208 |
AddNode addNode = (AddNode) forY;
|
48190
|
209 |
LogicNode canonical = canonicalizeXLowerXPlusA(forX, addNode, false, true, view);
|
46371
|
210 |
if (canonical != null) {
|
|
211 |
return canonical;
|
|
212 |
}
|
|
213 |
}
|
|
214 |
if (forX instanceof AddNode) {
|
|
215 |
AddNode addNode = (AddNode) forX;
|
48190
|
216 |
LogicNode canonical = canonicalizeXLowerXPlusA(forY, addNode, true, false, view);
|
46371
|
217 |
if (canonical != null) {
|
|
218 |
return canonical;
|
|
219 |
}
|
|
220 |
}
|
|
221 |
}
|
|
222 |
return null;
|
|
223 |
}
|
|
224 |
|
48190
|
225 |
private LogicNode canonicalizeXLowerXPlusA(ValueNode forX, AddNode addNode, boolean mirrored, boolean strict, NodeView view) {
|
46371
|
226 |
// x < x + a
|
|
227 |
IntegerStamp succeedingXStamp;
|
|
228 |
boolean exact;
|
48190
|
229 |
if (addNode.getX() == forX && addNode.getY().stamp(view) instanceof IntegerStamp) {
|
|
230 |
IntegerStamp aStamp = (IntegerStamp) addNode.getY().stamp(view);
|
46371
|
231 |
succeedingXStamp = getSucceedingStampForXLowerXPlusA(mirrored, strict, aStamp);
|
|
232 |
exact = aStamp.lowerBound() == aStamp.upperBound();
|
48190
|
233 |
} else if (addNode.getY() == forX && addNode.getX().stamp(view) instanceof IntegerStamp) {
|
|
234 |
IntegerStamp aStamp = (IntegerStamp) addNode.getX().stamp(view);
|
46371
|
235 |
succeedingXStamp = getSucceedingStampForXLowerXPlusA(mirrored, strict, aStamp);
|
|
236 |
exact = aStamp.lowerBound() == aStamp.upperBound();
|
|
237 |
} else {
|
|
238 |
return null;
|
|
239 |
}
|
48190
|
240 |
if (succeedingXStamp.join(forX.stamp(view)).isEmpty()) {
|
46371
|
241 |
return LogicConstantNode.contradiction();
|
|
242 |
} else if (exact && !succeedingXStamp.isEmpty()) {
|
|
243 |
int bits = succeedingXStamp.getBits();
|
|
244 |
if (compare(lowerBound(succeedingXStamp), minValue(bits)) > 0) {
|
|
245 |
assert upperBound(succeedingXStamp) == maxValue(bits);
|
|
246 |
// x must be in [L..MAX] <=> x >= L <=> !(x < L)
|
48190
|
247 |
return LogicNegationNode.create(create(forX, ConstantNode.forIntegerStamp(succeedingXStamp, lowerBound(succeedingXStamp)), view));
|
46371
|
248 |
} else if (compare(upperBound(succeedingXStamp), maxValue(bits)) < 0) {
|
|
249 |
// x must be in [MIN..H] <=> x <= H <=> !(H < x)
|
48190
|
250 |
return LogicNegationNode.create(create(ConstantNode.forIntegerStamp(succeedingXStamp, upperBound(succeedingXStamp)), forX, view));
|
46371
|
251 |
}
|
|
252 |
}
|
46344
|
253 |
return null;
|
|
254 |
}
|
|
255 |
|
|
256 |
protected TriState tryFold(Stamp xStampGeneric, Stamp yStampGeneric) {
|
|
257 |
if (xStampGeneric instanceof IntegerStamp && yStampGeneric instanceof IntegerStamp) {
|
|
258 |
IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
|
|
259 |
IntegerStamp yStamp = (IntegerStamp) yStampGeneric;
|
|
260 |
if (compare(upperBound(xStamp), lowerBound(yStamp)) < 0) {
|
|
261 |
return TriState.TRUE;
|
|
262 |
}
|
|
263 |
if (compare(lowerBound(xStamp), upperBound(yStamp)) >= 0) {
|
|
264 |
return TriState.FALSE;
|
|
265 |
}
|
|
266 |
}
|
|
267 |
return TriState.UNKNOWN;
|
|
268 |
}
|
|
269 |
|
|
270 |
protected IntegerStamp getSucceedingStampForX(IntegerStamp xStamp, IntegerStamp yStamp, boolean mirror, boolean strict) {
|
|
271 |
int bits = xStamp.getBits();
|
|
272 |
assert yStamp.getBits() == bits;
|
|
273 |
if (mirror) {
|
|
274 |
long low = lowerBound(yStamp);
|
|
275 |
if (strict) {
|
|
276 |
if (low == maxValue(bits)) {
|
|
277 |
return null;
|
|
278 |
}
|
|
279 |
low += 1;
|
|
280 |
}
|
|
281 |
if (compare(low, lowerBound(xStamp)) > 0) {
|
|
282 |
return forInteger(bits, low, upperBound(xStamp));
|
|
283 |
}
|
|
284 |
} else {
|
|
285 |
// x < y, i.e., x < y <= Y_UPPER_BOUND so x <= Y_UPPER_BOUND - 1
|
|
286 |
long low = upperBound(yStamp);
|
|
287 |
if (strict) {
|
|
288 |
if (low == minValue(bits)) {
|
|
289 |
return null;
|
|
290 |
}
|
|
291 |
low -= 1;
|
|
292 |
}
|
|
293 |
if (compare(low, upperBound(xStamp)) < 0) {
|
|
294 |
return forInteger(bits, lowerBound(xStamp), low);
|
|
295 |
}
|
|
296 |
}
|
|
297 |
return null;
|
|
298 |
}
|
|
299 |
|
46371
|
300 |
protected IntegerStamp getSucceedingStampForXLowerXPlusA(boolean mirrored, boolean strict, IntegerStamp a) {
|
46344
|
301 |
int bits = a.getBits();
|
|
302 |
long min = minValue(bits);
|
|
303 |
long max = maxValue(bits);
|
|
304 |
/*
|
|
305 |
* if x < x + a <=> x + a didn't overflow:
|
|
306 |
*
|
|
307 |
* x is outside ]MAX - a, MAX], i.e., inside [MIN, MAX - a]
|
|
308 |
*
|
|
309 |
* if a is negative those bounds wrap around correctly.
|
|
310 |
*
|
|
311 |
* If a is exactly zero this gives an unbounded stamp (any integer) in the positive case
|
|
312 |
* and an empty stamp in the negative case: if x |<| x is true, then either x has no
|
|
313 |
* value or any value...
|
|
314 |
*
|
|
315 |
* This does not use upper/lowerBound from LowerOp because it's about the (signed)
|
|
316 |
* addition not the comparison.
|
|
317 |
*/
|
46371
|
318 |
if (mirrored) {
|
46344
|
319 |
if (a.contains(0)) {
|
|
320 |
// a may be zero
|
|
321 |
return a.unrestricted();
|
|
322 |
}
|
|
323 |
return forInteger(bits, min(max - a.lowerBound() + 1, max - a.upperBound() + 1, bits), max);
|
|
324 |
} else {
|
|
325 |
long aLower = a.lowerBound();
|
|
326 |
long aUpper = a.upperBound();
|
|
327 |
if (strict) {
|
|
328 |
if (aLower == 0) {
|
|
329 |
aLower = 1;
|
|
330 |
}
|
|
331 |
if (aUpper == 0) {
|
|
332 |
aUpper = -1;
|
|
333 |
}
|
|
334 |
if (aLower > aUpper) {
|
|
335 |
// impossible
|
|
336 |
return a.empty();
|
|
337 |
}
|
|
338 |
}
|
|
339 |
if (aLower < 0 && aUpper > 0) {
|
|
340 |
// a may be zero
|
|
341 |
return a.unrestricted();
|
|
342 |
}
|
|
343 |
return forInteger(bits, min, max(max - aLower, max - aUpper, bits));
|
|
344 |
}
|
|
345 |
}
|
|
346 |
}
|
|
347 |
}
|