43972
|
1 |
/*
|
58299
|
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 |
|
52578
|
27 |
import static org.graalvm.compiler.graph.iterators.NodePredicates.isA;
|
|
28 |
|
43972
|
29 |
import java.util.List;
|
|
30 |
|
|
31 |
import org.graalvm.compiler.core.test.GraalCompilerTest;
|
46640
|
32 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
33 |
import org.graalvm.compiler.nodes.ReturnNode;
|
|
34 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
35 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
|
36 |
import org.graalvm.compiler.nodes.java.NewArrayNode;
|
|
37 |
import org.graalvm.compiler.nodes.java.NewInstanceNode;
|
52578
|
38 |
import org.graalvm.compiler.nodes.virtual.AllocatedObjectNode;
|
43972
|
39 |
import org.graalvm.compiler.nodes.virtual.CommitAllocationNode;
|
|
40 |
import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
|
|
41 |
import org.graalvm.compiler.phases.tiers.HighTierContext;
|
|
42 |
import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
|
46640
|
43 |
import org.junit.Assert;
|
|
44 |
|
|
45 |
import jdk.vm.ci.meta.JavaConstant;
|
|
46 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
43972
|
47 |
|
|
48 |
//JaCoCo Exclude
|
|
49 |
|
|
50 |
/**
|
|
51 |
* This base class for all Escape Analysis tests does not contain tests itself, therefore it is not
|
|
52 |
* automatically excluded from JaCoCo. Since it includes code that is used in the test snippets, it
|
|
53 |
* needs to be excluded manually.
|
|
54 |
*/
|
|
55 |
public class EATestBase extends GraalCompilerTest {
|
|
56 |
|
|
57 |
public static class TestClassInt {
|
|
58 |
public int x;
|
|
59 |
public int y;
|
|
60 |
public int z;
|
|
61 |
|
|
62 |
public TestClassInt() {
|
|
63 |
this(0, 0);
|
|
64 |
}
|
|
65 |
|
|
66 |
public TestClassInt(int x) {
|
|
67 |
this(x, 0);
|
|
68 |
}
|
|
69 |
|
|
70 |
public TestClassInt(int x, int y) {
|
|
71 |
this.x = x;
|
|
72 |
this.y = y;
|
|
73 |
}
|
|
74 |
|
|
75 |
@Override
|
|
76 |
public boolean equals(Object obj) {
|
|
77 |
TestClassInt other = (TestClassInt) obj;
|
|
78 |
return x == other.x && y == other.y && z == other.z;
|
|
79 |
}
|
|
80 |
|
|
81 |
@Override
|
|
82 |
public String toString() {
|
47798
|
83 |
return "{" + x + "," + y + "," + z + "}";
|
43972
|
84 |
}
|
|
85 |
|
|
86 |
@Override
|
|
87 |
public int hashCode() {
|
|
88 |
return x + 13 * y;
|
|
89 |
}
|
59095
|
90 |
|
|
91 |
public static final long fieldOffset1;
|
|
92 |
public static final long fieldOffset2;
|
|
93 |
public static final boolean firstFieldIsX;
|
|
94 |
|
|
95 |
static {
|
|
96 |
try {
|
|
97 |
long localFieldOffset1 = UNSAFE.objectFieldOffset(EATestBase.TestClassInt.class.getField("x"));
|
|
98 |
// Make the fields 8 byte aligned (Required for testing setLong on Architectures
|
|
99 |
// which does not support unaligned memory access
|
|
100 |
if (localFieldOffset1 % 8 == 0) {
|
|
101 |
fieldOffset1 = localFieldOffset1;
|
|
102 |
fieldOffset2 = UNSAFE.objectFieldOffset(EATestBase.TestClassInt.class.getField("y"));
|
|
103 |
firstFieldIsX = true;
|
|
104 |
} else {
|
|
105 |
fieldOffset1 = UNSAFE.objectFieldOffset(EATestBase.TestClassInt.class.getField("y"));
|
|
106 |
fieldOffset2 = UNSAFE.objectFieldOffset(EATestBase.TestClassInt.class.getField("z"));
|
|
107 |
firstFieldIsX = false;
|
|
108 |
}
|
|
109 |
assert fieldOffset2 == fieldOffset1 + 4;
|
|
110 |
} catch (Exception e) {
|
|
111 |
throw new RuntimeException(e);
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
public void setFirstField(int v) {
|
|
116 |
if (firstFieldIsX) {
|
|
117 |
x = v;
|
|
118 |
} else {
|
|
119 |
y = v;
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
public void setSecondField(int v) {
|
|
124 |
if (firstFieldIsX) {
|
|
125 |
y = v;
|
|
126 |
} else {
|
|
127 |
z = v;
|
|
128 |
}
|
|
129 |
}
|
43972
|
130 |
}
|
|
131 |
|
|
132 |
public static class TestClassObject {
|
|
133 |
public Object x;
|
|
134 |
public Object y;
|
|
135 |
|
|
136 |
public TestClassObject() {
|
|
137 |
this(null, null);
|
|
138 |
}
|
|
139 |
|
|
140 |
public TestClassObject(Object x) {
|
|
141 |
this(x, null);
|
|
142 |
}
|
|
143 |
|
|
144 |
public TestClassObject(Object x, Object y) {
|
|
145 |
this.x = x;
|
|
146 |
this.y = y;
|
|
147 |
}
|
|
148 |
|
|
149 |
@Override
|
|
150 |
public boolean equals(Object obj) {
|
|
151 |
TestClassObject other = (TestClassObject) obj;
|
|
152 |
return x == other.x && y == other.y;
|
|
153 |
}
|
|
154 |
|
|
155 |
@Override
|
|
156 |
public String toString() {
|
|
157 |
return "{" + x + "," + y + "}";
|
|
158 |
}
|
|
159 |
|
|
160 |
@Override
|
|
161 |
public int hashCode() {
|
|
162 |
return (x == null ? 0 : x.hashCode()) + 13 * (y == null ? 0 : y.hashCode());
|
|
163 |
}
|
|
164 |
}
|
|
165 |
|
|
166 |
protected static native void notInlineable();
|
|
167 |
|
|
168 |
protected StructuredGraph graph;
|
|
169 |
protected HighTierContext context;
|
|
170 |
protected List<ReturnNode> returnNodes;
|
|
171 |
|
|
172 |
/**
|
|
173 |
* Runs Escape Analysis on the given snippet and makes sure that no allocations remain in the
|
|
174 |
* graph.
|
|
175 |
*
|
|
176 |
* @param snippet the name of the method whose graph should be processed
|
|
177 |
* @param expectedConstantResult if this is non-null, the resulting graph needs to have the
|
|
178 |
* given constant return value
|
|
179 |
* @param iterativeEscapeAnalysis true if escape analysis should be run for more than one
|
|
180 |
* iteration
|
|
181 |
*/
|
|
182 |
protected void testEscapeAnalysis(String snippet, JavaConstant expectedConstantResult, boolean iterativeEscapeAnalysis) {
|
52578
|
183 |
testEscapeAnalysis(snippet, expectedConstantResult, iterativeEscapeAnalysis, 0);
|
|
184 |
}
|
|
185 |
|
|
186 |
protected void testEscapeAnalysis(String snippet, JavaConstant expectedConstantResult, boolean iterativeEscapeAnalysis, int expectedAllocationCount) {
|
43972
|
187 |
prepareGraph(snippet, iterativeEscapeAnalysis);
|
|
188 |
if (expectedConstantResult != null) {
|
|
189 |
for (ReturnNode returnNode : returnNodes) {
|
|
190 |
Assert.assertTrue(returnNode.result().toString(), returnNode.result().isConstant());
|
|
191 |
Assert.assertEquals(expectedConstantResult, returnNode.result().asConstant());
|
|
192 |
}
|
|
193 |
}
|
54328
|
194 |
int newInstanceCount = getAllocationCount();
|
52578
|
195 |
Assert.assertEquals("Expected allocation count does not match", expectedAllocationCount, newInstanceCount);
|
|
196 |
if (expectedAllocationCount == 0) {
|
|
197 |
Assert.assertTrue("Unexpected CommitAllocationNode", graph.getNodes().filter(CommitAllocationNode.class).isEmpty());
|
|
198 |
}
|
43972
|
199 |
}
|
|
200 |
|
54328
|
201 |
protected int getAllocationCount() {
|
|
202 |
return graph.getNodes().filter(isA(NewInstanceNode.class).or(NewArrayNode.class).or(AllocatedObjectNode.class)).count();
|
|
203 |
}
|
|
204 |
|
43972
|
205 |
@SuppressWarnings("try")
|
|
206 |
protected void prepareGraph(String snippet, boolean iterativeEscapeAnalysis) {
|
|
207 |
ResolvedJavaMethod method = getResolvedJavaMethod(snippet);
|
46640
|
208 |
DebugContext debug = getDebugContext();
|
|
209 |
try (DebugContext.Scope s = debug.scope(getClass(), method, getCodeCache())) {
|
|
210 |
graph = parseEager(method, AllowAssumptions.YES, debug);
|
43972
|
211 |
context = getDefaultHighTierContext();
|
52910
|
212 |
createInliningPhase().apply(graph, context);
|
43972
|
213 |
new DeadCodeEliminationPhase().apply(graph);
|
47798
|
214 |
canonicalizeGraph();
|
58877
|
215 |
new PartialEscapePhase(iterativeEscapeAnalysis, false, createCanonicalizerPhase(), null, graph.getOptions()).apply(graph, context);
|
47798
|
216 |
postEACanonicalizeGraph();
|
43972
|
217 |
returnNodes = graph.getNodes(ReturnNode.TYPE).snapshot();
|
|
218 |
} catch (Throwable e) {
|
46640
|
219 |
throw debug.handle(e);
|
43972
|
220 |
}
|
|
221 |
}
|
47798
|
222 |
|
|
223 |
protected void postEACanonicalizeGraph() {
|
|
224 |
}
|
|
225 |
|
|
226 |
protected void canonicalizeGraph() {
|
58877
|
227 |
this.createCanonicalizerPhase().apply(graph, context);
|
47798
|
228 |
}
|
43972
|
229 |
}
|