46344
|
1 |
/*
|
|
2 |
* Copyright (c) 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 |
*/
|
50858
|
23 |
|
|
24 |
|
46344
|
25 |
package org.graalvm.compiler.replacements.test;
|
|
26 |
|
|
27 |
import static org.junit.Assert.assertNotNull;
|
|
28 |
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.Collection;
|
|
31 |
import java.util.List;
|
|
32 |
|
|
33 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
34 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
|
35 |
import org.graalvm.compiler.core.test.GraalCompilerTest;
|
|
36 |
import org.graalvm.compiler.graph.Node;
|
48190
|
37 |
import org.graalvm.compiler.nodes.NodeView;
|
46344
|
38 |
import org.graalvm.compiler.nodes.ParameterNode;
|
|
39 |
import org.graalvm.compiler.nodes.PiNode;
|
|
40 |
import org.graalvm.compiler.nodes.ReturnNode;
|
|
41 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
42 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
|
43 |
import org.graalvm.compiler.nodes.StructuredGraph.GuardsStage;
|
|
44 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
45 |
import org.graalvm.compiler.nodes.spi.LoweringTool;
|
|
46 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
47 |
import org.graalvm.compiler.phases.common.LoweringPhase;
|
|
48 |
import org.graalvm.compiler.phases.tiers.HighTierContext;
|
|
49 |
import org.graalvm.compiler.phases.tiers.PhaseContext;
|
|
50 |
import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerExactArithmeticNode;
|
|
51 |
import org.graalvm.compiler.replacements.nodes.arithmetic.IntegerExactArithmeticSplitNode;
|
|
52 |
import org.junit.Assert;
|
|
53 |
import org.junit.Test;
|
|
54 |
import org.junit.runner.RunWith;
|
|
55 |
import org.junit.runners.Parameterized;
|
|
56 |
import org.junit.runners.Parameterized.Parameters;
|
|
57 |
|
|
58 |
@RunWith(Parameterized.class)
|
|
59 |
public class IntegerExactFoldTest extends GraalCompilerTest {
|
|
60 |
private final long lowerBoundA;
|
|
61 |
private final long upperBoundA;
|
|
62 |
private final long lowerBoundB;
|
|
63 |
private final long upperBoundB;
|
|
64 |
private final int bits;
|
|
65 |
private final Operation operation;
|
|
66 |
|
|
67 |
public IntegerExactFoldTest(long lowerBoundA, long upperBoundA, long lowerBoundB, long upperBoundB, int bits, Operation operation) {
|
|
68 |
this.lowerBoundA = lowerBoundA;
|
|
69 |
this.upperBoundA = upperBoundA;
|
|
70 |
this.lowerBoundB = lowerBoundB;
|
|
71 |
this.upperBoundB = upperBoundB;
|
|
72 |
this.bits = bits;
|
|
73 |
this.operation = operation;
|
|
74 |
|
|
75 |
assert bits == 32 || bits == 64;
|
|
76 |
assert lowerBoundA <= upperBoundA;
|
|
77 |
assert lowerBoundB <= upperBoundB;
|
|
78 |
assert bits == 64 || isInteger(lowerBoundA);
|
|
79 |
assert bits == 64 || isInteger(upperBoundA);
|
|
80 |
assert bits == 64 || isInteger(lowerBoundB);
|
|
81 |
assert bits == 64 || isInteger(upperBoundB);
|
|
82 |
}
|
|
83 |
|
|
84 |
@Test
|
|
85 |
public void testFolding() {
|
|
86 |
StructuredGraph graph = prepareGraph();
|
|
87 |
IntegerStamp a = StampFactory.forInteger(bits, lowerBoundA, upperBoundA);
|
|
88 |
IntegerStamp b = StampFactory.forInteger(bits, lowerBoundB, upperBoundB);
|
|
89 |
|
|
90 |
List<ParameterNode> params = graph.getNodes(ParameterNode.TYPE).snapshot();
|
|
91 |
params.get(0).replaceAtMatchingUsages(graph.addOrUnique(new PiNode(params.get(0), a)), x -> x instanceof IntegerExactArithmeticNode);
|
|
92 |
params.get(1).replaceAtMatchingUsages(graph.addOrUnique(new PiNode(params.get(1), b)), x -> x instanceof IntegerExactArithmeticNode);
|
|
93 |
|
|
94 |
Node originalNode = graph.getNodes().filter(x -> x instanceof IntegerExactArithmeticNode).first();
|
|
95 |
assertNotNull("original node must be in the graph", originalNode);
|
|
96 |
|
|
97 |
new CanonicalizerPhase().apply(graph, getDefaultHighTierContext());
|
|
98 |
ValueNode node = findNode(graph);
|
|
99 |
boolean overflowExpected = node instanceof IntegerExactArithmeticNode;
|
|
100 |
|
48190
|
101 |
IntegerStamp resultStamp = (IntegerStamp) node.stamp(NodeView.DEFAULT);
|
46344
|
102 |
operation.verifyOverflow(lowerBoundA, upperBoundA, lowerBoundB, upperBoundB, bits, overflowExpected, resultStamp);
|
|
103 |
}
|
|
104 |
|
|
105 |
@Test
|
|
106 |
public void testFoldingAfterLowering() {
|
|
107 |
StructuredGraph graph = prepareGraph();
|
|
108 |
|
|
109 |
Node originalNode = graph.getNodes().filter(x -> x instanceof IntegerExactArithmeticNode).first();
|
|
110 |
assertNotNull("original node must be in the graph", originalNode);
|
|
111 |
|
|
112 |
graph.setGuardsStage(GuardsStage.FIXED_DEOPTS);
|
|
113 |
CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
|
|
114 |
PhaseContext context = new PhaseContext(getProviders());
|
|
115 |
new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
|
|
116 |
IntegerExactArithmeticSplitNode loweredNode = graph.getNodes().filter(IntegerExactArithmeticSplitNode.class).first();
|
|
117 |
assertNotNull("the lowered node must be in the graph", loweredNode);
|
|
118 |
|
|
119 |
loweredNode.getX().setStamp(StampFactory.forInteger(bits, lowerBoundA, upperBoundA));
|
|
120 |
loweredNode.getY().setStamp(StampFactory.forInteger(bits, lowerBoundB, upperBoundB));
|
|
121 |
new CanonicalizerPhase().apply(graph, context);
|
|
122 |
|
|
123 |
ValueNode node = findNode(graph);
|
|
124 |
boolean overflowExpected = node instanceof IntegerExactArithmeticSplitNode;
|
|
125 |
|
48190
|
126 |
IntegerStamp resultStamp = (IntegerStamp) node.stamp(NodeView.DEFAULT);
|
46344
|
127 |
operation.verifyOverflow(lowerBoundA, upperBoundA, lowerBoundB, upperBoundB, bits, overflowExpected, resultStamp);
|
|
128 |
}
|
|
129 |
|
|
130 |
private static boolean isInteger(long value) {
|
|
131 |
return value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE;
|
|
132 |
}
|
|
133 |
|
|
134 |
private static ValueNode findNode(StructuredGraph graph) {
|
|
135 |
ValueNode resultNode = graph.getNodes().filter(ReturnNode.class).first().result();
|
|
136 |
assertNotNull("some node must be the returned value", resultNode);
|
|
137 |
return resultNode;
|
|
138 |
}
|
|
139 |
|
|
140 |
protected StructuredGraph prepareGraph() {
|
|
141 |
String snippet = "snippetInt" + bits;
|
|
142 |
StructuredGraph graph = parseEager(getResolvedJavaMethod(operation.getClass(), snippet), AllowAssumptions.NO);
|
|
143 |
HighTierContext context = getDefaultHighTierContext();
|
|
144 |
new CanonicalizerPhase().apply(graph, context);
|
|
145 |
return graph;
|
|
146 |
}
|
|
147 |
|
|
148 |
private static void addTest(ArrayList<Object[]> tests, long lowerBound1, long upperBound1, long lowerBound2, long upperBound2, int bits, Operation operation) {
|
|
149 |
tests.add(new Object[]{lowerBound1, upperBound1, lowerBound2, upperBound2, bits, operation});
|
|
150 |
}
|
|
151 |
|
|
152 |
@Parameters(name = "a[{0} / {1}], b[{2} / {3}], bits={4}, operation={5}")
|
|
153 |
public static Collection<Object[]> data() {
|
|
154 |
ArrayList<Object[]> tests = new ArrayList<>();
|
|
155 |
|
|
156 |
Operation[] operations = new Operation[]{new AddOperation(), new SubOperation(), new MulOperation()};
|
|
157 |
for (Operation operation : operations) {
|
|
158 |
for (int bits : new int[]{32, 64}) {
|
|
159 |
// zero related
|
|
160 |
addTest(tests, 0, 0, 1, 1, bits, operation);
|
|
161 |
addTest(tests, 1, 1, 0, 0, bits, operation);
|
|
162 |
addTest(tests, -1, 1, 0, 1, bits, operation);
|
|
163 |
|
|
164 |
// bounds
|
|
165 |
addTest(tests, -2, 2, 3, 3, bits, operation);
|
|
166 |
addTest(tests, -1, 1, 1, 1, bits, operation);
|
|
167 |
addTest(tests, -1, 1, -1, 1, bits, operation);
|
|
168 |
|
|
169 |
addTest(tests, Integer.MIN_VALUE, Integer.MIN_VALUE + 0xF, Integer.MAX_VALUE - 0xF, Integer.MAX_VALUE, bits, operation);
|
|
170 |
addTest(tests, Integer.MIN_VALUE, Integer.MIN_VALUE + 0xF, -1, -1, bits, operation);
|
|
171 |
addTest(tests, Integer.MAX_VALUE, Integer.MAX_VALUE, -1, -1, bits, operation);
|
|
172 |
addTest(tests, Integer.MIN_VALUE, Integer.MIN_VALUE, -1, -1, bits, operation);
|
|
173 |
addTest(tests, Integer.MAX_VALUE, Integer.MAX_VALUE, 1, 1, bits, operation);
|
|
174 |
addTest(tests, Integer.MIN_VALUE, Integer.MIN_VALUE, 1, 1, bits, operation);
|
|
175 |
}
|
|
176 |
|
|
177 |
// bit-specific test cases
|
|
178 |
addTest(tests, Integer.MIN_VALUE, Integer.MIN_VALUE + 0xF, Integer.MAX_VALUE - 0xF, Integer.MAX_VALUE, 64, operation);
|
|
179 |
addTest(tests, Integer.MIN_VALUE, Integer.MIN_VALUE + 0xF, -1, -1, 64, operation);
|
|
180 |
}
|
|
181 |
|
|
182 |
return tests;
|
|
183 |
}
|
|
184 |
|
|
185 |
private abstract static class Operation {
|
|
186 |
abstract void verifyOverflow(long lowerBoundA, long upperBoundA, long lowerBoundB, long upperBoundB, int bits, boolean overflowExpected, IntegerStamp resultStamp);
|
|
187 |
}
|
|
188 |
|
|
189 |
private static final class AddOperation extends Operation {
|
|
190 |
@Override
|
|
191 |
public void verifyOverflow(long lowerBoundA, long upperBoundA, long lowerBoundB, long upperBoundB, int bits, boolean overflowExpected, IntegerStamp resultStamp) {
|
|
192 |
try {
|
|
193 |
long res = addExact(lowerBoundA, lowerBoundB, bits);
|
|
194 |
resultStamp.contains(res);
|
|
195 |
res = addExact(upperBoundA, upperBoundB, bits);
|
|
196 |
resultStamp.contains(res);
|
|
197 |
Assert.assertFalse(overflowExpected);
|
|
198 |
} catch (ArithmeticException e) {
|
|
199 |
Assert.assertTrue(overflowExpected);
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
private static long addExact(long x, long y, int bits) {
|
|
204 |
if (bits == 32) {
|
|
205 |
return Math.addExact((int) x, (int) y);
|
|
206 |
} else {
|
|
207 |
return Math.addExact(x, y);
|
|
208 |
}
|
|
209 |
}
|
|
210 |
|
|
211 |
@SuppressWarnings("unused")
|
|
212 |
public static int snippetInt32(int a, int b) {
|
|
213 |
return Math.addExact(a, b);
|
|
214 |
}
|
|
215 |
|
|
216 |
@SuppressWarnings("unused")
|
|
217 |
public static long snippetInt64(long a, long b) {
|
|
218 |
return Math.addExact(a, b);
|
|
219 |
}
|
|
220 |
}
|
|
221 |
|
|
222 |
private static final class SubOperation extends Operation {
|
|
223 |
@Override
|
|
224 |
public void verifyOverflow(long lowerBoundA, long upperBoundA, long lowerBoundB, long upperBoundB, int bits, boolean overflowExpected, IntegerStamp resultStamp) {
|
|
225 |
try {
|
|
226 |
long res = subExact(lowerBoundA, upperBoundB, bits);
|
|
227 |
Assert.assertTrue(resultStamp.contains(res));
|
|
228 |
res = subExact(upperBoundA, lowerBoundB, bits);
|
|
229 |
Assert.assertTrue(resultStamp.contains(res));
|
|
230 |
Assert.assertFalse(overflowExpected);
|
|
231 |
} catch (ArithmeticException e) {
|
|
232 |
Assert.assertTrue(overflowExpected);
|
|
233 |
}
|
|
234 |
}
|
|
235 |
|
|
236 |
private static long subExact(long x, long y, int bits) {
|
|
237 |
if (bits == 32) {
|
|
238 |
return Math.subtractExact((int) x, (int) y);
|
|
239 |
} else {
|
|
240 |
return Math.subtractExact(x, y);
|
|
241 |
}
|
|
242 |
}
|
|
243 |
|
|
244 |
@SuppressWarnings("unused")
|
|
245 |
public static int snippetInt32(int a, int b) {
|
|
246 |
return Math.subtractExact(a, b);
|
|
247 |
}
|
|
248 |
|
|
249 |
@SuppressWarnings("unused")
|
|
250 |
public static long snippetInt64(long a, long b) {
|
|
251 |
return Math.subtractExact(a, b);
|
|
252 |
}
|
|
253 |
}
|
|
254 |
|
|
255 |
private static final class MulOperation extends Operation {
|
|
256 |
@Override
|
|
257 |
public void verifyOverflow(long lowerBoundA, long upperBoundA, long lowerBoundB, long upperBoundB, int bits, boolean overflowExpected, IntegerStamp resultStamp) {
|
|
258 |
// now check for all values in the stamp whether their products overflow overflow
|
|
259 |
boolean overflowOccurred = false;
|
|
260 |
|
|
261 |
for (long l1 = lowerBoundA; l1 <= upperBoundA; l1++) {
|
|
262 |
for (long l2 = lowerBoundB; l2 <= upperBoundB; l2++) {
|
|
263 |
try {
|
|
264 |
long res = mulExact(l1, l2, bits);
|
|
265 |
Assert.assertTrue(resultStamp.contains(res));
|
|
266 |
} catch (ArithmeticException e) {
|
|
267 |
overflowOccurred = true;
|
|
268 |
}
|
|
269 |
if (l2 == Long.MAX_VALUE) {
|
|
270 |
// do not want to overflow the check loop
|
|
271 |
break;
|
|
272 |
}
|
|
273 |
}
|
|
274 |
if (l1 == Long.MAX_VALUE) {
|
|
275 |
// do not want to overflow the check loop
|
|
276 |
break;
|
|
277 |
}
|
|
278 |
}
|
|
279 |
|
|
280 |
Assert.assertEquals(overflowExpected, overflowOccurred);
|
|
281 |
}
|
|
282 |
|
|
283 |
private static long mulExact(long x, long y, int bits) {
|
|
284 |
if (bits == 32) {
|
|
285 |
return Math.multiplyExact((int) x, (int) y);
|
|
286 |
} else {
|
|
287 |
return Math.multiplyExact(x, y);
|
|
288 |
}
|
|
289 |
}
|
|
290 |
|
|
291 |
@SuppressWarnings("unused")
|
|
292 |
public static int snippetInt32(int a, int b) {
|
|
293 |
return Math.multiplyExact(a, b);
|
|
294 |
}
|
|
295 |
|
|
296 |
@SuppressWarnings("unused")
|
|
297 |
public static long snippetInt64(long a, long b) {
|
|
298 |
return Math.multiplyExact(a, b);
|
|
299 |
}
|
|
300 |
}
|
|
301 |
}
|