43972
|
1 |
/*
|
58877
|
2 |
* Copyright (c) 2011, 2019, 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.core.test.ea;
|
|
26 |
|
|
27 |
import java.lang.ref.SoftReference;
|
|
28 |
|
|
29 |
import org.junit.Assert;
|
|
30 |
import org.junit.Ignore;
|
|
31 |
import org.junit.Test;
|
|
32 |
|
|
33 |
import org.graalvm.compiler.api.directives.GraalDirectives;
|
|
34 |
import org.graalvm.compiler.core.test.TypeSystemTest;
|
|
35 |
import org.graalvm.compiler.graph.Node;
|
|
36 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
|
37 |
import org.graalvm.compiler.nodes.ReturnNode;
|
|
38 |
import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
|
|
39 |
import org.graalvm.compiler.nodes.extended.BoxNode;
|
|
40 |
import org.graalvm.compiler.nodes.extended.UnboxNode;
|
|
41 |
import org.graalvm.compiler.nodes.java.LoadFieldNode;
|
|
42 |
import org.graalvm.compiler.nodes.java.LoadIndexedNode;
|
|
43 |
import org.graalvm.compiler.nodes.java.NewArrayNode;
|
|
44 |
import org.graalvm.compiler.nodes.java.NewInstanceNode;
|
|
45 |
import org.graalvm.compiler.nodes.java.StoreFieldNode;
|
|
46 |
import org.graalvm.compiler.nodes.virtual.CommitAllocationNode;
|
|
47 |
import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* The PartialEscapeAnalysisPhase is expected to remove all allocations and return the correct
|
|
51 |
* values.
|
|
52 |
*/
|
|
53 |
public class PartialEscapeAnalysisTest extends EATestBase {
|
|
54 |
|
|
55 |
public static class TestObject {
|
|
56 |
|
|
57 |
public int x;
|
|
58 |
public int y;
|
|
59 |
|
|
60 |
public TestObject(int x, int y) {
|
|
61 |
this.x = x;
|
|
62 |
this.y = y;
|
|
63 |
}
|
|
64 |
}
|
|
65 |
|
|
66 |
public static class TestObject2 {
|
|
67 |
|
|
68 |
public Object x;
|
|
69 |
public Object y;
|
|
70 |
|
|
71 |
public TestObject2(Object x, Object y) {
|
|
72 |
this.x = x;
|
|
73 |
this.y = y;
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
@Test
|
|
78 |
public void test1() {
|
|
79 |
testPartialEscapeAnalysis("test1Snippet", 0.25, 1);
|
|
80 |
}
|
|
81 |
|
|
82 |
@SuppressWarnings("all")
|
|
83 |
public static Object test1Snippet(int a, int b, Object x, Object y) {
|
|
84 |
TestObject2 obj = new TestObject2(x, y);
|
|
85 |
if (a < 0) {
|
|
86 |
if (b < 0) {
|
|
87 |
return obj;
|
|
88 |
} else {
|
|
89 |
return obj.y;
|
|
90 |
}
|
|
91 |
} else {
|
|
92 |
return obj.x;
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
@Test
|
|
97 |
public void test2() {
|
|
98 |
testPartialEscapeAnalysis("test2Snippet", 1.5, 3, LoadIndexedNode.class);
|
|
99 |
}
|
|
100 |
|
|
101 |
public static Object test2Snippet(int a, Object x, Object y, Object z) {
|
|
102 |
TestObject2 obj = new TestObject2(x, y);
|
|
103 |
obj.x = new TestObject2(obj, z);
|
|
104 |
if (a < 0) {
|
|
105 |
((TestObject2) obj.x).y = null;
|
|
106 |
obj.y = null;
|
|
107 |
return obj;
|
|
108 |
} else {
|
|
109 |
((TestObject2) obj.x).y = Integer.class;
|
|
110 |
((TestObject2) obj.x).x = null;
|
|
111 |
return obj.x;
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
@Test
|
|
116 |
public void test3() {
|
|
117 |
testPartialEscapeAnalysis("test3Snippet", 0.5, 1, StoreFieldNode.class, LoadFieldNode.class);
|
|
118 |
}
|
|
119 |
|
51436
|
120 |
@SuppressWarnings("deprecation")
|
43972
|
121 |
public static Object test3Snippet(int a) {
|
|
122 |
if (a < 0) {
|
|
123 |
TestObject obj = new TestObject(1, 2);
|
|
124 |
obj.x = 123;
|
|
125 |
obj.y = 234;
|
|
126 |
obj.x = 123111;
|
|
127 |
obj.y = new Integer(123).intValue();
|
|
128 |
return obj;
|
|
129 |
} else {
|
|
130 |
return null;
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
@Test
|
|
135 |
public void testArrayCopy() {
|
|
136 |
testPartialEscapeAnalysis("testArrayCopySnippet", 0, 0);
|
|
137 |
}
|
|
138 |
|
|
139 |
public static Object[] array = new Object[]{1, 2, 3, 4, 5, "asdf", "asdf"};
|
46371
|
140 |
public static char[] charArray = new char[]{1, 2, 3, 4, 5, 'a', 'f'};
|
43972
|
141 |
|
|
142 |
public static Object testArrayCopySnippet(int a) {
|
|
143 |
Object[] tmp = new Object[]{a != 1 ? array[a] : null};
|
|
144 |
Object[] tmp2 = new Object[5];
|
|
145 |
System.arraycopy(tmp, 0, tmp2, 4, 1);
|
|
146 |
return tmp2[4];
|
|
147 |
}
|
|
148 |
|
|
149 |
@Test
|
46371
|
150 |
public void testPrimitiveArraycopy() {
|
|
151 |
testPartialEscapeAnalysis("testPrimitiveArraycopySnippet", 0, 0);
|
|
152 |
}
|
|
153 |
|
|
154 |
public static Object testPrimitiveArraycopySnippet(int a) {
|
|
155 |
char[] tmp = new char[]{a != 1 ? charArray[a] : 0};
|
|
156 |
char[] tmp2 = new char[5];
|
|
157 |
System.arraycopy(tmp, 0, tmp2, 4, 1);
|
|
158 |
return tmp2[4];
|
|
159 |
}
|
|
160 |
|
|
161 |
@Test
|
43972
|
162 |
@Ignore
|
|
163 |
public void testCache() {
|
|
164 |
testPartialEscapeAnalysis("testCacheSnippet", 0.75, 1);
|
|
165 |
}
|
|
166 |
|
|
167 |
public static class CacheKey {
|
|
168 |
|
|
169 |
private final int idx;
|
|
170 |
private final Object ref;
|
|
171 |
|
|
172 |
public CacheKey(int idx, Object ref) {
|
|
173 |
this.idx = idx;
|
|
174 |
this.ref = ref;
|
|
175 |
}
|
|
176 |
|
|
177 |
@Override
|
|
178 |
public int hashCode() {
|
|
179 |
return 31 * idx + ref.hashCode();
|
|
180 |
}
|
|
181 |
|
|
182 |
public synchronized boolean equals(CacheKey other) {
|
|
183 |
return idx == other.idx && ref == other.ref;
|
|
184 |
}
|
|
185 |
}
|
|
186 |
|
|
187 |
public static CacheKey cacheKey = null;
|
|
188 |
public static Object value = null;
|
|
189 |
|
|
190 |
private static native Object createValue(CacheKey key);
|
|
191 |
|
|
192 |
public static Object testCacheSnippet(int idx, Object ref) {
|
|
193 |
CacheKey key = new CacheKey(idx, ref);
|
|
194 |
if (!key.equals(cacheKey)) {
|
|
195 |
cacheKey = key;
|
|
196 |
value = createValue(key);
|
|
197 |
}
|
|
198 |
return value;
|
|
199 |
}
|
|
200 |
|
|
201 |
public static int testReference1Snippet(Object a) {
|
|
202 |
SoftReference<Object> softReference = new SoftReference<>(a);
|
|
203 |
if (softReference.get().hashCode() == 0) {
|
|
204 |
return 1;
|
|
205 |
} else {
|
|
206 |
return 2;
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
@Test
|
|
211 |
public void testReference1() {
|
|
212 |
prepareGraph("testReference1Snippet", false);
|
|
213 |
assertDeepEquals(1, graph.getNodes().filter(NewInstanceNode.class).count());
|
|
214 |
}
|
|
215 |
|
|
216 |
public static int testCanonicalizeSnippet(int v) {
|
|
217 |
CacheKey key = new CacheKey(v, null);
|
|
218 |
|
|
219 |
CacheKey key2;
|
|
220 |
if (key.idx == v) {
|
|
221 |
key2 = new CacheKey(v, null);
|
|
222 |
} else {
|
|
223 |
key2 = null;
|
|
224 |
}
|
|
225 |
return key2.idx;
|
|
226 |
}
|
|
227 |
|
|
228 |
@Test
|
|
229 |
public void testCanonicalize() {
|
|
230 |
prepareGraph("testCanonicalizeSnippet", false);
|
|
231 |
assertTrue(graph.getNodes().filter(ReturnNode.class).count() == 1);
|
|
232 |
assertTrue(graph.getNodes().filter(ReturnNode.class).first().result() == graph.getParameter(0));
|
|
233 |
}
|
|
234 |
|
|
235 |
public static int testBoxLoopSnippet(int n) {
|
|
236 |
Integer sum = 0;
|
|
237 |
for (Integer i = 0; i < n; i++) {
|
|
238 |
if (sum == null) {
|
|
239 |
sum = null;
|
|
240 |
} else {
|
|
241 |
sum += i;
|
|
242 |
}
|
|
243 |
}
|
|
244 |
return sum;
|
|
245 |
}
|
|
246 |
|
|
247 |
@Test
|
|
248 |
public void testBoxLoop() {
|
|
249 |
testPartialEscapeAnalysis("testBoxLoopSnippet", 0, 0, BoxNode.class, UnboxNode.class);
|
|
250 |
}
|
|
251 |
|
|
252 |
static volatile int staticField;
|
|
253 |
static boolean executedDeoptimizeDirective;
|
|
254 |
|
|
255 |
static class A {
|
|
256 |
String field;
|
|
257 |
}
|
|
258 |
|
|
259 |
public static Object deoptWithVirtualObjectsSnippet() {
|
|
260 |
A a = new A();
|
|
261 |
a.field = "field";
|
|
262 |
|
|
263 |
staticField = 5;
|
|
264 |
if (staticField == 5) {
|
|
265 |
GraalDirectives.deoptimize();
|
|
266 |
executedDeoptimizeDirective = true;
|
|
267 |
}
|
|
268 |
|
|
269 |
return a.field;
|
|
270 |
}
|
|
271 |
|
|
272 |
/**
|
|
273 |
* Tests deoptimizing with virtual objects in debug info.
|
|
274 |
*/
|
|
275 |
@Test
|
|
276 |
public void testDeoptWithVirtualObjects() {
|
|
277 |
assertFalse(executedDeoptimizeDirective);
|
|
278 |
test("deoptWithVirtualObjectsSnippet");
|
|
279 |
assertTrue(executedDeoptimizeDirective);
|
|
280 |
}
|
|
281 |
|
|
282 |
@SafeVarargs
|
|
283 |
protected final void testPartialEscapeAnalysis(String snippet, double expectedProbability, int expectedCount, Class<? extends Node>... invalidNodeClasses) {
|
|
284 |
prepareGraph(snippet, false);
|
|
285 |
for (AbstractMergeNode merge : graph.getNodes(AbstractMergeNode.TYPE)) {
|
|
286 |
merge.setStateAfter(null);
|
|
287 |
}
|
|
288 |
new DeadCodeEliminationPhase().apply(graph);
|
58877
|
289 |
createCanonicalizerPhase().apply(graph, context);
|
43972
|
290 |
try {
|
|
291 |
Assert.assertTrue("partial escape analysis should have removed all NewInstanceNode allocations", graph.getNodes().filter(NewInstanceNode.class).isEmpty());
|
|
292 |
Assert.assertTrue("partial escape analysis should have removed all NewArrayNode allocations", graph.getNodes().filter(NewArrayNode.class).isEmpty());
|
|
293 |
|
|
294 |
ControlFlowGraph cfg = ControlFlowGraph.compute(graph, true, true, false, false);
|
52578
|
295 |
double frequencySum = 0;
|
43972
|
296 |
int materializeCount = 0;
|
|
297 |
for (CommitAllocationNode materialize : graph.getNodes().filter(CommitAllocationNode.class)) {
|
52578
|
298 |
frequencySum += cfg.blockFor(materialize).getRelativeFrequency() * materialize.getVirtualObjects().size();
|
43972
|
299 |
materializeCount += materialize.getVirtualObjects().size();
|
|
300 |
}
|
|
301 |
Assert.assertEquals("unexpected number of MaterializeObjectNodes", expectedCount, materializeCount);
|
52578
|
302 |
Assert.assertEquals("unexpected frequency of MaterializeObjectNodes", expectedProbability, frequencySum, 0.01);
|
43972
|
303 |
for (Node node : graph.getNodes()) {
|
|
304 |
for (Class<? extends Node> clazz : invalidNodeClasses) {
|
|
305 |
Assert.assertFalse("instance of invalid class: " + clazz.getSimpleName(), clazz.isInstance(node) && node.usages().isNotEmpty());
|
|
306 |
}
|
|
307 |
}
|
|
308 |
} catch (AssertionError e) {
|
|
309 |
TypeSystemTest.outputGraph(graph, snippet + ": " + e.getMessage());
|
|
310 |
throw e;
|
|
311 |
}
|
|
312 |
}
|
|
313 |
}
|