43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, 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.core.test;
|
|
24 |
|
|
25 |
import org.junit.Assert;
|
|
26 |
import org.junit.Test;
|
|
27 |
|
|
28 |
import org.graalvm.compiler.api.directives.GraalDirectives;
|
|
29 |
import org.graalvm.compiler.debug.Debug;
|
|
30 |
import org.graalvm.compiler.graph.Node;
|
|
31 |
import org.graalvm.compiler.graph.iterators.NodeIterable;
|
|
32 |
import org.graalvm.compiler.graph.spi.Canonicalizable;
|
|
33 |
import org.graalvm.compiler.graph.spi.SimplifierTool;
|
|
34 |
import org.graalvm.compiler.java.ComputeLoopFrequenciesClosure;
|
|
35 |
import org.graalvm.compiler.nodes.LoopBeginNode;
|
|
36 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
37 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
38 |
import org.graalvm.compiler.nodes.spi.NodeCostProvider;
|
|
39 |
import org.graalvm.compiler.phases.BasePhase;
|
|
40 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
41 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase.CustomCanonicalizer;
|
|
42 |
import org.graalvm.compiler.phases.contract.NodeCostUtil;
|
|
43 |
import org.graalvm.compiler.phases.tiers.HighTierContext;
|
|
44 |
import org.graalvm.compiler.phases.tiers.PhaseContext;
|
|
45 |
|
|
46 |
public class NodePropertiesTest extends GraalCompilerTest {
|
|
47 |
|
|
48 |
public static Object sideEffect;
|
|
49 |
|
|
50 |
public static Object[] array = new Object[]{new Object(), new Object(), new Object()};
|
|
51 |
|
|
52 |
public static int test1Snippet(int a) {
|
|
53 |
int x = 0;
|
|
54 |
if (a > 0) {
|
|
55 |
x = 1;
|
|
56 |
sideEffect = null;
|
|
57 |
} else {
|
|
58 |
x = 2;
|
|
59 |
sideEffect = null;
|
|
60 |
}
|
46371
|
61 |
int b = 4;
|
43972
|
62 |
sideEffect = null;
|
46371
|
63 |
int c = b % 5;
|
43972
|
64 |
// can shift
|
46371
|
65 |
return a * x * c;
|
43972
|
66 |
}
|
|
67 |
|
|
68 |
public static int test2Snippet(int a) {
|
|
69 |
int x = 0;
|
|
70 |
if (a > 0) {
|
|
71 |
x = 1;
|
|
72 |
sideEffect = null;
|
|
73 |
} else {
|
|
74 |
x = 2;
|
|
75 |
sideEffect = null;
|
|
76 |
}
|
|
77 |
sideEffect = null;
|
|
78 |
// cannot shift
|
46344
|
79 |
return a * x * 41;
|
43972
|
80 |
}
|
|
81 |
|
|
82 |
public static final int ITERATIONS_LOOP_1 = 128;
|
|
83 |
public static final int ITERATIONS_LOOP_2 = 256;
|
|
84 |
|
|
85 |
public static int testLoop01(int a) {
|
|
86 |
int res = 0;
|
|
87 |
for (int i = 0; GraalDirectives.injectIterationCount(ITERATIONS_LOOP_1, i < a); i++) {
|
|
88 |
res += i;
|
|
89 |
}
|
|
90 |
return res;
|
|
91 |
}
|
|
92 |
|
|
93 |
public static int testLoop02(int a) {
|
|
94 |
int res = 0;
|
|
95 |
for (int i = 0; GraalDirectives.injectIterationCount(ITERATIONS_LOOP_2, i < a); i++) {
|
|
96 |
res += i;
|
|
97 |
}
|
|
98 |
return res;
|
|
99 |
}
|
|
100 |
|
|
101 |
public static int testLoop03(int a) {
|
|
102 |
int res = 0;
|
|
103 |
for (int i = 0; GraalDirectives.injectIterationCount(ITERATIONS_LOOP_1, i < a); i++) {
|
|
104 |
res *= i;
|
|
105 |
}
|
|
106 |
return res;
|
|
107 |
}
|
|
108 |
|
|
109 |
public static int testLoop04(int a) {
|
|
110 |
int res = 0;
|
|
111 |
for (int i = 0; GraalDirectives.injectIterationCount(ITERATIONS_LOOP_1 * ITERATIONS_LOOP_2, i < a); i++) {
|
|
112 |
res += i;
|
|
113 |
}
|
|
114 |
return res;
|
|
115 |
}
|
|
116 |
|
|
117 |
public static int testLoop05(int a) {
|
|
118 |
int res = 0;
|
|
119 |
for (int i = 0; GraalDirectives.injectIterationCount(ITERATIONS_LOOP_1, i < a); i++) {
|
|
120 |
res += i;
|
|
121 |
for (int j = 0; GraalDirectives.injectIterationCount(ITERATIONS_LOOP_2, j < ITERATIONS_LOOP_2); j++) {
|
|
122 |
res += i;
|
|
123 |
}
|
|
124 |
}
|
|
125 |
return res;
|
|
126 |
}
|
|
127 |
|
|
128 |
public static int dontInline(int a) {
|
|
129 |
int res = 1;
|
|
130 |
for (int i = 0; i < a; i++) {
|
|
131 |
for (int j = 0; j < i; j++) {
|
|
132 |
for (int k = 0; k < j; k++) {
|
|
133 |
res += i + j + k;
|
|
134 |
}
|
|
135 |
}
|
|
136 |
}
|
|
137 |
sideEffect = res;
|
|
138 |
return (Integer) sideEffect;
|
|
139 |
}
|
|
140 |
|
|
141 |
public static int untrused01(int a) {
|
|
142 |
return dontInline(a);
|
|
143 |
}
|
|
144 |
|
|
145 |
public static int arrayLoadTest(int a) {
|
|
146 |
return array[a].hashCode();
|
|
147 |
}
|
|
148 |
|
|
149 |
public static int arrayStoreTest(int a) {
|
|
150 |
String s = String.valueOf(a);
|
|
151 |
array[2] = s;
|
|
152 |
return s.length();
|
|
153 |
}
|
|
154 |
|
|
155 |
public static int fieldLoad(int a) {
|
|
156 |
return sideEffect.hashCode() * a;
|
|
157 |
}
|
|
158 |
|
|
159 |
public static int fieldStore(int a) {
|
|
160 |
sideEffect = a;
|
|
161 |
return a;
|
|
162 |
}
|
|
163 |
|
|
164 |
@Test
|
|
165 |
public void testCanonicalizationExample() {
|
|
166 |
HighTierContext htc = getDefaultHighTierContext();
|
|
167 |
ImprovementSavingCanonicalizer c1 = new ImprovementSavingCanonicalizer(htc.getNodeCostProvider());
|
|
168 |
StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("test1Snippet"));
|
|
169 |
new CanonicalizerPhase(c1).apply(g1, htc);
|
|
170 |
ImprovementSavingCanonicalizer c2 = new ImprovementSavingCanonicalizer(htc.getNodeCostProvider());
|
|
171 |
StructuredGraph g2 = parseForCompile(getResolvedJavaMethod("test2Snippet"));
|
|
172 |
new CanonicalizerPhase(c2).apply(g2, htc);
|
|
173 |
Assert.assertTrue(c1.savedCycles > c2.savedCycles);
|
|
174 |
}
|
|
175 |
|
|
176 |
private static void prepareGraphForLoopFrequencies(StructuredGraph g, HighTierContext htc) {
|
|
177 |
// let canonicalizer work away branch probability nodes
|
|
178 |
new CanonicalizerPhase().apply(g, htc);
|
|
179 |
// recompute the loop frequencies
|
|
180 |
ComputeLoopFrequenciesClosure.compute(g);
|
|
181 |
}
|
|
182 |
|
|
183 |
private static void assertFrequency(StructuredGraph g, int iterations) {
|
|
184 |
NodeIterable<LoopBeginNode> loopBeginNodes = g.getNodes(LoopBeginNode.TYPE);
|
|
185 |
LoopBeginNode loopBeginNode = loopBeginNodes.first();
|
|
186 |
Assert.assertEquals("loop frequency of " + loopBeginNode, iterations, loopBeginNode.loopFrequency(), 0);
|
|
187 |
}
|
|
188 |
|
|
189 |
@Test
|
|
190 |
public void testDifferentLoopFaster() {
|
|
191 |
HighTierContext htc = getDefaultHighTierContext();
|
|
192 |
StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("testLoop01"));
|
|
193 |
StructuredGraph g2 = parseForCompile(getResolvedJavaMethod("testLoop03"));
|
|
194 |
prepareGraphForLoopFrequencies(g1, htc);
|
|
195 |
prepareGraphForLoopFrequencies(g2, htc);
|
|
196 |
assertFrequency(g1, ITERATIONS_LOOP_1);
|
|
197 |
assertFrequency(g2, ITERATIONS_LOOP_1);
|
|
198 |
GraphCostPhase gc1 = new GraphCostPhase();
|
|
199 |
GraphCostPhase gc2 = new GraphCostPhase();
|
|
200 |
gc1.apply(g1, htc);
|
|
201 |
gc2.apply(g2, htc);
|
|
202 |
Debug.log("Test testDifferentLoopFaster --> 1.Graph cycles:%f size:%f vs. 2.Graph cycles:%f size:%f\n", gc1.finalCycles, gc1.finalSize, gc2.finalCycles, gc2.finalSize);
|
|
203 |
Assert.assertTrue(gc2.finalCycles > gc1.finalCycles);
|
|
204 |
Assert.assertTrue(gc2.finalSize == gc1.finalSize);
|
|
205 |
}
|
|
206 |
|
|
207 |
@Test
|
|
208 |
public void testSameLoopMoreIterationsCostlier() {
|
|
209 |
HighTierContext htc = getDefaultHighTierContext();
|
|
210 |
StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("testLoop01"));
|
|
211 |
StructuredGraph g2 = parseForCompile(getResolvedJavaMethod("testLoop02"));
|
|
212 |
prepareGraphForLoopFrequencies(g1, htc);
|
|
213 |
prepareGraphForLoopFrequencies(g2, htc);
|
|
214 |
assertFrequency(g1, ITERATIONS_LOOP_1);
|
|
215 |
assertFrequency(g2, ITERATIONS_LOOP_2);
|
|
216 |
GraphCostPhase gc1 = new GraphCostPhase();
|
|
217 |
GraphCostPhase gc2 = new GraphCostPhase();
|
|
218 |
gc1.apply(g1, htc);
|
|
219 |
gc2.apply(g2, htc);
|
|
220 |
Debug.log("Test testSameLoopMoreIterationsCostlier --> 1.Graph cycles:%f size:%f vs. 2.Graph cycles:%f size:%f\n", gc1.finalCycles, gc1.finalSize, gc2.finalCycles, gc2.finalSize);
|
|
221 |
Assert.assertTrue(gc2.finalCycles > gc1.finalCycles);
|
|
222 |
Assert.assertTrue(gc2.finalSize == gc1.finalSize);
|
|
223 |
}
|
|
224 |
|
|
225 |
@Test
|
|
226 |
public void testDifferentLoopsInnerOuter() {
|
|
227 |
HighTierContext htc = getDefaultHighTierContext();
|
|
228 |
StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("testLoop04"));
|
|
229 |
StructuredGraph g2 = parseForCompile(getResolvedJavaMethod("testLoop05"));
|
|
230 |
prepareGraphForLoopFrequencies(g1, htc);
|
|
231 |
prepareGraphForLoopFrequencies(g2, htc);
|
|
232 |
assertFrequency(g1, ITERATIONS_LOOP_1 * ITERATIONS_LOOP_2);
|
|
233 |
GraphCostPhase gc1 = new GraphCostPhase();
|
|
234 |
GraphCostPhase gc2 = new GraphCostPhase();
|
|
235 |
gc1.apply(g1, htc);
|
|
236 |
gc2.apply(g2, htc);
|
|
237 |
Debug.log("Test testDifferentLoopsInnerOuter --> 1.Graph cycles:%f size:%f vs. 2.Graph cycles:%f size:%f\n", gc1.finalCycles, gc1.finalSize, gc2.finalCycles, gc2.finalSize);
|
|
238 |
Assert.assertTrue(gc2.finalSize > gc1.finalSize);
|
|
239 |
}
|
|
240 |
|
|
241 |
@Test
|
|
242 |
public void testGraphCost() {
|
|
243 |
StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("test1Snippet"));
|
|
244 |
StructuredGraph g2 = parseForCompile(getResolvedJavaMethod("test2Snippet"));
|
|
245 |
HighTierContext htc = getDefaultHighTierContext();
|
|
246 |
new CanonicalizerPhase().apply(g1, htc);
|
|
247 |
new CanonicalizerPhase().apply(g2, htc);
|
|
248 |
GraphCostPhase gc1 = new GraphCostPhase();
|
|
249 |
GraphCostPhase gc2 = new GraphCostPhase();
|
|
250 |
gc1.apply(g1, htc);
|
|
251 |
gc2.apply(g2, htc);
|
|
252 |
Debug.log("Test Graph Cost --> 1.Graph cost:%f vs. 2.Graph cost:%f\n", gc1.finalCycles, gc2.finalCycles);
|
|
253 |
Assert.assertTrue(gc2.finalCycles > gc1.finalCycles);
|
46371
|
254 |
Assert.assertTrue(gc2.finalSize == gc1.finalSize);
|
43972
|
255 |
}
|
|
256 |
|
|
257 |
@Test
|
|
258 |
public void testExpectUntrusted() {
|
|
259 |
StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("untrused01"));
|
|
260 |
HighTierContext htc = getDefaultHighTierContext();
|
|
261 |
new CanonicalizerPhase().apply(g1, htc);
|
|
262 |
GraphCostPhase gc1 = new GraphCostPhase();
|
|
263 |
gc1.apply(g1, htc);
|
|
264 |
}
|
|
265 |
|
|
266 |
@Test
|
|
267 |
public void testArrayLoad() {
|
|
268 |
StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("arrayLoadTest"));
|
|
269 |
HighTierContext htc = getDefaultHighTierContext();
|
|
270 |
new CanonicalizerPhase().apply(g1, htc);
|
|
271 |
GraphCostPhase gc1 = new GraphCostPhase();
|
|
272 |
gc1.apply(g1, htc);
|
|
273 |
Assert.assertEquals(35, gc1.finalCycles, 25);
|
|
274 |
}
|
|
275 |
|
|
276 |
@Test
|
|
277 |
public void testArrayStore() {
|
|
278 |
StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("arrayStoreTest"));
|
|
279 |
HighTierContext htc = getDefaultHighTierContext();
|
|
280 |
new CanonicalizerPhase().apply(g1, htc);
|
|
281 |
GraphCostPhase gc1 = new GraphCostPhase();
|
|
282 |
gc1.apply(g1, htc);
|
|
283 |
Assert.assertEquals(50, gc1.finalCycles, 25);
|
|
284 |
}
|
|
285 |
|
|
286 |
@Test
|
|
287 |
public void testFieldLoad() {
|
|
288 |
StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("fieldLoad"));
|
|
289 |
HighTierContext htc = getDefaultHighTierContext();
|
|
290 |
new CanonicalizerPhase().apply(g1, htc);
|
|
291 |
GraphCostPhase gc1 = new GraphCostPhase();
|
|
292 |
gc1.apply(g1, htc);
|
|
293 |
Assert.assertEquals(30, gc1.finalCycles, 25);
|
|
294 |
}
|
|
295 |
|
|
296 |
@Test
|
|
297 |
public void testFieldStore() {
|
|
298 |
StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("fieldStore"));
|
|
299 |
HighTierContext htc = getDefaultHighTierContext();
|
|
300 |
new CanonicalizerPhase().apply(g1, htc);
|
|
301 |
GraphCostPhase gc1 = new GraphCostPhase();
|
|
302 |
gc1.apply(g1, htc);
|
46344
|
303 |
Assert.assertEquals(40, gc1.finalCycles, 25);
|
43972
|
304 |
}
|
|
305 |
|
|
306 |
static class ImprovementSavingCanonicalizer extends CustomCanonicalizer {
|
|
307 |
private int savedCycles;
|
|
308 |
private final NodeCostProvider nodeCostProvider;
|
|
309 |
|
|
310 |
ImprovementSavingCanonicalizer(NodeCostProvider nodeCostProvider) {
|
|
311 |
this.nodeCostProvider = nodeCostProvider;
|
|
312 |
}
|
|
313 |
|
|
314 |
@Override
|
|
315 |
public void simplify(Node node, SimplifierTool tool) {
|
|
316 |
if (node instanceof Canonicalizable.Binary<?>) {
|
|
317 |
@SuppressWarnings("unchecked")
|
|
318 |
Canonicalizable.Binary<ValueNode> bc = (Canonicalizable.Binary<ValueNode>) node;
|
|
319 |
Node canonicalized = bc.canonical(tool, bc.getX(), bc.getY());
|
|
320 |
if (canonicalized != node) {
|
|
321 |
savedCycles += nodeCostProvider.getEstimatedCPUCycles(node) - nodeCostProvider.getEstimatedCPUCycles(canonicalized);
|
|
322 |
}
|
|
323 |
}
|
|
324 |
}
|
|
325 |
}
|
|
326 |
|
|
327 |
private static class GraphCostPhase extends BasePhase<PhaseContext> {
|
|
328 |
private double finalCycles;
|
|
329 |
private double finalSize;
|
|
330 |
|
|
331 |
@Override
|
|
332 |
protected void run(StructuredGraph graph, PhaseContext context) {
|
|
333 |
finalCycles = NodeCostUtil.computeGraphCycles(graph, context.getNodeCostProvider(), true);
|
|
334 |
finalSize = NodeCostUtil.computeGraphSize(graph, context.getNodeCostProvider());
|
|
335 |
}
|
|
336 |
|
|
337 |
}
|
|
338 |
|
|
339 |
}
|