51436
|
1 |
/*
|
58877
|
2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
|
51436
|
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 |
|
|
24 |
|
|
25 |
package org.graalvm.compiler.core.test.ea;
|
|
26 |
|
|
27 |
import java.lang.ref.WeakReference;
|
|
28 |
import java.util.List;
|
|
29 |
|
|
30 |
import org.graalvm.compiler.nodes.extended.BoxNode;
|
|
31 |
import org.graalvm.compiler.nodes.extended.UnboxNode;
|
|
32 |
import org.graalvm.compiler.nodes.java.StoreFieldNode;
|
|
33 |
import org.graalvm.compiler.nodes.virtual.CommitAllocationNode;
|
|
34 |
import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
|
|
35 |
import org.junit.Assert;
|
|
36 |
import org.junit.Test;
|
|
37 |
|
|
38 |
public class PartialEscapeAnalysisIterationTest extends EATestBase {
|
|
39 |
|
|
40 |
// remember boxing nodes from before PEA
|
|
41 |
private List<BoxNode> boxNodes;
|
|
42 |
|
|
43 |
@Override
|
|
44 |
protected void canonicalizeGraph() {
|
|
45 |
super.canonicalizeGraph();
|
|
46 |
boxNodes = graph.getNodes().filter(BoxNode.class).snapshot();
|
|
47 |
}
|
|
48 |
|
|
49 |
private static final class AllocatedObject {
|
|
50 |
private int value;
|
|
51 |
|
|
52 |
AllocatedObject(int value) {
|
|
53 |
this.value = value;
|
|
54 |
}
|
|
55 |
|
|
56 |
AllocatedObject() {
|
|
57 |
// empty
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
|
61 |
public static volatile Object obj1;
|
|
62 |
public static volatile Double object1 = (double) 123;
|
|
63 |
public static volatile AllocatedObject object2 = new AllocatedObject(123);
|
|
64 |
|
|
65 |
public static String moveIntoBranchBox(int id) {
|
|
66 |
Double box = object1 + 1;
|
|
67 |
if (id == 0) {
|
|
68 |
obj1 = new WeakReference<>(box);
|
|
69 |
}
|
|
70 |
return "value";
|
|
71 |
}
|
|
72 |
|
|
73 |
public static String moveIntoBranch(int id) {
|
|
74 |
AllocatedObject box = new AllocatedObject(object2.value + 1);
|
|
75 |
if (id == 0) {
|
|
76 |
obj1 = new WeakReference<>(box);
|
|
77 |
}
|
|
78 |
return "value";
|
|
79 |
}
|
|
80 |
|
|
81 |
@Test
|
|
82 |
public void testJMHBlackholePattern() {
|
|
83 |
/*
|
|
84 |
* The overall number of allocations in this methods does not change during PEA, but the
|
|
85 |
* effects still need to be applied since they move the allocation between blocks.
|
|
86 |
*/
|
|
87 |
|
|
88 |
// test with a boxing object
|
|
89 |
prepareGraph("moveIntoBranchBox", false);
|
|
90 |
Assert.assertEquals(1, graph.getNodes().filter(UnboxNode.class).count());
|
|
91 |
Assert.assertEquals(1, graph.getNodes().filter(BoxNode.class).count());
|
|
92 |
// the boxing needs to be moved into the branch
|
|
93 |
Assert.assertTrue(graph.getNodes().filter(BoxNode.class).first().next() instanceof StoreFieldNode);
|
|
94 |
|
|
95 |
// test with a normal object
|
|
96 |
prepareGraph("moveIntoBranch", false);
|
|
97 |
Assert.assertEquals(1, graph.getNodes().filter(CommitAllocationNode.class).count());
|
|
98 |
// the allocation needs to be moved into the branch
|
|
99 |
Assert.assertTrue(graph.getNodes().filter(CommitAllocationNode.class).first().next() instanceof StoreFieldNode);
|
|
100 |
}
|
|
101 |
|
|
102 |
public static String noLoopIterationBox(int id) {
|
|
103 |
Double box = object1 + 1;
|
|
104 |
for (int i = 0; i < 100; i++) {
|
|
105 |
if (id == i) {
|
|
106 |
obj1 = new WeakReference<>(box);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
return "value";
|
|
110 |
}
|
|
111 |
|
|
112 |
public static String noLoopIteration(int id) {
|
|
113 |
AllocatedObject box = new AllocatedObject(object2.value + 1);
|
|
114 |
for (int i = 0; i < 100; i++) {
|
|
115 |
if (id == i) {
|
|
116 |
obj1 = new WeakReference<>(box);
|
|
117 |
}
|
|
118 |
}
|
|
119 |
return "value";
|
|
120 |
}
|
|
121 |
|
|
122 |
public static String noLoopIterationEmpty(int id) {
|
|
123 |
AllocatedObject box = new AllocatedObject();
|
|
124 |
for (int i = 0; i < 100; i++) {
|
|
125 |
if (id == i) {
|
|
126 |
obj1 = new WeakReference<>(box);
|
|
127 |
}
|
|
128 |
}
|
|
129 |
return "value";
|
|
130 |
}
|
|
131 |
|
|
132 |
@Test
|
|
133 |
public void testNoLoopIteration() {
|
|
134 |
/*
|
|
135 |
* PEA should not apply any effects on this method, since it cannot move the allocation into
|
|
136 |
* the branch anyway (it needs to stay outside the loop).
|
|
137 |
*/
|
|
138 |
|
|
139 |
// test with a boxing object
|
|
140 |
prepareGraph("noLoopIterationBox", true);
|
|
141 |
Assert.assertEquals(1, boxNodes.size());
|
|
142 |
Assert.assertTrue(boxNodes.get(0).isAlive());
|
|
143 |
|
|
144 |
// test with a normal object (needs one iteration to replace NewInstance with
|
|
145 |
// CommitAllocation)
|
|
146 |
for (String name : new String[]{"noLoopIterationEmpty", "noLoopIteration"}) {
|
|
147 |
prepareGraph(name, false);
|
|
148 |
List<CommitAllocationNode> allocations = graph.getNodes().filter(CommitAllocationNode.class).snapshot();
|
58877
|
149 |
new PartialEscapePhase(true, false, createCanonicalizerPhase(), null, graph.getOptions()).apply(graph, context);
|
51436
|
150 |
Assert.assertEquals(1, allocations.size());
|
|
151 |
Assert.assertTrue(allocations.get(0).isAlive());
|
|
152 |
}
|
|
153 |
}
|
|
154 |
}
|